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