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