1 //===--- bolt/Passes/Hugify.cpp -------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "bolt/Passes/Hugify.h"
10 #include "llvm/Support/CommandLine.h"
12 #define DEBUG_TYPE "bolt-hugify"
19 void HugePage::runOnFunctions(BinaryContext
&BC
) {
20 auto *RtLibrary
= BC
.getRuntimeLibrary();
21 if (!RtLibrary
|| !BC
.isELF() || !BC
.StartFunctionAddress
) {
25 auto createSimpleFunction
=
26 [&](std::string Title
, std::vector
<MCInst
> Instrs
) -> BinaryFunction
* {
27 BinaryFunction
*Func
= BC
.createInjectedBinaryFunction(Title
);
29 std::vector
<std::unique_ptr
<BinaryBasicBlock
>> BBs
;
30 BBs
.emplace_back(Func
->createBasicBlock(nullptr));
31 BBs
.back()->addInstructions(Instrs
.begin(), Instrs
.end());
32 BBs
.back()->setCFIState(0);
33 BBs
.back()->setOffset(BinaryBasicBlock::INVALID_OFFSET
);
35 Func
->insertBasicBlocks(nullptr, std::move(BBs
),
36 /*UpdateLayout=*/true,
37 /*UpdateCFIState=*/false);
38 Func
->updateState(BinaryFunction::State::CFG_Finalized
);
42 const BinaryFunction
*const Start
=
43 BC
.getBinaryFunctionAtAddress(*BC
.StartFunctionAddress
);
44 assert(Start
&& "Entry point function not found");
45 const MCSymbol
*StartSym
= Start
->getSymbol();
46 createSimpleFunction("__bolt_hugify_start_program",
47 BC
.MIB
->createSymbolTrampoline(StartSym
, BC
.Ctx
.get()));