LLVM 19.0.0git
MipsELFObjectWriter.cpp
Go to the documentation of this file.
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
11#include "llvm/ADT/STLExtras.h"
13#include "llvm/MC/MCContext.h"
15#include "llvm/MC/MCFixup.h"
17#include "llvm/MC/MCSymbolELF.h"
20#include "llvm/Support/Debug.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
33using namespace llvm;
34
35namespace {
36
37/// Holds additional information needed by the relocation ordering algorithm.
38struct 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#ifndef NDEBUG
51raw_ostream &operator<<(raw_ostream &OS, const MipsRelocationEntry &RHS) {
52 RHS.print(OS);
53 return OS;
54}
55#endif
56
57class MipsELFObjectWriter : public MCELFObjectTargetWriter {
58public:
59 MipsELFObjectWriter(uint8_t OSABI, bool HasRelocationAddend, bool Is64);
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 MCValue &Val, 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.
72enum 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.
84template <class InputIt, class OutputIt1, class OutputIt2, class UnaryPredicate>
85static 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.
110template <class InputIt, class UnaryPredicate, class Comparator>
111static 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 LLVM_DEBUG(dbgs() << std::distance(First, I) << " is a match (";
119 I->print(dbgs()); dbgs() << ")\n");
120 if (Best == Last || BetterThan(*I, *Best)) {
121 LLVM_DEBUG(dbgs() << ".. and it beats the last one\n");
122 Best = I;
123 }
124 }
125 if (Matched == FindBest_PerfectMatch) {
126 LLVM_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.
141static 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 &&
152 return ELF::R_MIPS_NONE;
153
154 if (Type == ELF::R_MIPS_GOT16)
155 return ELF::R_MIPS_LO16;
156 if (Type == ELF::R_MICROMIPS_GOT16)
157 return ELF::R_MICROMIPS_LO16;
158 if (Type == ELF::R_MIPS16_GOT16)
159 return ELF::R_MIPS16_LO16;
160
161 return ELF::R_MIPS_NONE;
162}
163
164/// Determine whether a relocation (X) matches the one given in R.
165///
166/// A relocation matches if:
167/// - It's type matches that of a corresponding low part. This is provided in
168/// MatchingType for efficiency.
169/// - It's based on the same symbol.
170/// - It's offset of greater or equal to that of the one given in R.
171/// It should be noted that this rule assumes the programmer does not use
172/// offsets that exceed the alignment of the symbol. The carry-bit will be
173/// incorrect if this is not true.
174///
175/// A matching relocation is unbeatable if:
176/// - It is not already involved in a match.
177/// - It's offset is exactly that of the one given in R.
178static FindBestPredicateResult isMatchingReloc(const MipsRelocationEntry &X,
179 const ELFRelocationEntry &R,
180 unsigned MatchingType) {
181 if (X.R.Type == MatchingType && X.R.OriginalSymbol == R.OriginalSymbol) {
182 if (!X.Matched &&
183 X.R.OriginalAddend == R.OriginalAddend)
184 return FindBest_PerfectMatch;
185 else if (X.R.OriginalAddend >= R.OriginalAddend)
186 return FindBest_Match;
187 }
188 return FindBest_NoMatch;
189}
190
191/// Determine whether Candidate or PreviousBest is the better match.
192/// The return value is true if Candidate is the better match.
193///
194/// A matching relocation is a better match if:
195/// - It has a smaller addend.
196/// - It is not already involved in a match.
197static bool compareMatchingRelocs(const MipsRelocationEntry &Candidate,
198 const MipsRelocationEntry &PreviousBest) {
199 if (Candidate.R.OriginalAddend != PreviousBest.R.OriginalAddend)
200 return Candidate.R.OriginalAddend < PreviousBest.R.OriginalAddend;
201 return PreviousBest.Matched && !Candidate.Matched;
202}
203
204#ifndef NDEBUG
205/// Print all the relocations.
206template <class Container>
207static void dumpRelocs(const char *Prefix, const Container &Relocs) {
208 for (const auto &R : Relocs)
209 dbgs() << Prefix << R << "\n";
210}
211#endif
212
213MipsELFObjectWriter::MipsELFObjectWriter(uint8_t OSABI,
214 bool HasRelocationAddend, bool Is64)
215 : MCELFObjectTargetWriter(Is64, OSABI, ELF::EM_MIPS, HasRelocationAddend) {}
216
217unsigned MipsELFObjectWriter::getRelocType(MCContext &Ctx,
218 const MCValue &Target,
219 const MCFixup &Fixup,
220 bool IsPCRel) const {
221 // Determine the type of the relocation.
222 unsigned Kind = Fixup.getTargetKind();
223 if (Kind >= FirstLiteralRelocationKind)
225
226 switch (Kind) {
227 case FK_NONE:
228 return ELF::R_MIPS_NONE;
229 case FK_Data_1:
230 Ctx.reportError(Fixup.getLoc(),
231 "MIPS does not support one byte relocations");
232 return ELF::R_MIPS_NONE;
234 case FK_Data_2:
235 return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
237 case FK_Data_4:
238 return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
240 case FK_Data_8:
241 return IsPCRel
242 ? setRTypes(ELF::R_MIPS_PC32, ELF::R_MIPS_64, ELF::R_MIPS_NONE)
243 : (unsigned)ELF::R_MIPS_64;
244 }
245
246 if (IsPCRel) {
247 switch (Kind) {
250 return ELF::R_MIPS_PC16;
252 return ELF::R_MICROMIPS_PC7_S1;
254 return ELF::R_MICROMIPS_PC10_S1;
256 return ELF::R_MICROMIPS_PC16_S1;
258 return ELF::R_MICROMIPS_PC26_S1;
260 return ELF::R_MICROMIPS_PC19_S2;
262 return ELF::R_MICROMIPS_PC18_S3;
264 return ELF::R_MICROMIPS_PC21_S1;
266 return ELF::R_MIPS_PC19_S2;
268 return ELF::R_MIPS_PC18_S3;
270 return ELF::R_MIPS_PC21_S2;
272 return ELF::R_MIPS_PC26_S2;
274 return ELF::R_MIPS_PCHI16;
276 return ELF::R_MIPS_PCLO16;
277 }
278
279 llvm_unreachable("invalid PC-relative fixup kind!");
280 }
281
282 switch (Kind) {
283 case FK_DTPRel_4:
284 return ELF::R_MIPS_TLS_DTPREL32;
285 case FK_DTPRel_8:
286 return ELF::R_MIPS_TLS_DTPREL64;
287 case FK_TPRel_4:
288 return ELF::R_MIPS_TLS_TPREL32;
289 case FK_TPRel_8:
290 return ELF::R_MIPS_TLS_TPREL64;
291 case FK_GPRel_4:
292 return setRTypes(ELF::R_MIPS_GPREL32,
293 is64Bit() ? ELF::R_MIPS_64 : ELF::R_MIPS_NONE,
294 ELF::R_MIPS_NONE);
296 return ELF::R_MIPS_GPREL16;
298 return ELF::R_MIPS_26;
300 return ELF::R_MIPS_CALL16;
302 return ELF::R_MIPS_GOT16;
304 return ELF::R_MIPS_HI16;
306 return ELF::R_MIPS_LO16;
308 return ELF::R_MIPS_TLS_GD;
310 return ELF::R_MIPS_TLS_GOTTPREL;
312 return ELF::R_MIPS_TLS_TPREL_HI16;
314 return ELF::R_MIPS_TLS_TPREL_LO16;
316 return ELF::R_MIPS_TLS_LDM;
318 return ELF::R_MIPS_TLS_DTPREL_HI16;
320 return ELF::R_MIPS_TLS_DTPREL_LO16;
322 return ELF::R_MIPS_GOT_PAGE;
324 return ELF::R_MIPS_GOT_OFST;
326 return ELF::R_MIPS_GOT_DISP;
328 return setRTypes(ELF::R_MIPS_GPREL16, ELF::R_MIPS_SUB, ELF::R_MIPS_HI16);
330 return setRTypes(ELF::R_MICROMIPS_GPREL16, ELF::R_MICROMIPS_SUB,
331 ELF::R_MICROMIPS_HI16);
333 return setRTypes(ELF::R_MIPS_GPREL16, ELF::R_MIPS_SUB, ELF::R_MIPS_LO16);
335 return setRTypes(ELF::R_MICROMIPS_GPREL16, ELF::R_MICROMIPS_SUB,
336 ELF::R_MICROMIPS_LO16);
338 return ELF::R_MIPS_HIGHER;
340 return ELF::R_MIPS_HIGHEST;
342 return ELF::R_MIPS_SUB;
344 return ELF::R_MIPS_GOT_HI16;
346 return ELF::R_MIPS_GOT_LO16;
348 return ELF::R_MIPS_CALL_HI16;
350 return ELF::R_MIPS_CALL_LO16;
352 return ELF::R_MICROMIPS_26_S1;
354 return ELF::R_MICROMIPS_HI16;
356 return ELF::R_MICROMIPS_LO16;
358 return ELF::R_MICROMIPS_GOT16;
360 return ELF::R_MICROMIPS_CALL16;
362 return ELF::R_MICROMIPS_GOT_DISP;
364 return ELF::R_MICROMIPS_GOT_PAGE;
366 return ELF::R_MICROMIPS_GOT_OFST;
368 return ELF::R_MICROMIPS_TLS_GD;
370 return ELF::R_MICROMIPS_TLS_LDM;
372 return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
374 return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
376 return ELF::R_MICROMIPS_TLS_GOTTPREL;
378 return ELF::R_MICROMIPS_TLS_TPREL_HI16;
380 return ELF::R_MICROMIPS_TLS_TPREL_LO16;
382 return ELF::R_MICROMIPS_SUB;
384 return ELF::R_MICROMIPS_HIGHER;
386 return ELF::R_MICROMIPS_HIGHEST;
388 return ELF::R_MIPS_JALR;
390 return ELF::R_MICROMIPS_JALR;
391 }
392
393 llvm_unreachable("invalid fixup kind!");
394}
395
396/// Sort relocation table entries by offset except where another order is
397/// required by the MIPS ABI.
398///
399/// MIPS has a few relocations that have an AHL component in the expression used
400/// to evaluate them. This AHL component is an addend with the same number of
401/// bits as a symbol value but not all of our ABI's are able to supply a
402/// sufficiently sized addend in a single relocation.
403///
404/// The O32 ABI for example, uses REL relocations which store the addend in the
405/// section data. All the relocations with AHL components affect 16-bit fields
406/// so the addend for a single relocation is limited to 16-bit. This ABI
407/// resolves the limitation by linking relocations (e.g. R_MIPS_HI16 and
408/// R_MIPS_LO16) and distributing the addend between the linked relocations. The
409/// ABI mandates that such relocations must be next to each other in a
410/// particular order (e.g. R_MIPS_HI16 must be immediately followed by a
411/// matching R_MIPS_LO16) but the rule is less strict in practice.
412///
413/// The de facto standard is lenient in the following ways:
414/// - 'Immediately following' does not refer to the next relocation entry but
415/// the next matching relocation.
416/// - There may be multiple high parts relocations for one low part relocation.
417/// - There may be multiple low part relocations for one high part relocation.
418/// - The AHL addend in each part does not have to be exactly equal as long as
419/// the difference does not affect the carry bit from bit 15 into 16. This is
420/// to allow, for example, the use of %lo(foo) and %lo(foo+4) when loading
421/// both halves of a long long.
422///
423/// See getMatchingLoType() for a description of which high part relocations
424/// match which low part relocations. One particular thing to note is that
425/// R_MIPS_GOT16 and similar only have AHL addends if they refer to local
426/// symbols.
427///
428/// It should also be noted that this function is not affected by whether
429/// the symbol was kept or rewritten into a section-relative equivalent. We
430/// always match using the expressions from the source.
431void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
432 std::vector<ELFRelocationEntry> &Relocs) {
433 // We do not need to sort the relocation table for RELA relocations which
434 // N32/N64 uses as the relocation addend contains the value we require,
435 // rather than it being split across a pair of relocations.
436 if (hasRelocationAddend())
437 return;
438
439 if (Relocs.size() < 2)
440 return;
441
442 // Sort relocations by the address they are applied to.
443 llvm::sort(Relocs,
444 [](const ELFRelocationEntry &A, const ELFRelocationEntry &B) {
445 return A.Offset < B.Offset;
446 });
447
448 std::list<MipsRelocationEntry> Sorted;
449 std::list<ELFRelocationEntry> Remainder;
450
451 LLVM_DEBUG(dumpRelocs("R: ", Relocs));
452
453 // Separate the movable relocations (AHL relocations using the high bits) from
454 // the immobile relocations (everything else). This does not preserve high/low
455 // matches that already existed in the input.
456 copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
457 std::back_inserter(Sorted), [](const ELFRelocationEntry &Reloc) {
458 return getMatchingLoType(Reloc) != ELF::R_MIPS_NONE;
459 });
460
461 for (auto &R : Remainder) {
462 LLVM_DEBUG(dbgs() << "Matching: " << R << "\n");
463
464 unsigned MatchingType = getMatchingLoType(R);
465 assert(MatchingType != ELF::R_MIPS_NONE &&
466 "Wrong list for reloc that doesn't need a match");
467
468 // Find the best matching relocation for the current high part.
469 // See isMatchingReloc for a description of a matching relocation and
470 // compareMatchingRelocs for a description of what 'best' means.
471 auto InsertionPoint =
472 find_best(Sorted.begin(), Sorted.end(),
473 [&R, &MatchingType](const MipsRelocationEntry &X) {
474 return isMatchingReloc(X, R, MatchingType);
475 },
477
478 // If we matched then insert the high part in front of the match and mark
479 // both relocations as being involved in a match. We only mark the high
480 // part for cosmetic reasons in the debug output.
481 //
482 // If we failed to find a match then the high part is orphaned. This is not
483 // permitted since the relocation cannot be evaluated without knowing the
484 // carry-in. We can sometimes handle this using a matching low part that is
485 // already used in a match but we already cover that case in
486 // isMatchingReloc and compareMatchingRelocs. For the remaining cases we
487 // should insert the high part at the end of the list. This will cause the
488 // linker to fail but the alternative is to cause the linker to bind the
489 // high part to a semi-matching low part and silently calculate the wrong
490 // value. Unfortunately we have no means to warn the user that we did this
491 // so leave it up to the linker to complain about it.
492 if (InsertionPoint != Sorted.end())
493 InsertionPoint->Matched = true;
494 Sorted.insert(InsertionPoint, R)->Matched = true;
495 }
496
497 LLVM_DEBUG(dumpRelocs("S: ", Sorted));
498
499 assert(Relocs.size() == Sorted.size() && "Some relocs were not consumed");
500
501 // Overwrite the original vector with the sorted elements.
502 unsigned CopyTo = 0;
503 for (const auto &R : Sorted)
504 Relocs[CopyTo++] = R.R;
505}
506
507bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCValue &Val,
508 const MCSymbol &Sym,
509 unsigned Type) const {
510 // If it's a compound relocation for N64 then we need the relocation if any
511 // sub-relocation needs it.
512 if (!isUInt<8>(Type))
513 return needsRelocateWithSymbol(Val, Sym, Type & 0xff) ||
514 needsRelocateWithSymbol(Val, Sym, (Type >> 8) & 0xff) ||
515 needsRelocateWithSymbol(Val, Sym, (Type >> 16) & 0xff);
516
517 switch (Type) {
518 default:
519 errs() << Type << "\n";
520 llvm_unreachable("Unexpected relocation");
521 return true;
522
523 // This relocation doesn't affect the section data.
524 case ELF::R_MIPS_NONE:
525 return false;
526
527 // On REL ABI's (e.g. O32), these relocations form pairs. The pairing is done
528 // by the static linker by matching the symbol and offset.
529 // We only see one relocation at a time but it's still safe to relocate with
530 // the section so long as both relocations make the same decision.
531 //
532 // Some older linkers may require the symbol for particular cases. Such cases
533 // are not supported yet but can be added as required.
534 case ELF::R_MIPS_GOT16:
535 case ELF::R_MIPS16_GOT16:
536 case ELF::R_MICROMIPS_GOT16:
537 case ELF::R_MIPS_HIGHER:
538 case ELF::R_MIPS_HIGHEST:
539 case ELF::R_MIPS_HI16:
540 case ELF::R_MIPS16_HI16:
541 case ELF::R_MICROMIPS_HI16:
542 case ELF::R_MIPS_LO16:
543 case ELF::R_MIPS16_LO16:
544 case ELF::R_MICROMIPS_LO16:
545 // FIXME: It should be safe to return false for the STO_MIPS_MICROMIPS but
546 // we neglect to handle the adjustment to the LSB of the addend that
547 // it causes in applyFixup() and similar.
548 if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
549 return true;
550 return false;
551
552 case ELF::R_MIPS_GOT_PAGE:
553 case ELF::R_MICROMIPS_GOT_PAGE:
554 case ELF::R_MIPS_GOT_OFST:
555 case ELF::R_MICROMIPS_GOT_OFST:
556 case ELF::R_MIPS_16:
557 case ELF::R_MIPS_32:
558 case ELF::R_MIPS_GPREL32:
559 if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
560 return true;
561 [[fallthrough]];
562 case ELF::R_MIPS_26:
563 case ELF::R_MIPS_64:
564 case ELF::R_MIPS_GPREL16:
565 case ELF::R_MIPS_PC16:
566 case ELF::R_MIPS_SUB:
567 return false;
568
569 // FIXME: Many of these relocations should probably return false but this
570 // hasn't been confirmed to be safe yet.
571 case ELF::R_MIPS_REL32:
572 case ELF::R_MIPS_LITERAL:
573 case ELF::R_MIPS_CALL16:
574 case ELF::R_MIPS_SHIFT5:
575 case ELF::R_MIPS_SHIFT6:
576 case ELF::R_MIPS_GOT_DISP:
577 case ELF::R_MIPS_GOT_HI16:
578 case ELF::R_MIPS_GOT_LO16:
579 case ELF::R_MIPS_INSERT_A:
580 case ELF::R_MIPS_INSERT_B:
581 case ELF::R_MIPS_DELETE:
582 case ELF::R_MIPS_CALL_HI16:
583 case ELF::R_MIPS_CALL_LO16:
584 case ELF::R_MIPS_SCN_DISP:
585 case ELF::R_MIPS_REL16:
586 case ELF::R_MIPS_ADD_IMMEDIATE:
587 case ELF::R_MIPS_PJUMP:
588 case ELF::R_MIPS_RELGOT:
589 case ELF::R_MIPS_JALR:
590 case ELF::R_MIPS_TLS_DTPMOD32:
591 case ELF::R_MIPS_TLS_DTPREL32:
592 case ELF::R_MIPS_TLS_DTPMOD64:
593 case ELF::R_MIPS_TLS_DTPREL64:
594 case ELF::R_MIPS_TLS_GD:
595 case ELF::R_MIPS_TLS_LDM:
596 case ELF::R_MIPS_TLS_DTPREL_HI16:
597 case ELF::R_MIPS_TLS_DTPREL_LO16:
598 case ELF::R_MIPS_TLS_GOTTPREL:
599 case ELF::R_MIPS_TLS_TPREL32:
600 case ELF::R_MIPS_TLS_TPREL64:
601 case ELF::R_MIPS_TLS_TPREL_HI16:
602 case ELF::R_MIPS_TLS_TPREL_LO16:
603 case ELF::R_MIPS_GLOB_DAT:
604 case ELF::R_MIPS_PC21_S2:
605 case ELF::R_MIPS_PC26_S2:
606 case ELF::R_MIPS_PC18_S3:
607 case ELF::R_MIPS_PC19_S2:
608 case ELF::R_MIPS_PCHI16:
609 case ELF::R_MIPS_PCLO16:
610 case ELF::R_MIPS_COPY:
611 case ELF::R_MIPS_JUMP_SLOT:
612 case ELF::R_MIPS_NUM:
613 case ELF::R_MIPS_PC32:
614 case ELF::R_MIPS_EH:
615 case ELF::R_MICROMIPS_26_S1:
616 case ELF::R_MICROMIPS_GPREL16:
617 case ELF::R_MICROMIPS_LITERAL:
618 case ELF::R_MICROMIPS_PC7_S1:
619 case ELF::R_MICROMIPS_PC10_S1:
620 case ELF::R_MICROMIPS_PC16_S1:
621 case ELF::R_MICROMIPS_CALL16:
622 case ELF::R_MICROMIPS_GOT_DISP:
623 case ELF::R_MICROMIPS_GOT_HI16:
624 case ELF::R_MICROMIPS_GOT_LO16:
625 case ELF::R_MICROMIPS_SUB:
626 case ELF::R_MICROMIPS_HIGHER:
627 case ELF::R_MICROMIPS_HIGHEST:
628 case ELF::R_MICROMIPS_CALL_HI16:
629 case ELF::R_MICROMIPS_CALL_LO16:
630 case ELF::R_MICROMIPS_SCN_DISP:
631 case ELF::R_MICROMIPS_JALR:
632 case ELF::R_MICROMIPS_HI0_LO16:
633 case ELF::R_MICROMIPS_TLS_GD:
634 case ELF::R_MICROMIPS_TLS_LDM:
635 case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
636 case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
637 case ELF::R_MICROMIPS_TLS_GOTTPREL:
638 case ELF::R_MICROMIPS_TLS_TPREL_HI16:
639 case ELF::R_MICROMIPS_TLS_TPREL_LO16:
640 case ELF::R_MICROMIPS_GPREL7_S2:
641 case ELF::R_MICROMIPS_PC23_S2:
642 case ELF::R_MICROMIPS_PC21_S1:
643 case ELF::R_MICROMIPS_PC26_S1:
644 case ELF::R_MICROMIPS_PC18_S3:
645 case ELF::R_MICROMIPS_PC19_S2:
646 return true;
647
648 // FIXME: Many of these should probably return false but MIPS16 isn't
649 // supported by the integrated assembler.
650 case ELF::R_MIPS16_26:
651 case ELF::R_MIPS16_GPREL:
652 case ELF::R_MIPS16_CALL16:
653 case ELF::R_MIPS16_TLS_GD:
654 case ELF::R_MIPS16_TLS_LDM:
655 case ELF::R_MIPS16_TLS_DTPREL_HI16:
656 case ELF::R_MIPS16_TLS_DTPREL_LO16:
657 case ELF::R_MIPS16_TLS_GOTTPREL:
658 case ELF::R_MIPS16_TLS_TPREL_HI16:
659 case ELF::R_MIPS16_TLS_TPREL_LO16:
660 llvm_unreachable("Unsupported MIPS16 relocation");
661 return true;
662 }
663}
664
665std::unique_ptr<MCObjectTargetWriter>
667 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
668 bool IsN64 = TT.isArch64Bit() && !IsN32;
669 bool HasRelocationAddend = TT.isArch64Bit();
670 return std::make_unique<MipsELFObjectWriter>(OSABI, HasRelocationAddend,
671 IsN64);
672}
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_DEBUG(X)
Definition: Debug.h:101
Symbol * Sym
Definition: ELF_riscv.cpp:479
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
#define I(x, y, z)
Definition: MD5.cpp:58
static FindBestPredicateResult isMatchingReloc(const MipsRelocationEntry &X, const ELFRelocationEntry &R, unsigned MatchingType)
Determine whether a relocation (X) matches the one given in R.
static InputIt find_best(InputIt First, InputIt Last, UnaryPredicate Predicate, Comparator BetterThan)
Find the best match in the range [First, Last).
static void dumpRelocs(const char *Prefix, const Container &Relocs)
Print all the relocations.
static bool compareMatchingRelocs(const MipsRelocationEntry &Candidate, const MipsRelocationEntry &PreviousBest)
Determine whether Candidate or PreviousBest is the better match.
static std::pair< OutputIt1, OutputIt2 > copy_if_else(InputIt First, InputIt Last, OutputIt1 d1, OutputIt2 d2, UnaryPredicate Predicate)
Copy elements in the range [First, Last) to d1 when the predicate is true or d2 when the predicate is...
static unsigned getMatchingLoType(const ELFRelocationEntry &Reloc)
Determine the low relocation that matches the given relocation.
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
static bool is64Bit(const char *name)
Value * RHS
Context object for machine code objects.
Definition: MCContext.h:81
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1069
virtual void sortRelocs(const MCAssembler &Asm, std::vector< ELFRelocationEntry > &Relocs)
virtual bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym, unsigned Type) const
virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const =0
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
unsigned getBinding() const
Definition: MCSymbolELF.cpp:66
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
This represents an "assembler immediate".
Definition: MCValue.h:36
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Definition: AsmWriter.cpp:4996
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ EM_MIPS
Definition: ELF.h:141
@ STO_MIPS_MICROMIPS
Definition: ELF.h:590
@ STB_LOCAL
Definition: ELF.h:1310
@ fixup_MICROMIPS_TLS_TPREL_LO16
@ fixup_Mips_DTPREL_HI
@ fixup_MICROMIPS_PC7_S1
@ fixup_MICROMIPS_GOT_PAGE
@ fixup_MICROMIPS_PC16_S1
@ fixup_MICROMIPS_HIGHER
@ fixup_MICROMIPS_TLS_TPREL_HI16
@ fixup_MICROMIPS_PC21_S1
@ fixup_MICROMIPS_GPOFF_LO
@ fixup_MICROMIPS_PC19_S2
@ fixup_MICROMIPS_CALL16
@ fixup_MICROMIPS_TLS_LDM
@ fixup_MICROMIPS_GOT_OFST
@ fixup_MICROMIPS_TLS_DTPREL_HI16
@ fixup_MICROMIPS_PC10_S1
@ fixup_MICROMIPS_TLS_GD
@ fixup_MICROMIPS_HIGHEST
@ fixup_MICROMIPS_GOT_DISP
@ fixup_Mips_DTPREL_LO
@ fixup_MICROMIPS_PC18_S3
@ fixup_MICROMIPS_PC26_S1
@ fixup_MICROMIPS_GOTTPREL
@ fixup_MICROMIPS_TLS_DTPREL_LO16
@ fixup_Mips_Branch_PCRel
@ fixup_MICROMIPS_GPOFF_HI
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< MCObjectTargetWriter > createMipsELFObjectWriter(const Triple &TT, bool IsN32)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
@ FirstLiteralRelocationKind
The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for relocations coming from ....
Definition: MCFixup.h:50
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:23
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
@ FK_DTPRel_4
A four-byte dtp relative fixup.
Definition: MCFixup.h:36
@ FK_DTPRel_8
A eight-byte dtp relative fixup.
Definition: MCFixup.h:37
@ FK_NONE
A no-op fixup.
Definition: MCFixup.h:22
@ FK_TPRel_4
A four-byte tp relative fixup.
Definition: MCFixup.h:38
@ FK_GPRel_4
A four-byte gp relative fixup.
Definition: MCFixup.h:34
@ FK_TPRel_8
A eight-byte tp relative fixup.
Definition: MCFixup.h:39
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:24
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:293
const MCSymbolELF * OriginalSymbol