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 //===----------------------------------------------------------------------===//
29 #include "InputFiles.h"
30 #include "OutputSections.h"
31 #include "SymbolTable.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(uint32_t Type) {
44 StringRef S = getELFRelocationTypeName(elf::Config->EMachine, Type);
46 return ("Unknown (" + Twine(Type) + ")").str();
50 TargetInfo *elf::createTarget() {
51 switch (Config->EMachine) {
54 return createX86TargetInfo();
56 return createAArch64TargetInfo();
58 return createAMDGPUTargetInfo();
60 return createARMTargetInfo();
62 return createAVRTargetInfo();
64 switch (Config->EKind) {
66 return createMipsTargetInfo<ELF32LE>();
68 return createMipsTargetInfo<ELF32BE>();
70 return createMipsTargetInfo<ELF64LE>();
72 return createMipsTargetInfo<ELF64BE>();
74 fatal("unsupported MIPS target");
77 return createPPCTargetInfo();
79 return createPPC64TargetInfo();
81 if (Config->EKind == ELF32LEKind)
82 return createX32TargetInfo();
83 return createX86_64TargetInfo();
85 fatal("unknown target machine");
88 template <class ELFT> static std::string getErrorLoc(const uint8_t *Loc) {
89 for (InputSectionBase *D : InputSections) {
90 auto *IS = dyn_cast_or_null<InputSection>(D);
91 if (!IS || !IS->getParent())
94 uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
95 if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
96 return IS->template getLocation<ELFT>(Loc - ISLoc) + ": ";
101 std::string elf::getErrorLocation(const uint8_t *Loc) {
102 switch (Config->EKind) {
104 return getErrorLoc<ELF32LE>(Loc);
106 return getErrorLoc<ELF32BE>(Loc);
108 return getErrorLoc<ELF64LE>(Loc);
110 return getErrorLoc<ELF64BE>(Loc);
112 llvm_unreachable("unknown ELF type");
116 TargetInfo::~TargetInfo() {}
118 int64_t TargetInfo::getImplicitAddend(const uint8_t *Buf, uint32_t Type) const {
122 bool TargetInfo::usesOnlyLowPageBits(uint32_t Type) const { return false; }
124 bool TargetInfo::needsThunk(RelExpr Expr, uint32_t RelocType,
125 const InputFile *File, const SymbolBody &S) const {
129 void TargetInfo::writeIgotPlt(uint8_t *Buf, const SymbolBody &S) const {
133 RelExpr TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
134 RelExpr Expr) const {
138 void TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
139 llvm_unreachable("Should not have claimed to be relaxable");
142 void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
143 uint64_t Val) const {
144 llvm_unreachable("Should not have claimed to be relaxable");
147 void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
148 uint64_t Val) const {
149 llvm_unreachable("Should not have claimed to be relaxable");
152 void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
153 uint64_t Val) const {
154 llvm_unreachable("Should not have claimed to be relaxable");
157 void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
158 uint64_t Val) const {
159 llvm_unreachable("Should not have claimed to be relaxable");