2015-09-23 02:19:46 +08:00
|
|
|
//===- Target.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2015-09-23 02:19:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-10-14 03:51:57 +08:00
|
|
|
//
|
2015-10-16 03:52:27 +08:00
|
|
|
// Machine-specific things, such as applying relocations, creation of
|
|
|
|
// GOT or PLT entries, etc., are handled in this file.
|
|
|
|
//
|
2016-08-25 00:36:41 +08:00
|
|
|
// Refer the ELF spec for the single letter variables, S, A or P, used
|
2016-04-13 09:40:19 +08:00
|
|
|
// in this file.
|
2015-10-14 03:51:57 +08:00
|
|
|
//
|
2016-04-23 09:10:15 +08:00
|
|
|
// 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
|
|
|
|
//
|
2015-10-14 03:51:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2015-09-23 02:19:46 +08:00
|
|
|
|
|
|
|
#include "Target.h"
|
2016-04-01 05:26:23 +08:00
|
|
|
#include "InputFiles.h"
|
2015-10-09 04:06:07 +08:00
|
|
|
#include "OutputSections.h"
|
2016-12-21 08:05:39 +08:00
|
|
|
#include "SymbolTable.h"
|
2015-09-30 07:22:16 +08:00
|
|
|
#include "Symbols.h"
|
2020-01-07 20:29:47 +08:00
|
|
|
#include "SyntheticSections.h"
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2015-09-23 04:54:08 +08:00
|
|
|
#include "llvm/Object/ELF.h"
|
2015-09-23 02:19:46 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2015-09-23 04:54:08 +08:00
|
|
|
using namespace llvm::object;
|
2015-09-23 02:19:46 +08:00
|
|
|
using namespace llvm::ELF;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2017-06-17 01:32:43 +08:00
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
const TargetInfo *elf::target;
|
|
|
|
|
|
|
|
std::string lld::toString(RelType type) {
|
2017-01-26 05:27:59 +08:00
|
|
|
StringRef s = getELFRelocationTypeName(elf::config->emachine, type);
|
|
|
|
if (s == "Unknown")
|
|
|
|
return ("Unknown (" + Twine(type) + ")").str();
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(s);
|
2017-01-06 18:04:08 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
TargetInfo *elf::getTarget() {
|
2017-06-17 01:32:43 +08:00
|
|
|
switch (config->emachine) {
|
|
|
|
case EM_386:
|
|
|
|
case EM_IAMCU:
|
2017-06-17 04:15:03 +08:00
|
|
|
return getX86TargetInfo();
|
2017-06-17 01:32:43 +08:00
|
|
|
case EM_AARCH64:
|
2017-06-17 04:15:03 +08:00
|
|
|
return getAArch64TargetInfo();
|
2017-06-17 01:32:43 +08:00
|
|
|
case EM_AMDGPU:
|
2017-06-17 04:15:03 +08:00
|
|
|
return getAMDGPUTargetInfo();
|
2017-06-17 01:32:43 +08:00
|
|
|
case EM_ARM:
|
2017-06-17 04:15:03 +08:00
|
|
|
return getARMTargetInfo();
|
2017-06-17 01:32:43 +08:00
|
|
|
case EM_AVR:
|
2017-06-17 04:15:03 +08:00
|
|
|
return getAVRTargetInfo();
|
2018-06-14 02:45:25 +08:00
|
|
|
case EM_HEXAGON:
|
|
|
|
return getHexagonTargetInfo();
|
2017-06-17 01:32:43 +08:00
|
|
|
case EM_MIPS:
|
|
|
|
switch (config->ekind) {
|
|
|
|
case ELF32LEKind:
|
2017-06-17 04:15:03 +08:00
|
|
|
return getMipsTargetInfo<ELF32LE>();
|
2017-06-17 01:32:43 +08:00
|
|
|
case ELF32BEKind:
|
2017-06-17 04:15:03 +08:00
|
|
|
return getMipsTargetInfo<ELF32BE>();
|
2017-06-17 01:32:43 +08:00
|
|
|
case ELF64LEKind:
|
2017-06-17 04:15:03 +08:00
|
|
|
return getMipsTargetInfo<ELF64LE>();
|
2017-06-17 01:32:43 +08:00
|
|
|
case ELF64BEKind:
|
2017-06-17 04:15:03 +08:00
|
|
|
return getMipsTargetInfo<ELF64BE>();
|
2017-06-17 01:32:43 +08:00
|
|
|
default:
|
2018-11-02 04:08:29 +08:00
|
|
|
llvm_unreachable("unsupported MIPS target");
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
2019-01-10 21:43:06 +08:00
|
|
|
case EM_MSP430:
|
|
|
|
return getMSP430TargetInfo();
|
2017-06-17 01:32:43 +08:00
|
|
|
case EM_PPC:
|
2017-06-17 04:15:03 +08:00
|
|
|
return getPPCTargetInfo();
|
2017-06-17 01:32:43 +08:00
|
|
|
case EM_PPC64:
|
2017-06-17 04:15:03 +08:00
|
|
|
return getPPC64TargetInfo();
|
2018-08-10 01:59:56 +08:00
|
|
|
case EM_RISCV:
|
|
|
|
return getRISCVTargetInfo();
|
2017-06-29 01:05:39 +08:00
|
|
|
case EM_SPARCV9:
|
|
|
|
return getSPARCV9TargetInfo();
|
2017-06-17 01:32:43 +08:00
|
|
|
case EM_X86_64:
|
2017-06-17 04:15:03 +08:00
|
|
|
return getX86_64TargetInfo();
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
2018-11-02 04:08:29 +08:00
|
|
|
llvm_unreachable("unknown target machine");
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
2015-10-15 05:30:32 +08:00
|
|
|
|
2018-03-21 17:19:34 +08:00
|
|
|
template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *loc) {
|
2020-01-07 20:29:47 +08:00
|
|
|
assert(loc != nullptr);
|
2017-02-27 10:32:08 +08:00
|
|
|
for (InputSectionBase *d : inputSections) {
|
2018-07-02 22:13:11 +08:00
|
|
|
auto *isec = cast<InputSection>(d);
|
2020-01-15 22:32:14 +08:00
|
|
|
if (!isec->getParent() || (isec->type & SHT_NOBITS))
|
2016-12-21 08:05:39 +08:00
|
|
|
continue;
|
|
|
|
|
2020-01-07 20:29:47 +08:00
|
|
|
const uint8_t *isecLoc =
|
|
|
|
Out::bufferStart
|
|
|
|
? (Out::bufferStart + isec->getParent()->offset + isec->outSecOff)
|
|
|
|
: isec->data().data();
|
|
|
|
if (isecLoc == nullptr) {
|
|
|
|
assert(isa<SyntheticSection>(isec) && "No data but not synthetic?");
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-10 20:41:08 +08:00
|
|
|
if (isecLoc <= loc && loc < isecLoc + isec->getSize())
|
2018-03-21 17:19:34 +08:00
|
|
|
return {isec, isec->template getLocation<ELFT>(loc - isecLoc) + ": "};
|
2016-12-21 08:05:39 +08:00
|
|
|
}
|
2018-03-21 17:19:34 +08:00
|
|
|
return {};
|
2016-12-21 08:05:39 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
ErrorPlace elf::getErrorPlace(const uint8_t *loc) {
|
2016-12-21 08:05:39 +08:00
|
|
|
switch (config->ekind) {
|
|
|
|
case ELF32LEKind:
|
2018-03-21 17:19:34 +08:00
|
|
|
return getErrPlace<ELF32LE>(loc);
|
2016-12-21 08:05:39 +08:00
|
|
|
case ELF32BEKind:
|
2018-03-21 17:19:34 +08:00
|
|
|
return getErrPlace<ELF32BE>(loc);
|
2016-12-21 08:05:39 +08:00
|
|
|
case ELF64LEKind:
|
2018-03-21 17:19:34 +08:00
|
|
|
return getErrPlace<ELF64LE>(loc);
|
2016-12-21 08:05:39 +08:00
|
|
|
case ELF64BEKind:
|
2018-03-21 17:19:34 +08:00
|
|
|
return getErrPlace<ELF64BE>(loc);
|
2016-12-21 08:05:39 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown ELF type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 02:19:46 +08:00
|
|
|
TargetInfo::~TargetInfo() {}
|
|
|
|
|
2017-10-12 06:49:24 +08:00
|
|
|
int64_t TargetInfo::getImplicitAddend(const uint8_t *buf, RelType type) const {
|
2021-07-09 17:05:18 +08:00
|
|
|
internalLinkerError(getErrorLocation(buf),
|
|
|
|
"cannot read addend for relocation " + toString(type));
|
2016-03-30 20:40:38 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-12 06:49:24 +08:00
|
|
|
bool TargetInfo::usesOnlyLowPageBits(RelType type) const { return false; }
|
2015-12-11 16:59:37 +08:00
|
|
|
|
2017-10-12 06:49:24 +08:00
|
|
|
bool TargetInfo::needsThunk(RelExpr expr, RelType type, const InputFile *file,
|
2019-11-23 16:57:54 +08:00
|
|
|
uint64_t branchAddr, const Symbol &s,
|
|
|
|
int64_t a) const {
|
2017-02-01 18:26:03 +08:00
|
|
|
return false;
|
2016-04-01 05:26:23 +08:00
|
|
|
}
|
|
|
|
|
2018-10-17 01:13:01 +08:00
|
|
|
bool TargetInfo::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
|
|
|
|
uint8_t stOther) const {
|
2018-07-18 07:16:02 +08:00
|
|
|
llvm_unreachable("Target doesn't support split stacks.");
|
|
|
|
}
|
|
|
|
|
2017-10-12 06:49:24 +08:00
|
|
|
bool TargetInfo::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
|
2017-07-18 00:54:29 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-26 01:00:55 +08:00
|
|
|
RelExpr TargetInfo::adjustTlsExpr(RelType type, RelExpr expr) const {
|
[ELF] - Implemented support for test/binop relaxations from latest ABI.
Patch implements next relaxation from latest ABI:
"Convert memory operand of test and binop into immediate operand, where binop is one of adc, add, and, cmp, or,
sbb, sub, xor instructions, when position-independent code is disabled."
It is described in System V Application Binary Interface AMD64 Architecture Processor
Supplement Draft Version 0.99.8 (https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-r249.pdf,
B.2 "B.2 Optimize GOTPCRELX Relocations").
Differential revision: http://reviews.llvm.org/D20793
llvm-svn: 271405
2016-06-02 00:45:30 +08:00
|
|
|
return expr;
|
2016-05-25 22:31:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-26 00:43:26 +08:00
|
|
|
RelExpr TargetInfo::adjustGotPcExpr(RelType type, int64_t addend,
|
|
|
|
const uint8_t *data) const {
|
|
|
|
return R_GOT_PC;
|
|
|
|
}
|
|
|
|
|
2020-01-23 11:42:54 +08:00
|
|
|
void TargetInfo::relaxGot(uint8_t *loc, const Relocation &rel,
|
|
|
|
uint64_t val) const {
|
2016-05-25 22:31:37 +08:00
|
|
|
llvm_unreachable("Should not have claimed to be relaxable");
|
|
|
|
}
|
|
|
|
|
2020-01-23 11:42:54 +08:00
|
|
|
void TargetInfo::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
|
2016-04-13 09:40:19 +08:00
|
|
|
uint64_t val) const {
|
2016-03-17 03:03:58 +08:00
|
|
|
llvm_unreachable("Should not have claimed to be relaxable");
|
|
|
|
}
|
|
|
|
|
2020-01-23 11:42:54 +08:00
|
|
|
void TargetInfo::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
|
2016-04-13 09:40:19 +08:00
|
|
|
uint64_t val) const {
|
2016-03-17 03:03:58 +08:00
|
|
|
llvm_unreachable("Should not have claimed to be relaxable");
|
|
|
|
}
|
|
|
|
|
2020-01-23 11:42:54 +08:00
|
|
|
void TargetInfo::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
|
2016-04-13 09:40:19 +08:00
|
|
|
uint64_t val) const {
|
2016-03-17 03:03:58 +08:00
|
|
|
llvm_unreachable("Should not have claimed to be relaxable");
|
|
|
|
}
|
|
|
|
|
2020-01-23 11:42:54 +08:00
|
|
|
void TargetInfo::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
|
2016-04-13 09:40:19 +08:00
|
|
|
uint64_t val) const {
|
2016-03-17 03:03:58 +08:00
|
|
|
llvm_unreachable("Should not have claimed to be relaxable");
|
2015-11-26 05:46:05 +08:00
|
|
|
}
|
2017-10-10 18:09:35 +08:00
|
|
|
|
2019-03-29 01:05:09 +08:00
|
|
|
uint64_t TargetInfo::getImageBase() const {
|
2017-10-10 18:09:35 +08:00
|
|
|
// Use -image-base if set. Fall back to the target default if not.
|
|
|
|
if (config->imageBase)
|
|
|
|
return *config->imageBase;
|
|
|
|
return config->isPic ? 0 : defaultImageBase;
|
|
|
|
}
|