xref: /llvm-project/bolt/lib/Core/BinaryContext.cpp (revision 32d2473a5dba417eb8d34146575289e4e53c91fa)
12f09f445SMaksim Panchenko //===- bolt/Core/BinaryContext.cpp - Low-level context --------------------===//
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 //
92f09f445SMaksim Panchenko // This file implements the BinaryContext class.
102f09f445SMaksim Panchenko //
11a34c753fSRafael Auler //===----------------------------------------------------------------------===//
12a34c753fSRafael Auler 
13a34c753fSRafael Auler #include "bolt/Core/BinaryContext.h"
14a34c753fSRafael Auler #include "bolt/Core/BinaryEmitter.h"
15a34c753fSRafael Auler #include "bolt/Core/BinaryFunction.h"
16a34c753fSRafael Auler #include "bolt/Utils/CommandLineOpts.h"
17a34c753fSRafael Auler #include "bolt/Utils/NameResolver.h"
18a34c753fSRafael Auler #include "bolt/Utils/Utils.h"
19a34c753fSRafael Auler #include "llvm/ADT/Twine.h"
20290e4823Sserge-sans-paille #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
21a34c753fSRafael Auler #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
22a34c753fSRafael Auler #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
23a34c753fSRafael Auler #include "llvm/MC/MCAsmLayout.h"
24a34c753fSRafael Auler #include "llvm/MC/MCAssembler.h"
25a34c753fSRafael Auler #include "llvm/MC/MCContext.h"
26a34c753fSRafael Auler #include "llvm/MC/MCDisassembler/MCDisassembler.h"
27a34c753fSRafael Auler #include "llvm/MC/MCInstPrinter.h"
28a34c753fSRafael Auler #include "llvm/MC/MCObjectStreamer.h"
29a34c753fSRafael Auler #include "llvm/MC/MCObjectWriter.h"
3057f7c7d9Sserge-sans-paille #include "llvm/MC/MCRegisterInfo.h"
31a34c753fSRafael Auler #include "llvm/MC/MCSectionELF.h"
32a34c753fSRafael Auler #include "llvm/MC/MCStreamer.h"
3357f7c7d9Sserge-sans-paille #include "llvm/MC/MCSubtargetInfo.h"
34a34c753fSRafael Auler #include "llvm/MC/MCSymbol.h"
35a34c753fSRafael Auler #include "llvm/Support/CommandLine.h"
36*32d2473aSAmir Ayupov #include "llvm/Support/Error.h"
37a34c753fSRafael Auler #include "llvm/Support/Regex.h"
386aa735ceSAmir Ayupov #include <algorithm>
39a34c753fSRafael Auler #include <functional>
40a34c753fSRafael Auler #include <iterator>
416aa735ceSAmir Ayupov #include <unordered_set>
42a34c753fSRafael Auler 
43a34c753fSRafael Auler using namespace llvm;
44a34c753fSRafael Auler 
45a34c753fSRafael Auler #undef  DEBUG_TYPE
46a34c753fSRafael Auler #define DEBUG_TYPE "bolt"
47a34c753fSRafael Auler 
48a34c753fSRafael Auler namespace opts {
49a34c753fSRafael Auler 
50a34c753fSRafael Auler cl::opt<bool>
51a34c753fSRafael Auler NoHugePages("no-huge-pages",
52a34c753fSRafael Auler   cl::desc("use regular size pages for code alignment"),
53a34c753fSRafael Auler   cl::ZeroOrMore,
54a34c753fSRafael Auler   cl::Hidden,
55a34c753fSRafael Auler   cl::cat(BoltCategory));
56a34c753fSRafael Auler 
57a34c753fSRafael Auler static cl::opt<bool>
58a34c753fSRafael Auler PrintDebugInfo("print-debug-info",
59a34c753fSRafael Auler   cl::desc("print debug info when printing functions"),
60a34c753fSRafael Auler   cl::Hidden,
61a34c753fSRafael Auler   cl::ZeroOrMore,
62a34c753fSRafael Auler   cl::cat(BoltCategory));
63a34c753fSRafael Auler 
64a34c753fSRafael Auler cl::opt<bool>
65a34c753fSRafael Auler PrintRelocations("print-relocations",
66a34c753fSRafael Auler   cl::desc("print relocations when printing functions/objects"),
67a34c753fSRafael Auler   cl::Hidden,
68a34c753fSRafael Auler   cl::ZeroOrMore,
69a34c753fSRafael Auler   cl::cat(BoltCategory));
70a34c753fSRafael Auler 
71a34c753fSRafael Auler static cl::opt<bool>
72a34c753fSRafael Auler PrintMemData("print-mem-data",
73a34c753fSRafael Auler   cl::desc("print memory data annotations when printing functions"),
74a34c753fSRafael Auler   cl::Hidden,
75a34c753fSRafael Auler   cl::ZeroOrMore,
76a34c753fSRafael Auler   cl::cat(BoltCategory));
77a34c753fSRafael Auler 
78a34c753fSRafael Auler } // namespace opts
79a34c753fSRafael Auler 
80a34c753fSRafael Auler namespace llvm {
81a34c753fSRafael Auler namespace bolt {
82a34c753fSRafael Auler 
83a34c753fSRafael Auler BinaryContext::BinaryContext(std::unique_ptr<MCContext> Ctx,
84a34c753fSRafael Auler                              std::unique_ptr<DWARFContext> DwCtx,
85a34c753fSRafael Auler                              std::unique_ptr<Triple> TheTriple,
8640c2e0faSMaksim Panchenko                              const Target *TheTarget, std::string TripleName,
87a34c753fSRafael Auler                              std::unique_ptr<MCCodeEmitter> MCE,
88a34c753fSRafael Auler                              std::unique_ptr<MCObjectFileInfo> MOFI,
89a34c753fSRafael Auler                              std::unique_ptr<const MCAsmInfo> AsmInfo,
90a34c753fSRafael Auler                              std::unique_ptr<const MCInstrInfo> MII,
91a34c753fSRafael Auler                              std::unique_ptr<const MCSubtargetInfo> STI,
92a34c753fSRafael Auler                              std::unique_ptr<MCInstPrinter> InstPrinter,
93a34c753fSRafael Auler                              std::unique_ptr<const MCInstrAnalysis> MIA,
94a34c753fSRafael Auler                              std::unique_ptr<MCPlusBuilder> MIB,
95a34c753fSRafael Auler                              std::unique_ptr<const MCRegisterInfo> MRI,
96a34c753fSRafael Auler                              std::unique_ptr<MCDisassembler> DisAsm)
9740c2e0faSMaksim Panchenko     : Ctx(std::move(Ctx)), DwCtx(std::move(DwCtx)),
9840c2e0faSMaksim Panchenko       TheTriple(std::move(TheTriple)), TheTarget(TheTarget),
9940c2e0faSMaksim Panchenko       TripleName(TripleName), MCE(std::move(MCE)), MOFI(std::move(MOFI)),
10040c2e0faSMaksim Panchenko       AsmInfo(std::move(AsmInfo)), MII(std::move(MII)), STI(std::move(STI)),
10140c2e0faSMaksim Panchenko       InstPrinter(std::move(InstPrinter)), MIA(std::move(MIA)),
10240c2e0faSMaksim Panchenko       MIB(std::move(MIB)), MRI(std::move(MRI)), DisAsm(std::move(DisAsm)) {
103a34c753fSRafael Auler   Relocation::Arch = this->TheTriple->getArch();
104a34c753fSRafael Auler   PageAlign = opts::NoHugePages ? RegularPageSize : HugePageSize;
105a34c753fSRafael Auler }
106a34c753fSRafael Auler 
107a34c753fSRafael Auler BinaryContext::~BinaryContext() {
1083652483cSRafael Auler   for (BinarySection *Section : Sections)
109a34c753fSRafael Auler     delete Section;
1103652483cSRafael Auler   for (BinaryFunction *InjectedFunction : InjectedBinaryFunctions)
111a34c753fSRafael Auler     delete InjectedFunction;
1123652483cSRafael Auler   for (std::pair<const uint64_t, JumpTable *> JTI : JumpTables)
113a34c753fSRafael Auler     delete JTI.second;
114a34c753fSRafael Auler   clearBinaryData();
115a34c753fSRafael Auler }
116a34c753fSRafael Auler 
117a34c753fSRafael Auler /// Create BinaryContext for a given architecture \p ArchName and
118a34c753fSRafael Auler /// triple \p TripleName.
119*32d2473aSAmir Ayupov Expected<std::unique_ptr<BinaryContext>>
120a34c753fSRafael Auler BinaryContext::createBinaryContext(const ObjectFile *File, bool IsPIC,
121a34c753fSRafael Auler                                    std::unique_ptr<DWARFContext> DwCtx) {
122a34c753fSRafael Auler   StringRef ArchName = "";
123a34c753fSRafael Auler   StringRef FeaturesStr = "";
124a34c753fSRafael Auler   switch (File->getArch()) {
125a34c753fSRafael Auler   case llvm::Triple::x86_64:
126a34c753fSRafael Auler     ArchName = "x86-64";
127a34c753fSRafael Auler     FeaturesStr = "+nopl";
128a34c753fSRafael Auler     break;
129a34c753fSRafael Auler   case llvm::Triple::aarch64:
130a34c753fSRafael Auler     ArchName = "aarch64";
131a34c753fSRafael Auler     FeaturesStr = "+fp-armv8,+neon,+crypto,+dotprod,+crc,+lse,+ras,+rdm,"
132a34c753fSRafael Auler                   "+fullfp16,+spe,+fuse-aes,+rcpc";
133a34c753fSRafael Auler     break;
134a34c753fSRafael Auler   default:
135*32d2473aSAmir Ayupov     return createStringError(std::errc::not_supported,
136*32d2473aSAmir Ayupov                              "BOLT-ERROR: Unrecognized machine in ELF file");
137a34c753fSRafael Auler   }
138a34c753fSRafael Auler 
139a34c753fSRafael Auler   auto TheTriple = std::make_unique<Triple>(File->makeTriple());
140a34c753fSRafael Auler   const std::string TripleName = TheTriple->str();
141a34c753fSRafael Auler 
142a34c753fSRafael Auler   std::string Error;
143a34c753fSRafael Auler   const Target *TheTarget =
144a34c753fSRafael Auler       TargetRegistry::lookupTarget(std::string(ArchName), *TheTriple, Error);
145*32d2473aSAmir Ayupov   if (!TheTarget)
146*32d2473aSAmir Ayupov     return createStringError(make_error_code(std::errc::not_supported),
147*32d2473aSAmir Ayupov                              Twine("BOLT-ERROR: ", Error));
148a34c753fSRafael Auler 
149a34c753fSRafael Auler   std::unique_ptr<const MCRegisterInfo> MRI(
150a34c753fSRafael Auler       TheTarget->createMCRegInfo(TripleName));
151*32d2473aSAmir Ayupov   if (!MRI)
152*32d2473aSAmir Ayupov     return createStringError(
153*32d2473aSAmir Ayupov         make_error_code(std::errc::not_supported),
154*32d2473aSAmir Ayupov         Twine("BOLT-ERROR: no register info for target ", TripleName));
155a34c753fSRafael Auler 
156a34c753fSRafael Auler   // Set up disassembler.
157a34c753fSRafael Auler   std::unique_ptr<const MCAsmInfo> AsmInfo(
158a34c753fSRafael Auler       TheTarget->createMCAsmInfo(*MRI, TripleName, MCTargetOptions()));
159*32d2473aSAmir Ayupov   if (!AsmInfo)
160*32d2473aSAmir Ayupov     return createStringError(
161*32d2473aSAmir Ayupov         make_error_code(std::errc::not_supported),
162*32d2473aSAmir Ayupov         Twine("BOLT-ERROR: no assembly info for target ", TripleName));
163a34c753fSRafael Auler 
164a34c753fSRafael Auler   std::unique_ptr<const MCSubtargetInfo> STI(
165a34c753fSRafael Auler       TheTarget->createMCSubtargetInfo(TripleName, "", FeaturesStr));
166*32d2473aSAmir Ayupov   if (!STI)
167*32d2473aSAmir Ayupov     return createStringError(
168*32d2473aSAmir Ayupov         make_error_code(std::errc::not_supported),
169*32d2473aSAmir Ayupov         Twine("BOLT-ERROR: no subtarget info for target ", TripleName));
170a34c753fSRafael Auler 
171a34c753fSRafael Auler   std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
172*32d2473aSAmir Ayupov   if (!MII)
173*32d2473aSAmir Ayupov     return createStringError(
174*32d2473aSAmir Ayupov         make_error_code(std::errc::not_supported),
175*32d2473aSAmir Ayupov         Twine("BOLT-ERROR: no instruction info for target ", TripleName));
176a34c753fSRafael Auler 
177a34c753fSRafael Auler   std::unique_ptr<MCContext> Ctx(
178a34c753fSRafael Auler       new MCContext(*TheTriple, AsmInfo.get(), MRI.get(), STI.get()));
179a34c753fSRafael Auler   std::unique_ptr<MCObjectFileInfo> MOFI(
180a34c753fSRafael Auler       TheTarget->createMCObjectFileInfo(*Ctx, IsPIC));
181a34c753fSRafael Auler   Ctx->setObjectFileInfo(MOFI.get());
182a34c753fSRafael Auler   // We do not support X86 Large code model. Change this in the future.
183a34c753fSRafael Auler   bool Large = false;
184a34c753fSRafael Auler   if (TheTriple->getArch() == llvm::Triple::aarch64)
185a34c753fSRafael Auler     Large = true;
186a34c753fSRafael Auler   unsigned LSDAEncoding =
187a34c753fSRafael Auler       Large ? dwarf::DW_EH_PE_absptr : dwarf::DW_EH_PE_udata4;
188a34c753fSRafael Auler   unsigned TTypeEncoding =
189a34c753fSRafael Auler       Large ? dwarf::DW_EH_PE_absptr : dwarf::DW_EH_PE_udata4;
190a34c753fSRafael Auler   if (IsPIC) {
191a34c753fSRafael Auler     LSDAEncoding = dwarf::DW_EH_PE_pcrel |
192a34c753fSRafael Auler                    (Large ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4);
193a34c753fSRafael Auler     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
194a34c753fSRafael Auler                     (Large ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4);
195a34c753fSRafael Auler   }
196a34c753fSRafael Auler 
197a34c753fSRafael Auler   std::unique_ptr<MCDisassembler> DisAsm(
198a34c753fSRafael Auler       TheTarget->createMCDisassembler(*STI, *Ctx));
199a34c753fSRafael Auler 
200*32d2473aSAmir Ayupov   if (!DisAsm)
201*32d2473aSAmir Ayupov     return createStringError(
202*32d2473aSAmir Ayupov         make_error_code(std::errc::not_supported),
203*32d2473aSAmir Ayupov         Twine("BOLT-ERROR: no disassembler info for target ", TripleName));
204a34c753fSRafael Auler 
205a34c753fSRafael Auler   std::unique_ptr<const MCInstrAnalysis> MIA(
206a34c753fSRafael Auler       TheTarget->createMCInstrAnalysis(MII.get()));
207*32d2473aSAmir Ayupov   if (!MIA)
208*32d2473aSAmir Ayupov     return createStringError(
209*32d2473aSAmir Ayupov         make_error_code(std::errc::not_supported),
210*32d2473aSAmir Ayupov         Twine("BOLT-ERROR: failed to create instruction analysis for target ",
211*32d2473aSAmir Ayupov               TripleName));
212a34c753fSRafael Auler 
213a34c753fSRafael Auler   int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
214a34c753fSRafael Auler   std::unique_ptr<MCInstPrinter> InstructionPrinter(
215a34c753fSRafael Auler       TheTarget->createMCInstPrinter(*TheTriple, AsmPrinterVariant, *AsmInfo,
216a34c753fSRafael Auler                                      *MII, *MRI));
217*32d2473aSAmir Ayupov   if (!InstructionPrinter)
218*32d2473aSAmir Ayupov     return createStringError(
219*32d2473aSAmir Ayupov         make_error_code(std::errc::not_supported),
220*32d2473aSAmir Ayupov         Twine("BOLT-ERROR: no instruction printer for target ", TripleName));
221a34c753fSRafael Auler   InstructionPrinter->setPrintImmHex(true);
222a34c753fSRafael Auler 
223a34c753fSRafael Auler   std::unique_ptr<MCCodeEmitter> MCE(
2242aed07e9SShao-Ce SUN       TheTarget->createMCCodeEmitter(*MII, *Ctx));
225a34c753fSRafael Auler 
226a34c753fSRafael Auler   // Make sure we don't miss any output on core dumps.
227a34c753fSRafael Auler   outs().SetUnbuffered();
228a34c753fSRafael Auler   errs().SetUnbuffered();
229a34c753fSRafael Auler   dbgs().SetUnbuffered();
230a34c753fSRafael Auler 
231a34c753fSRafael Auler   auto BC = std::make_unique<BinaryContext>(
232a34c753fSRafael Auler       std::move(Ctx), std::move(DwCtx), std::move(TheTriple), TheTarget,
233a34c753fSRafael Auler       std::string(TripleName), std::move(MCE), std::move(MOFI),
234a34c753fSRafael Auler       std::move(AsmInfo), std::move(MII), std::move(STI),
23540c2e0faSMaksim Panchenko       std::move(InstructionPrinter), std::move(MIA), nullptr, std::move(MRI),
23640c2e0faSMaksim Panchenko       std::move(DisAsm));
237a34c753fSRafael Auler 
238a34c753fSRafael Auler   BC->TTypeEncoding = TTypeEncoding;
239a34c753fSRafael Auler   BC->LSDAEncoding = LSDAEncoding;
240a34c753fSRafael Auler 
241a34c753fSRafael Auler   BC->MAB = std::unique_ptr<MCAsmBackend>(
242a34c753fSRafael Auler       BC->TheTarget->createMCAsmBackend(*BC->STI, *BC->MRI, MCTargetOptions()));
243a34c753fSRafael Auler 
244a34c753fSRafael Auler   BC->setFilename(File->getFileName());
245a34c753fSRafael Auler 
246a34c753fSRafael Auler   BC->HasFixedLoadAddress = !IsPIC;
247a34c753fSRafael Auler 
248a34c753fSRafael Auler   return BC;
249a34c753fSRafael Auler }
250a34c753fSRafael Auler 
251a34c753fSRafael Auler bool BinaryContext::forceSymbolRelocations(StringRef SymbolName) const {
25240c2e0faSMaksim Panchenko   if (opts::HotText &&
25340c2e0faSMaksim Panchenko       (SymbolName == "__hot_start" || SymbolName == "__hot_end"))
254a34c753fSRafael Auler     return true;
255a34c753fSRafael Auler 
25640c2e0faSMaksim Panchenko   if (opts::HotData &&
25740c2e0faSMaksim Panchenko       (SymbolName == "__hot_data_start" || SymbolName == "__hot_data_end"))
258a34c753fSRafael Auler     return true;
259a34c753fSRafael Auler 
260a34c753fSRafael Auler   if (SymbolName == "_end")
261a34c753fSRafael Auler     return true;
262a34c753fSRafael Auler 
263a34c753fSRafael Auler   return false;
264a34c753fSRafael Auler }
265a34c753fSRafael Auler 
266a34c753fSRafael Auler std::unique_ptr<MCObjectWriter>
267a34c753fSRafael Auler BinaryContext::createObjectWriter(raw_pwrite_stream &OS) {
268a34c753fSRafael Auler   return MAB->createObjectWriter(OS);
269a34c753fSRafael Auler }
270a34c753fSRafael Auler 
271a34c753fSRafael Auler bool BinaryContext::validateObjectNesting() const {
272a34c753fSRafael Auler   auto Itr = BinaryDataMap.begin();
273a34c753fSRafael Auler   auto End = BinaryDataMap.end();
274a34c753fSRafael Auler   bool Valid = true;
275a34c753fSRafael Auler   while (Itr != End) {
276a34c753fSRafael Auler     auto Next = std::next(Itr);
277a34c753fSRafael Auler     while (Next != End &&
278a34c753fSRafael Auler            Itr->second->getSection() == Next->second->getSection() &&
279a34c753fSRafael Auler            Itr->second->containsRange(Next->second->getAddress(),
280a34c753fSRafael Auler                                       Next->second->getSize())) {
281a34c753fSRafael Auler       if (Next->second->Parent != Itr->second) {
282a34c753fSRafael Auler         errs() << "BOLT-WARNING: object nesting incorrect for:\n"
283a34c753fSRafael Auler                << "BOLT-WARNING:  " << *Itr->second << "\n"
284a34c753fSRafael Auler                << "BOLT-WARNING:  " << *Next->second << "\n";
285a34c753fSRafael Auler         Valid = false;
286a34c753fSRafael Auler       }
287a34c753fSRafael Auler       ++Next;
288a34c753fSRafael Auler     }
289a34c753fSRafael Auler     Itr = Next;
290a34c753fSRafael Auler   }
291a34c753fSRafael Auler   return Valid;
292a34c753fSRafael Auler }
293a34c753fSRafael Auler 
294a34c753fSRafael Auler bool BinaryContext::validateHoles() const {
295a34c753fSRafael Auler   bool Valid = true;
296a34c753fSRafael Auler   for (BinarySection &Section : sections()) {
297a34c753fSRafael Auler     for (const Relocation &Rel : Section.relocations()) {
298a34c753fSRafael Auler       uint64_t RelAddr = Rel.Offset + Section.getAddress();
299a34c753fSRafael Auler       const BinaryData *BD = getBinaryDataContainingAddress(RelAddr);
300a34c753fSRafael Auler       if (!BD) {
301a34c753fSRafael Auler         errs() << "BOLT-WARNING: no BinaryData found for relocation at address"
302a34c753fSRafael Auler                << " 0x" << Twine::utohexstr(RelAddr) << " in "
303a34c753fSRafael Auler                << Section.getName() << "\n";
304a34c753fSRafael Auler         Valid = false;
305a34c753fSRafael Auler       } else if (!BD->getAtomicRoot()) {
306a34c753fSRafael Auler         errs() << "BOLT-WARNING: no atomic BinaryData found for relocation at "
307a34c753fSRafael Auler                << "address 0x" << Twine::utohexstr(RelAddr) << " in "
308a34c753fSRafael Auler                << Section.getName() << "\n";
309a34c753fSRafael Auler         Valid = false;
310a34c753fSRafael Auler       }
311a34c753fSRafael Auler     }
312a34c753fSRafael Auler   }
313a34c753fSRafael Auler   return Valid;
314a34c753fSRafael Auler }
315a34c753fSRafael Auler 
316a34c753fSRafael Auler void BinaryContext::updateObjectNesting(BinaryDataMapType::iterator GAI) {
317a34c753fSRafael Auler   const uint64_t Address = GAI->second->getAddress();
318a34c753fSRafael Auler   const uint64_t Size = GAI->second->getSize();
319a34c753fSRafael Auler 
32040c2e0faSMaksim Panchenko   auto fixParents = [&](BinaryDataMapType::iterator Itr,
32140c2e0faSMaksim Panchenko                         BinaryData *NewParent) {
322a34c753fSRafael Auler     BinaryData *OldParent = Itr->second->Parent;
323a34c753fSRafael Auler     Itr->second->Parent = NewParent;
324a34c753fSRafael Auler     ++Itr;
325a34c753fSRafael Auler     while (Itr != BinaryDataMap.end() && OldParent &&
326a34c753fSRafael Auler            Itr->second->Parent == OldParent) {
327a34c753fSRafael Auler       Itr->second->Parent = NewParent;
328a34c753fSRafael Auler       ++Itr;
329a34c753fSRafael Auler     }
330a34c753fSRafael Auler   };
331a34c753fSRafael Auler 
332a34c753fSRafael Auler   // Check if the previous symbol contains the newly added symbol.
333a34c753fSRafael Auler   if (GAI != BinaryDataMap.begin()) {
334a34c753fSRafael Auler     BinaryData *Prev = std::prev(GAI)->second;
335a34c753fSRafael Auler     while (Prev) {
336a34c753fSRafael Auler       if (Prev->getSection() == GAI->second->getSection() &&
337a34c753fSRafael Auler           Prev->containsRange(Address, Size)) {
338a34c753fSRafael Auler         fixParents(GAI, Prev);
339a34c753fSRafael Auler       } else {
340a34c753fSRafael Auler         fixParents(GAI, nullptr);
341a34c753fSRafael Auler       }
342a34c753fSRafael Auler       Prev = Prev->Parent;
343a34c753fSRafael Auler     }
344a34c753fSRafael Auler   }
345a34c753fSRafael Auler 
346a34c753fSRafael Auler   // Check if the newly added symbol contains any subsequent symbols.
347a34c753fSRafael Auler   if (Size != 0) {
348a34c753fSRafael Auler     BinaryData *BD = GAI->second->Parent ? GAI->second->Parent : GAI->second;
349a34c753fSRafael Auler     auto Itr = std::next(GAI);
35040c2e0faSMaksim Panchenko     while (
35140c2e0faSMaksim Panchenko         Itr != BinaryDataMap.end() &&
35240c2e0faSMaksim Panchenko         BD->containsRange(Itr->second->getAddress(), Itr->second->getSize())) {
353a34c753fSRafael Auler       Itr->second->Parent = BD;
354a34c753fSRafael Auler       ++Itr;
355a34c753fSRafael Auler     }
356a34c753fSRafael Auler   }
357a34c753fSRafael Auler }
358a34c753fSRafael Auler 
359a34c753fSRafael Auler iterator_range<BinaryContext::binary_data_iterator>
360a34c753fSRafael Auler BinaryContext::getSubBinaryData(BinaryData *BD) {
361a34c753fSRafael Auler   auto Start = std::next(BinaryDataMap.find(BD->getAddress()));
362a34c753fSRafael Auler   auto End = Start;
3633652483cSRafael Auler   while (End != BinaryDataMap.end() && BD->isAncestorOf(End->second))
364a34c753fSRafael Auler     ++End;
365a34c753fSRafael Auler   return make_range(Start, End);
366a34c753fSRafael Auler }
367a34c753fSRafael Auler 
368a34c753fSRafael Auler std::pair<const MCSymbol *, uint64_t>
369a34c753fSRafael Auler BinaryContext::handleAddressRef(uint64_t Address, BinaryFunction &BF,
370a34c753fSRafael Auler                                 bool IsPCRel) {
371a34c753fSRafael Auler   uint64_t Addend = 0;
372a34c753fSRafael Auler 
373a34c753fSRafael Auler   if (isAArch64()) {
374a34c753fSRafael Auler     // Check if this is an access to a constant island and create bookkeeping
375a34c753fSRafael Auler     // to keep track of it and emit it later as part of this function.
376a34c753fSRafael Auler     if (MCSymbol *IslandSym = BF.getOrCreateIslandAccess(Address))
377a34c753fSRafael Auler       return std::make_pair(IslandSym, Addend);
378a34c753fSRafael Auler 
379a34c753fSRafael Auler     // Detect custom code written in assembly that refers to arbitrary
380a34c753fSRafael Auler     // constant islands from other functions. Write this reference so we
381a34c753fSRafael Auler     // can pull this constant island and emit it as part of this function
382a34c753fSRafael Auler     // too.
383a34c753fSRafael Auler     auto IslandIter = AddressToConstantIslandMap.lower_bound(Address);
384a34c753fSRafael Auler     if (IslandIter != AddressToConstantIslandMap.end()) {
385a34c753fSRafael Auler       if (MCSymbol *IslandSym =
386a34c753fSRafael Auler               IslandIter->second->getOrCreateProxyIslandAccess(Address, BF)) {
387a34c753fSRafael Auler         BF.createIslandDependency(IslandSym, IslandIter->second);
388a34c753fSRafael Auler         return std::make_pair(IslandSym, Addend);
389a34c753fSRafael Auler       }
390a34c753fSRafael Auler     }
391a34c753fSRafael Auler   }
392a34c753fSRafael Auler 
393a34c753fSRafael Auler   // Note that the address does not necessarily have to reside inside
394a34c753fSRafael Auler   // a section, it could be an absolute address too.
395a34c753fSRafael Auler   ErrorOr<BinarySection &> Section = getSectionForAddress(Address);
396a34c753fSRafael Auler   if (Section && Section->isText()) {
397a34c753fSRafael Auler     if (BF.containsAddress(Address, /*UseMaxSize=*/isAArch64())) {
398a34c753fSRafael Auler       if (Address != BF.getAddress()) {
399a34c753fSRafael Auler         // The address could potentially escape. Mark it as another entry
400a34c753fSRafael Auler         // point into the function.
401a34c753fSRafael Auler         if (opts::Verbosity >= 1) {
402a34c753fSRafael Auler           outs() << "BOLT-INFO: potentially escaped address 0x"
40340c2e0faSMaksim Panchenko                  << Twine::utohexstr(Address) << " in function " << BF << '\n';
404a34c753fSRafael Auler         }
405a34c753fSRafael Auler         BF.HasInternalLabelReference = true;
406a34c753fSRafael Auler         return std::make_pair(
40740c2e0faSMaksim Panchenko             BF.addEntryPointAtOffset(Address - BF.getAddress()), Addend);
408a34c753fSRafael Auler       }
409a34c753fSRafael Auler     } else {
410a34c753fSRafael Auler       BF.InterproceduralReferences.insert(Address);
411a34c753fSRafael Auler     }
412a34c753fSRafael Auler   }
413a34c753fSRafael Auler 
414a34c753fSRafael Auler   // With relocations, catch jump table references outside of the basic block
415a34c753fSRafael Auler   // containing the indirect jump.
416a34c753fSRafael Auler   if (HasRelocations) {
417a34c753fSRafael Auler     const MemoryContentsType MemType = analyzeMemoryAt(Address, BF);
418a34c753fSRafael Auler     if (MemType == MemoryContentsType::POSSIBLE_PIC_JUMP_TABLE && IsPCRel) {
419a34c753fSRafael Auler       const MCSymbol *Symbol =
420a34c753fSRafael Auler           getOrCreateJumpTable(BF, Address, JumpTable::JTT_PIC);
421a34c753fSRafael Auler 
422a34c753fSRafael Auler       return std::make_pair(Symbol, Addend);
423a34c753fSRafael Auler     }
424a34c753fSRafael Auler   }
425a34c753fSRafael Auler 
4263652483cSRafael Auler   if (BinaryData *BD = getBinaryDataContainingAddress(Address))
427a34c753fSRafael Auler     return std::make_pair(BD->getSymbol(), Address - BD->getAddress());
428a34c753fSRafael Auler 
429a34c753fSRafael Auler   // TODO: use DWARF info to get size/alignment here?
430a34c753fSRafael Auler   MCSymbol *TargetSymbol = getOrCreateGlobalSymbol(Address, "DATAat");
431a34c753fSRafael Auler   LLVM_DEBUG(dbgs() << "Created symbol " << TargetSymbol->getName() << '\n');
432a34c753fSRafael Auler   return std::make_pair(TargetSymbol, Addend);
433a34c753fSRafael Auler }
434a34c753fSRafael Auler 
43540c2e0faSMaksim Panchenko MemoryContentsType BinaryContext::analyzeMemoryAt(uint64_t Address,
43640c2e0faSMaksim Panchenko                                                   BinaryFunction &BF) {
437a34c753fSRafael Auler   if (!isX86())
438a34c753fSRafael Auler     return MemoryContentsType::UNKNOWN;
439a34c753fSRafael Auler 
440a34c753fSRafael Auler   ErrorOr<BinarySection &> Section = getSectionForAddress(Address);
441a34c753fSRafael Auler   if (!Section) {
442a34c753fSRafael Auler     // No section - possibly an absolute address. Since we don't allow
443a34c753fSRafael Auler     // internal function addresses to escape the function scope - we
444a34c753fSRafael Auler     // consider it a tail call.
445a34c753fSRafael Auler     if (opts::Verbosity > 1) {
446a34c753fSRafael Auler       errs() << "BOLT-WARNING: no section for address 0x"
44740c2e0faSMaksim Panchenko              << Twine::utohexstr(Address) << " referenced from function " << BF
44840c2e0faSMaksim Panchenko              << '\n';
449a34c753fSRafael Auler     }
450a34c753fSRafael Auler     return MemoryContentsType::UNKNOWN;
451a34c753fSRafael Auler   }
452a34c753fSRafael Auler 
453a34c753fSRafael Auler   if (Section->isVirtual()) {
454a34c753fSRafael Auler     // The contents are filled at runtime.
455a34c753fSRafael Auler     return MemoryContentsType::UNKNOWN;
456a34c753fSRafael Auler   }
457a34c753fSRafael Auler 
458a34c753fSRafael Auler   // No support for jump tables in code yet.
459a34c753fSRafael Auler   if (Section->isText())
460a34c753fSRafael Auler     return MemoryContentsType::UNKNOWN;
461a34c753fSRafael Auler 
462a34c753fSRafael Auler   // Start with checking for PIC jump table. We expect non-PIC jump tables
463a34c753fSRafael Auler   // to have high 32 bits set to 0.
464a34c753fSRafael Auler   if (analyzeJumpTable(Address, JumpTable::JTT_PIC, BF))
465a34c753fSRafael Auler     return MemoryContentsType::POSSIBLE_PIC_JUMP_TABLE;
466a34c753fSRafael Auler 
467a34c753fSRafael Auler   if (analyzeJumpTable(Address, JumpTable::JTT_NORMAL, BF))
468a34c753fSRafael Auler     return MemoryContentsType::POSSIBLE_JUMP_TABLE;
469a34c753fSRafael Auler 
470a34c753fSRafael Auler   return MemoryContentsType::UNKNOWN;
471a34c753fSRafael Auler }
472a34c753fSRafael Auler 
4736aa735ceSAmir Ayupov /// Check if <fragment restored name> == <parent restored name>.cold(.\d+)?
4746aa735ceSAmir Ayupov bool isPotentialFragmentByName(BinaryFunction &Fragment,
4756aa735ceSAmir Ayupov                                BinaryFunction &Parent) {
4766aa735ceSAmir Ayupov   for (StringRef Name : Parent.getNames()) {
4776aa735ceSAmir Ayupov     std::string NamePrefix = Regex::escape(NameResolver::restore(Name));
4786aa735ceSAmir Ayupov     std::string NameRegex = Twine(NamePrefix, "\\.cold(\\.[0-9]+)?").str();
4796aa735ceSAmir Ayupov     if (Fragment.hasRestoredNameRegex(NameRegex))
4806aa735ceSAmir Ayupov       return true;
4816aa735ceSAmir Ayupov   }
4826aa735ceSAmir Ayupov   return false;
4836aa735ceSAmir Ayupov }
4846aa735ceSAmir Ayupov 
485a34c753fSRafael Auler bool BinaryContext::analyzeJumpTable(const uint64_t Address,
486a34c753fSRafael Auler                                      const JumpTable::JumpTableType Type,
487a34c753fSRafael Auler                                      BinaryFunction &BF,
488a34c753fSRafael Auler                                      const uint64_t NextJTAddress,
489a34c753fSRafael Auler                                      JumpTable::OffsetsType *Offsets) {
490a34c753fSRafael Auler   // Is one of the targets __builtin_unreachable?
491a34c753fSRafael Auler   bool HasUnreachable = false;
492a34c753fSRafael Auler 
493a34c753fSRafael Auler   // Number of targets other than __builtin_unreachable.
494a34c753fSRafael Auler   uint64_t NumRealEntries = 0;
495a34c753fSRafael Auler 
496a34c753fSRafael Auler   constexpr uint64_t INVALID_OFFSET = std::numeric_limits<uint64_t>::max();
497a34c753fSRafael Auler   auto addOffset = [&](uint64_t Offset) {
498a34c753fSRafael Auler     if (Offsets)
499a34c753fSRafael Auler       Offsets->emplace_back(Offset);
500a34c753fSRafael Auler   };
501a34c753fSRafael Auler 
502a34c753fSRafael Auler   auto doesBelongToFunction = [&](const uint64_t Addr,
503a34c753fSRafael Auler                                   BinaryFunction *TargetBF) -> bool {
504a34c753fSRafael Auler     if (BF.containsAddress(Addr))
505a34c753fSRafael Auler       return true;
506a34c753fSRafael Auler     // Nothing to do if we failed to identify the containing function.
507a34c753fSRafael Auler     if (!TargetBF)
508a34c753fSRafael Auler       return false;
509a34c753fSRafael Auler     // Case 1: check if BF is a fragment and TargetBF is its parent.
510a34c753fSRafael Auler     if (BF.isFragment()) {
5116aa735ceSAmir Ayupov       // Parent function may or may not be already registered.
5126aa735ceSAmir Ayupov       // Set parent link based on function name matching heuristic.
5136aa735ceSAmir Ayupov       return registerFragment(BF, *TargetBF);
514a34c753fSRafael Auler     }
515a34c753fSRafael Auler     // Case 2: check if TargetBF is a fragment and BF is its parent.
5166aa735ceSAmir Ayupov     return TargetBF->isFragment() && registerFragment(*TargetBF, BF);
517a34c753fSRafael Auler   };
518a34c753fSRafael Auler 
519a34c753fSRafael Auler   ErrorOr<BinarySection &> Section = getSectionForAddress(Address);
520a34c753fSRafael Auler   if (!Section)
521a34c753fSRafael Auler     return false;
522a34c753fSRafael Auler 
523a34c753fSRafael Auler   // The upper bound is defined by containing object, section limits, and
524a34c753fSRafael Auler   // the next jump table in memory.
525a34c753fSRafael Auler   uint64_t UpperBound = Section->getEndAddress();
526a34c753fSRafael Auler   const BinaryData *JumpTableBD = getBinaryDataAtAddress(Address);
527a34c753fSRafael Auler   if (JumpTableBD && JumpTableBD->getSize()) {
528a34c753fSRafael Auler     assert(JumpTableBD->getEndAddress() <= UpperBound &&
529a34c753fSRafael Auler            "data object cannot cross a section boundary");
530a34c753fSRafael Auler     UpperBound = JumpTableBD->getEndAddress();
531a34c753fSRafael Auler   }
5323652483cSRafael Auler   if (NextJTAddress)
533a34c753fSRafael Auler     UpperBound = std::min(NextJTAddress, UpperBound);
534a34c753fSRafael Auler 
535a34c753fSRafael Auler   LLVM_DEBUG(dbgs() << "BOLT-DEBUG: analyzeJumpTable in " << BF.getPrintName()
536a34c753fSRafael Auler                     << '\n');
537a34c753fSRafael Auler   const uint64_t EntrySize = getJumpTableEntrySize(Type);
538a34c753fSRafael Auler   for (uint64_t EntryAddress = Address; EntryAddress <= UpperBound - EntrySize;
539a34c753fSRafael Auler        EntryAddress += EntrySize) {
540a34c753fSRafael Auler     LLVM_DEBUG(dbgs() << "  * Checking 0x" << Twine::utohexstr(EntryAddress)
541a34c753fSRafael Auler                       << " -> ");
542a34c753fSRafael Auler     // Check if there's a proper relocation against the jump table entry.
543a34c753fSRafael Auler     if (HasRelocations) {
544a34c753fSRafael Auler       if (Type == JumpTable::JTT_PIC &&
545a34c753fSRafael Auler           !DataPCRelocations.count(EntryAddress)) {
546a34c753fSRafael Auler         LLVM_DEBUG(
547a34c753fSRafael Auler             dbgs() << "FAIL: JTT_PIC table, no relocation for this address\n");
548a34c753fSRafael Auler         break;
549a34c753fSRafael Auler       }
550a34c753fSRafael Auler       if (Type == JumpTable::JTT_NORMAL && !getRelocationAt(EntryAddress)) {
551a34c753fSRafael Auler         LLVM_DEBUG(
552a34c753fSRafael Auler             dbgs()
553a34c753fSRafael Auler             << "FAIL: JTT_NORMAL table, no relocation for this address\n");
554a34c753fSRafael Auler         break;
555a34c753fSRafael Auler       }
556a34c753fSRafael Auler     }
557a34c753fSRafael Auler 
55840c2e0faSMaksim Panchenko     const uint64_t Value =
55940c2e0faSMaksim Panchenko         (Type == JumpTable::JTT_PIC)
560a34c753fSRafael Auler             ? Address + *getSignedValueAtAddress(EntryAddress, EntrySize)
561a34c753fSRafael Auler             : *getPointerAtAddress(EntryAddress);
562a34c753fSRafael Auler 
563a34c753fSRafael Auler     // __builtin_unreachable() case.
564a34c753fSRafael Auler     if (Value == BF.getAddress() + BF.getSize()) {
565a34c753fSRafael Auler       addOffset(Value - BF.getAddress());
566a34c753fSRafael Auler       HasUnreachable = true;
567a34c753fSRafael Auler       LLVM_DEBUG(dbgs() << "OK: __builtin_unreachable\n");
568a34c753fSRafael Auler       continue;
569a34c753fSRafael Auler     }
570a34c753fSRafael Auler 
571a34c753fSRafael Auler     // Function or one of its fragments.
572a34c753fSRafael Auler     BinaryFunction *TargetBF = getBinaryFunctionContainingAddress(Value);
573a34c753fSRafael Auler 
574a34c753fSRafael Auler     // We assume that a jump table cannot have function start as an entry.
575a34c753fSRafael Auler     if (!doesBelongToFunction(Value, TargetBF) || Value == BF.getAddress()) {
576a34c753fSRafael Auler       LLVM_DEBUG({
577a34c753fSRafael Auler         if (!BF.containsAddress(Value)) {
578a34c753fSRafael Auler           dbgs() << "FAIL: function doesn't contain this address\n";
579a34c753fSRafael Auler           if (TargetBF) {
580a34c753fSRafael Auler             dbgs() << "  ! function containing this address: "
581a34c753fSRafael Auler                    << TargetBF->getPrintName() << '\n';
582a34c753fSRafael Auler             if (TargetBF->isFragment())
583a34c753fSRafael Auler               dbgs() << "  ! is a fragment\n";
5846aa735ceSAmir Ayupov             for (BinaryFunction *TargetParent : TargetBF->ParentFragments)
585a34c753fSRafael Auler               dbgs() << "  ! its parent is "
586a34c753fSRafael Auler                      << (TargetParent ? TargetParent->getPrintName() : "(none)")
587a34c753fSRafael Auler                      << '\n';
588a34c753fSRafael Auler           }
589a34c753fSRafael Auler         }
590a34c753fSRafael Auler         if (Value == BF.getAddress())
591a34c753fSRafael Auler           dbgs() << "FAIL: jump table cannot have function start as an entry\n";
592a34c753fSRafael Auler       });
593a34c753fSRafael Auler       break;
594a34c753fSRafael Auler     }
595a34c753fSRafael Auler 
596a34c753fSRafael Auler     // Check there's an instruction at this offset.
597a34c753fSRafael Auler     if (TargetBF->getState() == BinaryFunction::State::Disassembled &&
598a34c753fSRafael Auler         !TargetBF->getInstructionAtOffset(Value - TargetBF->getAddress())) {
599a34c753fSRafael Auler       LLVM_DEBUG(dbgs() << "FAIL: no instruction at this offset\n");
600a34c753fSRafael Auler       break;
601a34c753fSRafael Auler     }
602a34c753fSRafael Auler 
603a34c753fSRafael Auler     ++NumRealEntries;
604a34c753fSRafael Auler 
605a34c753fSRafael Auler     if (TargetBF == &BF) {
606a34c753fSRafael Auler       // Address inside the function.
607a34c753fSRafael Auler       addOffset(Value - TargetBF->getAddress());
608a34c753fSRafael Auler       LLVM_DEBUG(dbgs() << "OK: real entry\n");
609a34c753fSRafael Auler     } else {
610a34c753fSRafael Auler       // Address in split fragment.
611a34c753fSRafael Auler       BF.setHasSplitJumpTable(true);
612a34c753fSRafael Auler       // Add invalid offset for proper identification of jump table size.
613a34c753fSRafael Auler       addOffset(INVALID_OFFSET);
6146aa735ceSAmir Ayupov       LLVM_DEBUG(dbgs() << "OK: address in split fragment "
6156aa735ceSAmir Ayupov                         << TargetBF->getPrintName() << '\n');
616a34c753fSRafael Auler     }
617a34c753fSRafael Auler   }
618a34c753fSRafael Auler 
619a34c753fSRafael Auler   // It's a jump table if the number of real entries is more than 1, or there's
620a34c753fSRafael Auler   // one real entry and "unreachable" targets. If there are only multiple
621a34c753fSRafael Auler   // "unreachable" targets, then it's not a jump table.
622a34c753fSRafael Auler   return NumRealEntries + HasUnreachable >= 2;
623a34c753fSRafael Auler }
624a34c753fSRafael Auler 
625a34c753fSRafael Auler void BinaryContext::populateJumpTables() {
626a34c753fSRafael Auler   LLVM_DEBUG(dbgs() << "DataPCRelocations: " << DataPCRelocations.size()
627a34c753fSRafael Auler                     << '\n');
628a34c753fSRafael Auler   for (auto JTI = JumpTables.begin(), JTE = JumpTables.end(); JTI != JTE;
629a34c753fSRafael Auler        ++JTI) {
630a34c753fSRafael Auler     JumpTable *JT = JTI->second;
631a34c753fSRafael Auler     BinaryFunction &BF = *JT->Parent;
632a34c753fSRafael Auler 
633a34c753fSRafael Auler     if (!BF.isSimple())
634a34c753fSRafael Auler       continue;
635a34c753fSRafael Auler 
636a34c753fSRafael Auler     uint64_t NextJTAddress = 0;
637a34c753fSRafael Auler     auto NextJTI = std::next(JTI);
6383652483cSRafael Auler     if (NextJTI != JTE)
639a34c753fSRafael Auler       NextJTAddress = NextJTI->second->getAddress();
640a34c753fSRafael Auler 
641a34c753fSRafael Auler     const bool Success = analyzeJumpTable(JT->getAddress(), JT->Type, BF,
642a34c753fSRafael Auler                                           NextJTAddress, &JT->OffsetEntries);
643a34c753fSRafael Auler     if (!Success) {
644a34c753fSRafael Auler       dbgs() << "failed to analyze jump table in function " << BF << '\n';
645a34c753fSRafael Auler       JT->print(dbgs());
646a34c753fSRafael Auler       if (NextJTI != JTE) {
647a34c753fSRafael Auler         dbgs() << "next jump table at 0x"
648a34c753fSRafael Auler                << Twine::utohexstr(NextJTI->second->getAddress())
649a34c753fSRafael Auler                << " belongs to function " << *NextJTI->second->Parent << '\n';
650a34c753fSRafael Auler         NextJTI->second->print(dbgs());
651a34c753fSRafael Auler       }
652a34c753fSRafael Auler       llvm_unreachable("jump table heuristic failure");
653a34c753fSRafael Auler     }
654a34c753fSRafael Auler 
655a34c753fSRafael Auler     for (uint64_t EntryOffset : JT->OffsetEntries) {
656a34c753fSRafael Auler       if (EntryOffset == BF.getSize())
657a34c753fSRafael Auler         BF.IgnoredBranches.emplace_back(EntryOffset, BF.getSize());
658a34c753fSRafael Auler       else
659a34c753fSRafael Auler         BF.registerReferencedOffset(EntryOffset);
660a34c753fSRafael Auler     }
661a34c753fSRafael Auler 
662a34c753fSRafael Auler     // In strict mode, erase PC-relative relocation record. Later we check that
663a34c753fSRafael Auler     // all such records are erased and thus have been accounted for.
664a34c753fSRafael Auler     if (opts::StrictMode && JT->Type == JumpTable::JTT_PIC) {
665a34c753fSRafael Auler       for (uint64_t Address = JT->getAddress();
666a34c753fSRafael Auler            Address < JT->getAddress() + JT->getSize();
667a34c753fSRafael Auler            Address += JT->EntrySize) {
668a34c753fSRafael Auler         DataPCRelocations.erase(DataPCRelocations.find(Address));
669a34c753fSRafael Auler       }
670a34c753fSRafael Auler     }
671a34c753fSRafael Auler 
672a34c753fSRafael Auler     // Mark to skip the function and all its fragments.
673a34c753fSRafael Auler     if (BF.hasSplitJumpTable())
6746aa735ceSAmir Ayupov       FragmentsToSkip.push_back(&BF);
675a34c753fSRafael Auler   }
676a34c753fSRafael Auler 
677a34c753fSRafael Auler   if (opts::StrictMode && DataPCRelocations.size()) {
678a34c753fSRafael Auler     LLVM_DEBUG({
679a34c753fSRafael Auler       dbgs() << DataPCRelocations.size()
680a34c753fSRafael Auler              << " unclaimed PC-relative relocations left in data:\n";
681a34c753fSRafael Auler       for (uint64_t Reloc : DataPCRelocations)
682a34c753fSRafael Auler         dbgs() << Twine::utohexstr(Reloc) << '\n';
683a34c753fSRafael Auler     });
684a34c753fSRafael Auler     assert(0 && "unclaimed PC-relative relocations left in data\n");
685a34c753fSRafael Auler   }
686a34c753fSRafael Auler   clearList(DataPCRelocations);
687a34c753fSRafael Auler }
6886aa735ceSAmir Ayupov 
6896aa735ceSAmir Ayupov void BinaryContext::skipMarkedFragments() {
6906aa735ceSAmir Ayupov   // Unique functions in the vector.
6916aa735ceSAmir Ayupov   std::unordered_set<BinaryFunction *> UniqueFunctions(FragmentsToSkip.begin(),
6926aa735ceSAmir Ayupov                                                        FragmentsToSkip.end());
6936aa735ceSAmir Ayupov   // Copy the functions back to FragmentsToSkip.
6946aa735ceSAmir Ayupov   FragmentsToSkip.assign(UniqueFunctions.begin(), UniqueFunctions.end());
6956aa735ceSAmir Ayupov   auto addToWorklist = [&](BinaryFunction *Function) -> void {
6966aa735ceSAmir Ayupov     if (UniqueFunctions.count(Function))
6976aa735ceSAmir Ayupov       return;
6986aa735ceSAmir Ayupov     FragmentsToSkip.push_back(Function);
6996aa735ceSAmir Ayupov     UniqueFunctions.insert(Function);
7006aa735ceSAmir Ayupov   };
7016aa735ceSAmir Ayupov   // Functions containing split jump tables need to be skipped with all
7026aa735ceSAmir Ayupov   // fragments (transitively).
7036aa735ceSAmir Ayupov   for (size_t I = 0; I != FragmentsToSkip.size(); I++) {
7046aa735ceSAmir Ayupov     BinaryFunction *BF = FragmentsToSkip[I];
7056aa735ceSAmir Ayupov     assert(UniqueFunctions.count(BF) &&
7066aa735ceSAmir Ayupov            "internal error in traversing function fragments");
7076aa735ceSAmir Ayupov     if (opts::Verbosity >= 1)
7086aa735ceSAmir Ayupov       errs() << "BOLT-WARNING: Ignoring " << BF->getPrintName() << '\n';
7096aa735ceSAmir Ayupov     BF->setIgnored();
7106aa735ceSAmir Ayupov     std::for_each(BF->Fragments.begin(), BF->Fragments.end(), addToWorklist);
7116aa735ceSAmir Ayupov     std::for_each(BF->ParentFragments.begin(), BF->ParentFragments.end(),
7126aa735ceSAmir Ayupov                   addToWorklist);
7136aa735ceSAmir Ayupov   }
714641e92d4SMaksim Panchenko   if (!FragmentsToSkip.empty())
715641e92d4SMaksim Panchenko     errs() << "BOLT-WARNING: ignored " << FragmentsToSkip.size() << " function"
716641e92d4SMaksim Panchenko            << (FragmentsToSkip.size() == 1 ? "" : "s")
717641e92d4SMaksim Panchenko            << " due to cold fragments\n";
7186aa735ceSAmir Ayupov   FragmentsToSkip.clear();
719a34c753fSRafael Auler }
720a34c753fSRafael Auler 
72140c2e0faSMaksim Panchenko MCSymbol *BinaryContext::getOrCreateGlobalSymbol(uint64_t Address, Twine Prefix,
722a34c753fSRafael Auler                                                  uint64_t Size,
723a34c753fSRafael Auler                                                  uint16_t Alignment,
724a34c753fSRafael Auler                                                  unsigned Flags) {
725a34c753fSRafael Auler   auto Itr = BinaryDataMap.find(Address);
726a34c753fSRafael Auler   if (Itr != BinaryDataMap.end()) {
727a34c753fSRafael Auler     assert(Itr->second->getSize() == Size || !Size);
728a34c753fSRafael Auler     return Itr->second->getSymbol();
729a34c753fSRafael Auler   }
730a34c753fSRafael Auler 
731a34c753fSRafael Auler   std::string Name = (Prefix + "0x" + Twine::utohexstr(Address)).str();
732a34c753fSRafael Auler   assert(!GlobalSymbols.count(Name) && "created name is not unique");
733a34c753fSRafael Auler   return registerNameAtAddress(Name, Address, Size, Alignment, Flags);
734a34c753fSRafael Auler }
735a34c753fSRafael Auler 
736a34c753fSRafael Auler MCSymbol *BinaryContext::getOrCreateUndefinedGlobalSymbol(StringRef Name) {
737a34c753fSRafael Auler   return Ctx->getOrCreateSymbol(Name);
738a34c753fSRafael Auler }
739a34c753fSRafael Auler 
740a34c753fSRafael Auler BinaryFunction *BinaryContext::createBinaryFunction(
741a34c753fSRafael Auler     const std::string &Name, BinarySection &Section, uint64_t Address,
742a34c753fSRafael Auler     uint64_t Size, uint64_t SymbolSize, uint16_t Alignment) {
743a34c753fSRafael Auler   auto Result = BinaryFunctions.emplace(
744a34c753fSRafael Auler       Address, BinaryFunction(Name, Section, Address, Size, *this));
745a34c753fSRafael Auler   assert(Result.second == true && "unexpected duplicate function");
746a34c753fSRafael Auler   BinaryFunction *BF = &Result.first->second;
747a34c753fSRafael Auler   registerNameAtAddress(Name, Address, SymbolSize ? SymbolSize : Size,
748a34c753fSRafael Auler                         Alignment);
749a34c753fSRafael Auler   setSymbolToFunctionMap(BF->getSymbol(), BF);
750a34c753fSRafael Auler   return BF;
751a34c753fSRafael Auler }
752a34c753fSRafael Auler 
753a34c753fSRafael Auler const MCSymbol *
754a34c753fSRafael Auler BinaryContext::getOrCreateJumpTable(BinaryFunction &Function, uint64_t Address,
755a34c753fSRafael Auler                                     JumpTable::JumpTableType Type) {
756a34c753fSRafael Auler   if (JumpTable *JT = getJumpTableContainingAddress(Address)) {
757a34c753fSRafael Auler     assert(JT->Type == Type && "jump table types have to match");
758a34c753fSRafael Auler     assert(JT->Parent == &Function &&
759a34c753fSRafael Auler            "cannot re-use jump table of a different function");
760a34c753fSRafael Auler     assert(Address == JT->getAddress() && "unexpected non-empty jump table");
761a34c753fSRafael Auler 
762a34c753fSRafael Auler     return JT->getFirstLabel();
763a34c753fSRafael Auler   }
764a34c753fSRafael Auler 
765a34c753fSRafael Auler   // Re-use the existing symbol if possible.
766a34c753fSRafael Auler   MCSymbol *JTLabel = nullptr;
767a34c753fSRafael Auler   if (BinaryData *Object = getBinaryDataAtAddress(Address)) {
768a34c753fSRafael Auler     if (!isInternalSymbolName(Object->getSymbol()->getName()))
769a34c753fSRafael Auler       JTLabel = Object->getSymbol();
770a34c753fSRafael Auler   }
771a34c753fSRafael Auler 
772a34c753fSRafael Auler   const uint64_t EntrySize = getJumpTableEntrySize(Type);
773a34c753fSRafael Auler   if (!JTLabel) {
774a34c753fSRafael Auler     const std::string JumpTableName = generateJumpTableName(Function, Address);
775a34c753fSRafael Auler     JTLabel = registerNameAtAddress(JumpTableName, Address, 0, EntrySize);
776a34c753fSRafael Auler   }
777a34c753fSRafael Auler 
778a34c753fSRafael Auler   LLVM_DEBUG(dbgs() << "BOLT-DEBUG: creating jump table " << JTLabel->getName()
779a34c753fSRafael Auler                     << " in function " << Function << '\n');
780a34c753fSRafael Auler 
781a34c753fSRafael Auler   JumpTable *JT = new JumpTable(*JTLabel, Address, EntrySize, Type,
782a34c753fSRafael Auler                                 JumpTable::LabelMapType{{0, JTLabel}}, Function,
783a34c753fSRafael Auler                                 *getSectionForAddress(Address));
784a34c753fSRafael Auler   JumpTables.emplace(Address, JT);
785a34c753fSRafael Auler 
786a34c753fSRafael Auler   // Duplicate the entry for the parent function for easy access.
787a34c753fSRafael Auler   Function.JumpTables.emplace(Address, JT);
788a34c753fSRafael Auler 
789a34c753fSRafael Auler   return JTLabel;
790a34c753fSRafael Auler }
791a34c753fSRafael Auler 
792a34c753fSRafael Auler std::pair<uint64_t, const MCSymbol *>
793a34c753fSRafael Auler BinaryContext::duplicateJumpTable(BinaryFunction &Function, JumpTable *JT,
794a34c753fSRafael Auler                                   const MCSymbol *OldLabel) {
795a34c753fSRafael Auler   auto L = scopeLock();
796a34c753fSRafael Auler   unsigned Offset = 0;
797a34c753fSRafael Auler   bool Found = false;
798a34c753fSRafael Auler   for (std::pair<const unsigned, MCSymbol *> Elmt : JT->Labels) {
799a34c753fSRafael Auler     if (Elmt.second != OldLabel)
800a34c753fSRafael Auler       continue;
801a34c753fSRafael Auler     Offset = Elmt.first;
802a34c753fSRafael Auler     Found = true;
803a34c753fSRafael Auler     break;
804a34c753fSRafael Auler   }
805a34c753fSRafael Auler   assert(Found && "Label not found");
806a34c753fSRafael Auler   MCSymbol *NewLabel = Ctx->createNamedTempSymbol("duplicatedJT");
807a34c753fSRafael Auler   JumpTable *NewJT =
808a34c753fSRafael Auler       new JumpTable(*NewLabel, JT->getAddress(), JT->EntrySize, JT->Type,
809a34c753fSRafael Auler                     JumpTable::LabelMapType{{Offset, NewLabel}}, Function,
810a34c753fSRafael Auler                     *getSectionForAddress(JT->getAddress()));
811a34c753fSRafael Auler   NewJT->Entries = JT->Entries;
812a34c753fSRafael Auler   NewJT->Counts = JT->Counts;
813a34c753fSRafael Auler   uint64_t JumpTableID = ++DuplicatedJumpTables;
814a34c753fSRafael Auler   // Invert it to differentiate from regular jump tables whose IDs are their
815a34c753fSRafael Auler   // addresses in the input binary memory space
816a34c753fSRafael Auler   JumpTableID = ~JumpTableID;
817a34c753fSRafael Auler   JumpTables.emplace(JumpTableID, NewJT);
818a34c753fSRafael Auler   Function.JumpTables.emplace(JumpTableID, NewJT);
819a34c753fSRafael Auler   return std::make_pair(JumpTableID, NewLabel);
820a34c753fSRafael Auler }
821a34c753fSRafael Auler 
822a34c753fSRafael Auler std::string BinaryContext::generateJumpTableName(const BinaryFunction &BF,
823a34c753fSRafael Auler                                                  uint64_t Address) {
824a34c753fSRafael Auler   size_t Id;
825a34c753fSRafael Auler   uint64_t Offset = 0;
826a34c753fSRafael Auler   if (const JumpTable *JT = BF.getJumpTableContainingAddress(Address)) {
827a34c753fSRafael Auler     Offset = Address - JT->getAddress();
828a34c753fSRafael Auler     auto Itr = JT->Labels.find(Offset);
8293652483cSRafael Auler     if (Itr != JT->Labels.end())
830a34c753fSRafael Auler       return std::string(Itr->second->getName());
831a34c753fSRafael Auler     Id = JumpTableIds.at(JT->getAddress());
832a34c753fSRafael Auler   } else {
833a34c753fSRafael Auler     Id = JumpTableIds[Address] = BF.JumpTables.size();
834a34c753fSRafael Auler   }
835a34c753fSRafael Auler   return ("JUMP_TABLE/" + BF.getOneName().str() + "." + std::to_string(Id) +
836a34c753fSRafael Auler           (Offset ? ("." + std::to_string(Offset)) : ""));
837a34c753fSRafael Auler }
838a34c753fSRafael Auler 
839a34c753fSRafael Auler bool BinaryContext::hasValidCodePadding(const BinaryFunction &BF) {
840a34c753fSRafael Auler   // FIXME: aarch64 support is missing.
841a34c753fSRafael Auler   if (!isX86())
842a34c753fSRafael Auler     return true;
843a34c753fSRafael Auler 
844a34c753fSRafael Auler   if (BF.getSize() == BF.getMaxSize())
845a34c753fSRafael Auler     return true;
846a34c753fSRafael Auler 
847a34c753fSRafael Auler   ErrorOr<ArrayRef<unsigned char>> FunctionData = BF.getData();
848a34c753fSRafael Auler   assert(FunctionData && "cannot get function as data");
849a34c753fSRafael Auler 
850a34c753fSRafael Auler   uint64_t Offset = BF.getSize();
851a34c753fSRafael Auler   MCInst Instr;
852a34c753fSRafael Auler   uint64_t InstrSize = 0;
853a34c753fSRafael Auler   uint64_t InstrAddress = BF.getAddress() + Offset;
854a34c753fSRafael Auler   using std::placeholders::_1;
855a34c753fSRafael Auler 
856a34c753fSRafael Auler   // Skip instructions that satisfy the predicate condition.
857a34c753fSRafael Auler   auto skipInstructions = [&](std::function<bool(const MCInst &)> Predicate) {
858a34c753fSRafael Auler     const uint64_t StartOffset = Offset;
859a34c753fSRafael Auler     for (; Offset < BF.getMaxSize();
860a34c753fSRafael Auler          Offset += InstrSize, InstrAddress += InstrSize) {
86140c2e0faSMaksim Panchenko       if (!DisAsm->getInstruction(Instr, InstrSize, FunctionData->slice(Offset),
86240c2e0faSMaksim Panchenko                                   InstrAddress, nulls()))
863a34c753fSRafael Auler         break;
864a34c753fSRafael Auler       if (!Predicate(Instr))
865a34c753fSRafael Auler         break;
866a34c753fSRafael Auler     }
867a34c753fSRafael Auler 
868a34c753fSRafael Auler     return Offset - StartOffset;
869a34c753fSRafael Auler   };
870a34c753fSRafael Auler 
871a34c753fSRafael Auler   // Skip a sequence of zero bytes.
872a34c753fSRafael Auler   auto skipZeros = [&]() {
873a34c753fSRafael Auler     const uint64_t StartOffset = Offset;
874a34c753fSRafael Auler     for (; Offset < BF.getMaxSize(); ++Offset)
875a34c753fSRafael Auler       if ((*FunctionData)[Offset] != 0)
876a34c753fSRafael Auler         break;
877a34c753fSRafael Auler 
878a34c753fSRafael Auler     return Offset - StartOffset;
879a34c753fSRafael Auler   };
880a34c753fSRafael Auler 
881a34c753fSRafael Auler   // Accept the whole padding area filled with breakpoints.
882a34c753fSRafael Auler   auto isBreakpoint = std::bind(&MCPlusBuilder::isBreakpoint, MIB.get(), _1);
883a34c753fSRafael Auler   if (skipInstructions(isBreakpoint) && Offset == BF.getMaxSize())
884a34c753fSRafael Auler     return true;
885a34c753fSRafael Auler 
886a34c753fSRafael Auler   auto isNoop = std::bind(&MCPlusBuilder::isNoop, MIB.get(), _1);
887a34c753fSRafael Auler 
888a34c753fSRafael Auler   // Some functions have a jump to the next function or to the padding area
889a34c753fSRafael Auler   // inserted after the body.
890a34c753fSRafael Auler   auto isSkipJump = [&](const MCInst &Instr) {
891a34c753fSRafael Auler     uint64_t TargetAddress = 0;
892a34c753fSRafael Auler     if (MIB->isUnconditionalBranch(Instr) &&
893a34c753fSRafael Auler         MIB->evaluateBranch(Instr, InstrAddress, InstrSize, TargetAddress)) {
894a34c753fSRafael Auler       if (TargetAddress >= InstrAddress + InstrSize &&
895a34c753fSRafael Auler           TargetAddress <= BF.getAddress() + BF.getMaxSize()) {
896a34c753fSRafael Auler         return true;
897a34c753fSRafael Auler       }
898a34c753fSRafael Auler     }
899a34c753fSRafael Auler     return false;
900a34c753fSRafael Auler   };
901a34c753fSRafael Auler 
902a34c753fSRafael Auler   // Skip over nops, jumps, and zero padding. Allow interleaving (this happens).
90340c2e0faSMaksim Panchenko   while (skipInstructions(isNoop) || skipInstructions(isSkipJump) ||
904a34c753fSRafael Auler          skipZeros())
905a34c753fSRafael Auler     ;
906a34c753fSRafael Auler 
907a34c753fSRafael Auler   if (Offset == BF.getMaxSize())
908a34c753fSRafael Auler     return true;
909a34c753fSRafael Auler 
910a34c753fSRafael Auler   if (opts::Verbosity >= 1) {
911a34c753fSRafael Auler     errs() << "BOLT-WARNING: bad padding at address 0x"
912a34c753fSRafael Auler            << Twine::utohexstr(BF.getAddress() + BF.getSize())
91340c2e0faSMaksim Panchenko            << " starting at offset " << (Offset - BF.getSize())
91440c2e0faSMaksim Panchenko            << " in function " << BF << '\n'
915a34c753fSRafael Auler            << FunctionData->slice(BF.getSize(), BF.getMaxSize() - BF.getSize())
916a34c753fSRafael Auler            << '\n';
917a34c753fSRafael Auler   }
918a34c753fSRafael Auler 
919a34c753fSRafael Auler   return false;
920a34c753fSRafael Auler }
921a34c753fSRafael Auler 
922a34c753fSRafael Auler void BinaryContext::adjustCodePadding() {
923a34c753fSRafael Auler   for (auto &BFI : BinaryFunctions) {
924a34c753fSRafael Auler     BinaryFunction &BF = BFI.second;
925a34c753fSRafael Auler     if (!shouldEmit(BF))
926a34c753fSRafael Auler       continue;
927a34c753fSRafael Auler 
928a34c753fSRafael Auler     if (!hasValidCodePadding(BF)) {
929a34c753fSRafael Auler       if (HasRelocations) {
930a34c753fSRafael Auler         if (opts::Verbosity >= 1) {
931a34c753fSRafael Auler           outs() << "BOLT-INFO: function " << BF
932a34c753fSRafael Auler                  << " has invalid padding. Ignoring the function.\n";
933a34c753fSRafael Auler         }
934a34c753fSRafael Auler         BF.setIgnored();
935a34c753fSRafael Auler       } else {
936a34c753fSRafael Auler         BF.setMaxSize(BF.getSize());
937a34c753fSRafael Auler       }
938a34c753fSRafael Auler     }
939a34c753fSRafael Auler   }
940a34c753fSRafael Auler }
941a34c753fSRafael Auler 
94240c2e0faSMaksim Panchenko MCSymbol *BinaryContext::registerNameAtAddress(StringRef Name, uint64_t Address,
943a34c753fSRafael Auler                                                uint64_t Size,
944a34c753fSRafael Auler                                                uint16_t Alignment,
945a34c753fSRafael Auler                                                unsigned Flags) {
946a34c753fSRafael Auler   // Register the name with MCContext.
947a34c753fSRafael Auler   MCSymbol *Symbol = Ctx->getOrCreateSymbol(Name);
948a34c753fSRafael Auler 
949a34c753fSRafael Auler   auto GAI = BinaryDataMap.find(Address);
950a34c753fSRafael Auler   BinaryData *BD;
951a34c753fSRafael Auler   if (GAI == BinaryDataMap.end()) {
952a34c753fSRafael Auler     ErrorOr<BinarySection &> SectionOrErr = getSectionForAddress(Address);
953a34c753fSRafael Auler     BinarySection &Section =
954a34c753fSRafael Auler         SectionOrErr ? SectionOrErr.get() : absoluteSection();
95540c2e0faSMaksim Panchenko     BD = new BinaryData(*Symbol, Address, Size, Alignment ? Alignment : 1,
95640c2e0faSMaksim Panchenko                         Section, Flags);
957a34c753fSRafael Auler     GAI = BinaryDataMap.emplace(Address, BD).first;
958a34c753fSRafael Auler     GlobalSymbols[Name] = BD;
959a34c753fSRafael Auler     updateObjectNesting(GAI);
960a34c753fSRafael Auler   } else {
961a34c753fSRafael Auler     BD = GAI->second;
962a34c753fSRafael Auler     if (!BD->hasName(Name)) {
963a34c753fSRafael Auler       GlobalSymbols[Name] = BD;
964a34c753fSRafael Auler       BD->Symbols.push_back(Symbol);
965a34c753fSRafael Auler     }
966a34c753fSRafael Auler   }
967a34c753fSRafael Auler 
968a34c753fSRafael Auler   return Symbol;
969a34c753fSRafael Auler }
970a34c753fSRafael Auler 
971a34c753fSRafael Auler const BinaryData *
972a34c753fSRafael Auler BinaryContext::getBinaryDataContainingAddressImpl(uint64_t Address) const {
973a34c753fSRafael Auler   auto NI = BinaryDataMap.lower_bound(Address);
974a34c753fSRafael Auler   auto End = BinaryDataMap.end();
975a34c753fSRafael Auler   if ((NI != End && Address == NI->first) ||
976a34c753fSRafael Auler       ((NI != BinaryDataMap.begin()) && (NI-- != BinaryDataMap.begin()))) {
9773652483cSRafael Auler     if (NI->second->containsAddress(Address))
978a34c753fSRafael Auler       return NI->second;
979a34c753fSRafael Auler 
980a34c753fSRafael Auler     // If this is a sub-symbol, see if a parent data contains the address.
981a34c753fSRafael Auler     const BinaryData *BD = NI->second->getParent();
982a34c753fSRafael Auler     while (BD) {
983a34c753fSRafael Auler       if (BD->containsAddress(Address))
984a34c753fSRafael Auler         return BD;
985a34c753fSRafael Auler       BD = BD->getParent();
986a34c753fSRafael Auler     }
987a34c753fSRafael Auler   }
988a34c753fSRafael Auler   return nullptr;
989a34c753fSRafael Auler }
990a34c753fSRafael Auler 
991a34c753fSRafael Auler bool BinaryContext::setBinaryDataSize(uint64_t Address, uint64_t Size) {
992a34c753fSRafael Auler   auto NI = BinaryDataMap.find(Address);
993a34c753fSRafael Auler   assert(NI != BinaryDataMap.end());
994a34c753fSRafael Auler   if (NI == BinaryDataMap.end())
995a34c753fSRafael Auler     return false;
996a34c753fSRafael Auler   // TODO: it's possible that a jump table starts at the same address
997a34c753fSRafael Auler   // as a larger blob of private data.  When we set the size of the
998a34c753fSRafael Auler   // jump table, it might be smaller than the total blob size.  In this
999a34c753fSRafael Auler   // case we just leave the original size since (currently) it won't really
1000933df2a4SMaksim Panchenko   // affect anything.
1001a34c753fSRafael Auler   assert((!NI->second->Size || NI->second->Size == Size ||
1002a34c753fSRafael Auler           (NI->second->isJumpTable() && NI->second->Size > Size)) &&
1003a34c753fSRafael Auler          "can't change the size of a symbol that has already had its "
1004a34c753fSRafael Auler          "size set");
1005a34c753fSRafael Auler   if (!NI->second->Size) {
1006a34c753fSRafael Auler     NI->second->Size = Size;
1007a34c753fSRafael Auler     updateObjectNesting(NI);
1008a34c753fSRafael Auler     return true;
1009a34c753fSRafael Auler   }
1010a34c753fSRafael Auler   return false;
1011a34c753fSRafael Auler }
1012a34c753fSRafael Auler 
1013a34c753fSRafael Auler void BinaryContext::generateSymbolHashes() {
1014a34c753fSRafael Auler   auto isPadding = [](const BinaryData &BD) {
1015a34c753fSRafael Auler     StringRef Contents = BD.getSection().getContents();
1016a34c753fSRafael Auler     StringRef SymData = Contents.substr(BD.getOffset(), BD.getSize());
1017a34c753fSRafael Auler     return (BD.getName().startswith("HOLEat") ||
1018a34c753fSRafael Auler             SymData.find_first_not_of(0) == StringRef::npos);
1019a34c753fSRafael Auler   };
1020a34c753fSRafael Auler 
1021a34c753fSRafael Auler   uint64_t NumCollisions = 0;
1022a34c753fSRafael Auler   for (auto &Entry : BinaryDataMap) {
1023a34c753fSRafael Auler     BinaryData &BD = *Entry.second;
1024a34c753fSRafael Auler     StringRef Name = BD.getName();
1025a34c753fSRafael Auler 
1026a34c753fSRafael Auler     if (!isInternalSymbolName(Name))
1027a34c753fSRafael Auler       continue;
1028a34c753fSRafael Auler 
1029a34c753fSRafael Auler     // First check if a non-anonymous alias exists and move it to the front.
1030a34c753fSRafael Auler     if (BD.getSymbols().size() > 1) {
103140c2e0faSMaksim Panchenko       auto Itr = std::find_if(BD.getSymbols().begin(), BD.getSymbols().end(),
1032a34c753fSRafael Auler                               [&](const MCSymbol *Symbol) {
1033a34c753fSRafael Auler                                 return !isInternalSymbolName(Symbol->getName());
1034a34c753fSRafael Auler                               });
1035a34c753fSRafael Auler       if (Itr != BD.getSymbols().end()) {
1036a34c753fSRafael Auler         size_t Idx = std::distance(BD.getSymbols().begin(), Itr);
1037a34c753fSRafael Auler         std::swap(BD.getSymbols()[0], BD.getSymbols()[Idx]);
1038a34c753fSRafael Auler         continue;
1039a34c753fSRafael Auler       }
1040a34c753fSRafael Auler     }
1041a34c753fSRafael Auler 
1042a34c753fSRafael Auler     // We have to skip 0 size symbols since they will all collide.
1043a34c753fSRafael Auler     if (BD.getSize() == 0) {
1044a34c753fSRafael Auler       continue;
1045a34c753fSRafael Auler     }
1046a34c753fSRafael Auler 
1047a34c753fSRafael Auler     const uint64_t Hash = BD.getSection().hash(BD);
1048a34c753fSRafael Auler     const size_t Idx = Name.find("0x");
104940c2e0faSMaksim Panchenko     std::string NewName =
105040c2e0faSMaksim Panchenko         (Twine(Name.substr(0, Idx)) + "_" + Twine::utohexstr(Hash)).str();
1051a34c753fSRafael Auler     if (getBinaryDataByName(NewName)) {
1052a34c753fSRafael Auler       // Ignore collisions for symbols that appear to be padding
1053a34c753fSRafael Auler       // (i.e. all zeros or a "hole")
1054a34c753fSRafael Auler       if (!isPadding(BD)) {
1055a34c753fSRafael Auler         if (opts::Verbosity) {
1056a34c753fSRafael Auler           errs() << "BOLT-WARNING: collision detected when hashing " << BD
1057a34c753fSRafael Auler                  << " with new name (" << NewName << "), skipping.\n";
1058a34c753fSRafael Auler         }
1059a34c753fSRafael Auler         ++NumCollisions;
1060a34c753fSRafael Auler       }
1061a34c753fSRafael Auler       continue;
1062a34c753fSRafael Auler     }
106340c2e0faSMaksim Panchenko     BD.Symbols.insert(BD.Symbols.begin(), Ctx->getOrCreateSymbol(NewName));
1064a34c753fSRafael Auler     GlobalSymbols[NewName] = &BD;
1065a34c753fSRafael Auler   }
1066a34c753fSRafael Auler   if (NumCollisions) {
1067a34c753fSRafael Auler     errs() << "BOLT-WARNING: " << NumCollisions
1068a34c753fSRafael Auler            << " collisions detected while hashing binary objects";
1069a34c753fSRafael Auler     if (!opts::Verbosity)
1070a34c753fSRafael Auler       errs() << ". Use -v=1 to see the list.";
1071a34c753fSRafael Auler     errs() << '\n';
1072a34c753fSRafael Auler   }
1073a34c753fSRafael Auler }
1074a34c753fSRafael Auler 
10756aa735ceSAmir Ayupov bool BinaryContext::registerFragment(BinaryFunction &TargetFunction,
1076a34c753fSRafael Auler                                      BinaryFunction &Function) const {
10776aa735ceSAmir Ayupov   if (!isPotentialFragmentByName(TargetFunction, Function))
10786aa735ceSAmir Ayupov     return false;
10796aa735ceSAmir Ayupov   assert(TargetFunction.isFragment() && "TargetFunction must be a fragment");
10806aa735ceSAmir Ayupov   if (TargetFunction.isParentFragment(&Function))
10816aa735ceSAmir Ayupov     return true;
10826aa735ceSAmir Ayupov   TargetFunction.addParentFragment(Function);
1083a34c753fSRafael Auler   Function.addFragment(TargetFunction);
1084a34c753fSRafael Auler   if (!HasRelocations) {
1085a34c753fSRafael Auler     TargetFunction.setSimple(false);
1086a34c753fSRafael Auler     Function.setSimple(false);
1087a34c753fSRafael Auler   }
1088a34c753fSRafael Auler   if (opts::Verbosity >= 1) {
108940c2e0faSMaksim Panchenko     outs() << "BOLT-INFO: marking " << TargetFunction << " as a fragment of "
109040c2e0faSMaksim Panchenko            << Function << '\n';
1091a34c753fSRafael Auler   }
10926aa735ceSAmir Ayupov   return true;
1093a34c753fSRafael Auler }
1094a34c753fSRafael Auler 
1095a34c753fSRafael Auler void BinaryContext::processInterproceduralReferences(BinaryFunction &Function) {
1096a34c753fSRafael Auler   for (uint64_t Address : Function.InterproceduralReferences) {
1097a34c753fSRafael Auler     if (!Address)
1098a34c753fSRafael Auler       continue;
1099a34c753fSRafael Auler 
1100a34c753fSRafael Auler     BinaryFunction *TargetFunction =
1101a34c753fSRafael Auler         getBinaryFunctionContainingAddress(Address);
1102a34c753fSRafael Auler     if (&Function == TargetFunction)
1103a34c753fSRafael Auler       continue;
1104a34c753fSRafael Auler 
1105a34c753fSRafael Auler     if (TargetFunction) {
11066aa735ceSAmir Ayupov       if (TargetFunction->IsFragment &&
11076aa735ceSAmir Ayupov           !registerFragment(*TargetFunction, Function)) {
11086aa735ceSAmir Ayupov         errs() << "BOLT-WARNING: interprocedural reference between unrelated "
11096aa735ceSAmir Ayupov                   "fragments: "
11106aa735ceSAmir Ayupov                << Function.getPrintName() << " and "
11116aa735ceSAmir Ayupov                << TargetFunction->getPrintName() << '\n';
11126aa735ceSAmir Ayupov       }
1113a34c753fSRafael Auler       if (uint64_t Offset = Address - TargetFunction->getAddress())
1114a34c753fSRafael Auler         TargetFunction->addEntryPointAtOffset(Offset);
1115a34c753fSRafael Auler 
1116a34c753fSRafael Auler       continue;
1117a34c753fSRafael Auler     }
1118a34c753fSRafael Auler 
1119a34c753fSRafael Auler     // Check if address falls in function padding space - this could be
1120a34c753fSRafael Auler     // unmarked data in code. In this case adjust the padding space size.
1121a34c753fSRafael Auler     ErrorOr<BinarySection &> Section = getSectionForAddress(Address);
1122a34c753fSRafael Auler     assert(Section && "cannot get section for referenced address");
1123a34c753fSRafael Auler 
1124a34c753fSRafael Auler     if (!Section->isText())
1125a34c753fSRafael Auler       continue;
1126a34c753fSRafael Auler 
1127a34c753fSRafael Auler     // PLT requires special handling and could be ignored in this context.
1128a34c753fSRafael Auler     StringRef SectionName = Section->getName();
1129a34c753fSRafael Auler     if (SectionName == ".plt" || SectionName == ".plt.got")
1130a34c753fSRafael Auler       continue;
1131a34c753fSRafael Auler 
1132a34c753fSRafael Auler     if (opts::processAllFunctions()) {
1133a34c753fSRafael Auler       errs() << "BOLT-ERROR: cannot process binaries with unmarked "
113440c2e0faSMaksim Panchenko              << "object in code at address 0x" << Twine::utohexstr(Address)
113540c2e0faSMaksim Panchenko              << " belonging to section " << SectionName << " in current mode\n";
1136a34c753fSRafael Auler       exit(1);
1137a34c753fSRafael Auler     }
1138a34c753fSRafael Auler 
113940c2e0faSMaksim Panchenko     TargetFunction = getBinaryFunctionContainingAddress(Address,
1140a34c753fSRafael Auler                                                         /*CheckPastEnd=*/false,
1141a34c753fSRafael Auler                                                         /*UseMaxSize=*/true);
1142a34c753fSRafael Auler     // We are not going to overwrite non-simple functions, but for simple
1143a34c753fSRafael Auler     // ones - adjust the padding size.
1144a34c753fSRafael Auler     if (TargetFunction && TargetFunction->isSimple()) {
1145a34c753fSRafael Auler       errs() << "BOLT-WARNING: function " << *TargetFunction
1146a34c753fSRafael Auler              << " has an object detected in a padding region at address 0x"
1147a34c753fSRafael Auler              << Twine::utohexstr(Address) << '\n';
1148a34c753fSRafael Auler       TargetFunction->setMaxSize(TargetFunction->getSize());
1149a34c753fSRafael Auler     }
1150a34c753fSRafael Auler   }
1151a34c753fSRafael Auler 
1152a34c753fSRafael Auler   clearList(Function.InterproceduralReferences);
1153a34c753fSRafael Auler }
1154a34c753fSRafael Auler 
1155a34c753fSRafael Auler void BinaryContext::postProcessSymbolTable() {
1156a34c753fSRafael Auler   fixBinaryDataHoles();
1157a34c753fSRafael Auler   bool Valid = true;
1158a34c753fSRafael Auler   for (auto &Entry : BinaryDataMap) {
1159a34c753fSRafael Auler     BinaryData *BD = Entry.second;
1160a34c753fSRafael Auler     if ((BD->getName().startswith("SYMBOLat") ||
1161a34c753fSRafael Auler          BD->getName().startswith("DATAat")) &&
116240c2e0faSMaksim Panchenko         !BD->getParent() && !BD->getSize() && !BD->isAbsolute() &&
1163a34c753fSRafael Auler         BD->getSection()) {
1164a34c753fSRafael Auler       errs() << "BOLT-WARNING: zero-sized top level symbol: " << *BD << "\n";
1165a34c753fSRafael Auler       Valid = false;
1166a34c753fSRafael Auler     }
1167a34c753fSRafael Auler   }
1168a34c753fSRafael Auler   assert(Valid);
1169a34c753fSRafael Auler   generateSymbolHashes();
1170a34c753fSRafael Auler }
1171a34c753fSRafael Auler 
1172a34c753fSRafael Auler void BinaryContext::foldFunction(BinaryFunction &ChildBF,
1173a34c753fSRafael Auler                                  BinaryFunction &ParentBF) {
1174a34c753fSRafael Auler   assert(!ChildBF.isMultiEntry() && !ParentBF.isMultiEntry() &&
1175a34c753fSRafael Auler          "cannot merge functions with multiple entry points");
1176a34c753fSRafael Auler 
1177a34c753fSRafael Auler   std::unique_lock<std::shared_timed_mutex> WriteCtxLock(CtxMutex,
1178a34c753fSRafael Auler                                                          std::defer_lock);
1179a34c753fSRafael Auler   std::unique_lock<std::shared_timed_mutex> WriteSymbolMapLock(
1180a34c753fSRafael Auler       SymbolToFunctionMapMutex, std::defer_lock);
1181a34c753fSRafael Auler 
1182a34c753fSRafael Auler   const StringRef ChildName = ChildBF.getOneName();
1183a34c753fSRafael Auler 
1184a34c753fSRafael Auler   // Move symbols over and update bookkeeping info.
1185a34c753fSRafael Auler   for (MCSymbol *Symbol : ChildBF.getSymbols()) {
1186a34c753fSRafael Auler     ParentBF.getSymbols().push_back(Symbol);
1187a34c753fSRafael Auler     WriteSymbolMapLock.lock();
1188a34c753fSRafael Auler     SymbolToFunctionMap[Symbol] = &ParentBF;
1189a34c753fSRafael Auler     WriteSymbolMapLock.unlock();
1190a34c753fSRafael Auler     // NB: there's no need to update BinaryDataMap and GlobalSymbols.
1191a34c753fSRafael Auler   }
1192a34c753fSRafael Auler   ChildBF.getSymbols().clear();
1193a34c753fSRafael Auler 
1194a34c753fSRafael Auler   // Move other names the child function is known under.
1195a34c753fSRafael Auler   std::move(ChildBF.Aliases.begin(), ChildBF.Aliases.end(),
1196a34c753fSRafael Auler             std::back_inserter(ParentBF.Aliases));
1197a34c753fSRafael Auler   ChildBF.Aliases.clear();
1198a34c753fSRafael Auler 
1199a34c753fSRafael Auler   if (HasRelocations) {
1200a34c753fSRafael Auler     // Merge execution counts of ChildBF into those of ParentBF.
1201a34c753fSRafael Auler     // Without relocations, we cannot reliably merge profiles as both functions
1202a34c753fSRafael Auler     // continue to exist and either one can be executed.
1203a34c753fSRafael Auler     ChildBF.mergeProfileDataInto(ParentBF);
1204a34c753fSRafael Auler 
1205a34c753fSRafael Auler     std::shared_lock<std::shared_timed_mutex> ReadBfsLock(BinaryFunctionsMutex,
1206a34c753fSRafael Auler                                                           std::defer_lock);
1207a34c753fSRafael Auler     std::unique_lock<std::shared_timed_mutex> WriteBfsLock(BinaryFunctionsMutex,
1208a34c753fSRafael Auler                                                            std::defer_lock);
1209a34c753fSRafael Auler     // Remove ChildBF from the global set of functions in relocs mode.
1210a34c753fSRafael Auler     ReadBfsLock.lock();
1211a34c753fSRafael Auler     auto FI = BinaryFunctions.find(ChildBF.getAddress());
1212a34c753fSRafael Auler     ReadBfsLock.unlock();
1213a34c753fSRafael Auler 
1214a34c753fSRafael Auler     assert(FI != BinaryFunctions.end() && "function not found");
1215a34c753fSRafael Auler     assert(&ChildBF == &FI->second && "function mismatch");
1216a34c753fSRafael Auler 
1217a34c753fSRafael Auler     WriteBfsLock.lock();
1218a34c753fSRafael Auler     ChildBF.clearDisasmState();
1219a34c753fSRafael Auler     FI = BinaryFunctions.erase(FI);
1220a34c753fSRafael Auler     WriteBfsLock.unlock();
1221a34c753fSRafael Auler 
1222a34c753fSRafael Auler   } else {
1223a34c753fSRafael Auler     // In non-relocation mode we keep the function, but rename it.
1224a34c753fSRafael Auler     std::string NewName = "__ICF_" + ChildName.str();
1225a34c753fSRafael Auler 
1226a34c753fSRafael Auler     WriteCtxLock.lock();
1227a34c753fSRafael Auler     ChildBF.getSymbols().push_back(Ctx->getOrCreateSymbol(NewName));
1228a34c753fSRafael Auler     WriteCtxLock.unlock();
1229a34c753fSRafael Auler 
1230a34c753fSRafael Auler     ChildBF.setFolded(&ParentBF);
1231a34c753fSRafael Auler   }
1232a34c753fSRafael Auler }
1233a34c753fSRafael Auler 
1234a34c753fSRafael Auler void BinaryContext::fixBinaryDataHoles() {
1235a34c753fSRafael Auler   assert(validateObjectNesting() && "object nesting inconsitency detected");
1236a34c753fSRafael Auler 
1237a34c753fSRafael Auler   for (BinarySection &Section : allocatableSections()) {
1238a34c753fSRafael Auler     std::vector<std::pair<uint64_t, uint64_t>> Holes;
1239a34c753fSRafael Auler 
1240a34c753fSRafael Auler     auto isNotHole = [&Section](const binary_data_iterator &Itr) {
1241a34c753fSRafael Auler       BinaryData *BD = Itr->second;
124240c2e0faSMaksim Panchenko       bool isHole = (!BD->getParent() && !BD->getSize() && BD->isObject() &&
1243a34c753fSRafael Auler                      (BD->getName().startswith("SYMBOLat0x") ||
1244a34c753fSRafael Auler                       BD->getName().startswith("DATAat0x") ||
1245a34c753fSRafael Auler                       BD->getName().startswith("ANONYMOUS")));
1246a34c753fSRafael Auler       return !isHole && BD->getSection() == Section && !BD->getParent();
1247a34c753fSRafael Auler     };
1248a34c753fSRafael Auler 
1249a34c753fSRafael Auler     auto BDStart = BinaryDataMap.begin();
1250a34c753fSRafael Auler     auto BDEnd = BinaryDataMap.end();
1251a34c753fSRafael Auler     auto Itr = FilteredBinaryDataIterator(isNotHole, BDStart, BDEnd);
1252a34c753fSRafael Auler     auto End = FilteredBinaryDataIterator(isNotHole, BDEnd, BDEnd);
1253a34c753fSRafael Auler 
1254a34c753fSRafael Auler     uint64_t EndAddress = Section.getAddress();
1255a34c753fSRafael Auler 
1256a34c753fSRafael Auler     while (Itr != End) {
1257a34c753fSRafael Auler       if (Itr->second->getAddress() > EndAddress) {
1258a34c753fSRafael Auler         uint64_t Gap = Itr->second->getAddress() - EndAddress;
1259a34c753fSRafael Auler         Holes.emplace_back(EndAddress, Gap);
1260a34c753fSRafael Auler       }
1261a34c753fSRafael Auler       EndAddress = Itr->second->getEndAddress();
1262a34c753fSRafael Auler       ++Itr;
1263a34c753fSRafael Auler     }
1264a34c753fSRafael Auler 
12653652483cSRafael Auler     if (EndAddress < Section.getEndAddress())
1266a34c753fSRafael Auler       Holes.emplace_back(EndAddress, Section.getEndAddress() - EndAddress);
1267a34c753fSRafael Auler 
1268a34c753fSRafael Auler     // If there is already a symbol at the start of the hole, grow that symbol
1269a34c753fSRafael Auler     // to cover the rest.  Otherwise, create a new symbol to cover the hole.
1270a34c753fSRafael Auler     for (std::pair<uint64_t, uint64_t> &Hole : Holes) {
1271a34c753fSRafael Auler       BinaryData *BD = getBinaryDataAtAddress(Hole.first);
1272a34c753fSRafael Auler       if (BD) {
1273a34c753fSRafael Auler         // BD->getSection() can be != Section if there are sections that
1274a34c753fSRafael Auler         // overlap.  In this case it is probably safe to just skip the holes
1275a34c753fSRafael Auler         // since the overlapping section will not(?) have any symbols in it.
1276a34c753fSRafael Auler         if (BD->getSection() == Section)
1277a34c753fSRafael Auler           setBinaryDataSize(Hole.first, Hole.second);
1278a34c753fSRafael Auler       } else {
1279a34c753fSRafael Auler         getOrCreateGlobalSymbol(Hole.first, "HOLEat", Hole.second, 1);
1280a34c753fSRafael Auler       }
1281a34c753fSRafael Auler     }
1282a34c753fSRafael Auler   }
1283a34c753fSRafael Auler 
1284a34c753fSRafael Auler   assert(validateObjectNesting() && "object nesting inconsitency detected");
1285a34c753fSRafael Auler   assert(validateHoles() && "top level hole detected in object map");
1286a34c753fSRafael Auler }
1287a34c753fSRafael Auler 
1288a34c753fSRafael Auler void BinaryContext::printGlobalSymbols(raw_ostream &OS) const {
1289a34c753fSRafael Auler   const BinarySection *CurrentSection = nullptr;
1290a34c753fSRafael Auler   bool FirstSection = true;
1291a34c753fSRafael Auler 
1292a34c753fSRafael Auler   for (auto &Entry : BinaryDataMap) {
1293a34c753fSRafael Auler     const BinaryData *BD = Entry.second;
1294a34c753fSRafael Auler     const BinarySection &Section = BD->getSection();
1295a34c753fSRafael Auler     if (FirstSection || Section != *CurrentSection) {
1296a34c753fSRafael Auler       uint64_t Address, Size;
1297a34c753fSRafael Auler       StringRef Name = Section.getName();
1298a34c753fSRafael Auler       if (Section) {
1299a34c753fSRafael Auler         Address = Section.getAddress();
1300a34c753fSRafael Auler         Size = Section.getSize();
1301a34c753fSRafael Auler       } else {
1302a34c753fSRafael Auler         Address = BD->getAddress();
1303a34c753fSRafael Auler         Size = BD->getSize();
1304a34c753fSRafael Auler       }
1305a34c753fSRafael Auler       OS << "BOLT-INFO: Section " << Name << ", "
1306a34c753fSRafael Auler          << "0x" + Twine::utohexstr(Address) << ":"
130740c2e0faSMaksim Panchenko          << "0x" + Twine::utohexstr(Address + Size) << "/" << Size << "\n";
1308a34c753fSRafael Auler       CurrentSection = &Section;
1309a34c753fSRafael Auler       FirstSection = false;
1310a34c753fSRafael Auler     }
1311a34c753fSRafael Auler 
1312a34c753fSRafael Auler     OS << "BOLT-INFO: ";
1313a34c753fSRafael Auler     const BinaryData *P = BD->getParent();
1314a34c753fSRafael Auler     while (P) {
1315a34c753fSRafael Auler       OS << "  ";
1316a34c753fSRafael Auler       P = P->getParent();
1317a34c753fSRafael Auler     }
1318a34c753fSRafael Auler     OS << *BD << "\n";
1319a34c753fSRafael Auler   }
1320a34c753fSRafael Auler }
1321a34c753fSRafael Auler 
1322a34c753fSRafael Auler Expected<unsigned>
1323a34c753fSRafael Auler BinaryContext::getDwarfFile(StringRef Directory, StringRef FileName,
1324a34c753fSRafael Auler                             unsigned FileNumber,
1325a34c753fSRafael Auler                             Optional<MD5::MD5Result> Checksum,
1326a34c753fSRafael Auler                             Optional<StringRef> Source, unsigned CUID) {
1327a34c753fSRafael Auler   DwarfLineTable &Table = DwarfLineTablesCUMap[CUID];
1328a34c753fSRafael Auler   return Table.tryGetFile(Directory, FileName, Checksum, Source,
1329a34c753fSRafael Auler                           Ctx->getDwarfVersion(), FileNumber);
1330a34c753fSRafael Auler }
1331a34c753fSRafael Auler 
1332a34c753fSRafael Auler unsigned BinaryContext::addDebugFilenameToUnit(const uint32_t DestCUID,
1333a34c753fSRafael Auler                                                const uint32_t SrcCUID,
1334a34c753fSRafael Auler                                                unsigned FileIndex) {
1335a34c753fSRafael Auler   DWARFCompileUnit *SrcUnit = DwCtx->getCompileUnitForOffset(SrcCUID);
1336a34c753fSRafael Auler   const DWARFDebugLine::LineTable *LineTable =
1337a34c753fSRafael Auler       DwCtx->getLineTableForUnit(SrcUnit);
1338a34c753fSRafael Auler   const std::vector<DWARFDebugLine::FileNameEntry> &FileNames =
1339a34c753fSRafael Auler       LineTable->Prologue.FileNames;
1340a34c753fSRafael Auler   // Dir indexes start at 1, as DWARF file numbers, and a dir index 0
1341a34c753fSRafael Auler   // means empty dir.
1342a34c753fSRafael Auler   assert(FileIndex > 0 && FileIndex <= FileNames.size() &&
1343a34c753fSRafael Auler          "FileIndex out of range for the compilation unit.");
1344a34c753fSRafael Auler   StringRef Dir = "";
1345a34c753fSRafael Auler   if (FileNames[FileIndex - 1].DirIdx != 0) {
1346a34c753fSRafael Auler     if (Optional<const char *> DirName = dwarf::toString(
1347a34c753fSRafael Auler             LineTable->Prologue
1348a34c753fSRafael Auler                 .IncludeDirectories[FileNames[FileIndex - 1].DirIdx - 1])) {
1349a34c753fSRafael Auler       Dir = *DirName;
1350a34c753fSRafael Auler     }
1351a34c753fSRafael Auler   }
1352a34c753fSRafael Auler   StringRef FileName = "";
1353a34c753fSRafael Auler   if (Optional<const char *> FName =
1354a34c753fSRafael Auler           dwarf::toString(FileNames[FileIndex - 1].Name))
1355a34c753fSRafael Auler     FileName = *FName;
1356a34c753fSRafael Auler   assert(FileName != "");
1357a34c753fSRafael Auler   return cantFail(getDwarfFile(Dir, FileName, 0, None, None, DestCUID));
1358a34c753fSRafael Auler }
1359a34c753fSRafael Auler 
1360a34c753fSRafael Auler std::vector<BinaryFunction *> BinaryContext::getSortedFunctions() {
1361a34c753fSRafael Auler   std::vector<BinaryFunction *> SortedFunctions(BinaryFunctions.size());
1362a34c753fSRafael Auler   std::transform(BinaryFunctions.begin(), BinaryFunctions.end(),
1363a34c753fSRafael Auler                  SortedFunctions.begin(),
1364a34c753fSRafael Auler                  [](std::pair<const uint64_t, BinaryFunction> &BFI) {
1365a34c753fSRafael Auler                    return &BFI.second;
1366a34c753fSRafael Auler                  });
1367a34c753fSRafael Auler 
1368a34c753fSRafael Auler   std::stable_sort(SortedFunctions.begin(), SortedFunctions.end(),
1369a34c753fSRafael Auler                    [](const BinaryFunction *A, const BinaryFunction *B) {
1370a34c753fSRafael Auler                      if (A->hasValidIndex() && B->hasValidIndex()) {
1371a34c753fSRafael Auler                        return A->getIndex() < B->getIndex();
1372a34c753fSRafael Auler                      }
1373a34c753fSRafael Auler                      return A->hasValidIndex();
1374a34c753fSRafael Auler                    });
1375a34c753fSRafael Auler   return SortedFunctions;
1376a34c753fSRafael Auler }
1377a34c753fSRafael Auler 
1378a34c753fSRafael Auler std::vector<BinaryFunction *> BinaryContext::getAllBinaryFunctions() {
1379a34c753fSRafael Auler   std::vector<BinaryFunction *> AllFunctions;
1380a34c753fSRafael Auler   AllFunctions.reserve(BinaryFunctions.size() + InjectedBinaryFunctions.size());
1381a34c753fSRafael Auler   std::transform(BinaryFunctions.begin(), BinaryFunctions.end(),
1382a34c753fSRafael Auler                  std::back_inserter(AllFunctions),
1383a34c753fSRafael Auler                  [](std::pair<const uint64_t, BinaryFunction> &BFI) {
1384a34c753fSRafael Auler                    return &BFI.second;
1385a34c753fSRafael Auler                  });
1386a34c753fSRafael Auler   std::copy(InjectedBinaryFunctions.begin(), InjectedBinaryFunctions.end(),
1387a34c753fSRafael Auler             std::back_inserter(AllFunctions));
1388a34c753fSRafael Auler 
1389a34c753fSRafael Auler   return AllFunctions;
1390a34c753fSRafael Auler }
1391a34c753fSRafael Auler 
1392a34c753fSRafael Auler Optional<DWARFUnit *> BinaryContext::getDWOCU(uint64_t DWOId) {
1393a34c753fSRafael Auler   auto Iter = DWOCUs.find(DWOId);
1394a34c753fSRafael Auler   if (Iter == DWOCUs.end())
1395a34c753fSRafael Auler     return None;
1396a34c753fSRafael Auler 
1397a34c753fSRafael Auler   return Iter->second;
1398a34c753fSRafael Auler }
1399a34c753fSRafael Auler 
1400a34c753fSRafael Auler DWARFContext *BinaryContext::getDWOContext() {
1401a34c753fSRafael Auler   if (DWOCUs.empty())
1402a34c753fSRafael Auler     return nullptr;
1403a34c753fSRafael Auler   return &DWOCUs.begin()->second->getContext();
1404a34c753fSRafael Auler }
1405a34c753fSRafael Auler 
1406a34c753fSRafael Auler /// Handles DWO sections that can either be in .o, .dwo or .dwp files.
1407a34c753fSRafael Auler void BinaryContext::preprocessDWODebugInfo() {
1408a34c753fSRafael Auler   for (const std::unique_ptr<DWARFUnit> &CU : DwCtx->compile_units()) {
1409a34c753fSRafael Auler     DWARFUnit *const DwarfUnit = CU.get();
1410a34c753fSRafael Auler     if (llvm::Optional<uint64_t> DWOId = DwarfUnit->getDWOId()) {
1411a34c753fSRafael Auler       DWARFUnit *DWOCU = DwarfUnit->getNonSkeletonUnitDIE(false).getDwarfUnit();
1412a34c753fSRafael Auler       if (!DWOCU->isDWOUnit()) {
1413a34c753fSRafael Auler         std::string DWOName = dwarf::toString(
1414a34c753fSRafael Auler             DwarfUnit->getUnitDIE().find(
1415a34c753fSRafael Auler                 {dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}),
1416a34c753fSRafael Auler             "");
1417a34c753fSRafael Auler         outs() << "BOLT-WARNING: Debug Fission: DWO debug information for "
1418a34c753fSRafael Auler                << DWOName
1419a34c753fSRafael Auler                << " was not retrieved and won't be updated. Please check "
1420a34c753fSRafael Auler                   "relative path.\n";
1421a34c753fSRafael Auler         continue;
1422a34c753fSRafael Auler       }
1423a34c753fSRafael Auler       DWOCUs[*DWOId] = DWOCU;
1424a34c753fSRafael Auler     }
1425a34c753fSRafael Auler   }
1426a34c753fSRafael Auler }
1427a34c753fSRafael Auler 
1428a34c753fSRafael Auler void BinaryContext::preprocessDebugInfo() {
1429a34c753fSRafael Auler   struct CURange {
1430a34c753fSRafael Auler     uint64_t LowPC;
1431a34c753fSRafael Auler     uint64_t HighPC;
1432a34c753fSRafael Auler     DWARFUnit *Unit;
1433a34c753fSRafael Auler 
143440c2e0faSMaksim Panchenko     bool operator<(const CURange &Other) const { return LowPC < Other.LowPC; }
1435a34c753fSRafael Auler   };
1436a34c753fSRafael Auler 
1437a34c753fSRafael Auler   // Building a map of address ranges to CUs similar to .debug_aranges and use
1438a34c753fSRafael Auler   // it to assign CU to functions.
1439a34c753fSRafael Auler   std::vector<CURange> AllRanges;
1440a34c753fSRafael Auler   AllRanges.reserve(DwCtx->getNumCompileUnits());
1441a34c753fSRafael Auler   for (const std::unique_ptr<DWARFUnit> &CU : DwCtx->compile_units()) {
1442a34c753fSRafael Auler     Expected<DWARFAddressRangesVector> RangesOrError =
1443a34c753fSRafael Auler         CU->getUnitDIE().getAddressRanges();
1444a34c753fSRafael Auler     if (!RangesOrError) {
1445a34c753fSRafael Auler       consumeError(RangesOrError.takeError());
1446a34c753fSRafael Auler       continue;
1447a34c753fSRafael Auler     }
1448a34c753fSRafael Auler     for (DWARFAddressRange &Range : *RangesOrError) {
1449a34c753fSRafael Auler       // Parts of the debug info could be invalidated due to corresponding code
1450a34c753fSRafael Auler       // being removed from the binary by the linker. Hence we check if the
1451a34c753fSRafael Auler       // address is a valid one.
1452a34c753fSRafael Auler       if (containsAddress(Range.LowPC))
1453a34c753fSRafael Auler         AllRanges.emplace_back(CURange{Range.LowPC, Range.HighPC, CU.get()});
1454a34c753fSRafael Auler     }
1455a34c753fSRafael Auler   }
1456a34c753fSRafael Auler 
1457a34c753fSRafael Auler   std::sort(AllRanges.begin(), AllRanges.end());
1458a34c753fSRafael Auler   for (auto &KV : BinaryFunctions) {
1459a34c753fSRafael Auler     const uint64_t FunctionAddress = KV.first;
1460a34c753fSRafael Auler     BinaryFunction &Function = KV.second;
1461a34c753fSRafael Auler 
146240c2e0faSMaksim Panchenko     auto It = std::partition_point(
146340c2e0faSMaksim Panchenko         AllRanges.begin(), AllRanges.end(),
1464a34c753fSRafael Auler         [=](CURange R) { return R.HighPC <= FunctionAddress; });
1465a34c753fSRafael Auler     if (It != AllRanges.end() && It->LowPC <= FunctionAddress) {
1466a34c753fSRafael Auler       Function.setDWARFUnit(It->Unit);
1467a34c753fSRafael Auler     }
1468a34c753fSRafael Auler   }
1469a34c753fSRafael Auler 
1470a34c753fSRafael Auler   // Discover units with debug info that needs to be updated.
1471a34c753fSRafael Auler   for (const auto &KV : BinaryFunctions) {
1472a34c753fSRafael Auler     const BinaryFunction &BF = KV.second;
1473a34c753fSRafael Auler     if (shouldEmit(BF) && BF.getDWARFUnit())
1474a34c753fSRafael Auler       ProcessedCUs.insert(BF.getDWARFUnit());
1475a34c753fSRafael Auler   }
1476a34c753fSRafael Auler 
1477a34c753fSRafael Auler   // Clear debug info for functions from units that we are not going to process.
1478a34c753fSRafael Auler   for (auto &KV : BinaryFunctions) {
1479a34c753fSRafael Auler     BinaryFunction &BF = KV.second;
1480a34c753fSRafael Auler     if (BF.getDWARFUnit() && !ProcessedCUs.count(BF.getDWARFUnit()))
1481a34c753fSRafael Auler       BF.setDWARFUnit(nullptr);
1482a34c753fSRafael Auler   }
1483a34c753fSRafael Auler 
1484a34c753fSRafael Auler   if (opts::Verbosity >= 1) {
1485a34c753fSRafael Auler     outs() << "BOLT-INFO: " << ProcessedCUs.size() << " out of "
1486a34c753fSRafael Auler            << DwCtx->getNumCompileUnits() << " CUs will be updated\n";
1487a34c753fSRafael Auler   }
1488a34c753fSRafael Auler 
1489a34c753fSRafael Auler   // Populate MCContext with DWARF files from all units.
1490a34c753fSRafael Auler   StringRef GlobalPrefix = AsmInfo->getPrivateGlobalPrefix();
1491a34c753fSRafael Auler   for (const std::unique_ptr<DWARFUnit> &CU : DwCtx->compile_units()) {
1492a34c753fSRafael Auler     const uint64_t CUID = CU->getOffset();
1493a34c753fSRafael Auler     getDwarfLineTable(CUID).setLabel(Ctx->getOrCreateSymbol(
1494a34c753fSRafael Auler         GlobalPrefix + "line_table_start" + Twine(CUID)));
1495a34c753fSRafael Auler 
1496a34c753fSRafael Auler     if (!ProcessedCUs.count(CU.get()))
1497a34c753fSRafael Auler       continue;
1498a34c753fSRafael Auler 
1499a34c753fSRafael Auler     const DWARFDebugLine::LineTable *LineTable =
1500a34c753fSRafael Auler         DwCtx->getLineTableForUnit(CU.get());
1501a34c753fSRafael Auler     const std::vector<DWARFDebugLine::FileNameEntry> &FileNames =
1502a34c753fSRafael Auler         LineTable->Prologue.FileNames;
1503a34c753fSRafael Auler 
1504a34c753fSRafael Auler     // Assign a unique label to every line table, one per CU.
1505a34c753fSRafael Auler     // Make sure empty debug line tables are registered too.
1506a34c753fSRafael Auler     if (FileNames.empty()) {
1507a34c753fSRafael Auler       cantFail(getDwarfFile("", "<unknown>", 0, None, None, CUID));
1508a34c753fSRafael Auler       continue;
1509a34c753fSRafael Auler     }
1510a34c753fSRafael Auler     for (size_t I = 0, Size = FileNames.size(); I != Size; ++I) {
1511a34c753fSRafael Auler       // Dir indexes start at 1, as DWARF file numbers, and a dir index 0
1512a34c753fSRafael Auler       // means empty dir.
1513a34c753fSRafael Auler       StringRef Dir = "";
1514a34c753fSRafael Auler       if (FileNames[I].DirIdx != 0)
1515a34c753fSRafael Auler         if (Optional<const char *> DirName = dwarf::toString(
1516a34c753fSRafael Auler                 LineTable->Prologue
1517a34c753fSRafael Auler                     .IncludeDirectories[FileNames[I].DirIdx - 1]))
1518a34c753fSRafael Auler           Dir = *DirName;
1519a34c753fSRafael Auler       StringRef FileName = "";
1520a34c753fSRafael Auler       if (Optional<const char *> FName = dwarf::toString(FileNames[I].Name))
1521a34c753fSRafael Auler         FileName = *FName;
1522a34c753fSRafael Auler       assert(FileName != "");
1523a34c753fSRafael Auler       cantFail(getDwarfFile(Dir, FileName, 0, None, None, CUID));
1524a34c753fSRafael Auler     }
1525a34c753fSRafael Auler   }
1526a34c753fSRafael Auler 
1527a34c753fSRafael Auler   preprocessDWODebugInfo();
1528a34c753fSRafael Auler }
1529a34c753fSRafael Auler 
1530a34c753fSRafael Auler bool BinaryContext::shouldEmit(const BinaryFunction &Function) const {
1531a34c753fSRafael Auler   if (opts::processAllFunctions())
1532a34c753fSRafael Auler     return true;
1533a34c753fSRafael Auler 
1534a34c753fSRafael Auler   if (Function.isIgnored())
1535a34c753fSRafael Auler     return false;
1536a34c753fSRafael Auler 
1537a34c753fSRafael Auler   // In relocation mode we will emit non-simple functions with CFG.
1538a34c753fSRafael Auler   // If the function does not have a CFG it should be marked as ignored.
1539a34c753fSRafael Auler   return HasRelocations || Function.isSimple();
1540a34c753fSRafael Auler }
1541a34c753fSRafael Auler 
1542a34c753fSRafael Auler void BinaryContext::printCFI(raw_ostream &OS, const MCCFIInstruction &Inst) {
1543a34c753fSRafael Auler   uint32_t Operation = Inst.getOperation();
1544a34c753fSRafael Auler   switch (Operation) {
1545a34c753fSRafael Auler   case MCCFIInstruction::OpSameValue:
1546a34c753fSRafael Auler     OS << "OpSameValue Reg" << Inst.getRegister();
1547a34c753fSRafael Auler     break;
1548a34c753fSRafael Auler   case MCCFIInstruction::OpRememberState:
1549a34c753fSRafael Auler     OS << "OpRememberState";
1550a34c753fSRafael Auler     break;
1551a34c753fSRafael Auler   case MCCFIInstruction::OpRestoreState:
1552a34c753fSRafael Auler     OS << "OpRestoreState";
1553a34c753fSRafael Auler     break;
1554a34c753fSRafael Auler   case MCCFIInstruction::OpOffset:
1555a34c753fSRafael Auler     OS << "OpOffset Reg" << Inst.getRegister() << " " << Inst.getOffset();
1556a34c753fSRafael Auler     break;
1557a34c753fSRafael Auler   case MCCFIInstruction::OpDefCfaRegister:
1558a34c753fSRafael Auler     OS << "OpDefCfaRegister Reg" << Inst.getRegister();
1559a34c753fSRafael Auler     break;
1560a34c753fSRafael Auler   case MCCFIInstruction::OpDefCfaOffset:
1561a34c753fSRafael Auler     OS << "OpDefCfaOffset " << Inst.getOffset();
1562a34c753fSRafael Auler     break;
1563a34c753fSRafael Auler   case MCCFIInstruction::OpDefCfa:
1564a34c753fSRafael Auler     OS << "OpDefCfa Reg" << Inst.getRegister() << " " << Inst.getOffset();
1565a34c753fSRafael Auler     break;
1566a34c753fSRafael Auler   case MCCFIInstruction::OpRelOffset:
1567a34c753fSRafael Auler     OS << "OpRelOffset Reg" << Inst.getRegister() << " " << Inst.getOffset();
1568a34c753fSRafael Auler     break;
1569a34c753fSRafael Auler   case MCCFIInstruction::OpAdjustCfaOffset:
1570a34c753fSRafael Auler     OS << "OfAdjustCfaOffset " << Inst.getOffset();
1571a34c753fSRafael Auler     break;
1572a34c753fSRafael Auler   case MCCFIInstruction::OpEscape:
1573a34c753fSRafael Auler     OS << "OpEscape";
1574a34c753fSRafael Auler     break;
1575a34c753fSRafael Auler   case MCCFIInstruction::OpRestore:
1576a34c753fSRafael Auler     OS << "OpRestore Reg" << Inst.getRegister();
1577a34c753fSRafael Auler     break;
1578a34c753fSRafael Auler   case MCCFIInstruction::OpUndefined:
1579a34c753fSRafael Auler     OS << "OpUndefined Reg" << Inst.getRegister();
1580a34c753fSRafael Auler     break;
1581a34c753fSRafael Auler   case MCCFIInstruction::OpRegister:
1582a34c753fSRafael Auler     OS << "OpRegister Reg" << Inst.getRegister() << " Reg"
1583a34c753fSRafael Auler        << Inst.getRegister2();
1584a34c753fSRafael Auler     break;
1585a34c753fSRafael Auler   case MCCFIInstruction::OpWindowSave:
1586a34c753fSRafael Auler     OS << "OpWindowSave";
1587a34c753fSRafael Auler     break;
1588a34c753fSRafael Auler   case MCCFIInstruction::OpGnuArgsSize:
1589a34c753fSRafael Auler     OS << "OpGnuArgsSize";
1590a34c753fSRafael Auler     break;
1591a34c753fSRafael Auler   default:
1592a34c753fSRafael Auler     OS << "Op#" << Operation;
1593a34c753fSRafael Auler     break;
1594a34c753fSRafael Auler   }
1595a34c753fSRafael Auler }
1596a34c753fSRafael Auler 
159740c2e0faSMaksim Panchenko void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,
1598a34c753fSRafael Auler                                      uint64_t Offset,
1599a34c753fSRafael Auler                                      const BinaryFunction *Function,
160040c2e0faSMaksim Panchenko                                      bool PrintMCInst, bool PrintMemData,
1601a34c753fSRafael Auler                                      bool PrintRelocations) const {
1602a34c753fSRafael Auler   if (MIB->isEHLabel(Instruction)) {
1603a34c753fSRafael Auler     OS << "  EH_LABEL: " << *MIB->getTargetSymbol(Instruction) << '\n';
1604a34c753fSRafael Auler     return;
1605a34c753fSRafael Auler   }
1606a34c753fSRafael Auler   OS << format("    %08" PRIx64 ": ", Offset);
1607a34c753fSRafael Auler   if (MIB->isCFI(Instruction)) {
1608a34c753fSRafael Auler     uint32_t Offset = Instruction.getOperand(0).getImm();
1609a34c753fSRafael Auler     OS << "\t!CFI\t$" << Offset << "\t; ";
1610a34c753fSRafael Auler     if (Function)
1611a34c753fSRafael Auler       printCFI(OS, *Function->getCFIFor(Instruction));
1612a34c753fSRafael Auler     OS << "\n";
1613a34c753fSRafael Auler     return;
1614a34c753fSRafael Auler   }
1615a34c753fSRafael Auler   InstPrinter->printInst(&Instruction, 0, "", *STI, OS);
1616a34c753fSRafael Auler   if (MIB->isCall(Instruction)) {
1617a34c753fSRafael Auler     if (MIB->isTailCall(Instruction))
1618a34c753fSRafael Auler       OS << " # TAILCALL ";
1619a34c753fSRafael Auler     if (MIB->isInvoke(Instruction)) {
1620a34c753fSRafael Auler       const Optional<MCPlus::MCLandingPad> EHInfo = MIB->getEHInfo(Instruction);
1621a34c753fSRafael Auler       OS << " # handler: ";
1622a34c753fSRafael Auler       if (EHInfo->first)
1623a34c753fSRafael Auler         OS << *EHInfo->first;
1624a34c753fSRafael Auler       else
1625a34c753fSRafael Auler         OS << '0';
1626a34c753fSRafael Auler       OS << "; action: " << EHInfo->second;
1627a34c753fSRafael Auler       const int64_t GnuArgsSize = MIB->getGnuArgsSize(Instruction);
1628a34c753fSRafael Auler       if (GnuArgsSize >= 0)
1629a34c753fSRafael Auler         OS << "; GNU_args_size = " << GnuArgsSize;
1630a34c753fSRafael Auler     }
1631a34c753fSRafael Auler   } else if (MIB->isIndirectBranch(Instruction)) {
1632a34c753fSRafael Auler     if (uint64_t JTAddress = MIB->getJumpTable(Instruction)) {
1633a34c753fSRafael Auler       OS << " # JUMPTABLE @0x" << Twine::utohexstr(JTAddress);
1634a34c753fSRafael Auler     } else {
1635a34c753fSRafael Auler       OS << " # UNKNOWN CONTROL FLOW";
1636a34c753fSRafael Auler     }
1637a34c753fSRafael Auler   }
1638a9cd49d5SAmir Ayupov   if (Optional<uint32_t> Offset = MIB->getOffset(Instruction))
1639a9cd49d5SAmir Ayupov     OS << " # Offset: " << *Offset;
1640a34c753fSRafael Auler 
1641a34c753fSRafael Auler   MIB->printAnnotations(Instruction, OS);
1642a34c753fSRafael Auler 
1643a34c753fSRafael Auler   if (opts::PrintDebugInfo) {
1644a34c753fSRafael Auler     DebugLineTableRowRef RowRef =
1645a34c753fSRafael Auler         DebugLineTableRowRef::fromSMLoc(Instruction.getLoc());
1646a34c753fSRafael Auler     if (RowRef != DebugLineTableRowRef::NULL_ROW) {
1647a34c753fSRafael Auler       const DWARFDebugLine::LineTable *LineTable;
1648a34c753fSRafael Auler       if (Function && Function->getDWARFUnit() &&
1649a34c753fSRafael Auler           Function->getDWARFUnit()->getOffset() == RowRef.DwCompileUnitIndex) {
1650a34c753fSRafael Auler         LineTable = Function->getDWARFLineTable();
1651a34c753fSRafael Auler       } else {
1652a34c753fSRafael Auler         LineTable = DwCtx->getLineTableForUnit(
1653a34c753fSRafael Auler             DwCtx->getCompileUnitForOffset(RowRef.DwCompileUnitIndex));
1654a34c753fSRafael Auler       }
1655a34c753fSRafael Auler       assert(LineTable &&
1656a34c753fSRafael Auler              "line table expected for instruction with debug info");
1657a34c753fSRafael Auler 
1658a34c753fSRafael Auler       const DWARFDebugLine::Row &Row = LineTable->Rows[RowRef.RowIndex - 1];
1659a34c753fSRafael Auler       StringRef FileName = "";
1660a34c753fSRafael Auler       if (Optional<const char *> FName =
1661a34c753fSRafael Auler               dwarf::toString(LineTable->Prologue.FileNames[Row.File - 1].Name))
1662a34c753fSRafael Auler         FileName = *FName;
1663a34c753fSRafael Auler       OS << " # debug line " << FileName << ":" << Row.Line;
1664a34c753fSRafael Auler       if (Row.Column)
1665a34c753fSRafael Auler         OS << ":" << Row.Column;
1666a34c753fSRafael Auler       if (Row.Discriminator)
1667a34c753fSRafael Auler         OS << " discriminator:" << Row.Discriminator;
1668a34c753fSRafael Auler     }
1669a34c753fSRafael Auler   }
1670a34c753fSRafael Auler 
1671a34c753fSRafael Auler   if ((opts::PrintRelocations || PrintRelocations) && Function) {
1672a34c753fSRafael Auler     const uint64_t Size = computeCodeSize(&Instruction, &Instruction + 1);
1673a34c753fSRafael Auler     Function->printRelocations(OS, Offset, Size);
1674a34c753fSRafael Auler   }
1675a34c753fSRafael Auler 
1676a34c753fSRafael Auler   OS << "\n";
1677a34c753fSRafael Auler 
1678a34c753fSRafael Auler   if (PrintMCInst) {
1679a34c753fSRafael Auler     Instruction.dump_pretty(OS, InstPrinter.get());
1680a34c753fSRafael Auler     OS << "\n";
1681a34c753fSRafael Auler   }
1682a34c753fSRafael Auler }
1683a34c753fSRafael Auler 
1684a34c753fSRafael Auler ErrorOr<BinarySection &> BinaryContext::getSectionForAddress(uint64_t Address) {
1685a34c753fSRafael Auler   auto SI = AddressToSection.upper_bound(Address);
1686a34c753fSRafael Auler   if (SI != AddressToSection.begin()) {
1687a34c753fSRafael Auler     --SI;
1688a34c753fSRafael Auler     uint64_t UpperBound = SI->first + SI->second->getSize();
1689a34c753fSRafael Auler     if (!SI->second->getSize())
1690a34c753fSRafael Auler       UpperBound += 1;
1691a34c753fSRafael Auler     if (UpperBound > Address)
1692a34c753fSRafael Auler       return *SI->second;
1693a34c753fSRafael Auler   }
1694a34c753fSRafael Auler   return std::make_error_code(std::errc::bad_address);
1695a34c753fSRafael Auler }
1696a34c753fSRafael Auler 
1697a34c753fSRafael Auler ErrorOr<StringRef>
1698a34c753fSRafael Auler BinaryContext::getSectionNameForAddress(uint64_t Address) const {
16993652483cSRafael Auler   if (ErrorOr<const BinarySection &> Section = getSectionForAddress(Address))
1700a34c753fSRafael Auler     return Section->getName();
1701a34c753fSRafael Auler   return std::make_error_code(std::errc::bad_address);
1702a34c753fSRafael Auler }
1703a34c753fSRafael Auler 
1704a34c753fSRafael Auler BinarySection &BinaryContext::registerSection(BinarySection *Section) {
1705a34c753fSRafael Auler   auto Res = Sections.insert(Section);
1706a34c753fSRafael Auler   (void)Res;
1707a34c753fSRafael Auler   assert(Res.second && "can't register the same section twice.");
1708a34c753fSRafael Auler 
1709a34c753fSRafael Auler   // Only register allocatable sections in the AddressToSection map.
1710a34c753fSRafael Auler   if (Section->isAllocatable() && Section->getAddress())
1711a34c753fSRafael Auler     AddressToSection.insert(std::make_pair(Section->getAddress(), Section));
1712a34c753fSRafael Auler   NameToSection.insert(
1713a34c753fSRafael Auler       std::make_pair(std::string(Section->getName()), Section));
1714a34c753fSRafael Auler   LLVM_DEBUG(dbgs() << "BOLT-DEBUG: registering " << *Section << "\n");
1715a34c753fSRafael Auler   return *Section;
1716a34c753fSRafael Auler }
1717a34c753fSRafael Auler 
1718a34c753fSRafael Auler BinarySection &BinaryContext::registerSection(SectionRef Section) {
1719a34c753fSRafael Auler   return registerSection(new BinarySection(*this, Section));
1720a34c753fSRafael Auler }
1721a34c753fSRafael Auler 
1722a34c753fSRafael Auler BinarySection &
1723a34c753fSRafael Auler BinaryContext::registerSection(StringRef SectionName,
1724a34c753fSRafael Auler                                const BinarySection &OriginalSection) {
172540c2e0faSMaksim Panchenko   return registerSection(
172640c2e0faSMaksim Panchenko       new BinarySection(*this, SectionName, OriginalSection));
1727a34c753fSRafael Auler }
1728a34c753fSRafael Auler 
172940c2e0faSMaksim Panchenko BinarySection &
173040c2e0faSMaksim Panchenko BinaryContext::registerOrUpdateSection(StringRef Name, unsigned ELFType,
173140c2e0faSMaksim Panchenko                                        unsigned ELFFlags, uint8_t *Data,
173240c2e0faSMaksim Panchenko                                        uint64_t Size, unsigned Alignment) {
1733a34c753fSRafael Auler   auto NamedSections = getSectionByName(Name);
1734a34c753fSRafael Auler   if (NamedSections.begin() != NamedSections.end()) {
1735a34c753fSRafael Auler     assert(std::next(NamedSections.begin()) == NamedSections.end() &&
1736a34c753fSRafael Auler            "can only update unique sections");
1737a34c753fSRafael Auler     BinarySection *Section = NamedSections.begin()->second;
1738a34c753fSRafael Auler 
1739a34c753fSRafael Auler     LLVM_DEBUG(dbgs() << "BOLT-DEBUG: updating " << *Section << " -> ");
1740a34c753fSRafael Auler     const bool Flag = Section->isAllocatable();
1741a34c753fSRafael Auler     (void)Flag;
1742a34c753fSRafael Auler     Section->update(Data, Size, Alignment, ELFType, ELFFlags);
1743a34c753fSRafael Auler     LLVM_DEBUG(dbgs() << *Section << "\n");
1744a34c753fSRafael Auler     // FIXME: Fix section flags/attributes for MachO.
1745a34c753fSRafael Auler     if (isELF())
1746a34c753fSRafael Auler       assert(Flag == Section->isAllocatable() &&
1747a34c753fSRafael Auler              "can't change section allocation status");
1748a34c753fSRafael Auler     return *Section;
1749a34c753fSRafael Auler   }
1750a34c753fSRafael Auler 
175140c2e0faSMaksim Panchenko   return registerSection(
175240c2e0faSMaksim Panchenko       new BinarySection(*this, Name, Data, Size, Alignment, ELFType, ELFFlags));
1753a34c753fSRafael Auler }
1754a34c753fSRafael Auler 
1755a34c753fSRafael Auler bool BinaryContext::deregisterSection(BinarySection &Section) {
1756a34c753fSRafael Auler   BinarySection *SectionPtr = &Section;
1757a34c753fSRafael Auler   auto Itr = Sections.find(SectionPtr);
1758a34c753fSRafael Auler   if (Itr != Sections.end()) {
1759a34c753fSRafael Auler     auto Range = AddressToSection.equal_range(SectionPtr->getAddress());
1760a34c753fSRafael Auler     while (Range.first != Range.second) {
1761a34c753fSRafael Auler       if (Range.first->second == SectionPtr) {
1762a34c753fSRafael Auler         AddressToSection.erase(Range.first);
1763a34c753fSRafael Auler         break;
1764a34c753fSRafael Auler       }
1765a34c753fSRafael Auler       ++Range.first;
1766a34c753fSRafael Auler     }
1767a34c753fSRafael Auler 
1768a34c753fSRafael Auler     auto NameRange =
1769a34c753fSRafael Auler         NameToSection.equal_range(std::string(SectionPtr->getName()));
1770a34c753fSRafael Auler     while (NameRange.first != NameRange.second) {
1771a34c753fSRafael Auler       if (NameRange.first->second == SectionPtr) {
1772a34c753fSRafael Auler         NameToSection.erase(NameRange.first);
1773a34c753fSRafael Auler         break;
1774a34c753fSRafael Auler       }
1775a34c753fSRafael Auler       ++NameRange.first;
1776a34c753fSRafael Auler     }
1777a34c753fSRafael Auler 
1778a34c753fSRafael Auler     Sections.erase(Itr);
1779a34c753fSRafael Auler     delete SectionPtr;
1780a34c753fSRafael Auler     return true;
1781a34c753fSRafael Auler   }
1782a34c753fSRafael Auler   return false;
1783a34c753fSRafael Auler }
1784a34c753fSRafael Auler 
1785a34c753fSRafael Auler void BinaryContext::printSections(raw_ostream &OS) const {
17863652483cSRafael Auler   for (BinarySection *const &Section : Sections)
1787a34c753fSRafael Auler     OS << "BOLT-INFO: " << *Section << "\n";
1788a34c753fSRafael Auler }
1789a34c753fSRafael Auler 
1790a34c753fSRafael Auler BinarySection &BinaryContext::absoluteSection() {
1791a34c753fSRafael Auler   if (ErrorOr<BinarySection &> Section = getUniqueSectionByName("<absolute>"))
1792a34c753fSRafael Auler     return *Section;
1793a34c753fSRafael Auler   return registerOrUpdateSection("<absolute>", ELF::SHT_NULL, 0u);
1794a34c753fSRafael Auler }
1795a34c753fSRafael Auler 
179640c2e0faSMaksim Panchenko ErrorOr<uint64_t> BinaryContext::getUnsignedValueAtAddress(uint64_t Address,
1797a34c753fSRafael Auler                                                            size_t Size) const {
1798a34c753fSRafael Auler   const ErrorOr<const BinarySection &> Section = getSectionForAddress(Address);
1799a34c753fSRafael Auler   if (!Section)
1800a34c753fSRafael Auler     return std::make_error_code(std::errc::bad_address);
1801a34c753fSRafael Auler 
1802a34c753fSRafael Auler   if (Section->isVirtual())
1803a34c753fSRafael Auler     return 0;
1804a34c753fSRafael Auler 
1805a34c753fSRafael Auler   DataExtractor DE(Section->getContents(), AsmInfo->isLittleEndian(),
1806a34c753fSRafael Auler                    AsmInfo->getCodePointerSize());
1807a34c753fSRafael Auler   auto ValueOffset = static_cast<uint64_t>(Address - Section->getAddress());
1808a34c753fSRafael Auler   return DE.getUnsigned(&ValueOffset, Size);
1809a34c753fSRafael Auler }
1810a34c753fSRafael Auler 
181140c2e0faSMaksim Panchenko ErrorOr<uint64_t> BinaryContext::getSignedValueAtAddress(uint64_t Address,
1812a34c753fSRafael Auler                                                          size_t Size) const {
1813a34c753fSRafael Auler   const ErrorOr<const BinarySection &> Section = getSectionForAddress(Address);
1814a34c753fSRafael Auler   if (!Section)
1815a34c753fSRafael Auler     return std::make_error_code(std::errc::bad_address);
1816a34c753fSRafael Auler 
1817a34c753fSRafael Auler   if (Section->isVirtual())
1818a34c753fSRafael Auler     return 0;
1819a34c753fSRafael Auler 
1820a34c753fSRafael Auler   DataExtractor DE(Section->getContents(), AsmInfo->isLittleEndian(),
1821a34c753fSRafael Auler                    AsmInfo->getCodePointerSize());
1822a34c753fSRafael Auler   auto ValueOffset = static_cast<uint64_t>(Address - Section->getAddress());
1823a34c753fSRafael Auler   return DE.getSigned(&ValueOffset, Size);
1824a34c753fSRafael Auler }
1825a34c753fSRafael Auler 
182640c2e0faSMaksim Panchenko void BinaryContext::addRelocation(uint64_t Address, MCSymbol *Symbol,
182740c2e0faSMaksim Panchenko                                   uint64_t Type, uint64_t Addend,
1828a34c753fSRafael Auler                                   uint64_t Value) {
1829a34c753fSRafael Auler   ErrorOr<BinarySection &> Section = getSectionForAddress(Address);
1830a34c753fSRafael Auler   assert(Section && "cannot find section for address");
183140c2e0faSMaksim Panchenko   Section->addRelocation(Address - Section->getAddress(), Symbol, Type, Addend,
1832a34c753fSRafael Auler                          Value);
1833a34c753fSRafael Auler }
1834a34c753fSRafael Auler 
183540c2e0faSMaksim Panchenko void BinaryContext::addDynamicRelocation(uint64_t Address, MCSymbol *Symbol,
183640c2e0faSMaksim Panchenko                                          uint64_t Type, uint64_t Addend,
1837a34c753fSRafael Auler                                          uint64_t Value) {
1838a34c753fSRafael Auler   ErrorOr<BinarySection &> Section = getSectionForAddress(Address);
1839a34c753fSRafael Auler   assert(Section && "cannot find section for address");
184040c2e0faSMaksim Panchenko   Section->addDynamicRelocation(Address - Section->getAddress(), Symbol, Type,
184140c2e0faSMaksim Panchenko                                 Addend, Value);
1842a34c753fSRafael Auler }
1843a34c753fSRafael Auler 
1844a34c753fSRafael Auler bool BinaryContext::removeRelocationAt(uint64_t Address) {
1845a34c753fSRafael Auler   ErrorOr<BinarySection &> Section = getSectionForAddress(Address);
1846a34c753fSRafael Auler   assert(Section && "cannot find section for address");
1847a34c753fSRafael Auler   return Section->removeRelocationAt(Address - Section->getAddress());
1848a34c753fSRafael Auler }
1849a34c753fSRafael Auler 
1850a34c753fSRafael Auler const Relocation *BinaryContext::getRelocationAt(uint64_t Address) {
1851a34c753fSRafael Auler   ErrorOr<BinarySection &> Section = getSectionForAddress(Address);
1852a34c753fSRafael Auler   if (!Section)
1853a34c753fSRafael Auler     return nullptr;
1854a34c753fSRafael Auler 
1855a34c753fSRafael Auler   return Section->getRelocationAt(Address - Section->getAddress());
1856a34c753fSRafael Auler }
1857a34c753fSRafael Auler 
1858a34c753fSRafael Auler const Relocation *BinaryContext::getDynamicRelocationAt(uint64_t Address) {
1859a34c753fSRafael Auler   ErrorOr<BinarySection &> Section = getSectionForAddress(Address);
1860a34c753fSRafael Auler   if (!Section)
1861a34c753fSRafael Auler     return nullptr;
1862a34c753fSRafael Auler 
1863a34c753fSRafael Auler   return Section->getDynamicRelocationAt(Address - Section->getAddress());
1864a34c753fSRafael Auler }
1865a34c753fSRafael Auler 
1866a34c753fSRafael Auler void BinaryContext::markAmbiguousRelocations(BinaryData &BD,
1867a34c753fSRafael Auler                                              const uint64_t Address) {
1868a34c753fSRafael Auler   auto setImmovable = [&](BinaryData &BD) {
1869a34c753fSRafael Auler     BinaryData *Root = BD.getAtomicRoot();
1870a34c753fSRafael Auler     LLVM_DEBUG(if (Root->isMoveable()) {
1871a34c753fSRafael Auler       dbgs() << "BOLT-DEBUG: setting " << *Root << " as immovable "
1872a34c753fSRafael Auler              << "due to ambiguous relocation referencing 0x"
1873a34c753fSRafael Auler              << Twine::utohexstr(Address) << '\n';
1874a34c753fSRafael Auler     });
1875a34c753fSRafael Auler     Root->setIsMoveable(false);
1876a34c753fSRafael Auler   };
1877a34c753fSRafael Auler 
1878a34c753fSRafael Auler   if (Address == BD.getAddress()) {
1879a34c753fSRafael Auler     setImmovable(BD);
1880a34c753fSRafael Auler 
1881a34c753fSRafael Auler     // Set previous symbol as immovable
1882a34c753fSRafael Auler     BinaryData *Prev = getBinaryDataContainingAddress(Address - 1);
1883a34c753fSRafael Auler     if (Prev && Prev->getEndAddress() == BD.getAddress())
1884a34c753fSRafael Auler       setImmovable(*Prev);
1885a34c753fSRafael Auler   }
1886a34c753fSRafael Auler 
1887a34c753fSRafael Auler   if (Address == BD.getEndAddress()) {
1888a34c753fSRafael Auler     setImmovable(BD);
1889a34c753fSRafael Auler 
1890a34c753fSRafael Auler     // Set next symbol as immovable
1891a34c753fSRafael Auler     BinaryData *Next = getBinaryDataContainingAddress(BD.getEndAddress());
1892a34c753fSRafael Auler     if (Next && Next->getAddress() == BD.getEndAddress())
1893a34c753fSRafael Auler       setImmovable(*Next);
1894a34c753fSRafael Auler   }
1895a34c753fSRafael Auler }
1896a34c753fSRafael Auler 
1897a34c753fSRafael Auler BinaryFunction *BinaryContext::getFunctionForSymbol(const MCSymbol *Symbol,
1898a34c753fSRafael Auler                                                     uint64_t *EntryDesc) {
1899a34c753fSRafael Auler   std::shared_lock<std::shared_timed_mutex> Lock(SymbolToFunctionMapMutex);
1900a34c753fSRafael Auler   auto BFI = SymbolToFunctionMap.find(Symbol);
1901a34c753fSRafael Auler   if (BFI == SymbolToFunctionMap.end())
1902a34c753fSRafael Auler     return nullptr;
1903a34c753fSRafael Auler 
1904a34c753fSRafael Auler   BinaryFunction *BF = BFI->second;
1905a34c753fSRafael Auler   if (EntryDesc)
1906a34c753fSRafael Auler     *EntryDesc = BF->getEntryIDForSymbol(Symbol);
1907a34c753fSRafael Auler 
1908a34c753fSRafael Auler   return BF;
1909a34c753fSRafael Auler }
1910a34c753fSRafael Auler 
1911a34c753fSRafael Auler void BinaryContext::exitWithBugReport(StringRef Message,
1912a34c753fSRafael Auler                                       const BinaryFunction &Function) const {
1913a34c753fSRafael Auler   errs() << "=======================================\n";
1914a34c753fSRafael Auler   errs() << "BOLT is unable to proceed because it couldn't properly understand "
1915a34c753fSRafael Auler             "this function.\n";
1916a34c753fSRafael Auler   errs() << "If you are running the most recent version of BOLT, you may "
1917a34c753fSRafael Auler             "want to "
1918a34c753fSRafael Auler             "report this and paste this dump.\nPlease check that there is no "
1919a34c753fSRafael Auler             "sensitive contents being shared in this dump.\n";
1920a34c753fSRafael Auler   errs() << "\nOffending function: " << Function.getPrintName() << "\n\n";
1921a34c753fSRafael Auler   ScopedPrinter SP(errs());
1922a34c753fSRafael Auler   SP.printBinaryBlock("Function contents", *Function.getData());
1923a34c753fSRafael Auler   errs() << "\n";
1924a34c753fSRafael Auler   Function.dump();
1925a34c753fSRafael Auler   errs() << "ERROR: " << Message;
1926a34c753fSRafael Auler   errs() << "\n=======================================\n";
1927a34c753fSRafael Auler   exit(1);
1928a34c753fSRafael Auler }
1929a34c753fSRafael Auler 
1930a34c753fSRafael Auler BinaryFunction *
1931a34c753fSRafael Auler BinaryContext::createInjectedBinaryFunction(const std::string &Name,
1932a34c753fSRafael Auler                                             bool IsSimple) {
1933a34c753fSRafael Auler   InjectedBinaryFunctions.push_back(new BinaryFunction(Name, *this, IsSimple));
1934a34c753fSRafael Auler   BinaryFunction *BF = InjectedBinaryFunctions.back();
1935a34c753fSRafael Auler   setSymbolToFunctionMap(BF->getSymbol(), BF);
1936a34c753fSRafael Auler   BF->CurrentState = BinaryFunction::State::CFG;
1937a34c753fSRafael Auler   return BF;
1938a34c753fSRafael Auler }
1939a34c753fSRafael Auler 
1940a34c753fSRafael Auler std::pair<size_t, size_t>
1941a34c753fSRafael Auler BinaryContext::calculateEmittedSize(BinaryFunction &BF, bool FixBranches) {
1942a34c753fSRafael Auler   // Adjust branch instruction to match the current layout.
1943a34c753fSRafael Auler   if (FixBranches)
1944a34c753fSRafael Auler     BF.fixBranches();
1945a34c753fSRafael Auler 
1946a34c753fSRafael Auler   // Create local MC context to isolate the effect of ephemeral code emission.
1947a34c753fSRafael Auler   IndependentCodeEmitter MCEInstance = createIndependentMCCodeEmitter();
1948a34c753fSRafael Auler   MCContext *LocalCtx = MCEInstance.LocalCtx.get();
1949a34c753fSRafael Auler   MCAsmBackend *MAB =
1950a34c753fSRafael Auler       TheTarget->createMCAsmBackend(*STI, *MRI, MCTargetOptions());
1951a34c753fSRafael Auler 
1952a34c753fSRafael Auler   SmallString<256> Code;
1953a34c753fSRafael Auler   raw_svector_ostream VecOS(Code);
1954a34c753fSRafael Auler 
1955a34c753fSRafael Auler   std::unique_ptr<MCObjectWriter> OW = MAB->createObjectWriter(VecOS);
1956a34c753fSRafael Auler   std::unique_ptr<MCStreamer> Streamer(TheTarget->createMCObjectStreamer(
1957a34c753fSRafael Auler       *TheTriple, *LocalCtx, std::unique_ptr<MCAsmBackend>(MAB), std::move(OW),
1958a34c753fSRafael Auler       std::unique_ptr<MCCodeEmitter>(MCEInstance.MCE.release()), *STI,
1959a34c753fSRafael Auler       /*RelaxAll=*/false,
1960a34c753fSRafael Auler       /*IncrementalLinkerCompatible=*/false,
1961a34c753fSRafael Auler       /*DWARFMustBeAtTheEnd=*/false));
1962a34c753fSRafael Auler 
1963a34c753fSRafael Auler   Streamer->initSections(false, *STI);
1964a34c753fSRafael Auler 
1965a34c753fSRafael Auler   MCSection *Section = MCEInstance.LocalMOFI->getTextSection();
1966a34c753fSRafael Auler   Section->setHasInstructions(true);
1967a34c753fSRafael Auler 
1968a34c753fSRafael Auler   // Create symbols in the LocalCtx so that they get destroyed with it.
1969a34c753fSRafael Auler   MCSymbol *StartLabel = LocalCtx->createTempSymbol();
1970a34c753fSRafael Auler   MCSymbol *EndLabel = LocalCtx->createTempSymbol();
1971a34c753fSRafael Auler   MCSymbol *ColdStartLabel = LocalCtx->createTempSymbol();
1972a34c753fSRafael Auler   MCSymbol *ColdEndLabel = LocalCtx->createTempSymbol();
1973a34c753fSRafael Auler 
1974a34c753fSRafael Auler   Streamer->SwitchSection(Section);
1975a34c753fSRafael Auler   Streamer->emitLabel(StartLabel);
1976a34c753fSRafael Auler   emitFunctionBody(*Streamer, BF, /*EmitColdPart=*/false,
1977a34c753fSRafael Auler                    /*EmitCodeOnly=*/true);
1978a34c753fSRafael Auler   Streamer->emitLabel(EndLabel);
1979a34c753fSRafael Auler 
1980a34c753fSRafael Auler   if (BF.isSplit()) {
1981a34c753fSRafael Auler     MCSectionELF *ColdSection =
1982a34c753fSRafael Auler         LocalCtx->getELFSection(BF.getColdCodeSectionName(), ELF::SHT_PROGBITS,
1983a34c753fSRafael Auler                                 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
1984a34c753fSRafael Auler     ColdSection->setHasInstructions(true);
1985a34c753fSRafael Auler 
1986a34c753fSRafael Auler     Streamer->SwitchSection(ColdSection);
1987a34c753fSRafael Auler     Streamer->emitLabel(ColdStartLabel);
1988a34c753fSRafael Auler     emitFunctionBody(*Streamer, BF, /*EmitColdPart=*/true,
1989a34c753fSRafael Auler                      /*EmitCodeOnly=*/true);
1990a34c753fSRafael Auler     Streamer->emitLabel(ColdEndLabel);
1991a34c753fSRafael Auler     // To avoid calling MCObjectStreamer::flushPendingLabels() which is private
1992a34c753fSRafael Auler     Streamer->emitBytes(StringRef(""));
1993a34c753fSRafael Auler     Streamer->SwitchSection(Section);
1994a34c753fSRafael Auler   }
1995a34c753fSRafael Auler 
1996a34c753fSRafael Auler   // To avoid calling MCObjectStreamer::flushPendingLabels() which is private or
1997a34c753fSRafael Auler   // MCStreamer::Finish(), which does more than we want
1998a34c753fSRafael Auler   Streamer->emitBytes(StringRef(""));
1999a34c753fSRafael Auler 
2000a34c753fSRafael Auler   MCAssembler &Assembler =
2001a34c753fSRafael Auler       static_cast<MCObjectStreamer *>(Streamer.get())->getAssembler();
2002a34c753fSRafael Auler   MCAsmLayout Layout(Assembler);
2003a34c753fSRafael Auler   Assembler.layout(Layout);
2004a34c753fSRafael Auler 
2005a34c753fSRafael Auler   const uint64_t HotSize =
2006a34c753fSRafael Auler       Layout.getSymbolOffset(*EndLabel) - Layout.getSymbolOffset(*StartLabel);
2007a34c753fSRafael Auler   const uint64_t ColdSize = BF.isSplit()
2008a34c753fSRafael Auler                                 ? Layout.getSymbolOffset(*ColdEndLabel) -
2009a34c753fSRafael Auler                                       Layout.getSymbolOffset(*ColdStartLabel)
2010a34c753fSRafael Auler                                 : 0ULL;
2011a34c753fSRafael Auler 
2012a34c753fSRafael Auler   // Clean-up the effect of the code emission.
2013a34c753fSRafael Auler   for (const MCSymbol &Symbol : Assembler.symbols()) {
2014a34c753fSRafael Auler     MCSymbol *MutableSymbol = const_cast<MCSymbol *>(&Symbol);
2015a34c753fSRafael Auler     MutableSymbol->setUndefined();
2016a34c753fSRafael Auler     MutableSymbol->setIsRegistered(false);
2017a34c753fSRafael Auler   }
2018a34c753fSRafael Auler 
2019a34c753fSRafael Auler   return std::make_pair(HotSize, ColdSize);
2020a34c753fSRafael Auler }
2021a34c753fSRafael Auler 
2022a34c753fSRafael Auler bool BinaryContext::validateEncoding(const MCInst &Inst,
2023a34c753fSRafael Auler                                      ArrayRef<uint8_t> InputEncoding) const {
2024a34c753fSRafael Auler   SmallString<256> Code;
2025a34c753fSRafael Auler   SmallVector<MCFixup, 4> Fixups;
2026a34c753fSRafael Auler   raw_svector_ostream VecOS(Code);
2027a34c753fSRafael Auler 
2028a34c753fSRafael Auler   MCE->encodeInstruction(Inst, VecOS, Fixups, *STI);
2029a34c753fSRafael Auler   auto EncodedData = ArrayRef<uint8_t>((uint8_t *)Code.data(), Code.size());
2030a34c753fSRafael Auler   if (InputEncoding != EncodedData) {
2031a34c753fSRafael Auler     if (opts::Verbosity > 1) {
2032a34c753fSRafael Auler       errs() << "BOLT-WARNING: mismatched encoding detected\n"
2033a34c753fSRafael Auler              << "      input: " << InputEncoding << '\n'
2034a34c753fSRafael Auler              << "     output: " << EncodedData << '\n';
2035a34c753fSRafael Auler     }
2036a34c753fSRafael Auler     return false;
2037a34c753fSRafael Auler   }
2038a34c753fSRafael Auler 
2039a34c753fSRafael Auler   return true;
2040a34c753fSRafael Auler }
2041a34c753fSRafael Auler 
2042a34c753fSRafael Auler uint64_t BinaryContext::getHotThreshold() const {
2043a34c753fSRafael Auler   static uint64_t Threshold = 0;
2044a34c753fSRafael Auler   if (Threshold == 0) {
204540c2e0faSMaksim Panchenko     Threshold = std::max(
204640c2e0faSMaksim Panchenko         (uint64_t)opts::ExecutionCountThreshold,
2047a34c753fSRafael Auler         NumProfiledFuncs ? SumExecutionCount / (2 * NumProfiledFuncs) : 1);
2048a34c753fSRafael Auler   }
2049a34c753fSRafael Auler   return Threshold;
2050a34c753fSRafael Auler }
2051a34c753fSRafael Auler 
205240c2e0faSMaksim Panchenko BinaryFunction *BinaryContext::getBinaryFunctionContainingAddress(
205340c2e0faSMaksim Panchenko     uint64_t Address, bool CheckPastEnd, bool UseMaxSize) {
2054a34c753fSRafael Auler   auto FI = BinaryFunctions.upper_bound(Address);
2055a34c753fSRafael Auler   if (FI == BinaryFunctions.begin())
2056a34c753fSRafael Auler     return nullptr;
2057a34c753fSRafael Auler   --FI;
2058a34c753fSRafael Auler 
2059a34c753fSRafael Auler   const uint64_t UsedSize =
2060a34c753fSRafael Auler       UseMaxSize ? FI->second.getMaxSize() : FI->second.getSize();
2061a34c753fSRafael Auler 
2062a34c753fSRafael Auler   if (Address >= FI->first + UsedSize + (CheckPastEnd ? 1 : 0))
2063a34c753fSRafael Auler     return nullptr;
2064a34c753fSRafael Auler 
2065a34c753fSRafael Auler   return &FI->second;
2066a34c753fSRafael Auler }
2067a34c753fSRafael Auler 
206840c2e0faSMaksim Panchenko BinaryFunction *BinaryContext::getBinaryFunctionAtAddress(uint64_t Address) {
2069a34c753fSRafael Auler   // First, try to find a function starting at the given address. If the
2070a34c753fSRafael Auler   // function was folded, this will get us the original folded function if it
2071a34c753fSRafael Auler   // wasn't removed from the list, e.g. in non-relocation mode.
2072a34c753fSRafael Auler   auto BFI = BinaryFunctions.find(Address);
20733652483cSRafael Auler   if (BFI != BinaryFunctions.end())
2074a34c753fSRafael Auler     return &BFI->second;
2075a34c753fSRafael Auler 
2076a34c753fSRafael Auler   // We might have folded the function matching the object at the given
2077a34c753fSRafael Auler   // address. In such case, we look for a function matching the symbol
2078a34c753fSRafael Auler   // registered at the original address. The new function (the one that the
2079a34c753fSRafael Auler   // original was folded into) will hold the symbol.
2080a34c753fSRafael Auler   if (const BinaryData *BD = getBinaryDataAtAddress(Address)) {
2081a34c753fSRafael Auler     uint64_t EntryID = 0;
2082a34c753fSRafael Auler     BinaryFunction *BF = getFunctionForSymbol(BD->getSymbol(), &EntryID);
2083a34c753fSRafael Auler     if (BF && EntryID == 0)
2084a34c753fSRafael Auler       return BF;
2085a34c753fSRafael Auler   }
2086a34c753fSRafael Auler   return nullptr;
2087a34c753fSRafael Auler }
2088a34c753fSRafael Auler 
2089a34c753fSRafael Auler DebugAddressRangesVector BinaryContext::translateModuleAddressRanges(
2090a34c753fSRafael Auler     const DWARFAddressRangesVector &InputRanges) const {
2091a34c753fSRafael Auler   DebugAddressRangesVector OutputRanges;
2092a34c753fSRafael Auler 
2093a34c753fSRafael Auler   for (const DWARFAddressRange Range : InputRanges) {
2094a34c753fSRafael Auler     auto BFI = BinaryFunctions.lower_bound(Range.LowPC);
2095a34c753fSRafael Auler     while (BFI != BinaryFunctions.end()) {
2096a34c753fSRafael Auler       const BinaryFunction &Function = BFI->second;
2097a34c753fSRafael Auler       if (Function.getAddress() >= Range.HighPC)
2098a34c753fSRafael Auler         break;
2099a34c753fSRafael Auler       const DebugAddressRangesVector FunctionRanges =
2100a34c753fSRafael Auler           Function.getOutputAddressRanges();
210140c2e0faSMaksim Panchenko       std::move(std::begin(FunctionRanges), std::end(FunctionRanges),
2102a34c753fSRafael Auler                 std::back_inserter(OutputRanges));
2103a34c753fSRafael Auler       std::advance(BFI, 1);
2104a34c753fSRafael Auler     }
2105a34c753fSRafael Auler   }
2106a34c753fSRafael Auler 
2107a34c753fSRafael Auler   return OutputRanges;
2108a34c753fSRafael Auler }
2109a34c753fSRafael Auler 
2110a34c753fSRafael Auler } // namespace bolt
2111a34c753fSRafael Auler } // namespace llvm
2112