2017-06-17 01:32:43 +08:00
|
|
|
//===- MIPS.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
|
2017-06-17 01:32:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "OutputSections.h"
|
|
|
|
#include "Symbols.h"
|
|
|
|
#include "SyntheticSections.h"
|
|
|
|
#include "Target.h"
|
|
|
|
#include "Thunks.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"
|
2017-06-17 01:32:43 +08:00
|
|
|
#include "llvm/Object/ELF.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
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
|
|
|
|
|
|
|
namespace {
|
|
|
|
template <class ELFT> class MIPS final : public TargetInfo {
|
|
|
|
public:
|
|
|
|
MIPS();
|
2017-10-25 01:01:40 +08:00
|
|
|
uint32_t calcEFlags() const override;
|
2017-11-04 05:21:47 +08:00
|
|
|
RelExpr getRelExpr(RelType type, const Symbol &s,
|
2017-06-17 01:32:43 +08:00
|
|
|
const uint8_t *loc) const override;
|
2017-10-12 06:49:24 +08:00
|
|
|
int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
|
|
|
|
RelType getDynRel(RelType type) const override;
|
2017-11-04 05:21:47 +08:00
|
|
|
void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
|
2017-06-17 01:32:43 +08:00
|
|
|
void writePltHeader(uint8_t *buf) const override;
|
2019-12-18 05:43:04 +08:00
|
|
|
void writePlt(uint8_t *buf, const Symbol &sym,
|
|
|
|
uint64_t pltEntryAddr) const override;
|
2017-10-12 06:49:24 +08:00
|
|
|
bool 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 override;
|
2020-01-23 13:39:16 +08:00
|
|
|
void relocate(uint8_t *loc, const Relocation &rel,
|
|
|
|
uint64_t val) const override;
|
2017-10-12 06:49:24 +08:00
|
|
|
bool usesOnlyLowPageBits(RelType type) const override;
|
2017-06-17 01:32:43 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
template <class ELFT> MIPS<ELFT>::MIPS() {
|
|
|
|
gotPltHeaderEntriesNum = 2;
|
|
|
|
defaultMaxPageSize = 65536;
|
|
|
|
pltEntrySize = 16;
|
|
|
|
pltHeaderSize = 32;
|
|
|
|
copyRel = R_MIPS_COPY;
|
|
|
|
pltRel = R_MIPS_JUMP_SLOT;
|
|
|
|
needsThunks = true;
|
2018-11-16 13:30:47 +08:00
|
|
|
|
|
|
|
// Set `sigrie 1` as a trap instruction.
|
|
|
|
write32(trapInstr.data(), 0x04170001);
|
2017-06-17 01:32:43 +08:00
|
|
|
|
|
|
|
if (ELFT::Is64Bits) {
|
|
|
|
relativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;
|
2019-06-11 20:59:30 +08:00
|
|
|
symbolicRel = R_MIPS_64;
|
2017-06-17 01:32:43 +08:00
|
|
|
tlsGotRel = R_MIPS_TLS_TPREL64;
|
|
|
|
tlsModuleIndexRel = R_MIPS_TLS_DTPMOD64;
|
|
|
|
tlsOffsetRel = R_MIPS_TLS_DTPREL64;
|
|
|
|
} else {
|
|
|
|
relativeRel = R_MIPS_REL32;
|
2019-06-11 20:59:30 +08:00
|
|
|
symbolicRel = R_MIPS_32;
|
2017-06-17 01:32:43 +08:00
|
|
|
tlsGotRel = R_MIPS_TLS_TPREL32;
|
|
|
|
tlsModuleIndexRel = R_MIPS_TLS_DTPMOD32;
|
|
|
|
tlsOffsetRel = R_MIPS_TLS_DTPREL32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 01:01:40 +08:00
|
|
|
template <class ELFT> uint32_t MIPS<ELFT>::calcEFlags() const {
|
|
|
|
return calcMipsEFlags<ELFT>();
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:32:43 +08:00
|
|
|
template <class ELFT>
|
2017-11-04 05:21:47 +08:00
|
|
|
RelExpr MIPS<ELFT>::getRelExpr(RelType type, const Symbol &s,
|
2017-06-17 01:32:43 +08:00
|
|
|
const uint8_t *loc) const {
|
|
|
|
// See comment in the calculateMipsRelChain.
|
|
|
|
if (ELFT::Is64Bits || config->mipsN32Abi)
|
|
|
|
type &= 0xff;
|
2017-10-12 11:14:06 +08:00
|
|
|
|
2017-06-17 01:32:43 +08:00
|
|
|
switch (type) {
|
|
|
|
case R_MIPS_JALR:
|
2019-11-20 21:19:16 +08:00
|
|
|
// Older versions of clang would erroneously emit this relocation not only
|
|
|
|
// against functions (loaded from the GOT) but also against data symbols
|
|
|
|
// (e.g. a table of function pointers). When we encounter this, ignore the
|
|
|
|
// relocation and emit a warning instead.
|
|
|
|
if (!s.isFunc() && s.type != STT_NOTYPE) {
|
|
|
|
warn(getErrorLocation(loc) +
|
|
|
|
"found R_MIPS_JALR relocation against non-function symbol " +
|
|
|
|
toString(s) + ". This is invalid and most likely a compiler bug.");
|
|
|
|
return R_NONE;
|
|
|
|
}
|
|
|
|
|
2019-09-26 17:13:20 +08:00
|
|
|
// If the target symbol is not preemptible and is not microMIPS,
|
|
|
|
// it might be possible to replace jalr/jr instruction by bal/b.
|
|
|
|
// It depends on the target symbol's offset.
|
|
|
|
if (!s.isPreemptible && !(s.getVA() & 0x1))
|
|
|
|
return R_PC;
|
|
|
|
return R_NONE;
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_JALR:
|
2019-09-26 17:13:20 +08:00
|
|
|
return R_NONE;
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_GPREL16:
|
|
|
|
case R_MIPS_GPREL32:
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_GPREL16:
|
|
|
|
case R_MICROMIPS_GPREL7_S2:
|
2017-06-17 01:32:43 +08:00
|
|
|
return R_MIPS_GOTREL;
|
|
|
|
case R_MIPS_26:
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_26_S1:
|
|
|
|
return R_PLT;
|
|
|
|
case R_MICROMIPS_PC26_S1:
|
|
|
|
return R_PLT_PC;
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_HI16:
|
|
|
|
case R_MIPS_LO16:
|
2017-09-21 22:40:32 +08:00
|
|
|
case R_MIPS_HIGHER:
|
|
|
|
case R_MIPS_HIGHEST:
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_HI16:
|
|
|
|
case R_MICROMIPS_LO16:
|
2017-06-17 01:32:43 +08:00
|
|
|
// R_MIPS_HI16/R_MIPS_LO16 relocations against _gp_disp calculate
|
|
|
|
// offset between start of function and 'gp' value which by default
|
|
|
|
// equal to the start of .got section. In that case we consider these
|
|
|
|
// relocations as relative.
|
|
|
|
if (&s == ElfSym::mipsGpDisp)
|
|
|
|
return R_MIPS_GOT_GP_PC;
|
|
|
|
if (&s == ElfSym::mipsLocalGp)
|
|
|
|
return R_MIPS_GOT_GP;
|
|
|
|
LLVM_FALLTHROUGH;
|
2017-09-21 22:40:32 +08:00
|
|
|
case R_MIPS_32:
|
|
|
|
case R_MIPS_64:
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_GOT_OFST:
|
2017-09-21 22:40:32 +08:00
|
|
|
case R_MIPS_SUB:
|
2021-10-31 12:58:43 +08:00
|
|
|
return R_ABS;
|
2017-09-21 22:40:32 +08:00
|
|
|
case R_MIPS_TLS_DTPREL_HI16:
|
|
|
|
case R_MIPS_TLS_DTPREL_LO16:
|
|
|
|
case R_MIPS_TLS_DTPREL32:
|
|
|
|
case R_MIPS_TLS_DTPREL64:
|
2019-07-24 19:37:13 +08:00
|
|
|
case R_MICROMIPS_TLS_DTPREL_HI16:
|
|
|
|
case R_MICROMIPS_TLS_DTPREL_LO16:
|
2021-10-31 12:58:43 +08:00
|
|
|
return R_DTPREL;
|
2017-09-21 22:40:32 +08:00
|
|
|
case R_MIPS_TLS_TPREL_HI16:
|
|
|
|
case R_MIPS_TLS_TPREL_LO16:
|
|
|
|
case R_MIPS_TLS_TPREL32:
|
|
|
|
case R_MIPS_TLS_TPREL64:
|
|
|
|
case R_MICROMIPS_TLS_TPREL_HI16:
|
|
|
|
case R_MICROMIPS_TLS_TPREL_LO16:
|
2020-12-19 00:24:42 +08:00
|
|
|
return R_TPREL;
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_PC32:
|
|
|
|
case R_MIPS_PC16:
|
|
|
|
case R_MIPS_PC19_S2:
|
|
|
|
case R_MIPS_PC21_S2:
|
|
|
|
case R_MIPS_PC26_S2:
|
|
|
|
case R_MIPS_PCHI16:
|
|
|
|
case R_MIPS_PCLO16:
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_PC7_S1:
|
|
|
|
case R_MICROMIPS_PC10_S1:
|
|
|
|
case R_MICROMIPS_PC16_S1:
|
|
|
|
case R_MICROMIPS_PC18_S3:
|
|
|
|
case R_MICROMIPS_PC19_S2:
|
|
|
|
case R_MICROMIPS_PC23_S2:
|
|
|
|
case R_MICROMIPS_PC21_S1:
|
2017-06-17 01:32:43 +08:00
|
|
|
return R_PC;
|
|
|
|
case R_MIPS_GOT16:
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_GOT16:
|
2017-06-17 01:32:43 +08:00
|
|
|
if (s.isLocal())
|
|
|
|
return R_MIPS_GOT_LOCAL_PAGE;
|
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case R_MIPS_CALL16:
|
|
|
|
case R_MIPS_GOT_DISP:
|
|
|
|
case R_MIPS_TLS_GOTTPREL:
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_CALL16:
|
|
|
|
case R_MICROMIPS_TLS_GOTTPREL:
|
2017-06-17 01:32:43 +08:00
|
|
|
return R_MIPS_GOT_OFF;
|
|
|
|
case R_MIPS_CALL_HI16:
|
|
|
|
case R_MIPS_CALL_LO16:
|
|
|
|
case R_MIPS_GOT_HI16:
|
|
|
|
case R_MIPS_GOT_LO16:
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_CALL_HI16:
|
|
|
|
case R_MICROMIPS_CALL_LO16:
|
|
|
|
case R_MICROMIPS_GOT_HI16:
|
|
|
|
case R_MICROMIPS_GOT_LO16:
|
2017-06-17 01:32:43 +08:00
|
|
|
return R_MIPS_GOT_OFF32;
|
|
|
|
case R_MIPS_GOT_PAGE:
|
|
|
|
return R_MIPS_GOT_LOCAL_PAGE;
|
|
|
|
case R_MIPS_TLS_GD:
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_TLS_GD:
|
2017-06-17 01:32:43 +08:00
|
|
|
return R_MIPS_TLSGD;
|
|
|
|
case R_MIPS_TLS_LDM:
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_TLS_LDM:
|
2017-06-17 01:32:43 +08:00
|
|
|
return R_MIPS_TLSLD;
|
2017-09-21 22:40:32 +08:00
|
|
|
case R_MIPS_NONE:
|
|
|
|
return R_NONE;
|
|
|
|
default:
|
2019-02-15 02:02:20 +08:00
|
|
|
error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
|
|
|
|
") against symbol " + toString(s));
|
|
|
|
return R_NONE;
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 06:49:24 +08:00
|
|
|
template <class ELFT> RelType MIPS<ELFT>::getDynRel(RelType type) const {
|
[ELF][ARM][AARCH64][MIPS][PPC] Simplify the logic to create R_*_RELATIVE for absolute relocation types in writable sections
Summary:
Our rule to create R_*_RELATIVE for absolute relocation types were
loose. D63121 made it stricter but it failed to create R_*_RELATIVE for
R_ARM_TARGET1 and R_PPC64_TOC. rLLD363236 worked around that by
reinstating the original behavior for ARM and PPC64.
This patch is an attempt to simplify the logic.
Note, in ld.bfd, R_ARM_TARGET2 --target2=abs also creates
R_ARM_RELATIVE. This seems a very uncommon scenario (moreover,
--target2=got-rel is the default), so I do not implement any logic
related to it.
Also, delete R_AARCH64_ABS32 from AArch64::getDynRel. We don't have
working ILP32 support yet. Allowing it would create an incorrect
R_AARCH64_RELATIVE.
For MIPS, the (if SymbolRel, then RelativeRel) code is to keep its
behavior unchanged.
Note, in ppc64-abs64-dyn.s, R_PPC64_TOC gets an incorrect addend because
computeAddend() doesn't compute the correct address. We seem to have the
wrong behavior for a long time. The important thing seems that a dynamic
relocation R_PPC64_TOC should not be created as the dynamic loader will
error R_PPC64_TOC is not supported.
Reviewers: atanasyan, grimar, peter.smith, ruiu, sfertile, espindola
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D63383
llvm-svn: 363928
2019-06-20 22:00:08 +08:00
|
|
|
if (type == symbolicRel)
|
|
|
|
return type;
|
2018-04-05 20:07:20 +08:00
|
|
|
return R_MIPS_NONE;
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2017-11-04 05:21:47 +08:00
|
|
|
void MIPS<ELFT>::writeGotPlt(uint8_t *buf, const Symbol &) const {
|
2018-09-26 03:26:58 +08:00
|
|
|
uint64_t va = in.plt->getVA();
|
2017-11-09 07:34:34 +08:00
|
|
|
if (isMicroMips())
|
|
|
|
va |= 1;
|
2019-10-07 16:30:46 +08:00
|
|
|
write32(buf, va);
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
|
2017-09-12 21:08:24 +08:00
|
|
|
template <endianness E> static uint32_t readShuffle(const uint8_t *loc) {
|
|
|
|
// The major opcode of a microMIPS instruction needs to appear
|
|
|
|
// in the first 16-bit word (lowest address) for efficient hardware
|
|
|
|
// decode so that it knows if the instruction is 16-bit or 32-bit
|
|
|
|
// as early as possible. To do so, little-endian binaries keep 16-bit
|
|
|
|
// words in a big-endian order. That is why we have to swap these
|
|
|
|
// words to get a correct value.
|
2019-10-07 16:30:46 +08:00
|
|
|
uint32_t v = read32(loc);
|
2017-09-12 21:08:24 +08:00
|
|
|
if (E == support::little)
|
|
|
|
return (v << 16) | (v >> 16);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2018-01-18 23:59:10 +08:00
|
|
|
static void writeValue(uint8_t *loc, uint64_t v, uint8_t bitsSize,
|
|
|
|
uint8_t shift) {
|
2019-10-07 16:30:46 +08:00
|
|
|
uint32_t instr = read32(loc);
|
2017-09-05 00:16:46 +08:00
|
|
|
uint32_t mask = 0xffffffff >> (32 - bitsSize);
|
|
|
|
uint32_t data = (instr & ~mask) | ((v >> shift) & mask);
|
2019-10-07 16:30:46 +08:00
|
|
|
write32(loc, data);
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
|
2017-09-12 21:08:24 +08:00
|
|
|
template <endianness E>
|
2018-01-29 22:00:51 +08:00
|
|
|
static void writeShuffleValue(uint8_t *loc, uint64_t v, uint8_t bitsSize,
|
|
|
|
uint8_t shift) {
|
2017-09-12 21:08:24 +08:00
|
|
|
// See comments in readShuffle for purpose of this code.
|
|
|
|
uint16_t *words = (uint16_t *)loc;
|
|
|
|
if (E == support::little)
|
|
|
|
std::swap(words[0], words[1]);
|
|
|
|
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, v, bitsSize, shift);
|
2017-09-12 21:08:24 +08:00
|
|
|
|
|
|
|
if (E == support::little)
|
|
|
|
std::swap(words[0], words[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <endianness E>
|
|
|
|
static void writeMicroRelocation16(uint8_t *loc, uint64_t v, uint8_t bitsSize,
|
|
|
|
uint8_t shift) {
|
2019-10-07 16:30:46 +08:00
|
|
|
uint16_t instr = read16(loc);
|
2017-09-12 21:08:24 +08:00
|
|
|
uint16_t mask = 0xffff >> (16 - bitsSize);
|
|
|
|
uint16_t data = (instr & ~mask) | ((v >> shift) & mask);
|
2019-10-07 16:30:46 +08:00
|
|
|
write16(loc, data);
|
2017-09-12 21:08:24 +08:00
|
|
|
}
|
|
|
|
|
2017-06-17 01:32:43 +08:00
|
|
|
template <class ELFT> void MIPS<ELFT>::writePltHeader(uint8_t *buf) const {
|
2017-10-02 22:56:41 +08:00
|
|
|
if (isMicroMips()) {
|
2018-09-26 03:26:58 +08:00
|
|
|
uint64_t gotPlt = in.gotPlt->getVA();
|
|
|
|
uint64_t plt = in.plt->getVA();
|
2017-09-12 21:08:24 +08:00
|
|
|
// Overwrite trap instructions written by Writer::writeTrapInstr.
|
|
|
|
memset(buf, 0, pltHeaderSize);
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2019-10-07 16:30:46 +08:00
|
|
|
write16(buf, isMipsR6() ? 0x7860 : 0x7980); // addiupc v1, (GOTPLT) - .
|
|
|
|
write16(buf + 4, 0xff23); // lw $25, 0($3)
|
|
|
|
write16(buf + 8, 0x0535); // subu16 $2, $2, $3
|
|
|
|
write16(buf + 10, 0x2525); // srl16 $2, $2, 2
|
|
|
|
write16(buf + 12, 0x3302); // addiu $24, $2, -2
|
|
|
|
write16(buf + 14, 0xfffe);
|
|
|
|
write16(buf + 16, 0x0dff); // move $15, $31
|
2017-10-02 22:56:41 +08:00
|
|
|
if (isMipsR6()) {
|
2019-10-07 16:30:46 +08:00
|
|
|
write16(buf + 18, 0x0f83); // move $28, $3
|
|
|
|
write16(buf + 20, 0x472b); // jalrc $25
|
|
|
|
write16(buf + 22, 0x0c00); // nop
|
2020-01-23 13:39:16 +08:00
|
|
|
relocateNoSym(buf, R_MICROMIPS_PC19_S2, gotPlt - plt);
|
2017-09-12 21:08:24 +08:00
|
|
|
} else {
|
2019-10-07 16:30:46 +08:00
|
|
|
write16(buf + 18, 0x45f9); // jalrc $25
|
|
|
|
write16(buf + 20, 0x0f83); // move $28, $3
|
|
|
|
write16(buf + 22, 0x0c00); // nop
|
2020-01-23 13:39:16 +08:00
|
|
|
relocateNoSym(buf, R_MICROMIPS_PC23_S2, gotPlt - plt);
|
2017-09-12 21:08:24 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:32:43 +08:00
|
|
|
if (config->mipsN32Abi) {
|
2019-10-07 16:30:46 +08:00
|
|
|
write32(buf, 0x3c0e0000); // lui $14, %hi(&GOTPLT[0])
|
|
|
|
write32(buf + 4, 0x8dd90000); // lw $25, %lo(&GOTPLT[0])($14)
|
|
|
|
write32(buf + 8, 0x25ce0000); // addiu $14, $14, %lo(&GOTPLT[0])
|
|
|
|
write32(buf + 12, 0x030ec023); // subu $24, $24, $14
|
|
|
|
write32(buf + 16, 0x03e07825); // move $15, $31
|
|
|
|
write32(buf + 20, 0x0018c082); // srl $24, $24, 2
|
2017-11-22 20:34:29 +08:00
|
|
|
} else if (ELFT::Is64Bits) {
|
2019-10-07 16:30:46 +08:00
|
|
|
write32(buf, 0x3c0e0000); // lui $14, %hi(&GOTPLT[0])
|
|
|
|
write32(buf + 4, 0xddd90000); // ld $25, %lo(&GOTPLT[0])($14)
|
|
|
|
write32(buf + 8, 0x25ce0000); // addiu $14, $14, %lo(&GOTPLT[0])
|
|
|
|
write32(buf + 12, 0x030ec023); // subu $24, $24, $14
|
|
|
|
write32(buf + 16, 0x03e07825); // move $15, $31
|
|
|
|
write32(buf + 20, 0x0018c0c2); // srl $24, $24, 3
|
2017-06-17 01:32:43 +08:00
|
|
|
} else {
|
2019-10-07 16:30:46 +08:00
|
|
|
write32(buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
|
|
|
|
write32(buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
|
|
|
|
write32(buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
|
|
|
|
write32(buf + 12, 0x031cc023); // subu $24, $24, $28
|
|
|
|
write32(buf + 16, 0x03e07825); // move $15, $31
|
|
|
|
write32(buf + 20, 0x0018c082); // srl $24, $24, 2
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
|
2018-02-21 07:49:17 +08:00
|
|
|
uint32_t jalrInst = config->zHazardplt ? 0x0320fc09 : 0x0320f809;
|
2019-10-07 16:30:46 +08:00
|
|
|
write32(buf + 24, jalrInst); // jalr.hb $25 or jalr $25
|
|
|
|
write32(buf + 28, 0x2718fffe); // subu $24, $24, 2
|
2017-06-17 01:32:43 +08:00
|
|
|
|
2018-09-26 03:26:58 +08:00
|
|
|
uint64_t gotPlt = in.gotPlt->getVA();
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(buf, gotPlt + 0x8000, 16, 16);
|
|
|
|
writeValue(buf + 4, gotPlt, 16, 0);
|
|
|
|
writeValue(buf + 8, gotPlt, 16, 0);
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2019-12-18 05:43:04 +08:00
|
|
|
void MIPS<ELFT>::writePlt(uint8_t *buf, const Symbol &sym,
|
|
|
|
uint64_t pltEntryAddr) const {
|
|
|
|
uint64_t gotPltEntryAddr = sym.getGotPltVA();
|
2017-10-02 22:56:41 +08:00
|
|
|
if (isMicroMips()) {
|
2017-09-12 21:08:24 +08:00
|
|
|
// Overwrite trap instructions written by Writer::writeTrapInstr.
|
|
|
|
memset(buf, 0, pltEntrySize);
|
|
|
|
|
2017-10-02 22:56:41 +08:00
|
|
|
if (isMipsR6()) {
|
2019-10-07 16:30:46 +08:00
|
|
|
write16(buf, 0x7840); // addiupc $2, (GOTPLT) - .
|
|
|
|
write16(buf + 4, 0xff22); // lw $25, 0($2)
|
|
|
|
write16(buf + 8, 0x0f02); // move $24, $2
|
|
|
|
write16(buf + 10, 0x4723); // jrc $25 / jr16 $25
|
2020-01-23 13:39:16 +08:00
|
|
|
relocateNoSym(buf, R_MICROMIPS_PC19_S2, gotPltEntryAddr - pltEntryAddr);
|
2017-09-12 21:08:24 +08:00
|
|
|
} else {
|
2019-10-07 16:30:46 +08:00
|
|
|
write16(buf, 0x7900); // addiupc $2, (GOTPLT) - .
|
|
|
|
write16(buf + 4, 0xff22); // lw $25, 0($2)
|
|
|
|
write16(buf + 8, 0x4599); // jrc $25 / jr16 $25
|
|
|
|
write16(buf + 10, 0x0f02); // move $24, $2
|
2020-01-23 13:39:16 +08:00
|
|
|
relocateNoSym(buf, R_MICROMIPS_PC23_S2, gotPltEntryAddr - pltEntryAddr);
|
2017-09-12 21:08:24 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-08 04:26:23 +08:00
|
|
|
uint32_t loadInst = ELFT::Is64Bits ? 0xddf90000 : 0x8df90000;
|
2018-02-22 04:01:43 +08:00
|
|
|
uint32_t jrInst = isMipsR6() ? (config->zHazardplt ? 0x03200409 : 0x03200009)
|
|
|
|
: (config->zHazardplt ? 0x03200408 : 0x03200008);
|
2019-05-08 04:26:23 +08:00
|
|
|
uint32_t addInst = ELFT::Is64Bits ? 0x65f80000 : 0x25f80000;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2019-10-07 16:30:46 +08:00
|
|
|
write32(buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
|
|
|
|
write32(buf + 4, loadInst); // l[wd] $25, %lo(.got.plt entry)($15)
|
|
|
|
write32(buf + 8, jrInst); // jr $25 / jr.hb $25
|
|
|
|
write32(buf + 12, addInst); // [d]addiu $24, $15, %lo(.got.plt entry)
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(buf, gotPltEntryAddr + 0x8000, 16, 16);
|
|
|
|
writeValue(buf + 4, gotPltEntryAddr, 16, 0);
|
|
|
|
writeValue(buf + 12, gotPltEntryAddr, 16, 0);
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2017-10-12 06:49:24 +08:00
|
|
|
bool MIPS<ELFT>::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-06-17 01:32:43 +08:00
|
|
|
// Any MIPS PIC code function is invoked with its address in register $t9.
|
|
|
|
// So if we have a branch instruction from non-PIC code to the PIC one
|
|
|
|
// we cannot make the jump directly and need to create a small stubs
|
|
|
|
// to save the target function address.
|
|
|
|
// See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
|
2019-02-19 19:11:12 +08:00
|
|
|
if (type != R_MIPS_26 && type != R_MIPS_PC26_S2 &&
|
|
|
|
type != R_MICROMIPS_26_S1 && type != R_MICROMIPS_PC26_S1)
|
2017-06-17 01:32:43 +08:00
|
|
|
return false;
|
2019-04-06 04:16:26 +08:00
|
|
|
auto *f = dyn_cast_or_null<ObjFile<ELFT>>(file);
|
2017-06-17 01:32:43 +08:00
|
|
|
if (!f)
|
|
|
|
return false;
|
|
|
|
// If current file has PIC code, LA25 stub is not required.
|
2020-09-09 22:03:53 +08:00
|
|
|
if (f->getObj().getHeader().e_flags & EF_MIPS_PIC)
|
2017-06-17 01:32:43 +08:00
|
|
|
return false;
|
2017-11-06 12:35:31 +08:00
|
|
|
auto *d = dyn_cast<Defined>(&s);
|
2017-06-17 01:32:43 +08:00
|
|
|
// LA25 is required if target file has PIC code
|
|
|
|
// or target symbol is a PIC symbol.
|
2017-11-07 08:04:22 +08:00
|
|
|
return d && isMipsPIC<ELFT>(d);
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2017-10-12 06:49:24 +08:00
|
|
|
int64_t MIPS<ELFT>::getImplicitAddend(const uint8_t *buf, RelType type) const {
|
2017-06-17 01:32:43 +08:00
|
|
|
const endianness e = ELFT::TargetEndianness;
|
|
|
|
switch (type) {
|
|
|
|
case R_MIPS_32:
|
2021-07-09 17:05:18 +08:00
|
|
|
case R_MIPS_REL32:
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_GPREL32:
|
|
|
|
case R_MIPS_TLS_DTPREL32:
|
2021-07-09 17:05:18 +08:00
|
|
|
case R_MIPS_TLS_DTPMOD32:
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_TLS_TPREL32:
|
2019-10-07 16:30:46 +08:00
|
|
|
return SignExtend64<32>(read32(buf));
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_26:
|
|
|
|
// FIXME (simon): If the relocation target symbol is not a PLT entry
|
|
|
|
// we should use another expression for calculation:
|
|
|
|
// ((A << 2) | (P & 0xf0000000)) >> 2
|
2019-10-07 16:30:46 +08:00
|
|
|
return SignExtend64<28>(read32(buf) << 2);
|
2021-07-09 17:05:18 +08:00
|
|
|
case R_MIPS_CALL_HI16:
|
2017-08-25 05:56:58 +08:00
|
|
|
case R_MIPS_GOT16:
|
2021-07-09 17:05:18 +08:00
|
|
|
case R_MIPS_GOT_HI16:
|
2017-08-25 05:56:58 +08:00
|
|
|
case R_MIPS_HI16:
|
|
|
|
case R_MIPS_PCHI16:
|
2019-10-07 16:30:46 +08:00
|
|
|
return SignExtend64<16>(read32(buf)) << 16;
|
2021-07-09 17:05:18 +08:00
|
|
|
case R_MIPS_CALL16:
|
|
|
|
case R_MIPS_CALL_LO16:
|
|
|
|
case R_MIPS_GOT_LO16:
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_GPREL16:
|
|
|
|
case R_MIPS_LO16:
|
|
|
|
case R_MIPS_PCLO16:
|
|
|
|
case R_MIPS_TLS_DTPREL_HI16:
|
|
|
|
case R_MIPS_TLS_DTPREL_LO16:
|
2021-07-09 17:05:18 +08:00
|
|
|
case R_MIPS_TLS_GD:
|
|
|
|
case R_MIPS_TLS_GOTTPREL:
|
|
|
|
case R_MIPS_TLS_LDM:
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_TLS_TPREL_HI16:
|
|
|
|
case R_MIPS_TLS_TPREL_LO16:
|
2019-10-07 16:30:46 +08:00
|
|
|
return SignExtend64<16>(read32(buf));
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_GOT16:
|
|
|
|
case R_MICROMIPS_HI16:
|
|
|
|
return SignExtend64<16>(readShuffle<e>(buf)) << 16;
|
2021-07-09 17:05:18 +08:00
|
|
|
case R_MICROMIPS_CALL16:
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_GPREL16:
|
|
|
|
case R_MICROMIPS_LO16:
|
|
|
|
case R_MICROMIPS_TLS_DTPREL_HI16:
|
|
|
|
case R_MICROMIPS_TLS_DTPREL_LO16:
|
2021-07-09 17:05:18 +08:00
|
|
|
case R_MICROMIPS_TLS_GD:
|
|
|
|
case R_MICROMIPS_TLS_GOTTPREL:
|
|
|
|
case R_MICROMIPS_TLS_LDM:
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_TLS_TPREL_HI16:
|
|
|
|
case R_MICROMIPS_TLS_TPREL_LO16:
|
|
|
|
return SignExtend64<16>(readShuffle<e>(buf));
|
|
|
|
case R_MICROMIPS_GPREL7_S2:
|
|
|
|
return SignExtend64<9>(readShuffle<e>(buf) << 2);
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_PC16:
|
2019-10-07 16:30:46 +08:00
|
|
|
return SignExtend64<18>(read32(buf) << 2);
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_PC19_S2:
|
2019-10-07 16:30:46 +08:00
|
|
|
return SignExtend64<21>(read32(buf) << 2);
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_PC21_S2:
|
2019-10-07 16:30:46 +08:00
|
|
|
return SignExtend64<23>(read32(buf) << 2);
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_PC26_S2:
|
2019-10-07 16:30:46 +08:00
|
|
|
return SignExtend64<28>(read32(buf) << 2);
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_PC32:
|
2019-10-07 16:30:46 +08:00
|
|
|
return SignExtend64<32>(read32(buf));
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_26_S1:
|
|
|
|
return SignExtend64<27>(readShuffle<e>(buf) << 1);
|
|
|
|
case R_MICROMIPS_PC7_S1:
|
2019-10-07 16:30:46 +08:00
|
|
|
return SignExtend64<8>(read16(buf) << 1);
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_PC10_S1:
|
2019-10-07 16:30:46 +08:00
|
|
|
return SignExtend64<11>(read16(buf) << 1);
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_PC16_S1:
|
|
|
|
return SignExtend64<17>(readShuffle<e>(buf) << 1);
|
|
|
|
case R_MICROMIPS_PC18_S3:
|
|
|
|
return SignExtend64<21>(readShuffle<e>(buf) << 3);
|
|
|
|
case R_MICROMIPS_PC19_S2:
|
|
|
|
return SignExtend64<21>(readShuffle<e>(buf) << 2);
|
|
|
|
case R_MICROMIPS_PC21_S1:
|
|
|
|
return SignExtend64<22>(readShuffle<e>(buf) << 1);
|
|
|
|
case R_MICROMIPS_PC23_S2:
|
|
|
|
return SignExtend64<25>(readShuffle<e>(buf) << 2);
|
|
|
|
case R_MICROMIPS_PC26_S1:
|
|
|
|
return SignExtend64<27>(readShuffle<e>(buf) << 1);
|
2021-07-09 17:05:18 +08:00
|
|
|
case R_MIPS_64:
|
|
|
|
case R_MIPS_TLS_DTPMOD64:
|
|
|
|
case R_MIPS_TLS_DTPREL64:
|
|
|
|
case R_MIPS_TLS_TPREL64:
|
|
|
|
case (R_MIPS_64 << 8) | R_MIPS_REL32:
|
|
|
|
return read64(buf);
|
|
|
|
case R_MIPS_COPY:
|
|
|
|
return config->is64 ? read64(buf) : read32(buf);
|
|
|
|
case R_MIPS_NONE:
|
|
|
|
case R_MIPS_JUMP_SLOT:
|
|
|
|
case R_MIPS_JALR:
|
|
|
|
// These relocations are defined as not having an implicit addend.
|
|
|
|
return 0;
|
2017-10-12 11:14:06 +08:00
|
|
|
default:
|
2021-07-09 17:05:18 +08:00
|
|
|
internalLinkerError(getErrorLocation(buf),
|
|
|
|
"cannot read addend for relocation " + toString(type));
|
2017-10-12 11:14:06 +08:00
|
|
|
return 0;
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::pair<uint32_t, uint64_t>
|
2017-10-12 06:49:24 +08:00
|
|
|
calculateMipsRelChain(uint8_t *loc, RelType type, uint64_t val) {
|
2017-06-17 01:32:43 +08:00
|
|
|
// MIPS N64 ABI packs multiple relocations into the single relocation
|
|
|
|
// record. In general, all up to three relocations can have arbitrary
|
|
|
|
// types. In fact, Clang and GCC uses only a few combinations. For now,
|
|
|
|
// we support two of them. That is allow to pass at least all LLVM
|
|
|
|
// test suite cases.
|
|
|
|
// <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16
|
|
|
|
// <any relocation> / R_MIPS_64 / R_MIPS_NONE
|
|
|
|
// The first relocation is a 'real' relocation which is calculated
|
|
|
|
// using the corresponding symbol's value. The second and the third
|
|
|
|
// relocations used to modify result of the first one: extend it to
|
|
|
|
// 64-bit, extract high or low part etc. For details, see part 2.9 Relocation
|
|
|
|
// at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf
|
2017-10-12 06:49:24 +08:00
|
|
|
RelType type2 = (type >> 8) & 0xff;
|
|
|
|
RelType type3 = (type >> 16) & 0xff;
|
2017-06-17 01:32:43 +08:00
|
|
|
if (type2 == R_MIPS_NONE && type3 == R_MIPS_NONE)
|
|
|
|
return std::make_pair(type, val);
|
|
|
|
if (type2 == R_MIPS_64 && type3 == R_MIPS_NONE)
|
|
|
|
return std::make_pair(type2, val);
|
|
|
|
if (type2 == R_MIPS_SUB && (type3 == R_MIPS_HI16 || type3 == R_MIPS_LO16))
|
|
|
|
return std::make_pair(type3, -val);
|
|
|
|
error(getErrorLocation(loc) + "unsupported relocations combination " +
|
|
|
|
Twine(type));
|
|
|
|
return std::make_pair(type & 0xff, val);
|
|
|
|
}
|
|
|
|
|
2019-02-19 18:36:58 +08:00
|
|
|
static bool isBranchReloc(RelType type) {
|
|
|
|
return type == R_MIPS_26 || type == R_MIPS_PC26_S2 ||
|
|
|
|
type == R_MIPS_PC21_S2 || type == R_MIPS_PC16;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isMicroBranchReloc(RelType type) {
|
|
|
|
return type == R_MICROMIPS_26_S1 || type == R_MICROMIPS_PC16_S1 ||
|
|
|
|
type == R_MICROMIPS_PC10_S1 || type == R_MICROMIPS_PC7_S1;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
static uint64_t fixupCrossModeJump(uint8_t *loc, RelType type, uint64_t val) {
|
|
|
|
// Here we need to detect jump/branch from regular MIPS code
|
|
|
|
// to a microMIPS target and vice versa. In that cases jump
|
|
|
|
// instructions need to be replaced by their "cross-mode"
|
|
|
|
// equivalents.
|
|
|
|
const endianness e = ELFT::TargetEndianness;
|
|
|
|
bool isMicroTgt = val & 0x1;
|
|
|
|
bool isCrossJump = (isMicroTgt && isBranchReloc(type)) ||
|
|
|
|
(!isMicroTgt && isMicroBranchReloc(type));
|
|
|
|
if (!isCrossJump)
|
|
|
|
return val;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2019-02-19 18:36:58 +08:00
|
|
|
switch (type) {
|
|
|
|
case R_MIPS_26: {
|
2019-10-07 16:30:46 +08:00
|
|
|
uint32_t inst = read32(loc) >> 26;
|
2019-02-19 18:36:58 +08:00
|
|
|
if (inst == 0x3 || inst == 0x1d) { // JAL or JALX
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, 0x1d << 26, 32, 0);
|
2019-02-19 18:36:58 +08:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R_MICROMIPS_26_S1: {
|
|
|
|
uint32_t inst = readShuffle<e>(loc) >> 26;
|
|
|
|
if (inst == 0x3d || inst == 0x3c) { // JAL32 or JALX32
|
|
|
|
val >>= 1;
|
|
|
|
writeShuffleValue<e>(loc, 0x3c << 26, 32, 0);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R_MIPS_PC26_S2:
|
|
|
|
case R_MIPS_PC21_S2:
|
|
|
|
case R_MIPS_PC16:
|
|
|
|
case R_MICROMIPS_PC16_S1:
|
|
|
|
case R_MICROMIPS_PC10_S1:
|
|
|
|
case R_MICROMIPS_PC7_S1:
|
|
|
|
// FIXME (simon): Support valid branch relocations.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unexpected jump/branch relocation");
|
|
|
|
}
|
|
|
|
|
|
|
|
error(getErrorLocation(loc) +
|
|
|
|
"unsupported jump/branch instruction between ISA modes referenced by " +
|
|
|
|
toString(type) + " relocation");
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:32:43 +08:00
|
|
|
template <class ELFT>
|
2020-01-23 13:39:16 +08:00
|
|
|
void MIPS<ELFT>::relocate(uint8_t *loc, const Relocation &rel,
|
|
|
|
uint64_t val) const {
|
2017-06-17 01:32:43 +08:00
|
|
|
const endianness e = ELFT::TargetEndianness;
|
2020-01-23 13:39:16 +08:00
|
|
|
RelType type = rel.type;
|
2017-10-12 11:14:06 +08:00
|
|
|
|
2018-01-18 23:59:05 +08:00
|
|
|
if (ELFT::Is64Bits || config->mipsN32Abi)
|
|
|
|
std::tie(type, val) = calculateMipsRelChain(loc, type, val);
|
|
|
|
|
2019-02-19 18:36:58 +08:00
|
|
|
// Detect cross-mode jump/branch and fix instruction.
|
|
|
|
val = fixupCrossModeJump<ELFT>(loc, type, val);
|
|
|
|
|
2017-06-17 01:32:43 +08:00
|
|
|
// Thread pointer and DRP offsets from the start of TLS data area.
|
|
|
|
// https://www.linux-mips.org/wiki/NPTL
|
|
|
|
if (type == R_MIPS_TLS_DTPREL_HI16 || type == R_MIPS_TLS_DTPREL_LO16 ||
|
2017-09-12 21:08:24 +08:00
|
|
|
type == R_MIPS_TLS_DTPREL32 || type == R_MIPS_TLS_DTPREL64 ||
|
|
|
|
type == R_MICROMIPS_TLS_DTPREL_HI16 ||
|
2017-10-12 11:14:06 +08:00
|
|
|
type == R_MICROMIPS_TLS_DTPREL_LO16) {
|
2017-06-17 01:32:43 +08:00
|
|
|
val -= 0x8000;
|
2017-10-12 11:14:06 +08:00
|
|
|
}
|
|
|
|
|
2017-06-17 01:32:43 +08:00
|
|
|
switch (type) {
|
|
|
|
case R_MIPS_32:
|
|
|
|
case R_MIPS_GPREL32:
|
|
|
|
case R_MIPS_TLS_DTPREL32:
|
|
|
|
case R_MIPS_TLS_TPREL32:
|
2019-10-07 16:30:46 +08:00
|
|
|
write32(loc, val);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_MIPS_64:
|
|
|
|
case R_MIPS_TLS_DTPREL64:
|
|
|
|
case R_MIPS_TLS_TPREL64:
|
2019-10-07 16:30:46 +08:00
|
|
|
write64(loc, val);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_MIPS_26:
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, val, 26, 2);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_MIPS_GOT16:
|
|
|
|
// The R_MIPS_GOT16 relocation's value in "relocatable" linking mode
|
|
|
|
// is updated addend (not a GOT index). In that case write high 16 bits
|
|
|
|
// to store a correct addend value.
|
2017-09-05 00:16:41 +08:00
|
|
|
if (config->relocatable) {
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, val + 0x8000, 16, 16);
|
2017-09-05 00:16:41 +08:00
|
|
|
} else {
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, val, 16, rel);
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, val, 16, 0);
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
break;
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_GOT16:
|
|
|
|
if (config->relocatable) {
|
2018-01-29 22:00:51 +08:00
|
|
|
writeShuffleValue<e>(loc, val + 0x8000, 16, 16);
|
2017-09-12 21:08:24 +08:00
|
|
|
} else {
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, val, 16, rel);
|
2018-01-29 22:00:51 +08:00
|
|
|
writeShuffleValue<e>(loc, val, 16, 0);
|
2017-09-12 21:08:24 +08:00
|
|
|
}
|
|
|
|
break;
|
2017-10-07 00:15:59 +08:00
|
|
|
case R_MIPS_CALL16:
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_GOT_DISP:
|
|
|
|
case R_MIPS_GOT_PAGE:
|
|
|
|
case R_MIPS_GPREL16:
|
|
|
|
case R_MIPS_TLS_GD:
|
2017-10-07 00:15:59 +08:00
|
|
|
case R_MIPS_TLS_GOTTPREL:
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_TLS_LDM:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, val, 16, rel);
|
2017-06-17 01:32:43 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case R_MIPS_CALL_LO16:
|
|
|
|
case R_MIPS_GOT_LO16:
|
|
|
|
case R_MIPS_GOT_OFST:
|
|
|
|
case R_MIPS_LO16:
|
|
|
|
case R_MIPS_PCLO16:
|
|
|
|
case R_MIPS_TLS_DTPREL_LO16:
|
|
|
|
case R_MIPS_TLS_TPREL_LO16:
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, val, 16, 0);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_GPREL16:
|
|
|
|
case R_MICROMIPS_TLS_GD:
|
|
|
|
case R_MICROMIPS_TLS_LDM:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, val, 16, rel);
|
2018-01-29 22:00:51 +08:00
|
|
|
writeShuffleValue<e>(loc, val, 16, 0);
|
2017-09-12 21:08:24 +08:00
|
|
|
break;
|
|
|
|
case R_MICROMIPS_CALL16:
|
|
|
|
case R_MICROMIPS_CALL_LO16:
|
|
|
|
case R_MICROMIPS_LO16:
|
|
|
|
case R_MICROMIPS_TLS_DTPREL_LO16:
|
|
|
|
case R_MICROMIPS_TLS_GOTTPREL:
|
|
|
|
case R_MICROMIPS_TLS_TPREL_LO16:
|
2018-01-29 22:00:51 +08:00
|
|
|
writeShuffleValue<e>(loc, val, 16, 0);
|
2017-09-12 21:08:24 +08:00
|
|
|
break;
|
|
|
|
case R_MICROMIPS_GPREL7_S2:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, val, 7, rel);
|
2018-01-29 22:00:51 +08:00
|
|
|
writeShuffleValue<e>(loc, val, 7, 2);
|
2017-09-12 21:08:24 +08:00
|
|
|
break;
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_CALL_HI16:
|
|
|
|
case R_MIPS_GOT_HI16:
|
|
|
|
case R_MIPS_HI16:
|
|
|
|
case R_MIPS_PCHI16:
|
|
|
|
case R_MIPS_TLS_DTPREL_HI16:
|
|
|
|
case R_MIPS_TLS_TPREL_HI16:
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, val + 0x8000, 16, 16);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_CALL_HI16:
|
|
|
|
case R_MICROMIPS_GOT_HI16:
|
|
|
|
case R_MICROMIPS_HI16:
|
|
|
|
case R_MICROMIPS_TLS_DTPREL_HI16:
|
|
|
|
case R_MICROMIPS_TLS_TPREL_HI16:
|
2018-01-29 22:00:51 +08:00
|
|
|
writeShuffleValue<e>(loc, val + 0x8000, 16, 16);
|
2017-09-12 21:08:24 +08:00
|
|
|
break;
|
2017-06-17 01:32:43 +08:00
|
|
|
case R_MIPS_HIGHER:
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, val + 0x80008000, 16, 32);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_MIPS_HIGHEST:
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, val + 0x800080008000, 16, 48);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_MIPS_JALR:
|
2019-09-26 17:13:20 +08:00
|
|
|
val -= 4;
|
|
|
|
// Replace jalr/jr instructions by bal/b if the target
|
|
|
|
// offset fits into the 18-bit range.
|
|
|
|
if (isInt<18>(val)) {
|
2019-10-07 16:30:46 +08:00
|
|
|
switch (read32(loc)) {
|
2019-09-26 17:13:20 +08:00
|
|
|
case 0x0320f809: // jalr $25 => bal sym
|
2019-10-07 16:30:46 +08:00
|
|
|
write32(loc, 0x04110000 | ((val >> 2) & 0xffff));
|
2019-09-26 17:13:20 +08:00
|
|
|
break;
|
|
|
|
case 0x03200008: // jr $25 => b sym
|
2019-10-07 16:30:46 +08:00
|
|
|
write32(loc, 0x10000000 | ((val >> 2) & 0xffff));
|
2019-09-26 17:13:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_JALR:
|
2017-06-17 01:32:43 +08:00
|
|
|
// Ignore this optimization relocation for now
|
|
|
|
break;
|
|
|
|
case R_MIPS_PC16:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkAlignment(loc, val, 4, rel);
|
|
|
|
checkInt(loc, val, 18, rel);
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, val, 16, 2);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_MIPS_PC19_S2:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkAlignment(loc, val, 4, rel);
|
|
|
|
checkInt(loc, val, 21, rel);
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, val, 19, 2);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_MIPS_PC21_S2:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkAlignment(loc, val, 4, rel);
|
|
|
|
checkInt(loc, val, 23, rel);
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, val, 21, 2);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_MIPS_PC26_S2:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkAlignment(loc, val, 4, rel);
|
|
|
|
checkInt(loc, val, 28, rel);
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, val, 26, 2);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_MIPS_PC32:
|
2019-10-07 16:52:07 +08:00
|
|
|
writeValue(loc, val, 32, 0);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_26_S1:
|
|
|
|
case R_MICROMIPS_PC26_S1:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, val, 27, rel);
|
2018-01-29 22:00:51 +08:00
|
|
|
writeShuffleValue<e>(loc, val, 26, 1);
|
2017-09-12 21:08:24 +08:00
|
|
|
break;
|
|
|
|
case R_MICROMIPS_PC7_S1:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, val, 8, rel);
|
2017-09-12 21:08:24 +08:00
|
|
|
writeMicroRelocation16<e>(loc, val, 7, 1);
|
|
|
|
break;
|
|
|
|
case R_MICROMIPS_PC10_S1:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, val, 11, rel);
|
2017-09-12 21:08:24 +08:00
|
|
|
writeMicroRelocation16<e>(loc, val, 10, 1);
|
|
|
|
break;
|
|
|
|
case R_MICROMIPS_PC16_S1:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, val, 17, rel);
|
2018-01-29 22:00:51 +08:00
|
|
|
writeShuffleValue<e>(loc, val, 16, 1);
|
2017-09-12 21:08:24 +08:00
|
|
|
break;
|
|
|
|
case R_MICROMIPS_PC18_S3:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, val, 21, rel);
|
2018-01-29 22:00:51 +08:00
|
|
|
writeShuffleValue<e>(loc, val, 18, 3);
|
2017-09-12 21:08:24 +08:00
|
|
|
break;
|
|
|
|
case R_MICROMIPS_PC19_S2:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, val, 21, rel);
|
2018-01-29 22:00:51 +08:00
|
|
|
writeShuffleValue<e>(loc, val, 19, 2);
|
2017-09-12 21:08:24 +08:00
|
|
|
break;
|
|
|
|
case R_MICROMIPS_PC21_S1:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, val, 22, rel);
|
2018-01-29 22:00:51 +08:00
|
|
|
writeShuffleValue<e>(loc, val, 21, 1);
|
2017-09-12 21:08:24 +08:00
|
|
|
break;
|
|
|
|
case R_MICROMIPS_PC23_S2:
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, val, 25, rel);
|
2018-01-29 22:00:51 +08:00
|
|
|
writeShuffleValue<e>(loc, val, 23, 2);
|
2017-09-12 21:08:24 +08:00
|
|
|
break;
|
2017-06-17 01:32:43 +08:00
|
|
|
default:
|
2019-02-15 02:02:20 +08:00
|
|
|
llvm_unreachable("unknown relocation");
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 06:49:24 +08:00
|
|
|
template <class ELFT> bool MIPS<ELFT>::usesOnlyLowPageBits(RelType type) const {
|
2017-09-12 21:08:24 +08:00
|
|
|
return type == R_MIPS_LO16 || type == R_MIPS_GOT_OFST ||
|
2018-07-06 13:50:41 +08:00
|
|
|
type == R_MICROMIPS_LO16;
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
|
2017-11-07 08:04:22 +08:00
|
|
|
// Return true if the symbol is a PIC function.
|
2020-05-15 13:18:58 +08:00
|
|
|
template <class ELFT> bool elf::isMipsPIC(const Defined *sym) {
|
2018-05-05 04:48:53 +08:00
|
|
|
if (!sym->isFunc())
|
2017-11-07 08:04:22 +08:00
|
|
|
return false;
|
|
|
|
|
2018-05-05 04:48:53 +08:00
|
|
|
if (sym->stOther & STO_MIPS_PIC)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!sym->section)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ObjFile<ELFT> *file =
|
|
|
|
cast<InputSectionBase>(sym->section)->template getFile<ELFT>();
|
2018-05-04 01:33:10 +08:00
|
|
|
if (!file)
|
|
|
|
return false;
|
|
|
|
|
2020-09-09 22:03:53 +08:00
|
|
|
return file->getObj().getHeader().e_flags & EF_MIPS_PIC;
|
2017-11-07 08:04:22 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
template <class ELFT> TargetInfo *elf::getMipsTargetInfo() {
|
2017-06-17 04:15:03 +08:00
|
|
|
static MIPS<ELFT> target;
|
|
|
|
return ⌖
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
template TargetInfo *elf::getMipsTargetInfo<ELF32LE>();
|
|
|
|
template TargetInfo *elf::getMipsTargetInfo<ELF32BE>();
|
|
|
|
template TargetInfo *elf::getMipsTargetInfo<ELF64LE>();
|
|
|
|
template TargetInfo *elf::getMipsTargetInfo<ELF64BE>();
|
2017-11-07 08:04:22 +08:00
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
template bool elf::isMipsPIC<ELF32LE>(const Defined *);
|
|
|
|
template bool elf::isMipsPIC<ELF32BE>(const Defined *);
|
|
|
|
template bool elf::isMipsPIC<ELF64LE>(const Defined *);
|
|
|
|
template bool elf::isMipsPIC<ELF64BE>(const Defined *);
|