]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Target.cpp
Merge ^/head r320573 through r320970.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / Target.cpp
1 //===- Target.cpp ---------------------------------------------------------===//
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 // Machine-specific things, such as applying relocations, creation of
11 // GOT or PLT entries, etc., are handled in this file.
12 //
13 // Refer the ELF spec for the single letter variables, S, A or P, used
14 // in this file.
15 //
16 // Some functions defined in this file has "relaxTls" as part of their names.
17 // They do peephole optimization for TLS variables by rewriting instructions.
18 // They are not part of the ABI but optional optimization, so you can skip
19 // them if you are not interested in how TLS variables are optimized.
20 // See the following paper for the details.
21 //
22 //   Ulrich Drepper, ELF Handling For Thread-Local Storage
23 //   http://www.akkadia.org/drepper/tls.pdf
24 //
25 //===----------------------------------------------------------------------===//
26
27 #include "Target.h"
28 #include "Error.h"
29 #include "InputFiles.h"
30 #include "OutputSections.h"
31 #include "SymbolTable.h"
32 #include "Symbols.h"
33 #include "llvm/Object/ELF.h"
34
35 using namespace llvm;
36 using namespace llvm::object;
37 using namespace llvm::ELF;
38 using namespace lld;
39 using namespace lld::elf;
40
41 TargetInfo *elf::Target;
42
43 std::string lld::toString(uint32_t Type) {
44   StringRef S = getELFRelocationTypeName(elf::Config->EMachine, Type);
45   if (S == "Unknown")
46     return ("Unknown (" + Twine(Type) + ")").str();
47   return S;
48 }
49
50 TargetInfo *elf::getTarget() {
51   switch (Config->EMachine) {
52   case EM_386:
53   case EM_IAMCU:
54     return getX86TargetInfo();
55   case EM_AARCH64:
56     return getAArch64TargetInfo();
57   case EM_AMDGPU:
58     return getAMDGPUTargetInfo();
59   case EM_ARM:
60     return getARMTargetInfo();
61   case EM_AVR:
62     return getAVRTargetInfo();
63   case EM_MIPS:
64     switch (Config->EKind) {
65     case ELF32LEKind:
66       return getMipsTargetInfo<ELF32LE>();
67     case ELF32BEKind:
68       return getMipsTargetInfo<ELF32BE>();
69     case ELF64LEKind:
70       return getMipsTargetInfo<ELF64LE>();
71     case ELF64BEKind:
72       return getMipsTargetInfo<ELF64BE>();
73     default:
74       fatal("unsupported MIPS target");
75     }
76   case EM_PPC:
77     return getPPCTargetInfo();
78   case EM_PPC64:
79     return getPPC64TargetInfo();
80   case EM_SPARCV9:
81     return getSPARCV9TargetInfo();
82   case EM_X86_64:
83     if (Config->EKind == ELF32LEKind)
84       return getX32TargetInfo();
85     return getX86_64TargetInfo();
86   }
87   fatal("unknown target machine");
88 }
89
90 template <class ELFT> static std::string getErrorLoc(const uint8_t *Loc) {
91   for (InputSectionBase *D : InputSections) {
92     auto *IS = dyn_cast_or_null<InputSection>(D);
93     if (!IS || !IS->getParent())
94       continue;
95
96     uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
97     if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
98       return IS->template getLocation<ELFT>(Loc - ISLoc) + ": ";
99   }
100   return "";
101 }
102
103 std::string elf::getErrorLocation(const uint8_t *Loc) {
104   switch (Config->EKind) {
105   case ELF32LEKind:
106     return getErrorLoc<ELF32LE>(Loc);
107   case ELF32BEKind:
108     return getErrorLoc<ELF32BE>(Loc);
109   case ELF64LEKind:
110     return getErrorLoc<ELF64LE>(Loc);
111   case ELF64BEKind:
112     return getErrorLoc<ELF64BE>(Loc);
113   default:
114     llvm_unreachable("unknown ELF type");
115   }
116 }
117
118 TargetInfo::~TargetInfo() {}
119
120 int64_t TargetInfo::getImplicitAddend(const uint8_t *Buf, uint32_t Type) const {
121   return 0;
122 }
123
124 bool TargetInfo::usesOnlyLowPageBits(uint32_t Type) const { return false; }
125
126 bool TargetInfo::needsThunk(RelExpr Expr, uint32_t RelocType,
127                             const InputFile *File, const SymbolBody &S) const {
128   return false;
129 }
130
131 void TargetInfo::writeIgotPlt(uint8_t *Buf, const SymbolBody &S) const {
132   writeGotPlt(Buf, S);
133 }
134
135 RelExpr TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
136                                     RelExpr Expr) const {
137   return Expr;
138 }
139
140 void TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
141   llvm_unreachable("Should not have claimed to be relaxable");
142 }
143
144 void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
145                                 uint64_t Val) const {
146   llvm_unreachable("Should not have claimed to be relaxable");
147 }
148
149 void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
150                                 uint64_t Val) const {
151   llvm_unreachable("Should not have claimed to be relaxable");
152 }
153
154 void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
155                                 uint64_t Val) const {
156   llvm_unreachable("Should not have claimed to be relaxable");
157 }
158
159 void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
160                                 uint64_t Val) const {
161   llvm_unreachable("Should not have claimed to be relaxable");
162 }