xref: /llvm-project-15.0.7/lld/ELF/Target.h (revision 8bd63e55)
1 //===- Target.h -------------------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLD_ELF_TARGET_H
11 #define LLD_ELF_TARGET_H
12 
13 #include "InputSection.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Object/ELF.h"
16 
17 #include <memory>
18 
19 namespace lld {
20 namespace elf {
21 class InputFile;
22 class SymbolBody;
23 
24 class TargetInfo {
25 public:
26   uint64_t getVAStart() const;
27   virtual bool isTlsInitialExecRel(uint32_t Type) const;
28   virtual bool isTlsLocalDynamicRel(uint32_t Type) const;
29   virtual bool isTlsGlobalDynamicRel(uint32_t Type) const;
30   virtual uint32_t getDynRel(uint32_t Type) const { return Type; }
31   virtual uint32_t getTlsGotRel(uint32_t Type) const { return TlsGotRel; }
32   virtual void writeGotHeader(uint8_t *Buf) const {}
33   virtual void writeGotPltHeader(uint8_t *Buf) const {}
34   virtual void writeGotPlt(uint8_t *Buf, uint64_t Plt) const {};
35   virtual uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const;
36 
37   // If lazy binding is supported, the first entry of the PLT has code
38   // to call the dynamic linker to resolve PLT entries the first time
39   // they are called. This function writes that code.
40   virtual void writePltZero(uint8_t *Buf) const {}
41 
42   virtual void writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
43                         uint64_t PltEntryAddr, int32_t Index,
44                         unsigned RelOff) const {}
45 
46   // Returns true if a relocation is just a hint for linker to make for example
47   // some code optimization. Such relocations should not be handled as a regular
48   // ones and lead to dynamic relocation creation etc.
49   virtual bool isHintRel(uint32_t Type) const;
50 
51   // Returns true if a relocation is relative to the place being relocated,
52   // such as relocations used for PC-relative instructions. Such relocations
53   // need not be fixed up if an image is loaded to a different address than
54   // the link-time address. So we don't have to emit a relocation for the
55   // dynamic linker if isRelRelative returns true.
56   virtual bool isRelRelative(uint32_t Type) const;
57 
58   virtual bool needsDynRelative(uint32_t Type) const { return false; }
59   virtual bool refersToGotEntry(uint32_t Type) const;
60 
61   enum PltNeed { Plt_No, Plt_Explicit, Plt_Implicit };
62   PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const;
63 
64   virtual bool needsThunk(uint32_t Type, const InputFile &File,
65                           const SymbolBody &S) const;
66 
67   virtual void writeThunk(uint8_t *Buf, uint64_t S) const {}
68 
69   virtual RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const = 0;
70   virtual void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const = 0;
71   bool canRelaxTls(uint32_t Type, const SymbolBody *S) const;
72   template <class ELFT>
73   bool needsCopyRel(uint32_t Type, const SymbolBody &S) const;
74   virtual ~TargetInfo();
75 
76   unsigned TlsGdToLeSkip = 1;
77   unsigned PageSize = 4096;
78 
79   // On freebsd x86_64 the first page cannot be mmaped.
80   // On linux that is controled by vm.mmap_min_addr. At least on some x86_64
81   // installs that is 65536, so the first 15 pages cannot be used.
82   // Given that, the smallest value that can be used in here is 0x10000.
83   // If using 2MB pages, the smallest page aligned address that works is
84   // 0x200000, but it looks like every OS uses 4k pages for executables.
85   uint64_t VAStart = 0x10000;
86 
87   uint32_t CopyRel;
88   uint32_t GotRel;
89   uint32_t PltRel;
90   uint32_t RelativeRel;
91   uint32_t IRelativeRel;
92   uint32_t TlsGotRel = 0;
93   uint32_t TlsModuleIndexRel;
94   uint32_t TlsOffsetRel;
95   unsigned PltEntrySize = 8;
96   unsigned PltZeroSize = 0;
97   unsigned GotHeaderEntriesNum = 0;
98   unsigned GotPltHeaderEntriesNum = 3;
99   uint32_t ThunkSize = 0;
100   bool UseLazyBinding = false;
101 
102   virtual void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const;
103   virtual void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const;
104   virtual void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const;
105   virtual void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const;
106 
107 private:
108   virtual bool needsCopyRelImpl(uint32_t Type) const;
109   virtual bool needsPltImpl(uint32_t Type) const;
110 };
111 
112 uint64_t getPPC64TocBase();
113 
114 const unsigned MipsGPOffset = 0x7ff0;
115 template <class ELFT> typename ELFT::uint getMipsGpAddr();
116 
117 extern TargetInfo *Target;
118 TargetInfo *createTarget();
119 }
120 }
121 
122 #endif
123