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 "lld/Common/ErrorHandler.h"
32 #include "llvm/Object/ELF.h"
35 using namespace llvm::object;
36 using namespace llvm::ELF;
38 using namespace lld::elf;
40 const TargetInfo *elf::target;
42 std::string lld::toString(RelType type) {
43 StringRef s = getELFRelocationTypeName(elf::config->emachine, type);
45 return ("Unknown (" + Twine(type) + ")").str();
49 TargetInfo *elf::getTarget() {
50 switch (config->emachine) {
53 return getX86TargetInfo();
55 return getAArch64TargetInfo();
57 return getAMDGPUTargetInfo();
59 return getARMTargetInfo();
61 return getAVRTargetInfo();
63 return getHexagonTargetInfo();
65 switch (config->ekind) {
67 return getMipsTargetInfo<ELF32LE>();
69 return getMipsTargetInfo<ELF32BE>();
71 return getMipsTargetInfo<ELF64LE>();
73 return getMipsTargetInfo<ELF64BE>();
75 llvm_unreachable("unsupported MIPS target");
78 return getMSP430TargetInfo();
80 return getPPCTargetInfo();
82 return getPPC64TargetInfo();
84 return getRISCVTargetInfo();
86 return getSPARCV9TargetInfo();
88 return getX86_64TargetInfo();
90 llvm_unreachable("unknown target machine");
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())
99 uint8_t *isecLoc = Out::bufferStart + isec->getParent()->offset + isec->outSecOff;
100 if (isecLoc <= loc && loc < isecLoc + isec->getSize())
101 return {isec, isec->template getLocation<ELFT>(loc - isecLoc) + ": "};
106 ErrorPlace elf::getErrorPlace(const uint8_t *loc) {
107 switch (config->ekind) {
109 return getErrPlace<ELF32LE>(loc);
111 return getErrPlace<ELF32BE>(loc);
113 return getErrPlace<ELF64LE>(loc);
115 return getErrPlace<ELF64BE>(loc);
117 llvm_unreachable("unknown ELF type");
121 TargetInfo::~TargetInfo() {}
123 int64_t TargetInfo::getImplicitAddend(const uint8_t *buf, RelType type) const {
127 bool TargetInfo::usesOnlyLowPageBits(RelType type) const { return false; }
129 bool TargetInfo::needsThunk(RelExpr expr, RelType type, const InputFile *file,
130 uint64_t branchAddr, const Symbol &s) const {
134 bool TargetInfo::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
135 uint8_t stOther) const {
136 llvm_unreachable("Target doesn't support split stacks.");
139 bool TargetInfo::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
143 void TargetInfo::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
147 RelExpr TargetInfo::adjustRelaxExpr(RelType type, const uint8_t *data,
148 RelExpr expr) const {
152 void TargetInfo::relaxGot(uint8_t *loc, RelType type, uint64_t val) const {
153 llvm_unreachable("Should not have claimed to be relaxable");
156 void TargetInfo::relaxTlsGdToLe(uint8_t *loc, RelType type,
157 uint64_t val) const {
158 llvm_unreachable("Should not have claimed to be relaxable");
161 void TargetInfo::relaxTlsGdToIe(uint8_t *loc, RelType type,
162 uint64_t val) const {
163 llvm_unreachable("Should not have claimed to be relaxable");
166 void TargetInfo::relaxTlsIeToLe(uint8_t *loc, RelType type,
167 uint64_t val) const {
168 llvm_unreachable("Should not have claimed to be relaxable");
171 void TargetInfo::relaxTlsLdToLe(uint8_t *loc, RelType type,
172 uint64_t val) const {
173 llvm_unreachable("Should not have claimed to be relaxable");
176 uint64_t TargetInfo::getImageBase() const {
177 // Use -image-base if set. Fall back to the target default if not.
178 if (config->imageBase)
179 return *config->imageBase;
180 return config->isPic ? 0 : defaultImageBase;