Home | History | Annotate | Download | only in MCTargetDesc
      1 //===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include <algorithm>
     11 #include <list>
     12 #include "MCTargetDesc/MipsBaseInfo.h"
     13 #include "MCTargetDesc/MipsFixupKinds.h"
     14 #include "MCTargetDesc/MipsMCExpr.h"
     15 #include "MCTargetDesc/MipsMCTargetDesc.h"
     16 #include "llvm/ADT/STLExtras.h"
     17 #include "llvm/MC/MCAssembler.h"
     18 #include "llvm/MC/MCELFObjectWriter.h"
     19 #include "llvm/MC/MCExpr.h"
     20 #include "llvm/MC/MCSection.h"
     21 #include "llvm/MC/MCSymbolELF.h"
     22 #include "llvm/MC/MCValue.h"
     23 #include "llvm/Support/Debug.h"
     24 #include "llvm/Support/ErrorHandling.h"
     25 
     26 #define DEBUG_TYPE "mips-elf-object-writer"
     27 
     28 using namespace llvm;
     29 
     30 namespace {
     31 /// Holds additional information needed by the relocation ordering algorithm.
     32 struct MipsRelocationEntry {
     33   const ELFRelocationEntry R; ///< The relocation.
     34   bool Matched;               ///< Is this relocation part of a match.
     35 
     36   MipsRelocationEntry(const ELFRelocationEntry &R) : R(R), Matched(false) {}
     37 
     38   void print(raw_ostream &Out) const {
     39     R.print(Out);
     40     Out << ", Matched=" << Matched;
     41   }
     42 };
     43 
     44 #ifndef NDEBUG
     45 raw_ostream &operator<<(raw_ostream &OS, const MipsRelocationEntry &RHS) {
     46   RHS.print(OS);
     47   return OS;
     48 }
     49 #endif
     50 
     51 class MipsELFObjectWriter : public MCELFObjectTargetWriter {
     52 public:
     53   MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI, bool _isN64,
     54                       bool IsLittleEndian);
     55 
     56   ~MipsELFObjectWriter() override;
     57 
     58   unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
     59                         const MCFixup &Fixup, bool IsPCRel) const override;
     60   bool needsRelocateWithSymbol(const MCSymbol &Sym,
     61                                unsigned Type) const override;
     62   virtual void sortRelocs(const MCAssembler &Asm,
     63                           std::vector<ELFRelocationEntry> &Relocs) override;
     64 };
     65 
     66 /// Copy elements in the range [First, Last) to d1 when the predicate is true or
     67 /// d2 when the predicate is false. This is essentially both std::copy_if and
     68 /// std::remove_copy_if combined into a single pass.
     69 template <class InputIt, class OutputIt1, class OutputIt2, class UnaryPredicate>
     70 std::pair<OutputIt1, OutputIt2> copy_if_else(InputIt First, InputIt Last,
     71                                              OutputIt1 d1, OutputIt2 d2,
     72                                              UnaryPredicate Predicate) {
     73   for (InputIt I = First; I != Last; ++I) {
     74     if (Predicate(*I)) {
     75       *d1 = *I;
     76       d1++;
     77     } else {
     78       *d2 = *I;
     79       d2++;
     80     }
     81   }
     82 
     83   return std::make_pair(d1, d2);
     84 }
     85 
     86 /// The possible results of the Predicate function used by find_best.
     87 enum FindBestPredicateResult {
     88   FindBest_NoMatch = 0,  ///< The current element is not a match.
     89   FindBest_Match,        ///< The current element is a match but better ones are
     90                          ///  possible.
     91   FindBest_PerfectMatch, ///< The current element is an unbeatable match.
     92 };
     93 
     94 /// Find the best match in the range [First, Last).
     95 ///
     96 /// An element matches when Predicate(X) returns FindBest_Match or
     97 /// FindBest_PerfectMatch. A value of FindBest_PerfectMatch also terminates
     98 /// the search. BetterThan(A, B) is a comparator that returns true when A is a
     99 /// better match than B. The return value is the position of the best match.
    100 ///
    101 /// This is similar to std::find_if but finds the best of multiple possible
    102 /// matches.
    103 template <class InputIt, class UnaryPredicate, class Comparator>
    104 InputIt find_best(InputIt First, InputIt Last, UnaryPredicate Predicate,
    105                   Comparator BetterThan) {
    106   InputIt Best = Last;
    107 
    108   for (InputIt I = First; I != Last; ++I) {
    109     unsigned Matched = Predicate(*I);
    110     if (Matched != FindBest_NoMatch) {
    111       DEBUG(dbgs() << std::distance(First, I) << " is a match (";
    112             I->print(dbgs()); dbgs() << ")\n");
    113       if (Best == Last || BetterThan(*I, *Best)) {
    114         DEBUG(dbgs() << ".. and it beats the last one\n");
    115         Best = I;
    116       }
    117     }
    118     if (Matched == FindBest_PerfectMatch) {
    119       DEBUG(dbgs() << ".. and it is unbeatable\n");
    120       break;
    121     }
    122   }
    123 
    124   return Best;
    125 }
    126 
    127 /// Determine the low relocation that matches the given relocation.
    128 /// If the relocation does not need a low relocation then the return value
    129 /// is ELF::R_MIPS_NONE.
    130 ///
    131 /// The relocations that need a matching low part are
    132 /// R_(MIPS|MICROMIPS|MIPS16)_HI16 for all symbols and
    133 /// R_(MIPS|MICROMIPS|MIPS16)_GOT16 for local symbols only.
    134 static unsigned getMatchingLoType(const ELFRelocationEntry &Reloc) {
    135   unsigned Type = Reloc.Type;
    136   if (Type == ELF::R_MIPS_HI16)
    137     return ELF::R_MIPS_LO16;
    138   if (Type == ELF::R_MICROMIPS_HI16)
    139     return ELF::R_MICROMIPS_LO16;
    140   if (Type == ELF::R_MIPS16_HI16)
    141     return ELF::R_MIPS16_LO16;
    142 
    143   if (Reloc.OriginalSymbol->getBinding() != ELF::STB_LOCAL)
    144     return ELF::R_MIPS_NONE;
    145 
    146   if (Type == ELF::R_MIPS_GOT16)
    147     return ELF::R_MIPS_LO16;
    148   if (Type == ELF::R_MICROMIPS_GOT16)
    149     return ELF::R_MICROMIPS_LO16;
    150   if (Type == ELF::R_MIPS16_GOT16)
    151     return ELF::R_MIPS16_LO16;
    152 
    153   return ELF::R_MIPS_NONE;
    154 }
    155 
    156 /// Determine whether a relocation (X) matches the one given in R.
    157 ///
    158 /// A relocation matches if:
    159 /// - It's type matches that of a corresponding low part. This is provided in
    160 ///   MatchingType for efficiency.
    161 /// - It's based on the same symbol.
    162 /// - It's offset of greater or equal to that of the one given in R.
    163 ///   It should be noted that this rule assumes the programmer does not use
    164 ///   offsets that exceed the alignment of the symbol. The carry-bit will be
    165 ///   incorrect if this is not true.
    166 ///
    167 /// A matching relocation is unbeatable if:
    168 /// - It is not already involved in a match.
    169 /// - It's offset is exactly that of the one given in R.
    170 static FindBestPredicateResult isMatchingReloc(const MipsRelocationEntry &X,
    171                                                const ELFRelocationEntry &R,
    172                                                unsigned MatchingType) {
    173   if (X.R.Type == MatchingType && X.R.OriginalSymbol == R.OriginalSymbol) {
    174     if (!X.Matched &&
    175         X.R.OriginalAddend == R.OriginalAddend)
    176       return FindBest_PerfectMatch;
    177     else if (X.R.OriginalAddend >= R.OriginalAddend)
    178       return FindBest_Match;
    179   }
    180   return FindBest_NoMatch;
    181 }
    182 
    183 /// Determine whether Candidate or PreviousBest is the better match.
    184 /// The return value is true if Candidate is the better match.
    185 ///
    186 /// A matching relocation is a better match if:
    187 /// - It has a smaller addend.
    188 /// - It is not already involved in a match.
    189 static bool compareMatchingRelocs(const MipsRelocationEntry &Candidate,
    190                                   const MipsRelocationEntry &PreviousBest) {
    191   if (Candidate.R.OriginalAddend != PreviousBest.R.OriginalAddend)
    192     return Candidate.R.OriginalAddend < PreviousBest.R.OriginalAddend;
    193   return PreviousBest.Matched && !Candidate.Matched;
    194 }
    195 
    196 #ifndef NDEBUG
    197 /// Print all the relocations.
    198 template <class Container>
    199 static void dumpRelocs(const char *Prefix, const Container &Relocs) {
    200   for (const auto &R : Relocs)
    201     dbgs() << Prefix << R << "\n";
    202 }
    203 #endif
    204 
    205 } // end anonymous namespace
    206 
    207 MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
    208                                          bool _isN64, bool IsLittleEndian)
    209     : MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS,
    210                               /*HasRelocationAddend*/ _isN64,
    211                               /*IsN64*/ _isN64) {}
    212 
    213 MipsELFObjectWriter::~MipsELFObjectWriter() {}
    214 
    215 unsigned MipsELFObjectWriter::getRelocType(MCContext &Ctx,
    216                                            const MCValue &Target,
    217                                            const MCFixup &Fixup,
    218                                            bool IsPCRel) const {
    219   // Determine the type of the relocation.
    220   unsigned Kind = (unsigned)Fixup.getKind();
    221 
    222   switch (Kind) {
    223   case Mips::fixup_Mips_NONE:
    224     return ELF::R_MIPS_NONE;
    225   case Mips::fixup_Mips_16:
    226   case FK_Data_2:
    227     return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
    228   case Mips::fixup_Mips_32:
    229   case FK_Data_4:
    230     return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
    231   }
    232 
    233   if (IsPCRel) {
    234     switch (Kind) {
    235     case Mips::fixup_Mips_Branch_PCRel:
    236     case Mips::fixup_Mips_PC16:
    237       return ELF::R_MIPS_PC16;
    238     case Mips::fixup_MICROMIPS_PC7_S1:
    239       return ELF::R_MICROMIPS_PC7_S1;
    240     case Mips::fixup_MICROMIPS_PC10_S1:
    241       return ELF::R_MICROMIPS_PC10_S1;
    242     case Mips::fixup_MICROMIPS_PC16_S1:
    243       return ELF::R_MICROMIPS_PC16_S1;
    244     case Mips::fixup_MICROMIPS_PC26_S1:
    245       return ELF::R_MICROMIPS_PC26_S1;
    246     case Mips::fixup_MICROMIPS_PC19_S2:
    247       return ELF::R_MICROMIPS_PC19_S2;
    248     case Mips::fixup_MICROMIPS_PC18_S3:
    249       return ELF::R_MICROMIPS_PC18_S3;
    250     case Mips::fixup_MICROMIPS_PC21_S1:
    251       return ELF::R_MICROMIPS_PC21_S1;
    252     case Mips::fixup_MIPS_PC19_S2:
    253       return ELF::R_MIPS_PC19_S2;
    254     case Mips::fixup_MIPS_PC18_S3:
    255       return ELF::R_MIPS_PC18_S3;
    256     case Mips::fixup_MIPS_PC21_S2:
    257       return ELF::R_MIPS_PC21_S2;
    258     case Mips::fixup_MIPS_PC26_S2:
    259       return ELF::R_MIPS_PC26_S2;
    260     case Mips::fixup_MIPS_PCHI16:
    261       return ELF::R_MIPS_PCHI16;
    262     case Mips::fixup_MIPS_PCLO16:
    263       return ELF::R_MIPS_PCLO16;
    264     }
    265 
    266     llvm_unreachable("invalid PC-relative fixup kind!");
    267   }
    268 
    269   switch (Kind) {
    270   case Mips::fixup_Mips_64:
    271   case FK_Data_8:
    272     return ELF::R_MIPS_64;
    273   case FK_GPRel_4:
    274     if (isN64()) {
    275       unsigned Type = (unsigned)ELF::R_MIPS_NONE;
    276       Type = setRType((unsigned)ELF::R_MIPS_GPREL32, Type);
    277       Type = setRType2((unsigned)ELF::R_MIPS_64, Type);
    278       Type = setRType3((unsigned)ELF::R_MIPS_NONE, Type);
    279       return Type;
    280     }
    281     return ELF::R_MIPS_GPREL32;
    282   case Mips::fixup_Mips_GPREL16:
    283     return ELF::R_MIPS_GPREL16;
    284   case Mips::fixup_Mips_26:
    285     return ELF::R_MIPS_26;
    286   case Mips::fixup_Mips_CALL16:
    287     return ELF::R_MIPS_CALL16;
    288   case Mips::fixup_Mips_GOT:
    289     return ELF::R_MIPS_GOT16;
    290   case Mips::fixup_Mips_HI16:
    291     return ELF::R_MIPS_HI16;
    292   case Mips::fixup_Mips_LO16:
    293     return ELF::R_MIPS_LO16;
    294   case Mips::fixup_Mips_TLSGD:
    295     return ELF::R_MIPS_TLS_GD;
    296   case Mips::fixup_Mips_GOTTPREL:
    297     return ELF::R_MIPS_TLS_GOTTPREL;
    298   case Mips::fixup_Mips_TPREL_HI:
    299     return ELF::R_MIPS_TLS_TPREL_HI16;
    300   case Mips::fixup_Mips_TPREL_LO:
    301     return ELF::R_MIPS_TLS_TPREL_LO16;
    302   case Mips::fixup_Mips_TLSLDM:
    303     return ELF::R_MIPS_TLS_LDM;
    304   case Mips::fixup_Mips_DTPREL_HI:
    305     return ELF::R_MIPS_TLS_DTPREL_HI16;
    306   case Mips::fixup_Mips_DTPREL_LO:
    307     return ELF::R_MIPS_TLS_DTPREL_LO16;
    308   case Mips::fixup_Mips_GOT_PAGE:
    309     return ELF::R_MIPS_GOT_PAGE;
    310   case Mips::fixup_Mips_GOT_OFST:
    311     return ELF::R_MIPS_GOT_OFST;
    312   case Mips::fixup_Mips_GOT_DISP:
    313     return ELF::R_MIPS_GOT_DISP;
    314   case Mips::fixup_Mips_GPOFF_HI: {
    315     unsigned Type = (unsigned)ELF::R_MIPS_NONE;
    316     Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
    317     Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
    318     Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
    319     return Type;
    320   }
    321   case Mips::fixup_Mips_GPOFF_LO: {
    322     unsigned Type = (unsigned)ELF::R_MIPS_NONE;
    323     Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
    324     Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
    325     Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
    326     return Type;
    327   }
    328   case Mips::fixup_Mips_HIGHER:
    329     return ELF::R_MIPS_HIGHER;
    330   case Mips::fixup_Mips_HIGHEST:
    331     return ELF::R_MIPS_HIGHEST;
    332   case Mips::fixup_Mips_GOT_HI16:
    333     return ELF::R_MIPS_GOT_HI16;
    334   case Mips::fixup_Mips_GOT_LO16:
    335     return ELF::R_MIPS_GOT_LO16;
    336   case Mips::fixup_Mips_CALL_HI16:
    337     return ELF::R_MIPS_CALL_HI16;
    338   case Mips::fixup_Mips_CALL_LO16:
    339     return ELF::R_MIPS_CALL_LO16;
    340   case Mips::fixup_MICROMIPS_26_S1:
    341     return ELF::R_MICROMIPS_26_S1;
    342   case Mips::fixup_MICROMIPS_HI16:
    343     return ELF::R_MICROMIPS_HI16;
    344   case Mips::fixup_MICROMIPS_LO16:
    345     return ELF::R_MICROMIPS_LO16;
    346   case Mips::fixup_MICROMIPS_GOT16:
    347     return ELF::R_MICROMIPS_GOT16;
    348   case Mips::fixup_MICROMIPS_CALL16:
    349     return ELF::R_MICROMIPS_CALL16;
    350   case Mips::fixup_MICROMIPS_GOT_DISP:
    351     return ELF::R_MICROMIPS_GOT_DISP;
    352   case Mips::fixup_MICROMIPS_GOT_PAGE:
    353     return ELF::R_MICROMIPS_GOT_PAGE;
    354   case Mips::fixup_MICROMIPS_GOT_OFST:
    355     return ELF::R_MICROMIPS_GOT_OFST;
    356   case Mips::fixup_MICROMIPS_TLS_GD:
    357     return ELF::R_MICROMIPS_TLS_GD;
    358   case Mips::fixup_MICROMIPS_TLS_LDM:
    359     return ELF::R_MICROMIPS_TLS_LDM;
    360   case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16:
    361     return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
    362   case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16:
    363     return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
    364   case Mips::fixup_MICROMIPS_TLS_TPREL_HI16:
    365     return ELF::R_MICROMIPS_TLS_TPREL_HI16;
    366   case Mips::fixup_MICROMIPS_TLS_TPREL_LO16:
    367     return ELF::R_MICROMIPS_TLS_TPREL_LO16;
    368   }
    369 
    370   llvm_unreachable("invalid fixup kind!");
    371 }
    372 
    373 /// Sort relocation table entries by offset except where another order is
    374 /// required by the MIPS ABI.
    375 ///
    376 /// MIPS has a few relocations that have an AHL component in the expression used
    377 /// to evaluate them. This AHL component is an addend with the same number of
    378 /// bits as a symbol value but not all of our ABI's are able to supply a
    379 /// sufficiently sized addend in a single relocation.
    380 ///
    381 /// The O32 ABI for example, uses REL relocations which store the addend in the
    382 /// section data. All the relocations with AHL components affect 16-bit fields
    383 /// so the addend for a single relocation is limited to 16-bit. This ABI
    384 /// resolves the limitation by linking relocations (e.g. R_MIPS_HI16 and
    385 /// R_MIPS_LO16) and distributing the addend between the linked relocations. The
    386 /// ABI mandates that such relocations must be next to each other in a
    387 /// particular order (e.g. R_MIPS_HI16 must be immediately followed by a
    388 /// matching R_MIPS_LO16) but the rule is less strict in practice.
    389 ///
    390 /// The de facto standard is lenient in the following ways:
    391 /// - 'Immediately following' does not refer to the next relocation entry but
    392 ///   the next matching relocation.
    393 /// - There may be multiple high parts relocations for one low part relocation.
    394 /// - There may be multiple low part relocations for one high part relocation.
    395 /// - The AHL addend in each part does not have to be exactly equal as long as
    396 ///   the difference does not affect the carry bit from bit 15 into 16. This is
    397 ///   to allow, for example, the use of %lo(foo) and %lo(foo+4) when loading
    398 ///   both halves of a long long.
    399 ///
    400 /// See getMatchingLoType() for a description of which high part relocations
    401 /// match which low part relocations. One particular thing to note is that
    402 /// R_MIPS_GOT16 and similar only have AHL addends if they refer to local
    403 /// symbols.
    404 ///
    405 /// It should also be noted that this function is not affected by whether
    406 /// the symbol was kept or rewritten into a section-relative equivalent. We
    407 /// always match using the expressions from the source.
    408 void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
    409                                      std::vector<ELFRelocationEntry> &Relocs) {
    410   if (Relocs.size() < 2)
    411     return;
    412 
    413   // Sort relocations by the address they are applied to.
    414   std::sort(Relocs.begin(), Relocs.end(),
    415             [](const ELFRelocationEntry &A, const ELFRelocationEntry &B) {
    416               return A.Offset < B.Offset;
    417             });
    418 
    419   std::list<MipsRelocationEntry> Sorted;
    420   std::list<ELFRelocationEntry> Remainder;
    421 
    422   DEBUG(dumpRelocs("R: ", Relocs));
    423 
    424   // Separate the movable relocations (AHL relocations using the high bits) from
    425   // the immobile relocations (everything else). This does not preserve high/low
    426   // matches that already existed in the input.
    427   copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
    428                std::back_inserter(Sorted), [](const ELFRelocationEntry &Reloc) {
    429                  return getMatchingLoType(Reloc) != ELF::R_MIPS_NONE;
    430                });
    431 
    432   for (auto &R : Remainder) {
    433     DEBUG(dbgs() << "Matching: " << R << "\n");
    434 
    435     unsigned MatchingType = getMatchingLoType(R);
    436     assert(MatchingType != ELF::R_MIPS_NONE &&
    437            "Wrong list for reloc that doesn't need a match");
    438 
    439     // Find the best matching relocation for the current high part.
    440     // See isMatchingReloc for a description of a matching relocation and
    441     // compareMatchingRelocs for a description of what 'best' means.
    442     auto InsertionPoint =
    443         find_best(Sorted.begin(), Sorted.end(),
    444                   [&R, &MatchingType](const MipsRelocationEntry &X) {
    445                     return isMatchingReloc(X, R, MatchingType);
    446                   },
    447                   compareMatchingRelocs);
    448 
    449     // If we matched then insert the high part in front of the match and mark
    450     // both relocations as being involved in a match. We only mark the high
    451     // part for cosmetic reasons in the debug output.
    452     //
    453     // If we failed to find a match then the high part is orphaned. This is not
    454     // permitted since the relocation cannot be evaluated without knowing the
    455     // carry-in. We can sometimes handle this using a matching low part that is
    456     // already used in a match but we already cover that case in
    457     // isMatchingReloc and compareMatchingRelocs. For the remaining cases we
    458     // should insert the high part at the end of the list. This will cause the
    459     // linker to fail but the alternative is to cause the linker to bind the
    460     // high part to a semi-matching low part and silently calculate the wrong
    461     // value. Unfortunately we have no means to warn the user that we did this
    462     // so leave it up to the linker to complain about it.
    463     if (InsertionPoint != Sorted.end())
    464       InsertionPoint->Matched = true;
    465     Sorted.insert(InsertionPoint, R)->Matched = true;
    466   }
    467 
    468   DEBUG(dumpRelocs("S: ", Sorted));
    469 
    470   assert(Relocs.size() == Sorted.size() && "Some relocs were not consumed");
    471 
    472   // Overwrite the original vector with the sorted elements. The caller expects
    473   // them in reverse order.
    474   unsigned CopyTo = 0;
    475   for (const auto &R : reverse(Sorted))
    476     Relocs[CopyTo++] = R.R;
    477 }
    478 
    479 bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
    480                                                   unsigned Type) const {
    481   // If it's a compound relocation for N64 then we need the relocation if any
    482   // sub-relocation needs it.
    483   if (!isUInt<8>(Type))
    484     return needsRelocateWithSymbol(Sym, Type & 0xff) ||
    485            needsRelocateWithSymbol(Sym, (Type >> 8) & 0xff) ||
    486            needsRelocateWithSymbol(Sym, (Type >> 16) & 0xff);
    487 
    488   switch (Type) {
    489   default:
    490     errs() << Type << "\n";
    491     llvm_unreachable("Unexpected relocation");
    492     return true;
    493 
    494   // This relocation doesn't affect the section data.
    495   case ELF::R_MIPS_NONE:
    496     return false;
    497 
    498   // On REL ABI's (e.g. O32), these relocations form pairs. The pairing is done
    499   // by the static linker by matching the symbol and offset.
    500   // We only see one relocation at a time but it's still safe to relocate with
    501   // the section so long as both relocations make the same decision.
    502   //
    503   // Some older linkers may require the symbol for particular cases. Such cases
    504   // are not supported yet but can be added as required.
    505   case ELF::R_MIPS_GOT16:
    506   case ELF::R_MIPS16_GOT16:
    507   case ELF::R_MICROMIPS_GOT16:
    508   case ELF::R_MIPS_HI16:
    509   case ELF::R_MIPS16_HI16:
    510   case ELF::R_MICROMIPS_HI16:
    511   case ELF::R_MIPS_LO16:
    512   case ELF::R_MIPS16_LO16:
    513   case ELF::R_MICROMIPS_LO16:
    514     // FIXME: It should be safe to return false for the STO_MIPS_MICROMIPS but
    515     //        we neglect to handle the adjustment to the LSB of the addend that
    516     //        it causes in applyFixup() and similar.
    517     if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
    518       return true;
    519     return false;
    520 
    521   case ELF::R_MIPS_16:
    522   case ELF::R_MIPS_32:
    523   case ELF::R_MIPS_GPREL32:
    524     if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
    525       return true;
    526     // fallthrough
    527   case ELF::R_MIPS_26:
    528   case ELF::R_MIPS_64:
    529   case ELF::R_MIPS_GPREL16:
    530   case ELF::R_MIPS_PC16:
    531   case ELF::R_MIPS_SUB:
    532     return false;
    533 
    534   // FIXME: Many of these relocations should probably return false but this
    535   //        hasn't been confirmed to be safe yet.
    536   case ELF::R_MIPS_REL32:
    537   case ELF::R_MIPS_LITERAL:
    538   case ELF::R_MIPS_CALL16:
    539   case ELF::R_MIPS_SHIFT5:
    540   case ELF::R_MIPS_SHIFT6:
    541   case ELF::R_MIPS_GOT_DISP:
    542   case ELF::R_MIPS_GOT_PAGE:
    543   case ELF::R_MIPS_GOT_OFST:
    544   case ELF::R_MIPS_GOT_HI16:
    545   case ELF::R_MIPS_GOT_LO16:
    546   case ELF::R_MIPS_INSERT_A:
    547   case ELF::R_MIPS_INSERT_B:
    548   case ELF::R_MIPS_DELETE:
    549   case ELF::R_MIPS_HIGHER:
    550   case ELF::R_MIPS_HIGHEST:
    551   case ELF::R_MIPS_CALL_HI16:
    552   case ELF::R_MIPS_CALL_LO16:
    553   case ELF::R_MIPS_SCN_DISP:
    554   case ELF::R_MIPS_REL16:
    555   case ELF::R_MIPS_ADD_IMMEDIATE:
    556   case ELF::R_MIPS_PJUMP:
    557   case ELF::R_MIPS_RELGOT:
    558   case ELF::R_MIPS_JALR:
    559   case ELF::R_MIPS_TLS_DTPMOD32:
    560   case ELF::R_MIPS_TLS_DTPREL32:
    561   case ELF::R_MIPS_TLS_DTPMOD64:
    562   case ELF::R_MIPS_TLS_DTPREL64:
    563   case ELF::R_MIPS_TLS_GD:
    564   case ELF::R_MIPS_TLS_LDM:
    565   case ELF::R_MIPS_TLS_DTPREL_HI16:
    566   case ELF::R_MIPS_TLS_DTPREL_LO16:
    567   case ELF::R_MIPS_TLS_GOTTPREL:
    568   case ELF::R_MIPS_TLS_TPREL32:
    569   case ELF::R_MIPS_TLS_TPREL64:
    570   case ELF::R_MIPS_TLS_TPREL_HI16:
    571   case ELF::R_MIPS_TLS_TPREL_LO16:
    572   case ELF::R_MIPS_GLOB_DAT:
    573   case ELF::R_MIPS_PC21_S2:
    574   case ELF::R_MIPS_PC26_S2:
    575   case ELF::R_MIPS_PC18_S3:
    576   case ELF::R_MIPS_PC19_S2:
    577   case ELF::R_MIPS_PCHI16:
    578   case ELF::R_MIPS_PCLO16:
    579   case ELF::R_MIPS_COPY:
    580   case ELF::R_MIPS_JUMP_SLOT:
    581   case ELF::R_MIPS_NUM:
    582   case ELF::R_MIPS_PC32:
    583   case ELF::R_MIPS_EH:
    584   case ELF::R_MICROMIPS_26_S1:
    585   case ELF::R_MICROMIPS_GPREL16:
    586   case ELF::R_MICROMIPS_LITERAL:
    587   case ELF::R_MICROMIPS_PC7_S1:
    588   case ELF::R_MICROMIPS_PC10_S1:
    589   case ELF::R_MICROMIPS_PC16_S1:
    590   case ELF::R_MICROMIPS_CALL16:
    591   case ELF::R_MICROMIPS_GOT_DISP:
    592   case ELF::R_MICROMIPS_GOT_PAGE:
    593   case ELF::R_MICROMIPS_GOT_OFST:
    594   case ELF::R_MICROMIPS_GOT_HI16:
    595   case ELF::R_MICROMIPS_GOT_LO16:
    596   case ELF::R_MICROMIPS_SUB:
    597   case ELF::R_MICROMIPS_HIGHER:
    598   case ELF::R_MICROMIPS_HIGHEST:
    599   case ELF::R_MICROMIPS_CALL_HI16:
    600   case ELF::R_MICROMIPS_CALL_LO16:
    601   case ELF::R_MICROMIPS_SCN_DISP:
    602   case ELF::R_MICROMIPS_JALR:
    603   case ELF::R_MICROMIPS_HI0_LO16:
    604   case ELF::R_MICROMIPS_TLS_GD:
    605   case ELF::R_MICROMIPS_TLS_LDM:
    606   case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
    607   case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
    608   case ELF::R_MICROMIPS_TLS_GOTTPREL:
    609   case ELF::R_MICROMIPS_TLS_TPREL_HI16:
    610   case ELF::R_MICROMIPS_TLS_TPREL_LO16:
    611   case ELF::R_MICROMIPS_GPREL7_S2:
    612   case ELF::R_MICROMIPS_PC23_S2:
    613   case ELF::R_MICROMIPS_PC21_S1:
    614   case ELF::R_MICROMIPS_PC26_S1:
    615   case ELF::R_MICROMIPS_PC18_S3:
    616   case ELF::R_MICROMIPS_PC19_S2:
    617     return true;
    618 
    619   // FIXME: Many of these should probably return false but MIPS16 isn't
    620   //        supported by the integrated assembler.
    621   case ELF::R_MIPS16_26:
    622   case ELF::R_MIPS16_GPREL:
    623   case ELF::R_MIPS16_CALL16:
    624   case ELF::R_MIPS16_TLS_GD:
    625   case ELF::R_MIPS16_TLS_LDM:
    626   case ELF::R_MIPS16_TLS_DTPREL_HI16:
    627   case ELF::R_MIPS16_TLS_DTPREL_LO16:
    628   case ELF::R_MIPS16_TLS_GOTTPREL:
    629   case ELF::R_MIPS16_TLS_TPREL_HI16:
    630   case ELF::R_MIPS16_TLS_TPREL_LO16:
    631     llvm_unreachable("Unsupported MIPS16 relocation");
    632     return true;
    633   }
    634 }
    635 
    636 MCObjectWriter *llvm::createMipsELFObjectWriter(raw_pwrite_stream &OS,
    637                                                 uint8_t OSABI,
    638                                                 bool IsLittleEndian,
    639                                                 bool Is64Bit) {
    640   MCELFObjectTargetWriter *MOTW =
    641       new MipsELFObjectWriter(Is64Bit, OSABI, Is64Bit, IsLittleEndian);
    642   return createELFObjectWriter(MOTW, OS, IsLittleEndian);
    643 }
    644