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