xref: /llvm-project-15.0.7/lld/ELF/Target.cpp (revision 2bbd32f5)
1 //===- Target.cpp ---------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Machine-specific things, such as applying relocations, creation of
10 // GOT or PLT entries, etc., are handled in this file.
11 //
12 // Refer the ELF spec for the single letter variables, S, A or P, used
13 // in this file.
14 //
15 // Some functions defined in this file has "relaxTls" as part of their names.
16 // They do peephole optimization for TLS variables by rewriting instructions.
17 // They are not part of the ABI but optional optimization, so you can skip
18 // them if you are not interested in how TLS variables are optimized.
19 // See the following paper for the details.
20 //
21 //   Ulrich Drepper, ELF Handling For Thread-Local Storage
22 //   http://www.akkadia.org/drepper/tls.pdf
23 //
24 //===----------------------------------------------------------------------===//
25 
26 #include "Target.h"
27 #include "InputFiles.h"
28 #include "OutputSections.h"
29 #include "SymbolTable.h"
30 #include "Symbols.h"
31 #include "lld/Common/ErrorHandler.h"
32 #include "llvm/Object/ELF.h"
33 
34 using namespace llvm;
35 using namespace llvm::object;
36 using namespace llvm::ELF;
37 
38 namespace lld {
39 std::string toString(elf::RelType type) {
40   StringRef s = getELFRelocationTypeName(elf::config->emachine, type);
41   if (s == "Unknown")
42     return ("Unknown (" + Twine(type) + ")").str();
43   return s;
44 }
45 
46 namespace elf {
47 const TargetInfo *target;
48 
49 TargetInfo *getTarget() {
50   switch (config->emachine) {
51   case EM_386:
52   case EM_IAMCU:
53     return getX86TargetInfo();
54   case EM_AARCH64:
55     return getAArch64TargetInfo();
56   case EM_AMDGPU:
57     return getAMDGPUTargetInfo();
58   case EM_ARM:
59     return getARMTargetInfo();
60   case EM_AVR:
61     return getAVRTargetInfo();
62   case EM_HEXAGON:
63     return getHexagonTargetInfo();
64   case EM_MIPS:
65     switch (config->ekind) {
66     case ELF32LEKind:
67       return getMipsTargetInfo<ELF32LE>();
68     case ELF32BEKind:
69       return getMipsTargetInfo<ELF32BE>();
70     case ELF64LEKind:
71       return getMipsTargetInfo<ELF64LE>();
72     case ELF64BEKind:
73       return getMipsTargetInfo<ELF64BE>();
74     default:
75       llvm_unreachable("unsupported MIPS target");
76     }
77   case EM_MSP430:
78     return getMSP430TargetInfo();
79   case EM_PPC:
80     return getPPCTargetInfo();
81   case EM_PPC64:
82     return getPPC64TargetInfo();
83   case EM_RISCV:
84     return getRISCVTargetInfo();
85   case EM_SPARCV9:
86     return getSPARCV9TargetInfo();
87   case EM_X86_64:
88     return getX86_64TargetInfo();
89   }
90   llvm_unreachable("unknown target machine");
91 }
92 
93 template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *loc) {
94   for (InputSectionBase *d : inputSections) {
95     auto *isec = cast<InputSection>(d);
96     if (!isec->getParent())
97       continue;
98 
99     const uint8_t *isecLoc =
100         Out::bufferStart
101             ? (Out::bufferStart + isec->getParent()->offset + isec->outSecOff)
102             : isec->data().data();
103     if (isecLoc <= loc && loc < isecLoc + isec->getSize())
104       return {isec, isec->template getLocation<ELFT>(loc - isecLoc) + ": "};
105   }
106   return {};
107 }
108 
109 ErrorPlace getErrorPlace(const uint8_t *loc) {
110   switch (config->ekind) {
111   case ELF32LEKind:
112     return getErrPlace<ELF32LE>(loc);
113   case ELF32BEKind:
114     return getErrPlace<ELF32BE>(loc);
115   case ELF64LEKind:
116     return getErrPlace<ELF64LE>(loc);
117   case ELF64BEKind:
118     return getErrPlace<ELF64BE>(loc);
119   default:
120     llvm_unreachable("unknown ELF type");
121   }
122 }
123 
124 TargetInfo::~TargetInfo() {}
125 
126 int64_t TargetInfo::getImplicitAddend(const uint8_t *buf, RelType type) const {
127   return 0;
128 }
129 
130 bool TargetInfo::usesOnlyLowPageBits(RelType type) const { return false; }
131 
132 bool TargetInfo::needsThunk(RelExpr expr, RelType type, const InputFile *file,
133                             uint64_t branchAddr, const Symbol &s,
134                             int64_t a) const {
135   return false;
136 }
137 
138 bool TargetInfo::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
139                                                   uint8_t stOther) const {
140   llvm_unreachable("Target doesn't support split stacks.");
141 }
142 
143 bool TargetInfo::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
144   return true;
145 }
146 
147 void TargetInfo::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
148   writeGotPlt(buf, s);
149 }
150 
151 RelExpr TargetInfo::adjustRelaxExpr(RelType type, const uint8_t *data,
152                                     RelExpr expr) const {
153   return expr;
154 }
155 
156 void TargetInfo::relaxGot(uint8_t *loc, RelType type, uint64_t val) const {
157   llvm_unreachable("Should not have claimed to be relaxable");
158 }
159 
160 void TargetInfo::relaxTlsGdToLe(uint8_t *loc, RelType type,
161                                 uint64_t val) const {
162   llvm_unreachable("Should not have claimed to be relaxable");
163 }
164 
165 void TargetInfo::relaxTlsGdToIe(uint8_t *loc, RelType type,
166                                 uint64_t val) const {
167   llvm_unreachable("Should not have claimed to be relaxable");
168 }
169 
170 void TargetInfo::relaxTlsIeToLe(uint8_t *loc, RelType type,
171                                 uint64_t val) const {
172   llvm_unreachable("Should not have claimed to be relaxable");
173 }
174 
175 void TargetInfo::relaxTlsLdToLe(uint8_t *loc, RelType type,
176                                 uint64_t val) const {
177   llvm_unreachable("Should not have claimed to be relaxable");
178 }
179 
180 uint64_t TargetInfo::getImageBase() const {
181   // Use -image-base if set. Fall back to the target default if not.
182   if (config->imageBase)
183     return *config->imageBase;
184   return config->isPic ? 0 : defaultImageBase;
185 }
186 
187 } // namespace elf
188 } // namespace lld
189