1292934Sdim//===- Target.cpp ---------------------------------------------------------===//
2292934Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6292934Sdim//
7292934Sdim//===----------------------------------------------------------------------===//
8292934Sdim//
9292934Sdim// Machine-specific things, such as applying relocations, creation of
10292934Sdim// GOT or PLT entries, etc., are handled in this file.
11292934Sdim//
12314564Sdim// Refer the ELF spec for the single letter variables, S, A or P, used
13303239Sdim// in this file.
14292934Sdim//
15303239Sdim// Some functions defined in this file has "relaxTls" as part of their names.
16303239Sdim// They do peephole optimization for TLS variables by rewriting instructions.
17303239Sdim// They are not part of the ABI but optional optimization, so you can skip
18303239Sdim// them if you are not interested in how TLS variables are optimized.
19303239Sdim// See the following paper for the details.
20303239Sdim//
21303239Sdim//   Ulrich Drepper, ELF Handling For Thread-Local Storage
22303239Sdim//   http://www.akkadia.org/drepper/tls.pdf
23303239Sdim//
24292934Sdim//===----------------------------------------------------------------------===//
25292934Sdim
26292934Sdim#include "Target.h"
27303239Sdim#include "InputFiles.h"
28292934Sdim#include "OutputSections.h"
29314564Sdim#include "SymbolTable.h"
30292934Sdim#include "Symbols.h"
31360784Sdim#include "SyntheticSections.h"
32327952Sdim#include "lld/Common/ErrorHandler.h"
33292934Sdim#include "llvm/Object/ELF.h"
34292934Sdim
35292934Sdimusing namespace llvm;
36292934Sdimusing namespace llvm::object;
37292934Sdimusing namespace llvm::ELF;
38292934Sdim
39360784Sdimnamespace lld {
40360784Sdimstd::string toString(elf::RelType type) {
41353358Sdim  StringRef s = getELFRelocationTypeName(elf::config->emachine, type);
42353358Sdim  if (s == "Unknown")
43353358Sdim    return ("Unknown (" + Twine(type) + ")").str();
44353358Sdim  return s;
45314564Sdim}
46314564Sdim
47360784Sdimnamespace elf {
48360784Sdimconst TargetInfo *target;
49360784Sdim
50360784SdimTargetInfo *getTarget() {
51353358Sdim  switch (config->emachine) {
52292934Sdim  case EM_386:
53314564Sdim  case EM_IAMCU:
54321369Sdim    return getX86TargetInfo();
55292934Sdim  case EM_AARCH64:
56321369Sdim    return getAArch64TargetInfo();
57293846Sdim  case EM_AMDGPU:
58321369Sdim    return getAMDGPUTargetInfo();
59303239Sdim  case EM_ARM:
60321369Sdim    return getARMTargetInfo();
61321369Sdim  case EM_AVR:
62321369Sdim    return getAVRTargetInfo();
63341825Sdim  case EM_HEXAGON:
64341825Sdim    return getHexagonTargetInfo();
65292934Sdim  case EM_MIPS:
66353358Sdim    switch (config->ekind) {
67292934Sdim    case ELF32LEKind:
68321369Sdim      return getMipsTargetInfo<ELF32LE>();
69292934Sdim    case ELF32BEKind:
70321369Sdim      return getMipsTargetInfo<ELF32BE>();
71303239Sdim    case ELF64LEKind:
72321369Sdim      return getMipsTargetInfo<ELF64LE>();
73303239Sdim    case ELF64BEKind:
74321369Sdim      return getMipsTargetInfo<ELF64BE>();
75292934Sdim    default:
76344779Sdim      llvm_unreachable("unsupported MIPS target");
77292934Sdim    }
78344779Sdim  case EM_MSP430:
79344779Sdim    return getMSP430TargetInfo();
80293846Sdim  case EM_PPC:
81321369Sdim    return getPPCTargetInfo();
82292934Sdim  case EM_PPC64:
83321369Sdim    return getPPC64TargetInfo();
84344779Sdim  case EM_RISCV:
85344779Sdim    return getRISCVTargetInfo();
86321369Sdim  case EM_SPARCV9:
87321369Sdim    return getSPARCV9TargetInfo();
88292934Sdim  case EM_X86_64:
89321369Sdim    return getX86_64TargetInfo();
90292934Sdim  }
91344779Sdim  llvm_unreachable("unknown target machine");
92292934Sdim}
93292934Sdim
94353358Sdimtemplate <class ELFT> static ErrorPlace getErrPlace(const uint8_t *loc) {
95360784Sdim  assert(loc != nullptr);
96353358Sdim  for (InputSectionBase *d : inputSections) {
97353358Sdim    auto *isec = cast<InputSection>(d);
98360784Sdim    if (!isec->getParent() || (isec->type & SHT_NOBITS))
99321369Sdim      continue;
100321369Sdim
101360784Sdim    const uint8_t *isecLoc =
102360784Sdim        Out::bufferStart
103360784Sdim            ? (Out::bufferStart + isec->getParent()->offset + isec->outSecOff)
104360784Sdim            : isec->data().data();
105360784Sdim    if (isecLoc == nullptr) {
106360784Sdim      assert(isa<SyntheticSection>(isec) && "No data but not synthetic?");
107360784Sdim      continue;
108360784Sdim    }
109353358Sdim    if (isecLoc <= loc && loc < isecLoc + isec->getSize())
110353358Sdim      return {isec, isec->template getLocation<ELFT>(loc - isecLoc) + ": "};
111321369Sdim  }
112341825Sdim  return {};
113321369Sdim}
114321369Sdim
115360784SdimErrorPlace getErrorPlace(const uint8_t *loc) {
116353358Sdim  switch (config->ekind) {
117321369Sdim  case ELF32LEKind:
118353358Sdim    return getErrPlace<ELF32LE>(loc);
119321369Sdim  case ELF32BEKind:
120353358Sdim    return getErrPlace<ELF32BE>(loc);
121321369Sdim  case ELF64LEKind:
122353358Sdim    return getErrPlace<ELF64LE>(loc);
123321369Sdim  case ELF64BEKind:
124353358Sdim    return getErrPlace<ELF64BE>(loc);
125321369Sdim  default:
126321369Sdim    llvm_unreachable("unknown ELF type");
127321369Sdim  }
128321369Sdim}
129321369Sdim
130292934SdimTargetInfo::~TargetInfo() {}
131292934Sdim
132353358Sdimint64_t TargetInfo::getImplicitAddend(const uint8_t *buf, RelType type) const {
133303239Sdim  return 0;
134292934Sdim}
135292934Sdim
136353358Sdimbool TargetInfo::usesOnlyLowPageBits(RelType type) const { return false; }
137292934Sdim
138353358Sdimbool TargetInfo::needsThunk(RelExpr expr, RelType type, const InputFile *file,
139360784Sdim                            uint64_t branchAddr, const Symbol &s,
140360784Sdim                            int64_t a) const {
141321369Sdim  return false;
142303239Sdim}
143303239Sdim
144353358Sdimbool TargetInfo::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
145353358Sdim                                                  uint8_t stOther) const {
146341825Sdim  llvm_unreachable("Target doesn't support split stacks.");
147341825Sdim}
148341825Sdim
149353358Sdimbool TargetInfo::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
150321369Sdim  return true;
151321369Sdim}
152303239Sdim
153353358SdimRelExpr TargetInfo::adjustRelaxExpr(RelType type, const uint8_t *data,
154353358Sdim                                    RelExpr expr) const {
155353358Sdim  return expr;
156303239Sdim}
157292934Sdim
158353358Sdimvoid TargetInfo::relaxGot(uint8_t *loc, RelType type, uint64_t val) const {
159303239Sdim  llvm_unreachable("Should not have claimed to be relaxable");
160303239Sdim}
161292934Sdim
162353358Sdimvoid TargetInfo::relaxTlsGdToLe(uint8_t *loc, RelType type,
163353358Sdim                                uint64_t val) const {
164303239Sdim  llvm_unreachable("Should not have claimed to be relaxable");
165303239Sdim}
166292934Sdim
167353358Sdimvoid TargetInfo::relaxTlsGdToIe(uint8_t *loc, RelType type,
168353358Sdim                                uint64_t val) const {
169303239Sdim  llvm_unreachable("Should not have claimed to be relaxable");
170292934Sdim}
171292934Sdim
172353358Sdimvoid TargetInfo::relaxTlsIeToLe(uint8_t *loc, RelType type,
173353358Sdim                                uint64_t val) const {
174303239Sdim  llvm_unreachable("Should not have claimed to be relaxable");
175303239Sdim}
176292934Sdim
177353358Sdimvoid TargetInfo::relaxTlsLdToLe(uint8_t *loc, RelType type,
178353358Sdim                                uint64_t val) const {
179303239Sdim  llvm_unreachable("Should not have claimed to be relaxable");
180303239Sdim}
181327952Sdim
182353358Sdimuint64_t TargetInfo::getImageBase() const {
183327952Sdim  // Use -image-base if set. Fall back to the target default if not.
184353358Sdim  if (config->imageBase)
185353358Sdim    return *config->imageBase;
186353358Sdim  return config->isPic ? 0 : defaultImageBase;
187327952Sdim}
188360784Sdim
189360784Sdim} // namespace elf
190360784Sdim} // namespace lld
191