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