llvm-project
73 строки · 2.5 Кб
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
12using namespace llvm;13
14namespace llvm {15namespace 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.
26void FixRelaxations::runOnFunction(BinaryFunction &BF) {27BinaryContext &BC = BF.getBinaryContext();28for (BinaryBasicBlock &BB : BF) {29for (auto II = BB.begin(); II != BB.end(); ++II) {30MCInst &Adrp = *II;31if (BC.MIB->isPseudo(Adrp) || !BC.MIB->isADRP(Adrp))32continue;33
34const MCSymbol *AdrpSymbol = BC.MIB->getTargetSymbol(Adrp);35if (!AdrpSymbol || AdrpSymbol->getName() != "__BOLT_got_zero")36continue;37
38auto NextII = std::next(II);39if (NextII == BB.end())40continue;41
42const MCInst &Add = *NextII;43if (!BC.MIB->matchAdrpAddPair(Adrp, Add))44continue;45
46const MCSymbol *Symbol = BC.MIB->getTargetSymbol(Add);47if (!Symbol || AdrpSymbol == Symbol)48continue;49
50auto L = BC.scopeLock();51const int64_t Addend = BC.MIB->getTargetAddend(Add);52BC.MIB->setOperandToSymbolRef(Adrp, /*OpNum*/ 1, Symbol, Addend,53BC.Ctx.get(), ELF::R_AARCH64_NONE);54}55}56}
57
58Error FixRelaxations::runOnFunctions(BinaryContext &BC) {59if (!BC.isAArch64() || !BC.HasRelocations)60return Error::success();61
62ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {63runOnFunction(BF);64};65
66ParallelUtilities::runOnEachFunction(67BC, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, WorkFun, nullptr,68"FixRelaxations");69return Error::success();70}
71
72} // namespace bolt73} // namespace llvm74