[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / bolt / lib / Passes / FixRelaxationPass.cpp
blob3dd19b6b43b76752381212f08292b1c5a097e598
1 #include "bolt/Passes/FixRelaxationPass.h"
2 #include "bolt/Core/ParallelUtilities.h"
4 using namespace llvm;
6 namespace llvm {
7 namespace bolt {
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.
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) {
22 MCInst &Adrp = *II;
23 if (BC.MIB->isPseudo(Adrp) || !BC.MIB->isADRP(Adrp))
24 continue;
26 const MCSymbol *AdrpSymbol = BC.MIB->getTargetSymbol(Adrp);
27 if (!AdrpSymbol || AdrpSymbol->getName() != "__BOLT_got_zero")
28 continue;
30 auto NextII = std::next(II);
31 if (NextII == BB.end())
32 continue;
34 const MCInst &Add = *NextII;
35 if (!BC.MIB->matchAdrpAddPair(Adrp, Add))
36 continue;
38 const MCSymbol *Symbol = BC.MIB->getTargetSymbol(Add);
39 if (!Symbol || AdrpSymbol == Symbol)
40 continue;
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)
52 return;
54 ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
55 runOnFunction(BF);
58 ParallelUtilities::runOnEachFunction(
59 BC, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, WorkFun, nullptr,
60 "FixRelaxations");
63 } // namespace bolt
64 } // namespace llvm