2015-09-22 18:19:46 +00:00
|
|
|
//===- Target.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00: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-22 18:19:46 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-10-13 19:51:57 +00:00
|
|
|
//
|
2015-10-15 19:52:27 +00:00
|
|
|
// Machine-specific things, such as applying relocations, creation of
|
|
|
|
// GOT or PLT entries, etc., are handled in this file.
|
|
|
|
//
|
2016-08-24 16:36:41 +00:00
|
|
|
// Refer the ELF spec for the single letter variables, S, A or P, used
|
2016-04-13 01:40:19 +00:00
|
|
|
// in this file.
|
2015-10-13 19:51:57 +00:00
|
|
|
//
|
2016-04-23 01:10:15 +00: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-13 19:51:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2015-09-22 18:19:46 +00:00
|
|
|
|
|
|
|
#include "Target.h"
|
2016-03-31 21:26:23 +00:00
|
|
|
#include "InputFiles.h"
|
2015-10-08 20:06:07 +00:00
|
|
|
#include "OutputSections.h"
|
2016-12-21 00:05:39 +00:00
|
|
|
#include "SymbolTable.h"
|
2015-09-29 23:22:16 +00:00
|
|
|
#include "Symbols.h"
|
2020-01-07 12:29:47 +00: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-25 22:28:38 +00:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2015-09-22 20:54:08 +00:00
|
|
|
#include "llvm/Object/ELF.h"
|
2015-09-22 18:19:46 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
2015-09-22 20:54:08 +00:00
|
|
|
using namespace llvm::object;
|
2015-09-22 18:19:46 +00:00
|
|
|
using namespace llvm::ELF;
|
2020-05-14 22:18:58 -07:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2017-06-16 17:32:43 +00:00
|
|
|
|
2024-11-16 11:58:10 -08:00
|
|
|
std::string elf::toStr(Ctx &ctx, RelType type) {
|
|
|
|
StringRef s = getELFRelocationTypeName(ctx.arg.emachine, type);
|
2017-01-25 21:27:59 +00:00
|
|
|
if (s == "Unknown")
|
|
|
|
return ("Unknown (" + Twine(type) + ")").str();
|
2020-01-28 20:23:46 +01:00
|
|
|
return std::string(s);
|
2017-01-06 10:04:08 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 08:25:58 -08:00
|
|
|
const ELFSyncStream &elf::operator<<(const ELFSyncStream &s, RelType type) {
|
2024-11-16 13:07:17 -08:00
|
|
|
s << toStr(s.ctx, type);
|
2024-11-06 08:25:58 -08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2024-10-07 23:14:02 -07:00
|
|
|
void elf::setTarget(Ctx &ctx) {
|
2024-09-21 22:46:13 -07:00
|
|
|
switch (ctx.arg.emachine) {
|
2017-06-16 17:32:43 +00:00
|
|
|
case EM_386:
|
|
|
|
case EM_IAMCU:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setX86TargetInfo(ctx);
|
2017-06-16 17:32:43 +00:00
|
|
|
case EM_AARCH64:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setAArch64TargetInfo(ctx);
|
2017-06-16 17:32:43 +00:00
|
|
|
case EM_AMDGPU:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setAMDGPUTargetInfo(ctx);
|
2017-06-16 17:32:43 +00:00
|
|
|
case EM_ARM:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setARMTargetInfo(ctx);
|
2017-06-16 17:32:43 +00:00
|
|
|
case EM_AVR:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setAVRTargetInfo(ctx);
|
2018-06-13 18:45:25 +00:00
|
|
|
case EM_HEXAGON:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setHexagonTargetInfo(ctx);
|
2023-07-25 17:03:28 +08:00
|
|
|
case EM_LOONGARCH:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setLoongArchTargetInfo(ctx);
|
2017-06-16 17:32:43 +00:00
|
|
|
case EM_MIPS:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setMipsTargetInfo(ctx);
|
2019-01-10 13:43:06 +00:00
|
|
|
case EM_MSP430:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setMSP430TargetInfo(ctx);
|
2017-06-16 17:32:43 +00:00
|
|
|
case EM_PPC:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setPPCTargetInfo(ctx);
|
2017-06-16 17:32:43 +00:00
|
|
|
case EM_PPC64:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setPPC64TargetInfo(ctx);
|
2018-08-09 17:59:56 +00:00
|
|
|
case EM_RISCV:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setRISCVTargetInfo(ctx);
|
2017-06-28 17:05:39 +00:00
|
|
|
case EM_SPARCV9:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setSPARCV9TargetInfo(ctx);
|
2024-02-13 11:29:21 +01:00
|
|
|
case EM_S390:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setSystemZTargetInfo(ctx);
|
2017-06-16 17:32:43 +00:00
|
|
|
case EM_X86_64:
|
2024-10-07 23:14:02 -07:00
|
|
|
return setX86_64TargetInfo(ctx);
|
2024-02-12 17:37:29 -08:00
|
|
|
default:
|
2024-11-24 12:13:01 -08:00
|
|
|
Fatal(ctx) << "unsupported e_machine value: " << ctx.arg.emachine;
|
2017-06-16 17:32:43 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-14 21:30:32 +00:00
|
|
|
|
2024-09-28 19:23:56 -07:00
|
|
|
ErrorPlace elf::getErrorPlace(Ctx &ctx, const uint8_t *loc) {
|
2020-01-07 12:29:47 +00:00
|
|
|
assert(loc != nullptr);
|
2022-10-16 00:49:48 -07:00
|
|
|
for (InputSectionBase *d : ctx.inputSections) {
|
2022-07-29 00:39:57 -07:00
|
|
|
auto *isec = dyn_cast<InputSection>(d);
|
|
|
|
if (!isec || !isec->getParent() || (isec->type & SHT_NOBITS))
|
2016-12-21 00:05:39 +00:00
|
|
|
continue;
|
|
|
|
|
2020-01-07 12:29:47 +00:00
|
|
|
const uint8_t *isecLoc =
|
2024-08-03 11:00:11 -07:00
|
|
|
ctx.bufferStart
|
|
|
|
? (ctx.bufferStart + isec->getParent()->offset + isec->outSecOff)
|
2022-11-20 22:43:22 +00:00
|
|
|
: isec->contentMaybeDecompress().data();
|
2020-01-07 12:29:47 +00:00
|
|
|
if (isecLoc == nullptr) {
|
|
|
|
assert(isa<SyntheticSection>(isec) && "No data but not synthetic?");
|
|
|
|
continue;
|
|
|
|
}
|
2021-10-28 09:38:45 -07:00
|
|
|
if (isecLoc <= loc && loc < isecLoc + isec->getSize()) {
|
2022-01-18 17:33:58 -08:00
|
|
|
std::string objLoc = isec->getLocation(loc - isecLoc);
|
2021-10-28 09:38:45 -07:00
|
|
|
// Return object file location and source file location.
|
2024-01-22 09:09:46 -08:00
|
|
|
Undefined dummy(ctx.internalFile, "", STB_LOCAL, 0, 0);
|
2024-11-29 17:18:22 -08:00
|
|
|
ELFSyncStream msg(ctx, DiagLevel::None);
|
|
|
|
if (isec->file)
|
|
|
|
msg << isec->getSrcMsg(dummy, loc - isecLoc);
|
|
|
|
return {isec, objLoc + ": ", std::string(msg.str())};
|
2021-10-28 09:38:45 -07:00
|
|
|
}
|
2016-12-21 00:05:39 +00:00
|
|
|
}
|
2018-03-21 09:19:34 +00:00
|
|
|
return {};
|
2016-12-21 00:05:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 18:19:46 +00:00
|
|
|
TargetInfo::~TargetInfo() {}
|
|
|
|
|
2017-10-11 22:49:24 +00:00
|
|
|
int64_t TargetInfo::getImplicitAddend(const uint8_t *buf, RelType type) const {
|
2024-11-16 13:07:17 -08:00
|
|
|
InternalErr(ctx, buf) << "cannot read addend for relocation " << type;
|
2016-03-30 12:40:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-11 22:49:24 +00:00
|
|
|
bool TargetInfo::usesOnlyLowPageBits(RelType type) const { return false; }
|
2015-12-11 08:59:37 +00:00
|
|
|
|
2017-10-11 22:49:24 +00:00
|
|
|
bool TargetInfo::needsThunk(RelExpr expr, RelType type, const InputFile *file,
|
2019-11-23 00:57:54 -08:00
|
|
|
uint64_t branchAddr, const Symbol &s,
|
|
|
|
int64_t a) const {
|
2017-02-01 10:26:03 +00:00
|
|
|
return false;
|
2016-03-31 21:26:23 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 17:13:01 +00:00
|
|
|
bool TargetInfo::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
|
|
|
|
uint8_t stOther) const {
|
2024-11-06 21:17:26 -08:00
|
|
|
Err(ctx) << "target doesn't support split stacks";
|
|
|
|
return false;
|
2018-07-17 23:16:02 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 22:49:24 +00:00
|
|
|
bool TargetInfo::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
|
2017-07-17 16:54:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-25 09: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-01 16:45:30 +00:00
|
|
|
return expr;
|
2016-05-25 14:31:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 08:43:26 -08:00
|
|
|
RelExpr TargetInfo::adjustGotPcExpr(RelType type, int64_t addend,
|
|
|
|
const uint8_t *data) const {
|
|
|
|
return R_GOT_PC;
|
|
|
|
}
|
|
|
|
|
2022-10-17 11:01:10 -07:00
|
|
|
void TargetInfo::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
|
2024-09-21 22:46:13 -07:00
|
|
|
const unsigned bits = ctx.arg.is64 ? 64 : 32;
|
2022-10-17 11:01:10 -07:00
|
|
|
uint64_t secAddr = sec.getOutputSection()->addr;
|
|
|
|
if (auto *s = dyn_cast<InputSection>(&sec))
|
|
|
|
secAddr += s->outSecOff;
|
2023-10-03 10:20:14 +01:00
|
|
|
else if (auto *ehIn = dyn_cast<EhInputSection>(&sec))
|
|
|
|
secAddr += ehIn->getParent()->outSecOff;
|
2022-11-21 04:12:03 +00:00
|
|
|
for (const Relocation &rel : sec.relocs()) {
|
2022-10-17 11:01:10 -07:00
|
|
|
uint8_t *loc = buf + rel.offset;
|
|
|
|
const uint64_t val = SignExtend64(
|
2024-10-06 16:34:09 -07:00
|
|
|
sec.getRelocTargetVA(ctx, rel, secAddr + rel.offset), bits);
|
2022-10-17 11:01:10 -07:00
|
|
|
if (rel.expr != R_RELAX_HINT)
|
|
|
|
relocate(loc, rel, val);
|
|
|
|
}
|
2015-11-25 21:46:05 +00:00
|
|
|
}
|
2017-10-10 10:09:35 +00:00
|
|
|
|
2019-03-28 17:05:09 +00:00
|
|
|
uint64_t TargetInfo::getImageBase() const {
|
2021-10-25 12:52:06 -07:00
|
|
|
// Use --image-base if set. Fall back to the target default if not.
|
2024-09-21 22:46:13 -07:00
|
|
|
if (ctx.arg.imageBase)
|
|
|
|
return *ctx.arg.imageBase;
|
|
|
|
return ctx.arg.isPic ? 0 : defaultImageBase;
|
2017-10-10 10:09:35 +00:00
|
|
|
}
|