74 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "bolt/Passes/FixRelaxationPass.h"10#include "bolt/Core/ParallelUtilities.h"11 12using namespace llvm;13 14namespace llvm {15namespace bolt {16 17// This function finds ADRP+ADD instruction sequences that originally before18// linker relaxations were ADRP+LDR. We've modified LDR/ADD relocation properly19// during relocation reading, so its targeting right symbol. As for ADRP its20// target is wrong before this pass since we won't be able to recognize and21// properly change R_AARCH64_ADR_GOT_PAGE relocation to22// R_AARCH64_ADR_PREL_PG_HI21 during relocation reading. Now we're searching for23// ADRP+ADD sequences, checking that ADRP points to the GOT-table symbol and the24// target of ADD is another symbol. When found change ADRP symbol reference to25// the ADDs one.26void 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(Adrp) || !BC.MIB->isADRP(Adrp))32 continue;33 34 const MCSymbol *AdrpSymbol = BC.MIB->getTargetSymbol(Adrp);35 if (!AdrpSymbol || AdrpSymbol->getName() != "__BOLT_got_zero")36 continue;37 38 auto NextII = std::next(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(Add);47 if (!Symbol || AdrpSymbol == Symbol)48 continue;49 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);54 }55 }56}57 58Error 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, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, WorkFun, nullptr,68 "FixRelaxations");69 return Error::success();70}71 72} // namespace bolt73} // namespace llvm74