Path: blob/main/contrib/llvm-project/lld/ELF/Thunks.h
34879 views
//===- Thunks.h --------------------------------------------------------===//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_THUNKS_H9#define LLD_ELF_THUNKS_H1011#include "llvm/ADT/SmallVector.h"12#include "Relocations.h"1314namespace lld::elf {15class Defined;16class InputFile;17class Symbol;18class ThunkSection;19// Class to describe an instance of a Thunk.20// A Thunk is a code-sequence inserted by the linker in between a caller and21// the callee. The relocation to the callee is redirected to the Thunk, which22// after executing transfers control to the callee. Typical uses of Thunks23// include transferring control from non-pi to pi and changing state on24// targets like ARM.25//26// Thunks can be created for Defined, Shared and Undefined Symbols.27// Thunks are assigned to synthetic ThunkSections28class Thunk {29public:30Thunk(Symbol &destination, int64_t addend);31virtual ~Thunk();3233virtual uint32_t size() = 0;34virtual void writeTo(uint8_t *buf) = 0;3536// All Thunks must define at least one symbol, known as the thunk target37// symbol, so that we can redirect relocations to it. The thunk may define38// additional symbols, but these are never targets for relocations.39virtual void addSymbols(ThunkSection &isec) = 0;4041void setOffset(uint64_t offset);42Defined *addSymbol(StringRef name, uint8_t type, uint64_t value,43InputSectionBase §ion);4445// Some Thunks must be placed immediately before their Target as they elide46// a branch and fall through to the first Symbol in the Target.47virtual InputSection *getTargetInputSection() const { return nullptr; }4849// To reuse a Thunk the InputSection and the relocation must be compatible50// with it.51virtual bool isCompatibleWith(const InputSection &,52const Relocation &) const {53return true;54}5556Defined *getThunkTargetSym() const { return syms[0]; }5758Symbol &destination;59int64_t addend;60llvm::SmallVector<Defined *, 3> syms;61uint64_t offset = 0;62// The alignment requirement for this Thunk, defaults to the size of the63// typical code section alignment.64uint32_t alignment = 4;65};6667// For a Relocation to symbol S create a Thunk to be added to a synthetic68// ThunkSection.69Thunk *addThunk(const InputSection &isec, Relocation &rel);7071void writePPC32PltCallStub(uint8_t *buf, uint64_t gotPltVA,72const InputFile *file, int64_t addend);73void writePPC64LoadAndBranch(uint8_t *buf, int64_t offset);7475} // namespace lld::elf7677#endif787980