Path: blob/main/contrib/llvm-project/lld/ELF/Arch/Mips.cpp
34878 views
//===- MIPS.cpp -----------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "InputFiles.h"9#include "OutputSections.h"10#include "Symbols.h"11#include "SyntheticSections.h"12#include "Target.h"13#include "lld/Common/ErrorHandler.h"14#include "llvm/BinaryFormat/ELF.h"1516using namespace llvm;17using namespace llvm::object;18using namespace llvm::ELF;19using namespace lld;20using namespace lld::elf;2122namespace {23template <class ELFT> class MIPS final : public TargetInfo {24public:25MIPS();26uint32_t calcEFlags() const override;27RelExpr getRelExpr(RelType type, const Symbol &s,28const uint8_t *loc) const override;29int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;30RelType getDynRel(RelType type) const override;31void writeGotPlt(uint8_t *buf, const Symbol &s) const override;32void writePltHeader(uint8_t *buf) const override;33void writePlt(uint8_t *buf, const Symbol &sym,34uint64_t pltEntryAddr) const override;35bool needsThunk(RelExpr expr, RelType type, const InputFile *file,36uint64_t branchAddr, const Symbol &s,37int64_t a) const override;38void relocate(uint8_t *loc, const Relocation &rel,39uint64_t val) const override;40bool usesOnlyLowPageBits(RelType type) const override;41};42} // namespace4344template <class ELFT> MIPS<ELFT>::MIPS() {45gotPltHeaderEntriesNum = 2;46defaultMaxPageSize = 65536;47pltEntrySize = 16;48pltHeaderSize = 32;49copyRel = R_MIPS_COPY;50pltRel = R_MIPS_JUMP_SLOT;51needsThunks = true;5253// Set `sigrie 1` as a trap instruction.54write32(trapInstr.data(), 0x04170001);5556if (ELFT::Is64Bits) {57relativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;58symbolicRel = R_MIPS_64;59tlsGotRel = R_MIPS_TLS_TPREL64;60tlsModuleIndexRel = R_MIPS_TLS_DTPMOD64;61tlsOffsetRel = R_MIPS_TLS_DTPREL64;62} else {63relativeRel = R_MIPS_REL32;64symbolicRel = R_MIPS_32;65tlsGotRel = R_MIPS_TLS_TPREL32;66tlsModuleIndexRel = R_MIPS_TLS_DTPMOD32;67tlsOffsetRel = R_MIPS_TLS_DTPREL32;68}69}7071template <class ELFT> uint32_t MIPS<ELFT>::calcEFlags() const {72return calcMipsEFlags<ELFT>();73}7475template <class ELFT>76RelExpr MIPS<ELFT>::getRelExpr(RelType type, const Symbol &s,77const uint8_t *loc) const {78// See comment in the calculateMipsRelChain.79if (ELFT::Is64Bits || config->mipsN32Abi)80type &= 0xff;8182switch (type) {83case R_MIPS_JALR:84// Older versions of clang would erroneously emit this relocation not only85// against functions (loaded from the GOT) but also against data symbols86// (e.g. a table of function pointers). When we encounter this, ignore the87// relocation and emit a warning instead.88if (!s.isFunc() && s.type != STT_NOTYPE) {89warn(getErrorLocation(loc) +90"found R_MIPS_JALR relocation against non-function symbol " +91toString(s) + ". This is invalid and most likely a compiler bug.");92return R_NONE;93}9495// If the target symbol is not preemptible and is not microMIPS,96// it might be possible to replace jalr/jr instruction by bal/b.97// It depends on the target symbol's offset.98if (!s.isPreemptible && !(s.getVA() & 0x1))99return R_PC;100return R_NONE;101case R_MICROMIPS_JALR:102return R_NONE;103case R_MIPS_GPREL16:104case R_MIPS_GPREL32:105case R_MICROMIPS_GPREL16:106case R_MICROMIPS_GPREL7_S2:107return R_MIPS_GOTREL;108case R_MIPS_26:109case R_MICROMIPS_26_S1:110return R_PLT;111case R_MICROMIPS_PC26_S1:112return R_PLT_PC;113case R_MIPS_HI16:114case R_MIPS_LO16:115case R_MIPS_HIGHER:116case R_MIPS_HIGHEST:117case R_MICROMIPS_HI16:118case R_MICROMIPS_LO16:119// R_MIPS_HI16/R_MIPS_LO16 relocations against _gp_disp calculate120// offset between start of function and 'gp' value which by default121// equal to the start of .got section. In that case we consider these122// relocations as relative.123if (&s == ElfSym::mipsGpDisp)124return R_MIPS_GOT_GP_PC;125if (&s == ElfSym::mipsLocalGp)126return R_MIPS_GOT_GP;127[[fallthrough]];128case R_MIPS_32:129case R_MIPS_64:130case R_MIPS_GOT_OFST:131case R_MIPS_SUB:132return R_ABS;133case R_MIPS_TLS_DTPREL_HI16:134case R_MIPS_TLS_DTPREL_LO16:135case R_MIPS_TLS_DTPREL32:136case R_MIPS_TLS_DTPREL64:137case R_MICROMIPS_TLS_DTPREL_HI16:138case R_MICROMIPS_TLS_DTPREL_LO16:139return R_DTPREL;140case R_MIPS_TLS_TPREL_HI16:141case R_MIPS_TLS_TPREL_LO16:142case R_MIPS_TLS_TPREL32:143case R_MIPS_TLS_TPREL64:144case R_MICROMIPS_TLS_TPREL_HI16:145case R_MICROMIPS_TLS_TPREL_LO16:146return R_TPREL;147case R_MIPS_PC32:148case R_MIPS_PC16:149case R_MIPS_PC19_S2:150case R_MIPS_PC21_S2:151case R_MIPS_PC26_S2:152case R_MIPS_PCHI16:153case R_MIPS_PCLO16:154case R_MICROMIPS_PC7_S1:155case R_MICROMIPS_PC10_S1:156case R_MICROMIPS_PC16_S1:157case R_MICROMIPS_PC18_S3:158case R_MICROMIPS_PC19_S2:159case R_MICROMIPS_PC23_S2:160case R_MICROMIPS_PC21_S1:161return R_PC;162case R_MIPS_GOT16:163case R_MICROMIPS_GOT16:164if (s.isLocal())165return R_MIPS_GOT_LOCAL_PAGE;166[[fallthrough]];167case R_MIPS_CALL16:168case R_MIPS_GOT_DISP:169case R_MIPS_TLS_GOTTPREL:170case R_MICROMIPS_CALL16:171case R_MICROMIPS_TLS_GOTTPREL:172return R_MIPS_GOT_OFF;173case R_MIPS_CALL_HI16:174case R_MIPS_CALL_LO16:175case R_MIPS_GOT_HI16:176case R_MIPS_GOT_LO16:177case R_MICROMIPS_CALL_HI16:178case R_MICROMIPS_CALL_LO16:179case R_MICROMIPS_GOT_HI16:180case R_MICROMIPS_GOT_LO16:181return R_MIPS_GOT_OFF32;182case R_MIPS_GOT_PAGE:183return R_MIPS_GOT_LOCAL_PAGE;184case R_MIPS_TLS_GD:185case R_MICROMIPS_TLS_GD:186return R_MIPS_TLSGD;187case R_MIPS_TLS_LDM:188case R_MICROMIPS_TLS_LDM:189return R_MIPS_TLSLD;190case R_MIPS_NONE:191return R_NONE;192default:193error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +194") against symbol " + toString(s));195return R_NONE;196}197}198199template <class ELFT> RelType MIPS<ELFT>::getDynRel(RelType type) const {200if (type == symbolicRel)201return type;202return R_MIPS_NONE;203}204205template <class ELFT>206void MIPS<ELFT>::writeGotPlt(uint8_t *buf, const Symbol &) const {207uint64_t va = in.plt->getVA();208if (isMicroMips())209va |= 1;210write32(buf, va);211}212213template <endianness E> static uint32_t readShuffle(const uint8_t *loc) {214// The major opcode of a microMIPS instruction needs to appear215// in the first 16-bit word (lowest address) for efficient hardware216// decode so that it knows if the instruction is 16-bit or 32-bit217// as early as possible. To do so, little-endian binaries keep 16-bit218// words in a big-endian order. That is why we have to swap these219// words to get a correct value.220uint32_t v = read32(loc);221if (E == llvm::endianness::little)222return (v << 16) | (v >> 16);223return v;224}225226static void writeValue(uint8_t *loc, uint64_t v, uint8_t bitsSize,227uint8_t shift) {228uint32_t instr = read32(loc);229uint32_t mask = 0xffffffff >> (32 - bitsSize);230uint32_t data = (instr & ~mask) | ((v >> shift) & mask);231write32(loc, data);232}233234template <endianness E>235static void writeShuffleValue(uint8_t *loc, uint64_t v, uint8_t bitsSize,236uint8_t shift) {237// See comments in readShuffle for purpose of this code.238uint16_t *words = (uint16_t *)loc;239if (E == llvm::endianness::little)240std::swap(words[0], words[1]);241242writeValue(loc, v, bitsSize, shift);243244if (E == llvm::endianness::little)245std::swap(words[0], words[1]);246}247248template <endianness E>249static void writeMicroRelocation16(uint8_t *loc, uint64_t v, uint8_t bitsSize,250uint8_t shift) {251uint16_t instr = read16(loc);252uint16_t mask = 0xffff >> (16 - bitsSize);253uint16_t data = (instr & ~mask) | ((v >> shift) & mask);254write16(loc, data);255}256257template <class ELFT> void MIPS<ELFT>::writePltHeader(uint8_t *buf) const {258if (isMicroMips()) {259uint64_t gotPlt = in.gotPlt->getVA();260uint64_t plt = in.plt->getVA();261// Overwrite trap instructions written by Writer::writeTrapInstr.262memset(buf, 0, pltHeaderSize);263264write16(buf, isMipsR6() ? 0x7860 : 0x7980); // addiupc v1, (GOTPLT) - .265write16(buf + 4, 0xff23); // lw $25, 0($3)266write16(buf + 8, 0x0535); // subu16 $2, $2, $3267write16(buf + 10, 0x2525); // srl16 $2, $2, 2268write16(buf + 12, 0x3302); // addiu $24, $2, -2269write16(buf + 14, 0xfffe);270write16(buf + 16, 0x0dff); // move $15, $31271if (isMipsR6()) {272write16(buf + 18, 0x0f83); // move $28, $3273write16(buf + 20, 0x472b); // jalrc $25274write16(buf + 22, 0x0c00); // nop275relocateNoSym(buf, R_MICROMIPS_PC19_S2, gotPlt - plt);276} else {277write16(buf + 18, 0x45f9); // jalrc $25278write16(buf + 20, 0x0f83); // move $28, $3279write16(buf + 22, 0x0c00); // nop280relocateNoSym(buf, R_MICROMIPS_PC23_S2, gotPlt - plt);281}282return;283}284285if (config->mipsN32Abi) {286write32(buf, 0x3c0e0000); // lui $14, %hi(&GOTPLT[0])287write32(buf + 4, 0x8dd90000); // lw $25, %lo(&GOTPLT[0])($14)288write32(buf + 8, 0x25ce0000); // addiu $14, $14, %lo(&GOTPLT[0])289write32(buf + 12, 0x030ec023); // subu $24, $24, $14290write32(buf + 16, 0x03e07825); // move $15, $31291write32(buf + 20, 0x0018c082); // srl $24, $24, 2292} else if (ELFT::Is64Bits) {293write32(buf, 0x3c0e0000); // lui $14, %hi(&GOTPLT[0])294write32(buf + 4, 0xddd90000); // ld $25, %lo(&GOTPLT[0])($14)295write32(buf + 8, 0x25ce0000); // addiu $14, $14, %lo(&GOTPLT[0])296write32(buf + 12, 0x030ec023); // subu $24, $24, $14297write32(buf + 16, 0x03e07825); // move $15, $31298write32(buf + 20, 0x0018c0c2); // srl $24, $24, 3299} else {300write32(buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])301write32(buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)302write32(buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])303write32(buf + 12, 0x031cc023); // subu $24, $24, $28304write32(buf + 16, 0x03e07825); // move $15, $31305write32(buf + 20, 0x0018c082); // srl $24, $24, 2306}307308uint32_t jalrInst = config->zHazardplt ? 0x0320fc09 : 0x0320f809;309write32(buf + 24, jalrInst); // jalr.hb $25 or jalr $25310write32(buf + 28, 0x2718fffe); // subu $24, $24, 2311312uint64_t gotPlt = in.gotPlt->getVA();313writeValue(buf, gotPlt + 0x8000, 16, 16);314writeValue(buf + 4, gotPlt, 16, 0);315writeValue(buf + 8, gotPlt, 16, 0);316}317318template <class ELFT>319void MIPS<ELFT>::writePlt(uint8_t *buf, const Symbol &sym,320uint64_t pltEntryAddr) const {321uint64_t gotPltEntryAddr = sym.getGotPltVA();322if (isMicroMips()) {323// Overwrite trap instructions written by Writer::writeTrapInstr.324memset(buf, 0, pltEntrySize);325326if (isMipsR6()) {327write16(buf, 0x7840); // addiupc $2, (GOTPLT) - .328write16(buf + 4, 0xff22); // lw $25, 0($2)329write16(buf + 8, 0x0f02); // move $24, $2330write16(buf + 10, 0x4723); // jrc $25 / jr16 $25331relocateNoSym(buf, R_MICROMIPS_PC19_S2, gotPltEntryAddr - pltEntryAddr);332} else {333write16(buf, 0x7900); // addiupc $2, (GOTPLT) - .334write16(buf + 4, 0xff22); // lw $25, 0($2)335write16(buf + 8, 0x4599); // jrc $25 / jr16 $25336write16(buf + 10, 0x0f02); // move $24, $2337relocateNoSym(buf, R_MICROMIPS_PC23_S2, gotPltEntryAddr - pltEntryAddr);338}339return;340}341342uint32_t loadInst = ELFT::Is64Bits ? 0xddf90000 : 0x8df90000;343uint32_t jrInst = isMipsR6() ? (config->zHazardplt ? 0x03200409 : 0x03200009)344: (config->zHazardplt ? 0x03200408 : 0x03200008);345uint32_t addInst = ELFT::Is64Bits ? 0x65f80000 : 0x25f80000;346347write32(buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)348write32(buf + 4, loadInst); // l[wd] $25, %lo(.got.plt entry)($15)349write32(buf + 8, jrInst); // jr $25 / jr.hb $25350write32(buf + 12, addInst); // [d]addiu $24, $15, %lo(.got.plt entry)351writeValue(buf, gotPltEntryAddr + 0x8000, 16, 16);352writeValue(buf + 4, gotPltEntryAddr, 16, 0);353writeValue(buf + 12, gotPltEntryAddr, 16, 0);354}355356template <class ELFT>357bool MIPS<ELFT>::needsThunk(RelExpr expr, RelType type, const InputFile *file,358uint64_t branchAddr, const Symbol &s,359int64_t /*a*/) const {360// Any MIPS PIC code function is invoked with its address in register $t9.361// So if we have a branch instruction from non-PIC code to the PIC one362// we cannot make the jump directly and need to create a small stubs363// to save the target function address.364// See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf365if (type != R_MIPS_26 && type != R_MIPS_PC26_S2 &&366type != R_MICROMIPS_26_S1 && type != R_MICROMIPS_PC26_S1)367return false;368auto *f = dyn_cast_or_null<ObjFile<ELFT>>(file);369if (!f)370return false;371// If current file has PIC code, LA25 stub is not required.372if (f->getObj().getHeader().e_flags & EF_MIPS_PIC)373return false;374auto *d = dyn_cast<Defined>(&s);375// LA25 is required if target file has PIC code376// or target symbol is a PIC symbol.377return d && isMipsPIC<ELFT>(d);378}379380template <class ELFT>381int64_t MIPS<ELFT>::getImplicitAddend(const uint8_t *buf, RelType type) const {382const endianness e = ELFT::Endianness;383switch (type) {384case R_MIPS_32:385case R_MIPS_REL32:386case R_MIPS_GPREL32:387case R_MIPS_TLS_DTPREL32:388case R_MIPS_TLS_DTPMOD32:389case R_MIPS_TLS_TPREL32:390return SignExtend64<32>(read32(buf));391case R_MIPS_26:392// FIXME (simon): If the relocation target symbol is not a PLT entry393// we should use another expression for calculation:394// ((A << 2) | (P & 0xf0000000)) >> 2395return SignExtend64<28>(read32(buf) << 2);396case R_MIPS_CALL_HI16:397case R_MIPS_GOT16:398case R_MIPS_GOT_HI16:399case R_MIPS_HI16:400case R_MIPS_PCHI16:401return SignExtend64<16>(read32(buf)) << 16;402case R_MIPS_CALL16:403case R_MIPS_CALL_LO16:404case R_MIPS_GOT_LO16:405case R_MIPS_GPREL16:406case R_MIPS_LO16:407case R_MIPS_PCLO16:408case R_MIPS_TLS_DTPREL_HI16:409case R_MIPS_TLS_DTPREL_LO16:410case R_MIPS_TLS_GD:411case R_MIPS_TLS_GOTTPREL:412case R_MIPS_TLS_LDM:413case R_MIPS_TLS_TPREL_HI16:414case R_MIPS_TLS_TPREL_LO16:415return SignExtend64<16>(read32(buf));416case R_MICROMIPS_GOT16:417case R_MICROMIPS_HI16:418return SignExtend64<16>(readShuffle<e>(buf)) << 16;419case R_MICROMIPS_CALL16:420case R_MICROMIPS_GPREL16:421case R_MICROMIPS_LO16:422case R_MICROMIPS_TLS_DTPREL_HI16:423case R_MICROMIPS_TLS_DTPREL_LO16:424case R_MICROMIPS_TLS_GD:425case R_MICROMIPS_TLS_GOTTPREL:426case R_MICROMIPS_TLS_LDM:427case R_MICROMIPS_TLS_TPREL_HI16:428case R_MICROMIPS_TLS_TPREL_LO16:429return SignExtend64<16>(readShuffle<e>(buf));430case R_MICROMIPS_GPREL7_S2:431return SignExtend64<9>(readShuffle<e>(buf) << 2);432case R_MIPS_PC16:433return SignExtend64<18>(read32(buf) << 2);434case R_MIPS_PC19_S2:435return SignExtend64<21>(read32(buf) << 2);436case R_MIPS_PC21_S2:437return SignExtend64<23>(read32(buf) << 2);438case R_MIPS_PC26_S2:439return SignExtend64<28>(read32(buf) << 2);440case R_MIPS_PC32:441return SignExtend64<32>(read32(buf));442case R_MICROMIPS_26_S1:443return SignExtend64<27>(readShuffle<e>(buf) << 1);444case R_MICROMIPS_PC7_S1:445return SignExtend64<8>(read16(buf) << 1);446case R_MICROMIPS_PC10_S1:447return SignExtend64<11>(read16(buf) << 1);448case R_MICROMIPS_PC16_S1:449return SignExtend64<17>(readShuffle<e>(buf) << 1);450case R_MICROMIPS_PC18_S3:451return SignExtend64<21>(readShuffle<e>(buf) << 3);452case R_MICROMIPS_PC19_S2:453return SignExtend64<21>(readShuffle<e>(buf) << 2);454case R_MICROMIPS_PC21_S1:455return SignExtend64<22>(readShuffle<e>(buf) << 1);456case R_MICROMIPS_PC23_S2:457return SignExtend64<25>(readShuffle<e>(buf) << 2);458case R_MICROMIPS_PC26_S1:459return SignExtend64<27>(readShuffle<e>(buf) << 1);460case R_MIPS_64:461case R_MIPS_TLS_DTPMOD64:462case R_MIPS_TLS_DTPREL64:463case R_MIPS_TLS_TPREL64:464case (R_MIPS_64 << 8) | R_MIPS_REL32:465return read64(buf);466case R_MIPS_COPY:467return config->is64 ? read64(buf) : read32(buf);468case R_MIPS_NONE:469case R_MIPS_JUMP_SLOT:470case R_MIPS_JALR:471// These relocations are defined as not having an implicit addend.472return 0;473default:474internalLinkerError(getErrorLocation(buf),475"cannot read addend for relocation " + toString(type));476return 0;477}478}479480static std::pair<uint32_t, uint64_t>481calculateMipsRelChain(uint8_t *loc, RelType type, uint64_t val) {482// MIPS N64 ABI packs multiple relocations into the single relocation483// record. In general, all up to three relocations can have arbitrary484// types. In fact, Clang and GCC uses only a few combinations. For now,485// we support two of them. That is allow to pass at least all LLVM486// test suite cases.487// <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16488// <any relocation> / R_MIPS_64 / R_MIPS_NONE489// The first relocation is a 'real' relocation which is calculated490// using the corresponding symbol's value. The second and the third491// relocations used to modify result of the first one: extend it to492// 64-bit, extract high or low part etc. For details, see part 2.9 Relocation493// at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf494RelType type2 = (type >> 8) & 0xff;495RelType type3 = (type >> 16) & 0xff;496if (type2 == R_MIPS_NONE && type3 == R_MIPS_NONE)497return std::make_pair(type, val);498if (type2 == R_MIPS_64 && type3 == R_MIPS_NONE)499return std::make_pair(type2, val);500if (type2 == R_MIPS_SUB && (type3 == R_MIPS_HI16 || type3 == R_MIPS_LO16))501return std::make_pair(type3, -val);502error(getErrorLocation(loc) + "unsupported relocations combination " +503Twine(type));504return std::make_pair(type & 0xff, val);505}506507static bool isBranchReloc(RelType type) {508return type == R_MIPS_26 || type == R_MIPS_PC26_S2 ||509type == R_MIPS_PC21_S2 || type == R_MIPS_PC16;510}511512static bool isMicroBranchReloc(RelType type) {513return type == R_MICROMIPS_26_S1 || type == R_MICROMIPS_PC16_S1 ||514type == R_MICROMIPS_PC10_S1 || type == R_MICROMIPS_PC7_S1;515}516517template <class ELFT>518static uint64_t fixupCrossModeJump(uint8_t *loc, RelType type, uint64_t val) {519// Here we need to detect jump/branch from regular MIPS code520// to a microMIPS target and vice versa. In that cases jump521// instructions need to be replaced by their "cross-mode"522// equivalents.523const endianness e = ELFT::Endianness;524bool isMicroTgt = val & 0x1;525bool isCrossJump = (isMicroTgt && isBranchReloc(type)) ||526(!isMicroTgt && isMicroBranchReloc(type));527if (!isCrossJump)528return val;529530switch (type) {531case R_MIPS_26: {532uint32_t inst = read32(loc) >> 26;533if (inst == 0x3 || inst == 0x1d) { // JAL or JALX534writeValue(loc, 0x1d << 26, 32, 0);535return val;536}537break;538}539case R_MICROMIPS_26_S1: {540uint32_t inst = readShuffle<e>(loc) >> 26;541if (inst == 0x3d || inst == 0x3c) { // JAL32 or JALX32542val >>= 1;543writeShuffleValue<e>(loc, 0x3c << 26, 32, 0);544return val;545}546break;547}548case R_MIPS_PC26_S2:549case R_MIPS_PC21_S2:550case R_MIPS_PC16:551case R_MICROMIPS_PC16_S1:552case R_MICROMIPS_PC10_S1:553case R_MICROMIPS_PC7_S1:554// FIXME (simon): Support valid branch relocations.555break;556default:557llvm_unreachable("unexpected jump/branch relocation");558}559560error(getErrorLocation(loc) +561"unsupported jump/branch instruction between ISA modes referenced by " +562toString(type) + " relocation");563return val;564}565566template <class ELFT>567void MIPS<ELFT>::relocate(uint8_t *loc, const Relocation &rel,568uint64_t val) const {569const endianness e = ELFT::Endianness;570RelType type = rel.type;571572if (ELFT::Is64Bits || config->mipsN32Abi)573std::tie(type, val) = calculateMipsRelChain(loc, type, val);574575// Detect cross-mode jump/branch and fix instruction.576val = fixupCrossModeJump<ELFT>(loc, type, val);577578// Thread pointer and DRP offsets from the start of TLS data area.579// https://www.linux-mips.org/wiki/NPTL580if (type == R_MIPS_TLS_DTPREL_HI16 || type == R_MIPS_TLS_DTPREL_LO16 ||581type == R_MIPS_TLS_DTPREL32 || type == R_MIPS_TLS_DTPREL64 ||582type == R_MICROMIPS_TLS_DTPREL_HI16 ||583type == R_MICROMIPS_TLS_DTPREL_LO16) {584val -= 0x8000;585}586587switch (type) {588case R_MIPS_32:589case R_MIPS_GPREL32:590case R_MIPS_TLS_DTPREL32:591case R_MIPS_TLS_TPREL32:592write32(loc, val);593break;594case R_MIPS_64:595case R_MIPS_TLS_DTPREL64:596case R_MIPS_TLS_TPREL64:597write64(loc, val);598break;599case R_MIPS_26:600writeValue(loc, val, 26, 2);601break;602case R_MIPS_GOT16:603// The R_MIPS_GOT16 relocation's value in "relocatable" linking mode604// is updated addend (not a GOT index). In that case write high 16 bits605// to store a correct addend value.606if (config->relocatable) {607writeValue(loc, val + 0x8000, 16, 16);608} else {609checkInt(loc, val, 16, rel);610writeValue(loc, val, 16, 0);611}612break;613case R_MICROMIPS_GOT16:614if (config->relocatable) {615writeShuffleValue<e>(loc, val + 0x8000, 16, 16);616} else {617checkInt(loc, val, 16, rel);618writeShuffleValue<e>(loc, val, 16, 0);619}620break;621case R_MIPS_CALL16:622case R_MIPS_GOT_DISP:623case R_MIPS_GOT_PAGE:624case R_MIPS_GPREL16:625case R_MIPS_TLS_GD:626case R_MIPS_TLS_GOTTPREL:627case R_MIPS_TLS_LDM:628checkInt(loc, val, 16, rel);629[[fallthrough]];630case R_MIPS_CALL_LO16:631case R_MIPS_GOT_LO16:632case R_MIPS_GOT_OFST:633case R_MIPS_LO16:634case R_MIPS_PCLO16:635case R_MIPS_TLS_DTPREL_LO16:636case R_MIPS_TLS_TPREL_LO16:637writeValue(loc, val, 16, 0);638break;639case R_MICROMIPS_GPREL16:640case R_MICROMIPS_TLS_GD:641case R_MICROMIPS_TLS_LDM:642checkInt(loc, val, 16, rel);643writeShuffleValue<e>(loc, val, 16, 0);644break;645case R_MICROMIPS_CALL16:646case R_MICROMIPS_CALL_LO16:647case R_MICROMIPS_LO16:648case R_MICROMIPS_TLS_DTPREL_LO16:649case R_MICROMIPS_TLS_GOTTPREL:650case R_MICROMIPS_TLS_TPREL_LO16:651writeShuffleValue<e>(loc, val, 16, 0);652break;653case R_MICROMIPS_GPREL7_S2:654checkInt(loc, val, 7, rel);655writeShuffleValue<e>(loc, val, 7, 2);656break;657case R_MIPS_CALL_HI16:658case R_MIPS_GOT_HI16:659case R_MIPS_HI16:660case R_MIPS_PCHI16:661case R_MIPS_TLS_DTPREL_HI16:662case R_MIPS_TLS_TPREL_HI16:663writeValue(loc, val + 0x8000, 16, 16);664break;665case R_MICROMIPS_CALL_HI16:666case R_MICROMIPS_GOT_HI16:667case R_MICROMIPS_HI16:668case R_MICROMIPS_TLS_DTPREL_HI16:669case R_MICROMIPS_TLS_TPREL_HI16:670writeShuffleValue<e>(loc, val + 0x8000, 16, 16);671break;672case R_MIPS_HIGHER:673writeValue(loc, val + 0x80008000, 16, 32);674break;675case R_MIPS_HIGHEST:676writeValue(loc, val + 0x800080008000, 16, 48);677break;678case R_MIPS_JALR:679val -= 4;680// Replace jalr/jr instructions by bal/b if the target681// offset fits into the 18-bit range.682if (isInt<18>(val)) {683switch (read32(loc)) {684case 0x0320f809: // jalr $25 => bal sym685write32(loc, 0x04110000 | ((val >> 2) & 0xffff));686break;687case 0x03200008: // jr $25 => b sym688write32(loc, 0x10000000 | ((val >> 2) & 0xffff));689break;690}691}692break;693case R_MICROMIPS_JALR:694// Ignore this optimization relocation for now695break;696case R_MIPS_PC16:697checkAlignment(loc, val, 4, rel);698checkInt(loc, val, 18, rel);699writeValue(loc, val, 16, 2);700break;701case R_MIPS_PC19_S2:702checkAlignment(loc, val, 4, rel);703checkInt(loc, val, 21, rel);704writeValue(loc, val, 19, 2);705break;706case R_MIPS_PC21_S2:707checkAlignment(loc, val, 4, rel);708checkInt(loc, val, 23, rel);709writeValue(loc, val, 21, 2);710break;711case R_MIPS_PC26_S2:712checkAlignment(loc, val, 4, rel);713checkInt(loc, val, 28, rel);714writeValue(loc, val, 26, 2);715break;716case R_MIPS_PC32:717writeValue(loc, val, 32, 0);718break;719case R_MICROMIPS_26_S1:720case R_MICROMIPS_PC26_S1:721checkInt(loc, val, 27, rel);722writeShuffleValue<e>(loc, val, 26, 1);723break;724case R_MICROMIPS_PC7_S1:725checkInt(loc, val, 8, rel);726writeMicroRelocation16<e>(loc, val, 7, 1);727break;728case R_MICROMIPS_PC10_S1:729checkInt(loc, val, 11, rel);730writeMicroRelocation16<e>(loc, val, 10, 1);731break;732case R_MICROMIPS_PC16_S1:733checkInt(loc, val, 17, rel);734writeShuffleValue<e>(loc, val, 16, 1);735break;736case R_MICROMIPS_PC18_S3:737checkInt(loc, val, 21, rel);738writeShuffleValue<e>(loc, val, 18, 3);739break;740case R_MICROMIPS_PC19_S2:741checkInt(loc, val, 21, rel);742writeShuffleValue<e>(loc, val, 19, 2);743break;744case R_MICROMIPS_PC21_S1:745checkInt(loc, val, 22, rel);746writeShuffleValue<e>(loc, val, 21, 1);747break;748case R_MICROMIPS_PC23_S2:749checkInt(loc, val, 25, rel);750writeShuffleValue<e>(loc, val, 23, 2);751break;752default:753llvm_unreachable("unknown relocation");754}755}756757template <class ELFT> bool MIPS<ELFT>::usesOnlyLowPageBits(RelType type) const {758return type == R_MIPS_LO16 || type == R_MIPS_GOT_OFST ||759type == R_MICROMIPS_LO16;760}761762// Return true if the symbol is a PIC function.763template <class ELFT> bool elf::isMipsPIC(const Defined *sym) {764if (!sym->isFunc())765return false;766767if (sym->stOther & STO_MIPS_PIC)768return true;769770if (!sym->section)771return false;772773InputFile *file = cast<InputSectionBase>(sym->section)->file;774if (!file || file->isInternal())775return false;776777return cast<ObjFile<ELFT>>(file)->getObj().getHeader().e_flags & EF_MIPS_PIC;778}779780template <class ELFT> TargetInfo *elf::getMipsTargetInfo() {781static MIPS<ELFT> target;782return ⌖783}784785template TargetInfo *elf::getMipsTargetInfo<ELF32LE>();786template TargetInfo *elf::getMipsTargetInfo<ELF32BE>();787template TargetInfo *elf::getMipsTargetInfo<ELF64LE>();788template TargetInfo *elf::getMipsTargetInfo<ELF64BE>();789790template bool elf::isMipsPIC<ELF32LE>(const Defined *);791template bool elf::isMipsPIC<ELF32BE>(const Defined *);792template bool elf::isMipsPIC<ELF64LE>(const Defined *);793template bool elf::isMipsPIC<ELF64BE>(const Defined *);794795796