Path: blob/main/contrib/llvm-project/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
35294 views
//===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===//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 "MCTargetDesc/MipsFixupKinds.h"9#include "MCTargetDesc/MipsMCTargetDesc.h"10#include "llvm/ADT/STLExtras.h"11#include "llvm/BinaryFormat/ELF.h"12#include "llvm/MC/MCContext.h"13#include "llvm/MC/MCELFObjectWriter.h"14#include "llvm/MC/MCFixup.h"15#include "llvm/MC/MCObjectWriter.h"16#include "llvm/MC/MCSymbolELF.h"17#include "llvm/Support/Casting.h"18#include "llvm/Support/Compiler.h"19#include "llvm/Support/Debug.h"20#include "llvm/Support/ErrorHandling.h"21#include "llvm/Support/MathExtras.h"22#include "llvm/Support/raw_ostream.h"23#include <algorithm>24#include <cassert>25#include <cstdint>26#include <iterator>27#include <list>28#include <utility>2930#define DEBUG_TYPE "mips-elf-object-writer"3132using namespace llvm;3334namespace {3536/// Holds additional information needed by the relocation ordering algorithm.37struct MipsRelocationEntry {38const ELFRelocationEntry R; ///< The relocation.39bool Matched = false; ///< Is this relocation part of a match.4041MipsRelocationEntry(const ELFRelocationEntry &R) : R(R) {}4243void print(raw_ostream &Out) const {44R.print(Out);45Out << ", Matched=" << Matched;46}47};4849#ifndef NDEBUG50raw_ostream &operator<<(raw_ostream &OS, const MipsRelocationEntry &RHS) {51RHS.print(OS);52return OS;53}54#endif5556class MipsELFObjectWriter : public MCELFObjectTargetWriter {57public:58MipsELFObjectWriter(uint8_t OSABI, bool HasRelocationAddend, bool Is64);5960~MipsELFObjectWriter() override = default;6162unsigned getRelocType(MCContext &Ctx, const MCValue &Target,63const MCFixup &Fixup, bool IsPCRel) const override;64bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,65unsigned Type) const override;66void sortRelocs(const MCAssembler &Asm,67std::vector<ELFRelocationEntry> &Relocs) override;68};6970/// The possible results of the Predicate function used by find_best.71enum FindBestPredicateResult {72FindBest_NoMatch = 0, ///< The current element is not a match.73FindBest_Match, ///< The current element is a match but better ones are74/// possible.75FindBest_PerfectMatch, ///< The current element is an unbeatable match.76};7778} // end anonymous namespace7980/// Copy elements in the range [First, Last) to d1 when the predicate is true or81/// d2 when the predicate is false. This is essentially both std::copy_if and82/// std::remove_copy_if combined into a single pass.83template <class InputIt, class OutputIt1, class OutputIt2, class UnaryPredicate>84static std::pair<OutputIt1, OutputIt2> copy_if_else(InputIt First, InputIt Last,85OutputIt1 d1, OutputIt2 d2,86UnaryPredicate Predicate) {87for (InputIt I = First; I != Last; ++I) {88if (Predicate(*I)) {89*d1 = *I;90d1++;91} else {92*d2 = *I;93d2++;94}95}9697return std::make_pair(d1, d2);98}99100/// Find the best match in the range [First, Last).101///102/// An element matches when Predicate(X) returns FindBest_Match or103/// FindBest_PerfectMatch. A value of FindBest_PerfectMatch also terminates104/// the search. BetterThan(A, B) is a comparator that returns true when A is a105/// better match than B. The return value is the position of the best match.106///107/// This is similar to std::find_if but finds the best of multiple possible108/// matches.109template <class InputIt, class UnaryPredicate, class Comparator>110static InputIt find_best(InputIt First, InputIt Last, UnaryPredicate Predicate,111Comparator BetterThan) {112InputIt Best = Last;113114for (InputIt I = First; I != Last; ++I) {115unsigned Matched = Predicate(*I);116if (Matched != FindBest_NoMatch) {117LLVM_DEBUG(dbgs() << std::distance(First, I) << " is a match (";118I->print(dbgs()); dbgs() << ")\n");119if (Best == Last || BetterThan(*I, *Best)) {120LLVM_DEBUG(dbgs() << ".. and it beats the last one\n");121Best = I;122}123}124if (Matched == FindBest_PerfectMatch) {125LLVM_DEBUG(dbgs() << ".. and it is unbeatable\n");126break;127}128}129130return Best;131}132133/// Determine the low relocation that matches the given relocation.134/// If the relocation does not need a low relocation then the return value135/// is ELF::R_MIPS_NONE.136///137/// The relocations that need a matching low part are138/// R_(MIPS|MICROMIPS|MIPS16)_HI16 for all symbols and139/// R_(MIPS|MICROMIPS|MIPS16)_GOT16 for local symbols only.140static unsigned getMatchingLoType(const ELFRelocationEntry &Reloc) {141unsigned Type = Reloc.Type;142if (Type == ELF::R_MIPS_HI16)143return ELF::R_MIPS_LO16;144if (Type == ELF::R_MICROMIPS_HI16)145return ELF::R_MICROMIPS_LO16;146if (Type == ELF::R_MIPS16_HI16)147return ELF::R_MIPS16_LO16;148149if (Reloc.OriginalSymbol &&150Reloc.OriginalSymbol->getBinding() != ELF::STB_LOCAL)151return ELF::R_MIPS_NONE;152153if (Type == ELF::R_MIPS_GOT16)154return ELF::R_MIPS_LO16;155if (Type == ELF::R_MICROMIPS_GOT16)156return ELF::R_MICROMIPS_LO16;157if (Type == ELF::R_MIPS16_GOT16)158return ELF::R_MIPS16_LO16;159160return ELF::R_MIPS_NONE;161}162163/// Determine whether a relocation (X) matches the one given in R.164///165/// A relocation matches if:166/// - It's type matches that of a corresponding low part. This is provided in167/// MatchingType for efficiency.168/// - It's based on the same symbol.169/// - It's offset of greater or equal to that of the one given in R.170/// It should be noted that this rule assumes the programmer does not use171/// offsets that exceed the alignment of the symbol. The carry-bit will be172/// incorrect if this is not true.173///174/// A matching relocation is unbeatable if:175/// - It is not already involved in a match.176/// - It's offset is exactly that of the one given in R.177static FindBestPredicateResult isMatchingReloc(const MipsRelocationEntry &X,178const ELFRelocationEntry &R,179unsigned MatchingType) {180if (X.R.Type == MatchingType && X.R.OriginalSymbol == R.OriginalSymbol) {181if (!X.Matched &&182X.R.OriginalAddend == R.OriginalAddend)183return FindBest_PerfectMatch;184else if (X.R.OriginalAddend >= R.OriginalAddend)185return FindBest_Match;186}187return FindBest_NoMatch;188}189190/// Determine whether Candidate or PreviousBest is the better match.191/// The return value is true if Candidate is the better match.192///193/// A matching relocation is a better match if:194/// - It has a smaller addend.195/// - It is not already involved in a match.196static bool compareMatchingRelocs(const MipsRelocationEntry &Candidate,197const MipsRelocationEntry &PreviousBest) {198if (Candidate.R.OriginalAddend != PreviousBest.R.OriginalAddend)199return Candidate.R.OriginalAddend < PreviousBest.R.OriginalAddend;200return PreviousBest.Matched && !Candidate.Matched;201}202203#ifndef NDEBUG204/// Print all the relocations.205template <class Container>206static void dumpRelocs(const char *Prefix, const Container &Relocs) {207for (const auto &R : Relocs)208dbgs() << Prefix << R << "\n";209}210#endif211212MipsELFObjectWriter::MipsELFObjectWriter(uint8_t OSABI,213bool HasRelocationAddend, bool Is64)214: MCELFObjectTargetWriter(Is64, OSABI, ELF::EM_MIPS, HasRelocationAddend) {}215216unsigned MipsELFObjectWriter::getRelocType(MCContext &Ctx,217const MCValue &Target,218const MCFixup &Fixup,219bool IsPCRel) const {220// Determine the type of the relocation.221unsigned Kind = Fixup.getTargetKind();222if (Kind >= FirstLiteralRelocationKind)223return Kind - FirstLiteralRelocationKind;224225switch (Kind) {226case FK_NONE:227return ELF::R_MIPS_NONE;228case FK_Data_1:229Ctx.reportError(Fixup.getLoc(),230"MIPS does not support one byte relocations");231return ELF::R_MIPS_NONE;232case Mips::fixup_Mips_16:233case FK_Data_2:234return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;235case Mips::fixup_Mips_32:236case FK_Data_4:237return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;238case Mips::fixup_Mips_64:239case FK_Data_8:240return IsPCRel241? setRTypes(ELF::R_MIPS_PC32, ELF::R_MIPS_64, ELF::R_MIPS_NONE)242: (unsigned)ELF::R_MIPS_64;243}244245if (IsPCRel) {246switch (Kind) {247case Mips::fixup_Mips_Branch_PCRel:248case Mips::fixup_Mips_PC16:249return ELF::R_MIPS_PC16;250case Mips::fixup_MICROMIPS_PC7_S1:251return ELF::R_MICROMIPS_PC7_S1;252case Mips::fixup_MICROMIPS_PC10_S1:253return ELF::R_MICROMIPS_PC10_S1;254case Mips::fixup_MICROMIPS_PC16_S1:255return ELF::R_MICROMIPS_PC16_S1;256case Mips::fixup_MICROMIPS_PC26_S1:257return ELF::R_MICROMIPS_PC26_S1;258case Mips::fixup_MICROMIPS_PC19_S2:259return ELF::R_MICROMIPS_PC19_S2;260case Mips::fixup_MICROMIPS_PC18_S3:261return ELF::R_MICROMIPS_PC18_S3;262case Mips::fixup_MICROMIPS_PC21_S1:263return ELF::R_MICROMIPS_PC21_S1;264case Mips::fixup_MIPS_PC19_S2:265return ELF::R_MIPS_PC19_S2;266case Mips::fixup_MIPS_PC18_S3:267return ELF::R_MIPS_PC18_S3;268case Mips::fixup_MIPS_PC21_S2:269return ELF::R_MIPS_PC21_S2;270case Mips::fixup_MIPS_PC26_S2:271return ELF::R_MIPS_PC26_S2;272case Mips::fixup_MIPS_PCHI16:273return ELF::R_MIPS_PCHI16;274case Mips::fixup_MIPS_PCLO16:275return ELF::R_MIPS_PCLO16;276}277278llvm_unreachable("invalid PC-relative fixup kind!");279}280281switch (Kind) {282case FK_DTPRel_4:283return ELF::R_MIPS_TLS_DTPREL32;284case FK_DTPRel_8:285return ELF::R_MIPS_TLS_DTPREL64;286case FK_TPRel_4:287return ELF::R_MIPS_TLS_TPREL32;288case FK_TPRel_8:289return ELF::R_MIPS_TLS_TPREL64;290case FK_GPRel_4:291return setRTypes(ELF::R_MIPS_GPREL32,292is64Bit() ? ELF::R_MIPS_64 : ELF::R_MIPS_NONE,293ELF::R_MIPS_NONE);294case Mips::fixup_Mips_GPREL16:295return ELF::R_MIPS_GPREL16;296case Mips::fixup_Mips_26:297return ELF::R_MIPS_26;298case Mips::fixup_Mips_CALL16:299return ELF::R_MIPS_CALL16;300case Mips::fixup_Mips_GOT:301return ELF::R_MIPS_GOT16;302case Mips::fixup_Mips_HI16:303return ELF::R_MIPS_HI16;304case Mips::fixup_Mips_LO16:305return ELF::R_MIPS_LO16;306case Mips::fixup_Mips_TLSGD:307return ELF::R_MIPS_TLS_GD;308case Mips::fixup_Mips_GOTTPREL:309return ELF::R_MIPS_TLS_GOTTPREL;310case Mips::fixup_Mips_TPREL_HI:311return ELF::R_MIPS_TLS_TPREL_HI16;312case Mips::fixup_Mips_TPREL_LO:313return ELF::R_MIPS_TLS_TPREL_LO16;314case Mips::fixup_Mips_TLSLDM:315return ELF::R_MIPS_TLS_LDM;316case Mips::fixup_Mips_DTPREL_HI:317return ELF::R_MIPS_TLS_DTPREL_HI16;318case Mips::fixup_Mips_DTPREL_LO:319return ELF::R_MIPS_TLS_DTPREL_LO16;320case Mips::fixup_Mips_GOT_PAGE:321return ELF::R_MIPS_GOT_PAGE;322case Mips::fixup_Mips_GOT_OFST:323return ELF::R_MIPS_GOT_OFST;324case Mips::fixup_Mips_GOT_DISP:325return ELF::R_MIPS_GOT_DISP;326case Mips::fixup_Mips_GPOFF_HI:327return setRTypes(ELF::R_MIPS_GPREL16, ELF::R_MIPS_SUB, ELF::R_MIPS_HI16);328case Mips::fixup_MICROMIPS_GPOFF_HI:329return setRTypes(ELF::R_MICROMIPS_GPREL16, ELF::R_MICROMIPS_SUB,330ELF::R_MICROMIPS_HI16);331case Mips::fixup_Mips_GPOFF_LO:332return setRTypes(ELF::R_MIPS_GPREL16, ELF::R_MIPS_SUB, ELF::R_MIPS_LO16);333case Mips::fixup_MICROMIPS_GPOFF_LO:334return setRTypes(ELF::R_MICROMIPS_GPREL16, ELF::R_MICROMIPS_SUB,335ELF::R_MICROMIPS_LO16);336case Mips::fixup_Mips_HIGHER:337return ELF::R_MIPS_HIGHER;338case Mips::fixup_Mips_HIGHEST:339return ELF::R_MIPS_HIGHEST;340case Mips::fixup_Mips_SUB:341return ELF::R_MIPS_SUB;342case Mips::fixup_Mips_GOT_HI16:343return ELF::R_MIPS_GOT_HI16;344case Mips::fixup_Mips_GOT_LO16:345return ELF::R_MIPS_GOT_LO16;346case Mips::fixup_Mips_CALL_HI16:347return ELF::R_MIPS_CALL_HI16;348case Mips::fixup_Mips_CALL_LO16:349return ELF::R_MIPS_CALL_LO16;350case Mips::fixup_MICROMIPS_26_S1:351return ELF::R_MICROMIPS_26_S1;352case Mips::fixup_MICROMIPS_HI16:353return ELF::R_MICROMIPS_HI16;354case Mips::fixup_MICROMIPS_LO16:355return ELF::R_MICROMIPS_LO16;356case Mips::fixup_MICROMIPS_GOT16:357return ELF::R_MICROMIPS_GOT16;358case Mips::fixup_MICROMIPS_CALL16:359return ELF::R_MICROMIPS_CALL16;360case Mips::fixup_MICROMIPS_GOT_DISP:361return ELF::R_MICROMIPS_GOT_DISP;362case Mips::fixup_MICROMIPS_GOT_PAGE:363return ELF::R_MICROMIPS_GOT_PAGE;364case Mips::fixup_MICROMIPS_GOT_OFST:365return ELF::R_MICROMIPS_GOT_OFST;366case Mips::fixup_MICROMIPS_TLS_GD:367return ELF::R_MICROMIPS_TLS_GD;368case Mips::fixup_MICROMIPS_TLS_LDM:369return ELF::R_MICROMIPS_TLS_LDM;370case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16:371return ELF::R_MICROMIPS_TLS_DTPREL_HI16;372case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16:373return ELF::R_MICROMIPS_TLS_DTPREL_LO16;374case Mips::fixup_MICROMIPS_GOTTPREL:375return ELF::R_MICROMIPS_TLS_GOTTPREL;376case Mips::fixup_MICROMIPS_TLS_TPREL_HI16:377return ELF::R_MICROMIPS_TLS_TPREL_HI16;378case Mips::fixup_MICROMIPS_TLS_TPREL_LO16:379return ELF::R_MICROMIPS_TLS_TPREL_LO16;380case Mips::fixup_MICROMIPS_SUB:381return ELF::R_MICROMIPS_SUB;382case Mips::fixup_MICROMIPS_HIGHER:383return ELF::R_MICROMIPS_HIGHER;384case Mips::fixup_MICROMIPS_HIGHEST:385return ELF::R_MICROMIPS_HIGHEST;386case Mips::fixup_Mips_JALR:387return ELF::R_MIPS_JALR;388case Mips::fixup_MICROMIPS_JALR:389return ELF::R_MICROMIPS_JALR;390}391392llvm_unreachable("invalid fixup kind!");393}394395/// Sort relocation table entries by offset except where another order is396/// required by the MIPS ABI.397///398/// MIPS has a few relocations that have an AHL component in the expression used399/// to evaluate them. This AHL component is an addend with the same number of400/// bits as a symbol value but not all of our ABI's are able to supply a401/// sufficiently sized addend in a single relocation.402///403/// The O32 ABI for example, uses REL relocations which store the addend in the404/// section data. All the relocations with AHL components affect 16-bit fields405/// so the addend for a single relocation is limited to 16-bit. This ABI406/// resolves the limitation by linking relocations (e.g. R_MIPS_HI16 and407/// R_MIPS_LO16) and distributing the addend between the linked relocations. The408/// ABI mandates that such relocations must be next to each other in a409/// particular order (e.g. R_MIPS_HI16 must be immediately followed by a410/// matching R_MIPS_LO16) but the rule is less strict in practice.411///412/// The de facto standard is lenient in the following ways:413/// - 'Immediately following' does not refer to the next relocation entry but414/// the next matching relocation.415/// - There may be multiple high parts relocations for one low part relocation.416/// - There may be multiple low part relocations for one high part relocation.417/// - The AHL addend in each part does not have to be exactly equal as long as418/// the difference does not affect the carry bit from bit 15 into 16. This is419/// to allow, for example, the use of %lo(foo) and %lo(foo+4) when loading420/// both halves of a long long.421///422/// See getMatchingLoType() for a description of which high part relocations423/// match which low part relocations. One particular thing to note is that424/// R_MIPS_GOT16 and similar only have AHL addends if they refer to local425/// symbols.426///427/// It should also be noted that this function is not affected by whether428/// the symbol was kept or rewritten into a section-relative equivalent. We429/// always match using the expressions from the source.430void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,431std::vector<ELFRelocationEntry> &Relocs) {432// We do not need to sort the relocation table for RELA relocations which433// N32/N64 uses as the relocation addend contains the value we require,434// rather than it being split across a pair of relocations.435if (hasRelocationAddend())436return;437438if (Relocs.size() < 2)439return;440441// Sort relocations by the address they are applied to.442llvm::sort(Relocs,443[](const ELFRelocationEntry &A, const ELFRelocationEntry &B) {444return A.Offset < B.Offset;445});446447std::list<MipsRelocationEntry> Sorted;448std::list<ELFRelocationEntry> Remainder;449450LLVM_DEBUG(dumpRelocs("R: ", Relocs));451452// Separate the movable relocations (AHL relocations using the high bits) from453// the immobile relocations (everything else). This does not preserve high/low454// matches that already existed in the input.455copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),456std::back_inserter(Sorted), [](const ELFRelocationEntry &Reloc) {457return getMatchingLoType(Reloc) != ELF::R_MIPS_NONE;458});459460for (auto &R : Remainder) {461LLVM_DEBUG(dbgs() << "Matching: " << R << "\n");462463unsigned MatchingType = getMatchingLoType(R);464assert(MatchingType != ELF::R_MIPS_NONE &&465"Wrong list for reloc that doesn't need a match");466467// Find the best matching relocation for the current high part.468// See isMatchingReloc for a description of a matching relocation and469// compareMatchingRelocs for a description of what 'best' means.470auto InsertionPoint =471find_best(Sorted.begin(), Sorted.end(),472[&R, &MatchingType](const MipsRelocationEntry &X) {473return isMatchingReloc(X, R, MatchingType);474},475compareMatchingRelocs);476477// If we matched then insert the high part in front of the match and mark478// both relocations as being involved in a match. We only mark the high479// part for cosmetic reasons in the debug output.480//481// If we failed to find a match then the high part is orphaned. This is not482// permitted since the relocation cannot be evaluated without knowing the483// carry-in. We can sometimes handle this using a matching low part that is484// already used in a match but we already cover that case in485// isMatchingReloc and compareMatchingRelocs. For the remaining cases we486// should insert the high part at the end of the list. This will cause the487// linker to fail but the alternative is to cause the linker to bind the488// high part to a semi-matching low part and silently calculate the wrong489// value. Unfortunately we have no means to warn the user that we did this490// so leave it up to the linker to complain about it.491if (InsertionPoint != Sorted.end())492InsertionPoint->Matched = true;493Sorted.insert(InsertionPoint, R)->Matched = true;494}495496LLVM_DEBUG(dumpRelocs("S: ", Sorted));497498assert(Relocs.size() == Sorted.size() && "Some relocs were not consumed");499500// Overwrite the original vector with the sorted elements.501unsigned CopyTo = 0;502for (const auto &R : Sorted)503Relocs[CopyTo++] = R.R;504}505506bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCValue &Val,507const MCSymbol &Sym,508unsigned Type) const {509// If it's a compound relocation for N64 then we need the relocation if any510// sub-relocation needs it.511if (!isUInt<8>(Type))512return needsRelocateWithSymbol(Val, Sym, Type & 0xff) ||513needsRelocateWithSymbol(Val, Sym, (Type >> 8) & 0xff) ||514needsRelocateWithSymbol(Val, Sym, (Type >> 16) & 0xff);515516switch (Type) {517default:518errs() << Type << "\n";519llvm_unreachable("Unexpected relocation");520return true;521522// This relocation doesn't affect the section data.523case ELF::R_MIPS_NONE:524return false;525526// On REL ABI's (e.g. O32), these relocations form pairs. The pairing is done527// by the static linker by matching the symbol and offset.528// We only see one relocation at a time but it's still safe to relocate with529// the section so long as both relocations make the same decision.530//531// Some older linkers may require the symbol for particular cases. Such cases532// are not supported yet but can be added as required.533case ELF::R_MIPS_GOT16:534case ELF::R_MIPS16_GOT16:535case ELF::R_MICROMIPS_GOT16:536case ELF::R_MIPS_HIGHER:537case ELF::R_MIPS_HIGHEST:538case ELF::R_MIPS_HI16:539case ELF::R_MIPS16_HI16:540case ELF::R_MICROMIPS_HI16:541case ELF::R_MIPS_LO16:542case ELF::R_MIPS16_LO16:543case ELF::R_MICROMIPS_LO16:544// FIXME: It should be safe to return false for the STO_MIPS_MICROMIPS but545// we neglect to handle the adjustment to the LSB of the addend that546// it causes in applyFixup() and similar.547if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)548return true;549return false;550551case ELF::R_MIPS_GOT_PAGE:552case ELF::R_MICROMIPS_GOT_PAGE:553case ELF::R_MIPS_GOT_OFST:554case ELF::R_MICROMIPS_GOT_OFST:555case ELF::R_MIPS_16:556case ELF::R_MIPS_32:557case ELF::R_MIPS_GPREL32:558if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)559return true;560[[fallthrough]];561case ELF::R_MIPS_26:562case ELF::R_MIPS_64:563case ELF::R_MIPS_GPREL16:564case ELF::R_MIPS_PC16:565case ELF::R_MIPS_SUB:566return false;567568// FIXME: Many of these relocations should probably return false but this569// hasn't been confirmed to be safe yet.570case ELF::R_MIPS_REL32:571case ELF::R_MIPS_LITERAL:572case ELF::R_MIPS_CALL16:573case ELF::R_MIPS_SHIFT5:574case ELF::R_MIPS_SHIFT6:575case ELF::R_MIPS_GOT_DISP:576case ELF::R_MIPS_GOT_HI16:577case ELF::R_MIPS_GOT_LO16:578case ELF::R_MIPS_INSERT_A:579case ELF::R_MIPS_INSERT_B:580case ELF::R_MIPS_DELETE:581case ELF::R_MIPS_CALL_HI16:582case ELF::R_MIPS_CALL_LO16:583case ELF::R_MIPS_SCN_DISP:584case ELF::R_MIPS_REL16:585case ELF::R_MIPS_ADD_IMMEDIATE:586case ELF::R_MIPS_PJUMP:587case ELF::R_MIPS_RELGOT:588case ELF::R_MIPS_JALR:589case ELF::R_MIPS_TLS_DTPMOD32:590case ELF::R_MIPS_TLS_DTPREL32:591case ELF::R_MIPS_TLS_DTPMOD64:592case ELF::R_MIPS_TLS_DTPREL64:593case ELF::R_MIPS_TLS_GD:594case ELF::R_MIPS_TLS_LDM:595case ELF::R_MIPS_TLS_DTPREL_HI16:596case ELF::R_MIPS_TLS_DTPREL_LO16:597case ELF::R_MIPS_TLS_GOTTPREL:598case ELF::R_MIPS_TLS_TPREL32:599case ELF::R_MIPS_TLS_TPREL64:600case ELF::R_MIPS_TLS_TPREL_HI16:601case ELF::R_MIPS_TLS_TPREL_LO16:602case ELF::R_MIPS_GLOB_DAT:603case ELF::R_MIPS_PC21_S2:604case ELF::R_MIPS_PC26_S2:605case ELF::R_MIPS_PC18_S3:606case ELF::R_MIPS_PC19_S2:607case ELF::R_MIPS_PCHI16:608case ELF::R_MIPS_PCLO16:609case ELF::R_MIPS_COPY:610case ELF::R_MIPS_JUMP_SLOT:611case ELF::R_MIPS_NUM:612case ELF::R_MIPS_PC32:613case ELF::R_MIPS_EH:614case ELF::R_MICROMIPS_26_S1:615case ELF::R_MICROMIPS_GPREL16:616case ELF::R_MICROMIPS_LITERAL:617case ELF::R_MICROMIPS_PC7_S1:618case ELF::R_MICROMIPS_PC10_S1:619case ELF::R_MICROMIPS_PC16_S1:620case ELF::R_MICROMIPS_CALL16:621case ELF::R_MICROMIPS_GOT_DISP:622case ELF::R_MICROMIPS_GOT_HI16:623case ELF::R_MICROMIPS_GOT_LO16:624case ELF::R_MICROMIPS_SUB:625case ELF::R_MICROMIPS_HIGHER:626case ELF::R_MICROMIPS_HIGHEST:627case ELF::R_MICROMIPS_CALL_HI16:628case ELF::R_MICROMIPS_CALL_LO16:629case ELF::R_MICROMIPS_SCN_DISP:630case ELF::R_MICROMIPS_JALR:631case ELF::R_MICROMIPS_HI0_LO16:632case ELF::R_MICROMIPS_TLS_GD:633case ELF::R_MICROMIPS_TLS_LDM:634case ELF::R_MICROMIPS_TLS_DTPREL_HI16:635case ELF::R_MICROMIPS_TLS_DTPREL_LO16:636case ELF::R_MICROMIPS_TLS_GOTTPREL:637case ELF::R_MICROMIPS_TLS_TPREL_HI16:638case ELF::R_MICROMIPS_TLS_TPREL_LO16:639case ELF::R_MICROMIPS_GPREL7_S2:640case ELF::R_MICROMIPS_PC23_S2:641case ELF::R_MICROMIPS_PC21_S1:642case ELF::R_MICROMIPS_PC26_S1:643case ELF::R_MICROMIPS_PC18_S3:644case ELF::R_MICROMIPS_PC19_S2:645return true;646647// FIXME: Many of these should probably return false but MIPS16 isn't648// supported by the integrated assembler.649case ELF::R_MIPS16_26:650case ELF::R_MIPS16_GPREL:651case ELF::R_MIPS16_CALL16:652case ELF::R_MIPS16_TLS_GD:653case ELF::R_MIPS16_TLS_LDM:654case ELF::R_MIPS16_TLS_DTPREL_HI16:655case ELF::R_MIPS16_TLS_DTPREL_LO16:656case ELF::R_MIPS16_TLS_GOTTPREL:657case ELF::R_MIPS16_TLS_TPREL_HI16:658case ELF::R_MIPS16_TLS_TPREL_LO16:659llvm_unreachable("Unsupported MIPS16 relocation");660return true;661}662}663664std::unique_ptr<MCObjectTargetWriter>665llvm::createMipsELFObjectWriter(const Triple &TT, bool IsN32) {666uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());667bool IsN64 = TT.isArch64Bit() && !IsN32;668bool HasRelocationAddend = TT.isArch64Bit();669return std::make_unique<MipsELFObjectWriter>(OSABI, HasRelocationAddend,670IsN64);671}672673674