1//===- Target.cpp ---------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// Machine-specific things, such as applying relocations, creation of
10// GOT or PLT entries, etc., are handled in this file.
11//
12// Refer the ELF spec for the single letter variables, S, A or P, used
13// in this file.
14//
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.
20//
21// Ulrich Drepper, ELF Handling For Thread-Local Storage
22// http://www.akkadia.org/drepper/tls.pdf
23//
24//===----------------------------------------------------------------------===//
25
26#include "Target.h"
27#include "InputFiles.h"
28#include "OutputSections.h"
29#include "RelocScan.h"
30#include "SymbolTable.h"
31#include "Symbols.h"
32#include "lld/Common/ErrorHandler.h"
33#include "llvm/Object/ELF.h"
34
35using namespace llvm;
36using namespace llvm::object;
37using namespace llvm::ELF;
38using namespace lld;
39using namespace lld::elf;
40
41std::string elf::toStr(Ctx &ctx, RelType type) {
42 StringRef s = getELFRelocationTypeName(Machine: ctx.arg.emachine, Type: type);
43 if (s == "Unknown")
44 return ("Unknown (" + Twine(type) + ")").str();
45 return std::string(s);
46}
47
48const ELFSyncStream &elf::operator<<(const ELFSyncStream &s, RelType type) {
49 s << toStr(ctx&: s.ctx, type);
50 return s;
51}
52
53void elf::setTarget(Ctx &ctx) {
54 switch (ctx.arg.emachine) {
55 case EM_386:
56 case EM_IAMCU:
57 return setX86TargetInfo(ctx);
58 case EM_AARCH64:
59 return setAArch64TargetInfo(ctx);
60 case EM_AMDGPU:
61 return setAMDGPUTargetInfo(ctx);
62 case EM_ARM:
63 return setARMTargetInfo(ctx);
64 case EM_AVR:
65 return setAVRTargetInfo(ctx);
66 case EM_HEXAGON:
67 return setHexagonTargetInfo(ctx);
68 case EM_LOONGARCH:
69 return setLoongArchTargetInfo(ctx);
70 case EM_MIPS:
71 return setMipsTargetInfo(ctx);
72 case EM_MSP430:
73 return setMSP430TargetInfo(ctx);
74 case EM_PPC:
75 return setPPCTargetInfo(ctx);
76 case EM_PPC64:
77 return setPPC64TargetInfo(ctx);
78 case EM_RISCV:
79 return setRISCVTargetInfo(ctx);
80 case EM_SPARCV9:
81 return setSPARCV9TargetInfo(ctx);
82 case EM_S390:
83 return setSystemZTargetInfo(ctx);
84 case EM_X86_64:
85 return setX86_64TargetInfo(ctx);
86 default:
87 Fatal(ctx) << "unsupported e_machine value: " << ctx.arg.emachine;
88 }
89}
90
91ErrorPlace elf::getErrorPlace(Ctx &ctx, const uint8_t *loc) {
92 assert(loc != nullptr);
93 for (InputSectionBase *d : ctx.inputSections) {
94 auto *isec = dyn_cast<InputSection>(Val: d);
95 if (!isec || !isec->getParent() || (isec->type & SHT_NOBITS))
96 continue;
97
98 const uint8_t *isecLoc =
99 ctx.bufferStart
100 ? (ctx.bufferStart + isec->getParent()->offset + isec->outSecOff)
101 : isec->contentMaybeDecompress().data();
102 if (isecLoc == nullptr) {
103 assert(isa<SyntheticSection>(isec) && "No data but not synthetic?");
104 continue;
105 }
106 if (isecLoc <= loc && loc < isecLoc + isec->getSize()) {
107 std::string objLoc = isec->getLocation(offset: loc - isecLoc);
108 // Return object file location and source file location.
109 ELFSyncStream msg(ctx, DiagLevel::None);
110 if (isec->file)
111 msg << isec->getSrcMsg(sym: *ctx.dummySym, offset: loc - isecLoc);
112 return {.isec: isec, .loc: objLoc + ": ", .srcLoc: std::string(msg.str())};
113 }
114 }
115 return {};
116}
117
118TargetInfo::~TargetInfo() {}
119
120int64_t TargetInfo::getImplicitAddend(const uint8_t *buf, RelType type) const {
121 InternalErr(ctx, buf) << "cannot read addend for relocation " << type;
122 return 0;
123}
124
125bool TargetInfo::usesOnlyLowPageBits(RelType type) const { return false; }
126
127bool TargetInfo::needsThunk(RelExpr expr, RelType type, const InputFile *file,
128 uint64_t branchAddr, const Symbol &s,
129 int64_t a) const {
130 return false;
131}
132
133bool TargetInfo::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
134 uint8_t stOther) const {
135 Err(ctx) << "target doesn't support split stacks";
136 return false;
137}
138
139bool TargetInfo::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
140 return true;
141}
142
143RelExpr TargetInfo::adjustTlsExpr(RelType type, RelExpr expr) const {
144 return expr;
145}
146
147RelExpr TargetInfo::adjustGotPcExpr(RelType type, int64_t addend,
148 const uint8_t *data) const {
149 return R_GOT_PC;
150}
151
152static void relocateImpl(const TargetInfo &target, InputSectionBase &sec,
153 uint64_t secAddr, uint8_t *buf) {
154 auto &ctx = target.ctx;
155 const unsigned bits = ctx.arg.is64 ? 64 : 32;
156 for (const Relocation &rel : sec.relocs()) {
157 uint8_t *loc = buf + rel.offset;
158 const uint64_t val = SignExtend64(
159 X: sec.getRelocTargetVA(ctx, r: rel, p: secAddr + rel.offset), B: bits);
160 if (rel.expr != R_RELAX_HINT)
161 target.relocate(loc, rel, val);
162 }
163}
164
165void TargetInfo::relocateAlloc(InputSection &sec, uint8_t *buf) const {
166 uint64_t secAddr = sec.getOutputSection()->addr + sec.outSecOff;
167 relocateImpl(target: *this, sec, secAddr, buf);
168}
169
170// A variant of relocateAlloc that processes an EhInputSection.
171void TargetInfo::relocateEh(EhInputSection &sec, uint8_t *buf) const {
172 uint64_t secAddr = sec.getOutputSection()->addr + sec.getParent()->outSecOff;
173 relocateImpl(target: *this, sec, secAddr, buf);
174}
175
176uint64_t TargetInfo::getImageBase() const {
177 // Use --image-base if set. Fall back to the target default if not.
178 if (ctx.arg.imageBase)
179 return *ctx.arg.imageBase;
180 return ctx.arg.isPic ? 0 : defaultImageBase;
181}
182