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
;
39 using namespace lld::elf
;
41 const TargetInfo
*elf::target
;
43 std::string
lld::toString(RelType type
) {
44 StringRef s
= getELFRelocationTypeName(elf::config
->emachine
, type
);
46 return ("Unknown (" + Twine(type
) + ")").str();
47 return std::string(s
);
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 return getLoongArchTargetInfo();
68 switch (config
->ekind
) {
70 return getMipsTargetInfo
<ELF32LE
>();
72 return getMipsTargetInfo
<ELF32BE
>();
74 return getMipsTargetInfo
<ELF64LE
>();
76 return getMipsTargetInfo
<ELF64BE
>();
78 llvm_unreachable("unsupported MIPS target");
81 return getMSP430TargetInfo();
83 return getPPCTargetInfo();
85 return getPPC64TargetInfo();
87 return getRISCVTargetInfo();
89 return getSPARCV9TargetInfo();
91 return getSystemZTargetInfo();
93 return getX86_64TargetInfo();
95 fatal("unsupported e_machine value: " + Twine(config
->emachine
));
99 ErrorPlace
elf::getErrorPlace(const uint8_t *loc
) {
100 assert(loc
!= nullptr);
101 for (InputSectionBase
*d
: ctx
.inputSections
) {
102 auto *isec
= dyn_cast
<InputSection
>(d
);
103 if (!isec
|| !isec
->getParent() || (isec
->type
& SHT_NOBITS
))
106 const uint8_t *isecLoc
=
108 ? (Out::bufferStart
+ isec
->getParent()->offset
+ isec
->outSecOff
)
109 : isec
->contentMaybeDecompress().data();
110 if (isecLoc
== nullptr) {
111 assert(isa
<SyntheticSection
>(isec
) && "No data but not synthetic?");
114 if (isecLoc
<= loc
&& loc
< isecLoc
+ isec
->getSize()) {
115 std::string objLoc
= isec
->getLocation(loc
- isecLoc
);
116 // Return object file location and source file location.
117 // TODO: Refactor getSrcMsg not to take a variable.
118 Undefined
dummy(ctx
.internalFile
, "", STB_LOCAL
, 0, 0);
119 return {isec
, objLoc
+ ": ",
120 isec
->file
? isec
->getSrcMsg(dummy
, loc
- isecLoc
) : ""};
126 TargetInfo::~TargetInfo() {}
128 int64_t TargetInfo::getImplicitAddend(const uint8_t *buf
, RelType type
) const {
129 internalLinkerError(getErrorLocation(buf
),
130 "cannot read addend for relocation " + toString(type
));
134 bool TargetInfo::usesOnlyLowPageBits(RelType type
) const { return false; }
136 bool TargetInfo::needsThunk(RelExpr expr
, RelType type
, const InputFile
*file
,
137 uint64_t branchAddr
, const Symbol
&s
,
142 bool TargetInfo::adjustPrologueForCrossSplitStack(uint8_t *loc
, uint8_t *end
,
143 uint8_t stOther
) const {
144 fatal("target doesn't support split stacks");
147 bool TargetInfo::inBranchRange(RelType type
, uint64_t src
, uint64_t dst
) const {
151 RelExpr
TargetInfo::adjustTlsExpr(RelType type
, RelExpr expr
) const {
155 RelExpr
TargetInfo::adjustGotPcExpr(RelType type
, int64_t addend
,
156 const uint8_t *data
) const {
160 void TargetInfo::relocateAlloc(InputSectionBase
&sec
, uint8_t *buf
) const {
161 const unsigned bits
= config
->is64
? 64 : 32;
162 uint64_t secAddr
= sec
.getOutputSection()->addr
;
163 if (auto *s
= dyn_cast
<InputSection
>(&sec
))
164 secAddr
+= s
->outSecOff
;
165 else if (auto *ehIn
= dyn_cast
<EhInputSection
>(&sec
))
166 secAddr
+= ehIn
->getParent()->outSecOff
;
167 for (const Relocation
&rel
: sec
.relocs()) {
168 uint8_t *loc
= buf
+ rel
.offset
;
169 const uint64_t val
= SignExtend64(
170 sec
.getRelocTargetVA(sec
.file
, rel
.type
, rel
.addend
,
171 secAddr
+ rel
.offset
, *rel
.sym
, rel
.expr
),
173 if (rel
.expr
!= R_RELAX_HINT
)
174 relocate(loc
, rel
, val
);
178 uint64_t TargetInfo::getImageBase() const {
179 // Use --image-base if set. Fall back to the target default if not.
180 if (config
->imageBase
)
181 return *config
->imageBase
;
182 return config
->isPic
? 0 : defaultImageBase
;