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