llvm-project
145 строк · 5.0 Кб
1//===- bolt/Passes/PatchEntries.cpp - Pass for patching function entries --===//
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 PatchEntries class that is used for patching
10// the original function entry points.
11//
12//===----------------------------------------------------------------------===//
13
14#include "bolt/Passes/PatchEntries.h"
15#include "bolt/Utils/NameResolver.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/Support/CommandLine.h"
18
19namespace opts {
20
21extern llvm::cl::OptionCategory BoltCategory;
22
23extern llvm::cl::opt<unsigned> Verbosity;
24
25llvm::cl::opt<bool>
26ForcePatch("force-patch",
27llvm::cl::desc("force patching of original entry points"),
28llvm::cl::Hidden, llvm::cl::cat(BoltCategory));
29}
30
31namespace llvm {
32namespace bolt {
33
34Error PatchEntries::runOnFunctions(BinaryContext &BC) {
35if (!opts::ForcePatch) {
36// Mark the binary for patching if we did not create external references
37// for original code in any of functions we are not going to emit.
38bool NeedsPatching = llvm::any_of(
39llvm::make_second_range(BC.getBinaryFunctions()),
40[&](BinaryFunction &BF) {
41return !BC.shouldEmit(BF) && !BF.hasExternalRefRelocations();
42});
43
44if (!NeedsPatching)
45return Error::success();
46}
47
48if (opts::Verbosity >= 1)
49BC.outs() << "BOLT-INFO: patching entries in original code\n";
50
51// Calculate the size of the patch.
52static size_t PatchSize = 0;
53if (!PatchSize) {
54InstructionListType Seq;
55BC.MIB->createLongTailCall(Seq, BC.Ctx->createTempSymbol(), BC.Ctx.get());
56PatchSize = BC.computeCodeSize(Seq.begin(), Seq.end());
57}
58
59for (auto &BFI : BC.getBinaryFunctions()) {
60BinaryFunction &Function = BFI.second;
61
62// Patch original code only for functions that will be emitted.
63if (!BC.shouldEmit(Function))
64continue;
65
66// Check if we can skip patching the function.
67if (!opts::ForcePatch && !Function.hasEHRanges() &&
68Function.getSize() < PatchThreshold)
69continue;
70
71// List of patches for function entries. We either successfully patch
72// all entries or, if we cannot patch one or more, do no patch any and
73// mark the function as ignorable.
74std::vector<Patch> PendingPatches;
75
76uint64_t NextValidByte = 0; // offset of the byte past the last patch
77bool Success = Function.forEachEntryPoint([&](uint64_t Offset,
78const MCSymbol *Symbol) {
79if (Offset < NextValidByte) {
80if (opts::Verbosity >= 1)
81BC.outs() << "BOLT-INFO: unable to patch entry point in " << Function
82<< " at offset 0x" << Twine::utohexstr(Offset) << '\n';
83return false;
84}
85
86PendingPatches.emplace_back(Patch{Symbol, Function.getAddress() + Offset,
87Function.getFileOffset() + Offset,
88Function.getOriginSection()});
89NextValidByte = Offset + PatchSize;
90if (NextValidByte > Function.getMaxSize()) {
91if (opts::Verbosity >= 1)
92BC.outs() << "BOLT-INFO: function " << Function
93<< " too small to patch its entry point\n";
94return false;
95}
96
97return true;
98});
99
100if (!Success) {
101// We can't change output layout for AArch64 due to LongJmp pass
102if (BC.isAArch64()) {
103if (opts::ForcePatch) {
104BC.errs() << "BOLT-ERROR: unable to patch entries in " << Function
105<< "\n";
106return createFatalBOLTError("");
107}
108
109continue;
110}
111
112// If the original function entries cannot be patched, then we cannot
113// safely emit new function body.
114BC.errs() << "BOLT-WARNING: failed to patch entries in " << Function
115<< ". The function will not be optimized.\n";
116Function.setIgnored();
117continue;
118}
119
120for (Patch &Patch : PendingPatches) {
121BinaryFunction *PatchFunction = BC.createInjectedBinaryFunction(
122NameResolver::append(Patch.Symbol->getName(), ".org.0"));
123// Force the function to be emitted at the given address.
124PatchFunction->setOutputAddress(Patch.Address);
125PatchFunction->setFileOffset(Patch.FileOffset);
126PatchFunction->setOriginSection(Patch.Section);
127
128InstructionListType Seq;
129BC.MIB->createLongTailCall(Seq, Patch.Symbol, BC.Ctx.get());
130PatchFunction->addBasicBlock()->addInstructions(Seq);
131
132// Verify the size requirements.
133uint64_t HotSize, ColdSize;
134std::tie(HotSize, ColdSize) = BC.calculateEmittedSize(*PatchFunction);
135assert(!ColdSize && "unexpected cold code");
136assert(HotSize <= PatchSize && "max patch size exceeded");
137}
138
139Function.setIsPatched(true);
140}
141return Error::success();
142}
143
144} // end namespace bolt
145} // end namespace llvm
146