1 //===- bolt/Passes/FixRelaxationPass.cpp ------------------------*- C++ -*-===//
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/FixRelaxationPass.h"
10 #include "bolt/Core/ParallelUtilities.h"
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
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
) {
31 if (BC
.MIB
->isPseudo(Adrp
) || !BC
.MIB
->isADRP(Adrp
))
34 const MCSymbol
*AdrpSymbol
= BC
.MIB
->getTargetSymbol(Adrp
);
35 if (!AdrpSymbol
|| AdrpSymbol
->getName() != "__BOLT_got_zero")
38 auto NextII
= std::next(II
);
39 if (NextII
== BB
.end())
42 const MCInst
&Add
= *NextII
;
43 if (!BC
.MIB
->matchAdrpAddPair(Adrp
, Add
))
46 const MCSymbol
*Symbol
= BC
.MIB
->getTargetSymbol(Add
);
47 if (!Symbol
|| AdrpSymbol
== Symbol
)
50 auto L
= BC
.scopeLock();
51 const int64_t Addend
= BC
.MIB
->getTargetAddend(Add
);
52 BC
.MIB
->setOperandToSymbolRef(Adrp
, /*OpNum*/ 1, Symbol
, Addend
,
53 BC
.Ctx
.get(), ELF::R_AARCH64_NONE
);
58 Error
FixRelaxations::runOnFunctions(BinaryContext
&BC
) {
59 if (!BC
.isAArch64() || !BC
.HasRelocations
)
60 return Error::success();
62 ParallelUtilities::WorkFuncTy WorkFun
= [&](BinaryFunction
&BF
) {
66 ParallelUtilities::runOnEachFunction(
67 BC
, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR
, WorkFun
, nullptr,
69 return Error::success();