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