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 | |
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(Name: Title); |
27 | |
28 | std::vector<std::unique_ptr<BinaryBasicBlock>> BBs; |
29 | BBs.emplace_back(args: Func->createBasicBlock(Label: nullptr)); |
30 | BBs.back()->addInstructions(Begin: Instrs.begin(), End: Instrs.end()); |
31 | BBs.back()->setCFIState(0); |
32 | BBs.back()->setOffset(BinaryBasicBlock::INVALID_OFFSET); |
33 | |
34 | Func->insertBasicBlocks(Start: nullptr, NewBBs: std::move(BBs), |
35 | /*UpdateLayout=*/true, |
36 | /*UpdateCFIState=*/false); |
37 | Func->updateState(State: BinaryFunction::State::CFG_Finalized); |
38 | return Func; |
39 | }; |
40 | |
41 | const BinaryFunction *const Start = |
42 | BC.getBinaryFunctionAtAddress(Address: *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(TgtSym: StartSym, Ctx: BC.Ctx.get())); |
47 | return Error::success(); |
48 | } |
49 | } // namespace bolt |
50 | } // namespace llvm |
51 |