xref: /llvm-project/bolt/lib/RuntimeLibs/HugifyRuntimeLibrary.cpp (revision abc2eae68290c453e1899a94eccc4ed5ea3b69c1)
12f09f445SMaksim Panchenko //===- bolt/RuntimeLibs/HugifyRuntimeLibrary.cpp - Hugify RT Library ------===//
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 //===----------------------------------------------------------------------===//
82f09f445SMaksim Panchenko //
92f09f445SMaksim Panchenko // This file implements the HugifyRuntimeLibrary class.
102f09f445SMaksim Panchenko //
112f09f445SMaksim Panchenko //===----------------------------------------------------------------------===//
12a34c753fSRafael Auler 
13a34c753fSRafael Auler #include "bolt/RuntimeLibs/HugifyRuntimeLibrary.h"
14fd38366eSAmir Ayupov #include "bolt/Core/BinaryContext.h"
1505634f73SJob Noorman #include "bolt/Core/Linker.h"
16a34c753fSRafael Auler #include "llvm/MC/MCStreamer.h"
17a34c753fSRafael Auler #include "llvm/Support/CommandLine.h"
18a34c753fSRafael Auler 
19a34c753fSRafael Auler using namespace llvm;
20a34c753fSRafael Auler using namespace bolt;
21a34c753fSRafael Auler 
22a34c753fSRafael Auler namespace opts {
23a34c753fSRafael Auler 
24a34c753fSRafael Auler extern cl::OptionCategory BoltOptCategory;
25a34c753fSRafael Auler 
26a34c753fSRafael Auler extern cl::opt<bool> HotText;
27a34c753fSRafael Auler 
28a34c753fSRafael Auler cl::opt<bool>
29a34c753fSRafael Auler     Hugify("hugify",
30a34c753fSRafael Auler            cl::desc("Automatically put hot code on 2MB page(s) (hugify) at "
31a34c753fSRafael Auler                     "runtime. No manual call to hugify is needed in the binary "
32a34c753fSRafael Auler                     "(which is what --hot-text relies on)."),
33b92436efSFangrui Song            cl::cat(BoltOptCategory));
34a34c753fSRafael Auler 
35*abc2eae6STristan Ross static cl::opt<std::string>
36*abc2eae6STristan Ross     RuntimeHugifyLib("runtime-hugify-lib",
37*abc2eae6STristan Ross                      cl::desc("specify path of the runtime hugify library"),
38a34c753fSRafael Auler                      cl::init("libbolt_rt_hugify.a"), cl::cat(BoltOptCategory));
39a34c753fSRafael Auler 
40a34c753fSRafael Auler } // namespace opts
41a34c753fSRafael Auler 
42a34c753fSRafael Auler void HugifyRuntimeLibrary::adjustCommandLineOptions(
43a34c753fSRafael Auler     const BinaryContext &BC) const {
44a34c753fSRafael Auler   if (opts::HotText) {
45a34c753fSRafael Auler     errs()
46a34c753fSRafael Auler         << "BOLT-ERROR: -hot-text should be applied to binaries with "
47a34c753fSRafael Auler            "pre-compiled manual hugify support, while -hugify will add hugify "
489966b3e7SGabriel Ravier            "support automatically. These two options cannot both be present.\n";
49a34c753fSRafael Auler     exit(1);
50a34c753fSRafael Auler   }
51a34c753fSRafael Auler   // After the check, we set HotText to be true because automated hugify support
52a34c753fSRafael Auler   // relies on it.
53a34c753fSRafael Auler   opts::HotText = true;
54a34c753fSRafael Auler   if (!BC.StartFunctionAddress) {
55a34c753fSRafael Auler     errs() << "BOLT-ERROR: hugify runtime libraries require a known entry "
56a34c753fSRafael Auler               "point of "
57a34c753fSRafael Auler               "the input binary\n";
58a34c753fSRafael Auler     exit(1);
59a34c753fSRafael Auler   }
60a34c753fSRafael Auler }
61a34c753fSRafael Auler 
62a34c753fSRafael Auler void HugifyRuntimeLibrary::link(BinaryContext &BC, StringRef ToolPath,
6305634f73SJob Noorman                                 BOLTLinker &Linker,
6405634f73SJob Noorman                                 BOLTLinker::SectionsMapper MapSections) {
6505634f73SJob Noorman 
66a34c753fSRafael Auler   std::string LibPath = getLibPath(ToolPath, opts::RuntimeHugifyLib);
6705634f73SJob Noorman   loadLibrary(LibPath, Linker, MapSections);
68a34c753fSRafael Auler 
69a34c753fSRafael Auler   assert(!RuntimeStartAddress &&
70a34c753fSRafael Auler          "We don't currently support linking multiple runtime libraries");
7105634f73SJob Noorman   RuntimeStartAddress = Linker.lookupSymbol("__bolt_hugify_self").value_or(0);
72a34c753fSRafael Auler   if (!RuntimeStartAddress) {
7366432943SVladislav Khmelevsky     errs() << "BOLT-ERROR: hugify library does not define __bolt_hugify_self: "
74a34c753fSRafael Auler            << LibPath << "\n";
75a34c753fSRafael Auler     exit(1);
76a34c753fSRafael Auler   }
77a34c753fSRafael Auler }
78