Path: blob/main/contrib/llvm-project/lld/ELF/Arch/TargetImpl.h
213766 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLD_ELF_ARCH_TARGETIMPL_H9#define LLD_ELF_ARCH_TARGETIMPL_H1011#include "InputFiles.h"12#include "InputSection.h"13#include "Relocations.h"14#include "Symbols.h"15#include "llvm/BinaryFormat/ELF.h"1617namespace lld::elf {1819// getControlTransferAddend: If this relocation is used for control transfer20// instructions (e.g. branch, branch-link or call) or code references (e.g.21// virtual function pointers) and indicates an address-insignificant reference,22// return the effective addend for the relocation, otherwise return23// std::nullopt. The effective addend for a relocation is the addend that is24// used to determine its branch destination.25//26// getBranchInfoAtTarget: If a control transfer relocation referring to27// is+offset directly transfers control to a relocated branch instruction in the28// specified section, return the relocation for the branch target as well as its29// effective addend (see above). Otherwise return {nullptr, 0}.30//31// redirectControlTransferRelocations: Given r1, a relocation for which32// getControlTransferAddend() returned a value, and r2, a relocation returned by33// getBranchInfo(), modify r1 so that it branches directly to the target of r2.34template <typename GetControlTransferAddend, typename GetBranchInfoAtTarget,35typename RedirectControlTransferRelocations>36inline void applyBranchToBranchOptImpl(37Ctx &ctx, GetControlTransferAddend getControlTransferAddend,38GetBranchInfoAtTarget getBranchInfoAtTarget,39RedirectControlTransferRelocations redirectControlTransferRelocations) {40// Needs to run serially because it writes to the relocations array as well as41// reading relocations of other sections.42for (ELFFileBase *f : ctx.objectFiles) {43auto getRelocBranchInfo =44[&getBranchInfoAtTarget](45Relocation &r,46uint64_t addend) -> std::pair<Relocation *, uint64_t> {47auto *target = dyn_cast_or_null<Defined>(r.sym);48// We don't allow preemptible symbols or ifuncs (may go somewhere else),49// absolute symbols (runtime behavior unknown), non-executable or writable50// memory (ditto) or non-regular sections (no section data).51if (!target || target->isPreemptible || target->isGnuIFunc() ||52!target->section ||53!(target->section->flags & llvm::ELF::SHF_EXECINSTR) ||54(target->section->flags & llvm::ELF::SHF_WRITE) ||55target->section->kind() != SectionBase::Regular)56return {nullptr, 0};57return getBranchInfoAtTarget(*cast<InputSection>(target->section),58target->value + addend);59};60for (InputSectionBase *sb : f->getSections()) {61auto *s = dyn_cast_or_null<InputSection>(sb);62if (!s)63continue;64for (Relocation &r : s->relocations) {65std::optional<uint64_t> addend = getControlTransferAddend(*s, r);66if (!addend)67continue;68std::pair<Relocation *, uint64_t> targetAndAddend =69getRelocBranchInfo(r, *addend);70if (!targetAndAddend.first)71continue;72// Avoid getting stuck in an infinite loop if we encounter a branch73// that (possibly indirectly) branches to itself. It is unlikely74// that more than 5 iterations will ever be needed in practice.75size_t iterations = 5;76while (iterations--) {77std::pair<Relocation *, uint64_t> nextTargetAndAddend =78getRelocBranchInfo(*targetAndAddend.first,79targetAndAddend.second);80if (!nextTargetAndAddend.first)81break;82targetAndAddend = nextTargetAndAddend;83}84redirectControlTransferRelocations(r, *targetAndAddend.first);85}86}87}88}8990} // namespace lld::elf9192#endif939495