blob: a8575b4cdb59bb58a8849e47ec400e63e8468180 [file] [log] [blame]
Peter Smithfb05cd92016-07-08 16:10:27 +00001//===- Thunks.h --------------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Peter Smithfb05cd92016-07-08 16:10:27 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLD_ELF_THUNKS_H
10#define LLD_ELF_THUNKS_H
11
12#include "Relocations.h"
13
14namespace lld {
15namespace elf {
Peter Collingbournec5391ce2018-03-29 22:32:13 +000016class Defined;
Fangrui Songfb2944b2019-12-17 11:23:37 -080017class InputFile;
Rui Ueyamaf52496e2017-11-03 21:21:47 +000018class Symbol;
George Rimar7b827042017-03-16 10:40:50 +000019class ThunkSection;
Peter Smithfb05cd92016-07-08 16:10:27 +000020// Class to describe an instance of a Thunk.
21// A Thunk is a code-sequence inserted by the linker in between a caller and
22// the callee. The relocation to the callee is redirected to the Thunk, which
23// after executing transfers control to the callee. Typical uses of Thunks
24// include transferring control from non-pi to pi and changing state on
25// targets like ARM.
26//
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +000027// Thunks can be created for Defined, Shared and Undefined Symbols.
Peter Smith3a52eb02017-02-01 10:26:03 +000028// Thunks are assigned to synthetic ThunkSections
George Rimar7b827042017-03-16 10:40:50 +000029class Thunk {
Peter Smithfb05cd92016-07-08 16:10:27 +000030public:
Fangrui Songbf535ac2019-11-23 00:57:54 -080031 Thunk(Symbol &destination, int64_t addend);
Peter Smithfb05cd92016-07-08 16:10:27 +000032 virtual ~Thunk();
33
Peter Collingbournecebab4a2018-03-28 21:33:31 +000034 virtual uint32_t size() = 0;
Rui Ueyama3837f422019-07-10 05:00:37 +000035 virtual void writeTo(uint8_t *buf) = 0;
Rui Ueyama3d2bbb12016-07-09 22:52:30 +000036
Peter Collingbournec5391ce2018-03-29 22:32:13 +000037 // All Thunks must define at least one symbol, known as the thunk target
38 // symbol, so that we can redirect relocations to it. The thunk may define
39 // additional symbols, but these are never targets for relocations.
Rui Ueyama3837f422019-07-10 05:00:37 +000040 virtual void addSymbols(ThunkSection &isec) = 0;
Peter Smith3a52eb02017-02-01 10:26:03 +000041
Rui Ueyama3837f422019-07-10 05:00:37 +000042 void setOffset(uint64_t offset);
43 Defined *addSymbol(StringRef name, uint8_t type, uint64_t value,
44 InputSectionBase &section);
Peter Collingbournec5391ce2018-03-29 22:32:13 +000045
Peter Smith3a52eb02017-02-01 10:26:03 +000046 // Some Thunks must be placed immediately before their Target as they elide
47 // a branch and fall through to the first Symbol in the Target.
Rafael Espindola774ea7d2017-02-23 16:49:07 +000048 virtual InputSection *getTargetInputSection() const { return nullptr; }
Peter Smith3a52eb02017-02-01 10:26:03 +000049
Fangrui Song82442ad2019-06-06 17:03:00 +000050 // To reuse a Thunk the InputSection and the relocation must be compatible
51 // with it.
52 virtual bool isCompatibleWith(const InputSection &,
53 const Relocation &) const {
54 return true;
55 }
Peter Smith7d66e842017-07-05 09:36:03 +000056
Rui Ueyama3837f422019-07-10 05:00:37 +000057 Defined *getThunkTargetSym() const { return syms[0]; }
Peter Collingbournec5391ce2018-03-29 22:32:13 +000058
Rui Ueyama3837f422019-07-10 05:00:37 +000059 Symbol &destination;
Fangrui Songbf535ac2019-11-23 00:57:54 -080060 int64_t addend;
Rui Ueyama3837f422019-07-10 05:00:37 +000061 llvm::SmallVector<Defined *, 3> syms;
62 uint64_t offset = 0;
Fangrui Songbf535ac2019-11-23 00:57:54 -080063 // The alignment requirement for this Thunk, defaults to the size of the
64 // typical code section alignment.
Rui Ueyama3837f422019-07-10 05:00:37 +000065 uint32_t alignment = 4;
Peter Smithfb05cd92016-07-08 16:10:27 +000066};
67
Peter Smith3a52eb02017-02-01 10:26:03 +000068// For a Relocation to symbol S create a Thunk to be added to a synthetic
Fangrui Song82442ad2019-06-06 17:03:00 +000069// ThunkSection.
Rui Ueyama3837f422019-07-10 05:00:37 +000070Thunk *addThunk(const InputSection &isec, Relocation &rel);
Peter Smithfb05cd92016-07-08 16:10:27 +000071
Fangrui Songfb2944b2019-12-17 11:23:37 -080072void writePPC32PltCallStub(uint8_t *buf, uint64_t gotPltVA,
73 const InputFile *file, int64_t addend);
Fangrui Song45acc352019-12-13 18:30:21 -080074void writePPC64LoadAndBranch(uint8_t *buf, int64_t offset);
75
Peter Smithfb05cd92016-07-08 16:10:27 +000076} // namespace elf
77} // namespace lld
78
79#endif