xref: /llvm-project/bolt/lib/RuntimeLibs/HugifyRuntimeLibrary.cpp (revision 05634f7346a59f6dab89cde53f39b40d9a70b9c9)
1 //===- bolt/RuntimeLibs/HugifyRuntimeLibrary.cpp - Hugify RT Library ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the HugifyRuntimeLibrary class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "bolt/RuntimeLibs/HugifyRuntimeLibrary.h"
14 #include "bolt/Core/BinaryFunction.h"
15 #include "bolt/Core/Linker.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/Support/Alignment.h"
18 #include "llvm/Support/CommandLine.h"
19 
20 using namespace llvm;
21 using namespace bolt;
22 
23 namespace opts {
24 
25 extern cl::OptionCategory BoltOptCategory;
26 
27 extern cl::opt<bool> HotText;
28 
29 cl::opt<bool>
30     Hugify("hugify",
31            cl::desc("Automatically put hot code on 2MB page(s) (hugify) at "
32                     "runtime. No manual call to hugify is needed in the binary "
33                     "(which is what --hot-text relies on)."),
34            cl::cat(BoltOptCategory));
35 
36 static cl::opt<std::string> RuntimeHugifyLib(
37     "runtime-hugify-lib",
38     cl::desc("specify file name of the runtime hugify library"),
39     cl::init("libbolt_rt_hugify.a"), cl::cat(BoltOptCategory));
40 
41 } // namespace opts
42 
43 void HugifyRuntimeLibrary::adjustCommandLineOptions(
44     const BinaryContext &BC) const {
45   if (opts::HotText) {
46     errs()
47         << "BOLT-ERROR: -hot-text should be applied to binaries with "
48            "pre-compiled manual hugify support, while -hugify will add hugify "
49            "support automatically. These two options cannot both be present.\n";
50     exit(1);
51   }
52   // After the check, we set HotText to be true because automated hugify support
53   // relies on it.
54   opts::HotText = true;
55   if (!BC.StartFunctionAddress) {
56     errs() << "BOLT-ERROR: hugify runtime libraries require a known entry "
57               "point of "
58               "the input binary\n";
59     exit(1);
60   }
61 }
62 
63 void HugifyRuntimeLibrary::link(BinaryContext &BC, StringRef ToolPath,
64                                 BOLTLinker &Linker,
65                                 BOLTLinker::SectionsMapper MapSections) {
66 
67   std::string LibPath = getLibPath(ToolPath, opts::RuntimeHugifyLib);
68   loadLibrary(LibPath, Linker, MapSections);
69 
70   assert(!RuntimeStartAddress &&
71          "We don't currently support linking multiple runtime libraries");
72   RuntimeStartAddress = Linker.lookupSymbol("__bolt_hugify_self").value_or(0);
73   if (!RuntimeStartAddress) {
74     errs() << "BOLT-ERROR: instrumentation library does not define "
75               "__bolt_hugify_self: "
76            << LibPath << "\n";
77     exit(1);
78   }
79 }
80