1 #include "bolt/Passes/FixRelaxationPass.h"
2 #include "bolt/Core/ParallelUtilities.h"
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
18 void FixRelaxations::runOnFunction(BinaryFunction
&BF
) {
19 BinaryContext
&BC
= BF
.getBinaryContext();
20 for (BinaryBasicBlock
&BB
: BF
) {
21 for (auto II
= BB
.begin(); II
!= BB
.end(); ++II
) {
23 if (BC
.MIB
->isPseudo(Adrp
) || !BC
.MIB
->isADRP(Adrp
))
26 const MCSymbol
*AdrpSymbol
= BC
.MIB
->getTargetSymbol(Adrp
);
27 if (!AdrpSymbol
|| AdrpSymbol
->getName() != "__BOLT_got_zero")
30 auto NextII
= std::next(II
);
31 if (NextII
== BB
.end())
34 const MCInst
&Add
= *NextII
;
35 if (!BC
.MIB
->matchAdrpAddPair(Adrp
, Add
))
38 const MCSymbol
*Symbol
= BC
.MIB
->getTargetSymbol(Add
);
39 if (!Symbol
|| AdrpSymbol
== Symbol
)
42 auto L
= BC
.scopeLock();
43 const int64_t Addend
= BC
.MIB
->getTargetAddend(Add
);
44 BC
.MIB
->setOperandToSymbolRef(Adrp
, /*OpNum*/ 1, Symbol
, Addend
,
45 BC
.Ctx
.get(), ELF::R_AARCH64_NONE
);
50 void FixRelaxations::runOnFunctions(BinaryContext
&BC
) {
51 if (!BC
.isAArch64() || !BC
.HasRelocations
)
54 ParallelUtilities::WorkFuncTy WorkFun
= [&](BinaryFunction
&BF
) {
58 ParallelUtilities::runOnEachFunction(
59 BC
, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR
, WorkFun
, nullptr,