xref: /llvm-project/bolt/lib/Passes/Hugify.cpp (revision fd38366e4525c5507bbb2a2fc1f7d113a964224e)
1 //===--- bolt/Passes/Hugify.cpp -------------------------------------------===//
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 #include "bolt/Passes/Hugify.h"
10 
11 #define DEBUG_TYPE "bolt-hugify"
12 
13 using namespace llvm;
14 
15 namespace llvm {
16 namespace bolt {
17 
runOnFunctions(BinaryContext & BC)18 Error HugePage::runOnFunctions(BinaryContext &BC) {
19   auto *RtLibrary = BC.getRuntimeLibrary();
20   if (!RtLibrary || !BC.isELF() || !BC.StartFunctionAddress) {
21     return Error::success();
22   }
23 
24   auto createSimpleFunction =
25       [&](std::string Title, std::vector<MCInst> Instrs) -> BinaryFunction * {
26     BinaryFunction *Func = BC.createInjectedBinaryFunction(Title);
27 
28     std::vector<std::unique_ptr<BinaryBasicBlock>> BBs;
29     BBs.emplace_back(Func->createBasicBlock(nullptr));
30     BBs.back()->addInstructions(Instrs.begin(), Instrs.end());
31     BBs.back()->setCFIState(0);
32     BBs.back()->setOffset(BinaryBasicBlock::INVALID_OFFSET);
33 
34     Func->insertBasicBlocks(nullptr, std::move(BBs),
35                             /*UpdateLayout=*/true,
36                             /*UpdateCFIState=*/false);
37     Func->updateState(BinaryFunction::State::CFG_Finalized);
38     return Func;
39   };
40 
41   const BinaryFunction *const Start =
42       BC.getBinaryFunctionAtAddress(*BC.StartFunctionAddress);
43   assert(Start && "Entry point function not found");
44   const MCSymbol *StartSym = Start->getSymbol();
45   createSimpleFunction("__bolt_hugify_start_program",
46                        BC.MIB->createSymbolTrampoline(StartSym, BC.Ctx.get()));
47   return Error::success();
48 }
49 } // namespace bolt
50 } // namespace llvm
51