1 //===- Target.cpp ---------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Machine-specific things, such as applying relocations, creation of
10 // GOT or PLT entries, etc., are handled in this file.
12 // Refer the ELF spec for the single letter variables, S, A or P, used
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.
21 // Ulrich Drepper, ELF Handling For Thread-Local Storage
22 // http://www.akkadia.org/drepper/tls.pdf
24 //===----------------------------------------------------------------------===//
27 #include "InputFiles.h"
28 #include "OutputSections.h"
29 #include "SymbolTable.h"
31 #include "SyntheticSections.h"
32 #include "lld/Common/ErrorHandler.h"
33 #include "llvm/Object/ELF.h"
36 using namespace llvm::object;
37 using namespace llvm::ELF;
40 std::string toString(elf::RelType type) {
41 StringRef s = getELFRelocationTypeName(elf::config->emachine, type);
43 return ("Unknown (" + Twine(type) + ")").str();
48 const TargetInfo *target;
50 TargetInfo *getTarget() {
51 switch (config->emachine) {
54 return getX86TargetInfo();
56 return getAArch64TargetInfo();
58 return getAMDGPUTargetInfo();
60 return getARMTargetInfo();
62 return getAVRTargetInfo();
64 return getHexagonTargetInfo();
66 switch (config->ekind) {
68 return getMipsTargetInfo<ELF32LE>();
70 return getMipsTargetInfo<ELF32BE>();
72 return getMipsTargetInfo<ELF64LE>();
74 return getMipsTargetInfo<ELF64BE>();
76 llvm_unreachable("unsupported MIPS target");
79 return getMSP430TargetInfo();
81 return getPPCTargetInfo();
83 return getPPC64TargetInfo();
85 return getRISCVTargetInfo();
87 return getSPARCV9TargetInfo();
89 return getX86_64TargetInfo();
91 llvm_unreachable("unknown target machine");
94 template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *loc) {
95 assert(loc != nullptr);
96 for (InputSectionBase *d : inputSections) {
97 auto *isec = cast<InputSection>(d);
98 if (!isec->getParent() || (isec->type & SHT_NOBITS))
101 const uint8_t *isecLoc =
103 ? (Out::bufferStart + isec->getParent()->offset + isec->outSecOff)
104 : isec->data().data();
105 if (isecLoc == nullptr) {
106 assert(isa<SyntheticSection>(isec) && "No data but not synthetic?");
109 if (isecLoc <= loc && loc < isecLoc + isec->getSize())
110 return {isec, isec->template getLocation<ELFT>(loc - isecLoc) + ": "};
115 ErrorPlace getErrorPlace(const uint8_t *loc) {
116 switch (config->ekind) {
118 return getErrPlace<ELF32LE>(loc);
120 return getErrPlace<ELF32BE>(loc);
122 return getErrPlace<ELF64LE>(loc);
124 return getErrPlace<ELF64BE>(loc);
126 llvm_unreachable("unknown ELF type");
130 TargetInfo::~TargetInfo() {}
132 int64_t TargetInfo::getImplicitAddend(const uint8_t *buf, RelType type) const {
136 bool TargetInfo::usesOnlyLowPageBits(RelType type) const { return false; }
138 bool TargetInfo::needsThunk(RelExpr expr, RelType type, const InputFile *file,
139 uint64_t branchAddr, const Symbol &s,
144 bool TargetInfo::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
145 uint8_t stOther) const {
146 llvm_unreachable("Target doesn't support split stacks.");
149 bool TargetInfo::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
153 RelExpr TargetInfo::adjustRelaxExpr(RelType type, const uint8_t *data,
154 RelExpr expr) const {
158 void TargetInfo::relaxGot(uint8_t *loc, RelType type, uint64_t val) const {
159 llvm_unreachable("Should not have claimed to be relaxable");
162 void TargetInfo::relaxTlsGdToLe(uint8_t *loc, RelType type,
163 uint64_t val) const {
164 llvm_unreachable("Should not have claimed to be relaxable");
167 void TargetInfo::relaxTlsGdToIe(uint8_t *loc, RelType type,
168 uint64_t val) const {
169 llvm_unreachable("Should not have claimed to be relaxable");
172 void TargetInfo::relaxTlsIeToLe(uint8_t *loc, RelType type,
173 uint64_t val) const {
174 llvm_unreachable("Should not have claimed to be relaxable");
177 void TargetInfo::relaxTlsLdToLe(uint8_t *loc, RelType type,
178 uint64_t val) const {
179 llvm_unreachable("Should not have claimed to be relaxable");
182 uint64_t TargetInfo::getImageBase() const {
183 // Use -image-base if set. Fall back to the target default if not.
184 if (config->imageBase)
185 return *config->imageBase;
186 return config->isPic ? 0 : defaultImageBase;