1 //===- Thunks.h --------------------------------------------------------===// 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 #ifndef LLD_ELF_THUNKS_H 10 #define LLD_ELF_THUNKS_H 11 12 #include "Relocations.h" 13 14 namespace lld { 15 namespace elf { 16 class Defined; 17 class Symbol; 18 class 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 and 21 // the callee. The relocation to the callee is redirected to the Thunk, which 22 // after executing transfers control to the callee. Typical uses of Thunks 23 // include transferring control from non-pi to pi and changing state on 24 // targets like ARM. 25 // 26 // Thunks can be created for Defined, Shared and Undefined Symbols. 27 // Thunks are assigned to synthetic ThunkSections 28 class Thunk { 29 public: 30 Thunk(Symbol &destination); 31 virtual ~Thunk(); 32 33 virtual uint32_t size() = 0; 34 virtual void writeTo(uint8_t *buf) = 0; 35 36 // All Thunks must define at least one symbol, known as the thunk target 37 // symbol, so that we can redirect relocations to it. The thunk may define 38 // additional symbols, but these are never targets for relocations. 39 virtual void addSymbols(ThunkSection &isec) = 0; 40 41 void setOffset(uint64_t offset); 42 Defined *addSymbol(StringRef name, uint8_t type, uint64_t value, 43 InputSectionBase §ion); 44 45 // Some Thunks must be placed immediately before their Target as they elide 46 // a branch and fall through to the first Symbol in the Target. 47 virtual InputSection *getTargetInputSection() const { return nullptr; } 48 49 // To reuse a Thunk the InputSection and the relocation must be compatible 50 // with it. 51 virtual bool isCompatibleWith(const InputSection &, 52 const Relocation &) const { 53 return true; 54 } 55 56 Defined *getThunkTargetSym() const { return syms[0]; } 57 58 // The alignment requirement for this Thunk, defaults to the size of the 59 // typical code section alignment. 60 Symbol &destination; 61 llvm::SmallVector<Defined *, 3> syms; 62 uint64_t offset = 0; 63 uint32_t alignment = 4; 64 }; 65 66 // For a Relocation to symbol S create a Thunk to be added to a synthetic 67 // ThunkSection. 68 Thunk *addThunk(const InputSection &isec, Relocation &rel); 69 70 } // namespace elf 71 } // namespace lld 72 73 #endif 74