1 //===- Target.h -------------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_ELF_TARGET_H
11 #define LLD_ELF_TARGET_H
14 #include "InputSection.h"
15 #include "llvm/Object/ELF.h"
18 std::string toString(uint32_t RelType);
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;
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 {}
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;
50 // Decide whether a Thunk is needed for the relocation from File
52 virtual bool needsThunk(RelExpr Expr, uint32_t RelocType,
53 const InputFile *File, const SymbolBody &S) const;
54 virtual RelExpr getRelExpr(uint32_t Type, const SymbolBody &S,
55 const uint8_t *Loc) const = 0;
56 virtual void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const = 0;
57 virtual ~TargetInfo();
59 unsigned TlsGdRelaxSkip = 1;
60 unsigned PageSize = 4096;
61 unsigned DefaultMaxPageSize = 4096;
63 // On FreeBSD x86_64 the first page cannot be mmaped.
64 // On Linux that is controled by vm.mmap_min_addr. At least on some x86_64
65 // installs that is 65536, so the first 15 pages cannot be used.
66 // Given that, the smallest value that can be used in here is 0x10000.
67 uint64_t DefaultImageBase = 0x10000;
73 uint32_t IRelativeRel;
76 uint32_t TlsModuleIndexRel;
77 uint32_t TlsOffsetRel;
78 unsigned GotEntrySize = 0;
79 unsigned GotPltEntrySize = 0;
80 unsigned PltEntrySize;
81 unsigned PltHeaderSize;
83 // At least on x86_64 positions 1 and 2 are used by the first plt entry
84 // to support lazy loading.
85 unsigned GotPltHeaderEntriesNum = 3;
87 // Set to 0 for variant 2
90 bool NeedsThunks = false;
92 // A 4-byte field corresponding to one or more trap instructions, used to pad
93 // executable OutputSections.
94 uint32_t TrapInstr = 0;
96 virtual RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
98 virtual void relaxGot(uint8_t *Loc, uint64_t Val) const;
99 virtual void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const;
100 virtual void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const;
101 virtual void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const;
102 virtual void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const;
105 TargetInfo *createAArch64TargetInfo();
106 TargetInfo *createAMDGPUTargetInfo();
107 TargetInfo *createARMTargetInfo();
108 TargetInfo *createAVRTargetInfo();
109 TargetInfo *createPPC64TargetInfo();
110 TargetInfo *createPPCTargetInfo();
111 TargetInfo *createX32TargetInfo();
112 TargetInfo *createX86TargetInfo();
113 TargetInfo *createX86_64TargetInfo();
114 template <class ELFT> TargetInfo *createMipsTargetInfo();
116 std::string getErrorLocation(const uint8_t *Loc);
118 uint64_t getPPC64TocBase();
119 uint64_t getAArch64Page(uint64_t Expr);
121 extern TargetInfo *Target;
122 TargetInfo *createTarget();
124 template <unsigned N>
125 static void checkInt(uint8_t *Loc, int64_t V, uint32_t Type) {
126 if (!llvm::isInt<N>(V))
127 error(getErrorLocation(Loc) + "relocation " + lld::toString(Type) +
131 template <unsigned N>
132 static void checkUInt(uint8_t *Loc, uint64_t V, uint32_t Type) {
133 if (!llvm::isUInt<N>(V))
134 error(getErrorLocation(Loc) + "relocation " + lld::toString(Type) +
138 template <unsigned N>
139 static void checkIntUInt(uint8_t *Loc, uint64_t V, uint32_t Type) {
140 if (!llvm::isInt<N>(V) && !llvm::isUInt<N>(V))
141 error(getErrorLocation(Loc) + "relocation " + lld::toString(Type) +
145 template <unsigned N>
146 static void checkAlignment(uint8_t *Loc, uint64_t V, uint32_t Type) {
147 if ((V & (N - 1)) != 0)
148 error(getErrorLocation(Loc) + "improper alignment for relocation " +
149 lld::toString(Type));