1#include "bolt/Passes/FixRelaxationPass.h"
2#include "bolt/Core/ParallelUtilities.h"
3
4using namespace llvm;
5
6namespace llvm {
7namespace bolt {
8
9// This function finds ADRP+ADD instruction sequences that originally before
10// linker relaxations were ADRP+LDR. We've modified LDR/ADD relocation properly
11// during relocation reading, so its targeting right symbol. As for ADRP its
12// target is wrong before this pass since we won't be able to recognize and
13// properly change R_AARCH64_ADR_GOT_PAGE relocation to
14// R_AARCH64_ADR_PREL_PG_HI21 during relocation reading. Now we're searching for
15// ADRP+ADD sequences, checking that ADRP points to the GOT-table symbol and the
16// target of ADD is another symbol. When found change ADRP symbol reference to
17// the ADDs one.
18void FixRelaxations::runOnFunction(BinaryFunction &BF) {
19 BinaryContext &BC = BF.getBinaryContext();
20 for (BinaryBasicBlock &BB : BF) {
21 for (auto II = BB.begin(); II != BB.end(); ++II) {
22 MCInst &Adrp = *II;
23 if (BC.MIB->isPseudo(Inst: Adrp) || !BC.MIB->isADRP(Inst: Adrp))
24 continue;
25
26 const MCSymbol *AdrpSymbol = BC.MIB->getTargetSymbol(Inst: Adrp);
27 if (!AdrpSymbol || AdrpSymbol->getName() != "__BOLT_got_zero")
28 continue;
29
30 auto NextII = std::next(x: II);
31 if (NextII == BB.end())
32 continue;
33
34 const MCInst &Add = *NextII;
35 if (!BC.MIB->matchAdrpAddPair(Adrp, Add))
36 continue;
37
38 const MCSymbol *Symbol = BC.MIB->getTargetSymbol(Inst: Add);
39 if (!Symbol || AdrpSymbol == Symbol)
40 continue;
41
42 auto L = BC.scopeLock();
43 const int64_t Addend = BC.MIB->getTargetAddend(Inst: Add);
44 BC.MIB->setOperandToSymbolRef(Inst&: Adrp, /*OpNum*/ 1, Symbol, Addend,
45 Ctx: BC.Ctx.get(), RelType: ELF::R_AARCH64_NONE);
46 }
47 }
48}
49
50Error FixRelaxations::runOnFunctions(BinaryContext &BC) {
51 if (!BC.isAArch64() || !BC.HasRelocations)
52 return Error::success();
53
54 ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
55 runOnFunction(BF);
56 };
57
58 ParallelUtilities::runOnEachFunction(
59 BC, SchedPolicy: ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, WorkFunction: WorkFun, SkipPredicate: nullptr,
60 LogName: "FixRelaxations");
61 return Error::success();
62}
63
64} // namespace bolt
65} // namespace llvm
66

source code of bolt/lib/Passes/FixRelaxationPass.cpp