1 //===- Target.cpp ---------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Machine-specific things, such as applying relocations, creation of
11 // GOT or PLT entries, etc., are handled in this file.
13 // Refer the ELF spec for the single letter variables, S, A or P, used
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.
22 // Ulrich Drepper, ELF Handling For Thread-Local Storage
23 // http://www.akkadia.org/drepper/tls.pdf
25 //===----------------------------------------------------------------------===//
28 #include "InputFiles.h"
29 #include "OutputSections.h"
30 #include "SymbolTable.h"
32 #include "lld/Common/ErrorHandler.h"
33 #include "llvm/Object/ELF.h"
36 using namespace llvm::object;
37 using namespace llvm::ELF;
39 using namespace lld::elf;
41 TargetInfo *elf::Target;
43 std::string lld::toString(RelType Type) {
44 StringRef S = getELFRelocationTypeName(elf::Config->EMachine, Type);
46 return ("Unknown (" + Twine(Type) + ")").str();
50 TargetInfo *elf::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 fatal("unsupported MIPS target");
79 return getPPCTargetInfo();
81 return getPPC64TargetInfo();
83 return getSPARCV9TargetInfo();
85 if (Config->EKind == ELF32LEKind)
86 return getX32TargetInfo();
87 return getX86_64TargetInfo();
89 fatal("unknown target machine");
92 template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *Loc) {
93 for (InputSectionBase *D : InputSections) {
94 auto *IS = cast<InputSection>(D);
98 uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
99 if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
100 return {IS, IS->template getLocation<ELFT>(Loc - ISLoc) + ": "};
105 ErrorPlace elf::getErrorPlace(const uint8_t *Loc) {
106 switch (Config->EKind) {
108 return getErrPlace<ELF32LE>(Loc);
110 return getErrPlace<ELF32BE>(Loc);
112 return getErrPlace<ELF64LE>(Loc);
114 return getErrPlace<ELF64BE>(Loc);
116 llvm_unreachable("unknown ELF type");
120 TargetInfo::~TargetInfo() {}
122 int64_t TargetInfo::getImplicitAddend(const uint8_t *Buf, RelType Type) const {
126 bool TargetInfo::usesOnlyLowPageBits(RelType Type) const { return false; }
128 bool TargetInfo::needsThunk(RelExpr Expr, RelType Type, const InputFile *File,
129 uint64_t BranchAddr, const Symbol &S) const {
133 bool TargetInfo::adjustPrologueForCrossSplitStack(uint8_t *Loc,
134 uint8_t *End) const {
135 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, 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() {
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->Pic ? 0 : DefaultImageBase;