]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Target.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r308421, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / Target.h
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 "Error.h"
14 #include "InputSection.h"
15 #include "llvm/Object/ELF.h"
16
17 namespace lld {
18 std::string toString(uint32_t RelType);
19
20 namespace elf {
21 class InputFile;
22 class SymbolBody;
23
24 class TargetInfo {
25 public:
26   virtual bool isPicRel(uint32_t Type) const { return true; }
27   virtual uint32_t getDynRel(uint32_t Type) const { return Type; }
28   virtual void writeGotPltHeader(uint8_t *Buf) const {}
29   virtual void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const {};
30   virtual void writeIgotPlt(uint8_t *Buf, const SymbolBody &S) const;
31   virtual int64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const;
32
33   // If lazy binding is supported, the first entry of the PLT has code
34   // to call the dynamic linker to resolve PLT entries the first time
35   // they are called. This function writes that code.
36   virtual void writePltHeader(uint8_t *Buf) const {}
37
38   virtual void writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
39                         uint64_t PltEntryAddr, int32_t Index,
40                         unsigned RelOff) const {}
41   virtual void addPltHeaderSymbols(InputSectionBase *IS) const {}
42   virtual void addPltSymbols(InputSectionBase *IS, uint64_t Off) const {}
43   // Returns true if a relocation only uses the low bits of a value such that
44   // all those bits are in in the same page. For example, if the relocation
45   // only uses the low 12 bits in a system with 4k pages. If this is true, the
46   // bits will always have the same value at runtime and we don't have to emit
47   // a dynamic relocation.
48   virtual bool usesOnlyLowPageBits(uint32_t Type) const;
49
50   // Decide whether a Thunk is needed for the relocation from File
51   // targeting S.
52   virtual bool needsThunk(RelExpr Expr, uint32_t RelocType,
53                           const InputFile *File, const SymbolBody &S) const;
54   // Return true if we can reach Dst from Src with Relocation RelocType
55   virtual bool inBranchRange(uint32_t RelocType, uint64_t Src,
56                              uint64_t Dst) const;
57   virtual RelExpr getRelExpr(uint32_t Type, const SymbolBody &S,
58                              const uint8_t *Loc) const = 0;
59   virtual void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const = 0;
60   virtual ~TargetInfo();
61
62   unsigned TlsGdRelaxSkip = 1;
63   unsigned PageSize = 4096;
64   unsigned DefaultMaxPageSize = 4096;
65
66   // On FreeBSD x86_64 the first page cannot be mmaped.
67   // On Linux that is controled by vm.mmap_min_addr. At least on some x86_64
68   // installs that is 65536, so the first 15 pages cannot be used.
69   // Given that, the smallest value that can be used in here is 0x10000.
70   uint64_t DefaultImageBase = 0x10000;
71
72   // Offset of _GLOBAL_OFFSET_TABLE_ from base of .got section. Use -1 for
73   // end of .got
74   uint64_t GotBaseSymOff = 0;
75
76   uint32_t CopyRel;
77   uint32_t GotRel;
78   uint32_t PltRel;
79   uint32_t RelativeRel;
80   uint32_t IRelativeRel;
81   uint32_t TlsDescRel;
82   uint32_t TlsGotRel;
83   uint32_t TlsModuleIndexRel;
84   uint32_t TlsOffsetRel;
85   unsigned GotEntrySize = 0;
86   unsigned GotPltEntrySize = 0;
87   unsigned PltEntrySize;
88   unsigned PltHeaderSize;
89
90   // At least on x86_64 positions 1 and 2 are used by the first plt entry
91   // to support lazy loading.
92   unsigned GotPltHeaderEntriesNum = 3;
93
94   // Set to 0 for variant 2
95   unsigned TcbSize = 0;
96
97   bool NeedsThunks = false;
98
99   // A 4-byte field corresponding to one or more trap instructions, used to pad
100   // executable OutputSections.
101   uint32_t TrapInstr = 0;
102
103   virtual RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
104                                   RelExpr Expr) const;
105   virtual void relaxGot(uint8_t *Loc, uint64_t Val) const;
106   virtual void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const;
107   virtual void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const;
108   virtual void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const;
109   virtual void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const;
110 };
111
112 TargetInfo *getAArch64TargetInfo();
113 TargetInfo *getAMDGPUTargetInfo();
114 TargetInfo *getARMTargetInfo();
115 TargetInfo *getAVRTargetInfo();
116 TargetInfo *getPPC64TargetInfo();
117 TargetInfo *getPPCTargetInfo();
118 TargetInfo *getSPARCV9TargetInfo();
119 TargetInfo *getX32TargetInfo();
120 TargetInfo *getX86TargetInfo();
121 TargetInfo *getX86_64TargetInfo();
122 template <class ELFT> TargetInfo *getMipsTargetInfo();
123
124 std::string getErrorLocation(const uint8_t *Loc);
125
126 uint64_t getPPC64TocBase();
127 uint64_t getAArch64Page(uint64_t Expr);
128
129 extern TargetInfo *Target;
130 TargetInfo *getTarget();
131
132 template <unsigned N>
133 static void checkInt(uint8_t *Loc, int64_t V, uint32_t Type) {
134   if (!llvm::isInt<N>(V))
135     error(getErrorLocation(Loc) + "relocation " + lld::toString(Type) +
136           " out of range");
137 }
138
139 template <unsigned N>
140 static void checkUInt(uint8_t *Loc, uint64_t V, uint32_t Type) {
141   if (!llvm::isUInt<N>(V))
142     error(getErrorLocation(Loc) + "relocation " + lld::toString(Type) +
143           " out of range");
144 }
145
146 template <unsigned N>
147 static void checkIntUInt(uint8_t *Loc, uint64_t V, uint32_t Type) {
148   if (!llvm::isInt<N>(V) && !llvm::isUInt<N>(V))
149     error(getErrorLocation(Loc) + "relocation " + lld::toString(Type) +
150           " out of range");
151 }
152
153 template <unsigned N>
154 static void checkAlignment(uint8_t *Loc, uint64_t V, uint32_t Type) {
155   if ((V & (N - 1)) != 0)
156     error(getErrorLocation(Loc) + "improper alignment for relocation " +
157           lld::toString(Type));
158 }
159 } // namespace elf
160 } // namespace lld
161
162 #endif