forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathTarget.cpp
181 lines (159 loc) · 5.46 KB
/
Target.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//===- Target.cpp ---------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Machine-specific things, such as applying relocations, creation of
// GOT or PLT entries, etc., are handled in this file.
//
// Refer the ELF spec for the single letter variables, S, A or P, used
// in this file.
//
// Some functions defined in this file has "relaxTls" as part of their names.
// They do peephole optimization for TLS variables by rewriting instructions.
// They are not part of the ABI but optional optimization, so you can skip
// them if you are not interested in how TLS variables are optimized.
// See the following paper for the details.
//
// Ulrich Drepper, ELF Handling For Thread-Local Storage
// http://www.akkadia.org/drepper/tls.pdf
//
//===----------------------------------------------------------------------===//
#include"Target.h"
#include"InputFiles.h"
#include"OutputSections.h"
#include"SymbolTable.h"
#include"Symbols.h"
#include"lld/Common/ErrorHandler.h"
#include"llvm/Object/ELF.h"
usingnamespacellvm;
usingnamespacellvm::object;
usingnamespacellvm::ELF;
usingnamespacelld;
usingnamespacelld::elf;
const TargetInfo *elf::Target;
std::string lld::toString(RelType Type) {
StringRef S = getELFRelocationTypeName(elf::Config->EMachine, Type);
if (S == "Unknown")
return ("Unknown (" + Twine(Type) + ")").str();
return S;
}
TargetInfo *elf::getTarget() {
switch (Config->EMachine) {
case EM_386:
case EM_IAMCU:
returngetX86TargetInfo();
case EM_AARCH64:
returngetAArch64TargetInfo();
case EM_AMDGPU:
returngetAMDGPUTargetInfo();
case EM_ARM:
returngetARMTargetInfo();
case EM_AVR:
returngetAVRTargetInfo();
case EM_HEXAGON:
returngetHexagonTargetInfo();
case EM_MIPS:
switch (Config->EKind) {
case ELF32LEKind:
return getMipsTargetInfo<ELF32LE>();
case ELF32BEKind:
return getMipsTargetInfo<ELF32BE>();
case ELF64LEKind:
return getMipsTargetInfo<ELF64LE>();
case ELF64BEKind:
return getMipsTargetInfo<ELF64BE>();
default:
llvm_unreachable("unsupported MIPS target");
}
case EM_MSP430:
returngetMSP430TargetInfo();
case EM_PPC:
returngetPPCTargetInfo();
case EM_PPC64:
returngetPPC64TargetInfo();
case EM_RISCV:
returngetRISCVTargetInfo();
case EM_SPARCV9:
returngetSPARCV9TargetInfo();
case EM_X86_64:
returngetX86_64TargetInfo();
}
llvm_unreachable("unknown target machine");
}
template <classELFT> static ErrorPlace getErrPlace(constuint8_t *Loc) {
for (InputSectionBase *D : InputSections) {
auto *IS = cast<InputSection>(D);
if (!IS->getParent())
continue;
uint8_t *ISLoc = Out::BufferStart + IS->getParent()->Offset + IS->OutSecOff;
if (ISLoc <= Loc && Loc < ISLoc + IS->getSize())
return {IS, IS->templategetLocation<ELFT>(Loc - ISLoc) + ": "};
}
return {};
}
ErrorPlace elf::getErrorPlace(constuint8_t *Loc) {
switch (Config->EKind) {
case ELF32LEKind:
return getErrPlace<ELF32LE>(Loc);
case ELF32BEKind:
return getErrPlace<ELF32BE>(Loc);
case ELF64LEKind:
return getErrPlace<ELF64LE>(Loc);
case ELF64BEKind:
return getErrPlace<ELF64BE>(Loc);
default:
llvm_unreachable("unknown ELF type");
}
}
TargetInfo::~TargetInfo() {}
int64_tTargetInfo::getImplicitAddend(constuint8_t *Buf, RelType Type) const {
return0;
}
boolTargetInfo::usesOnlyLowPageBits(RelType Type) const { returnfalse; }
boolTargetInfo::needsThunk(RelExpr Expr, RelType Type, const InputFile *File,
uint64_t BranchAddr, const Symbol &S) const {
returnfalse;
}
boolTargetInfo::adjustPrologueForCrossSplitStack(uint8_t *Loc, uint8_t *End,
uint8_t StOther) const {
llvm_unreachable("Target doesn't support split stacks.");
}
boolTargetInfo::inBranchRange(RelType Type, uint64_t Src, uint64_t Dst) const {
returntrue;
}
voidTargetInfo::writeIgotPlt(uint8_t *Buf, const Symbol &S) const {
writeGotPlt(Buf, S);
}
RelExpr TargetInfo::adjustRelaxExpr(RelType Type, constuint8_t *Data,
RelExpr Expr) const {
return Expr;
}
voidTargetInfo::relaxGot(uint8_t *Loc, RelType Type, uint64_t Val) const {
llvm_unreachable("Should not have claimed to be relaxable");
}
voidTargetInfo::relaxTlsGdToLe(uint8_t *Loc, RelType Type,
uint64_t Val) const {
llvm_unreachable("Should not have claimed to be relaxable");
}
voidTargetInfo::relaxTlsGdToIe(uint8_t *Loc, RelType Type,
uint64_t Val) const {
llvm_unreachable("Should not have claimed to be relaxable");
}
voidTargetInfo::relaxTlsIeToLe(uint8_t *Loc, RelType Type,
uint64_t Val) const {
llvm_unreachable("Should not have claimed to be relaxable");
}
voidTargetInfo::relaxTlsLdToLe(uint8_t *Loc, RelType Type,
uint64_t Val) const {
llvm_unreachable("Should not have claimed to be relaxable");
}
uint64_tTargetInfo::getImageBase() const {
// Use -image-base if set. Fall back to the target default if not.
if (Config->ImageBase)
return *Config->ImageBase;
return Config->Pic ? 0 : DefaultImageBase;
}