32 #define DEBUG_TYPE "mips-elf-object-writer" 39 struct MipsRelocationEntry {
47 Out <<
", Matched=" << Matched;
60 MipsELFObjectWriter(uint8_t OSABI,
bool HasRelocationAddend,
bool Is64);
62 ~MipsELFObjectWriter()
override =
default;
66 bool needsRelocateWithSymbol(
const MCSymbol &Sym,
67 unsigned Type)
const override;
69 std::vector<ELFRelocationEntry> &Relocs)
override;
77 FindBest_PerfectMatch,
85 template <
class InputIt,
class OutputIt1,
class OutputIt2,
class UnaryPredicate>
86 static std::pair<OutputIt1, OutputIt2>
copy_if_else(InputIt First, InputIt Last,
87 OutputIt1 d1, OutputIt2 d2,
89 for (InputIt
I = First;
I != Last; ++
I) {
99 return std::make_pair(d1, d2);
111 template <
class InputIt,
class UnaryPredicate,
class Comparator>
113 Comparator BetterThan) {
116 for (InputIt
I = First;
I != Last; ++
I) {
118 if (Matched != FindBest_NoMatch) {
121 if (Best == Last || BetterThan(*
I, *Best)) {
126 if (Matched == FindBest_PerfectMatch) {
144 if (Type == ELF::R_MIPS_HI16)
145 return ELF::R_MIPS_LO16;
146 if (Type == ELF::R_MICROMIPS_HI16)
147 return ELF::R_MICROMIPS_LO16;
148 if (Type == ELF::R_MIPS16_HI16)
149 return ELF::R_MIPS16_LO16;
153 return ELF::R_MIPS_NONE;
155 if (Type == ELF::R_MIPS_GOT16)
156 return ELF::R_MIPS_LO16;
157 if (Type == ELF::R_MICROMIPS_GOT16)
158 return ELF::R_MICROMIPS_LO16;
159 if (Type == ELF::R_MIPS16_GOT16)
160 return ELF::R_MIPS16_LO16;
162 return ELF::R_MIPS_NONE;
181 unsigned MatchingType) {
182 if (X.R.Type == MatchingType && X.R.OriginalSymbol == R.
OriginalSymbol) {
185 return FindBest_PerfectMatch;
187 return FindBest_Match;
189 return FindBest_NoMatch;
199 const MipsRelocationEntry &PreviousBest) {
200 if (Candidate.R.OriginalAddend != PreviousBest.R.OriginalAddend)
201 return Candidate.R.OriginalAddend < PreviousBest.R.OriginalAddend;
202 return PreviousBest.Matched && !Candidate.Matched;
207 template <
class Container>
209 for (
const auto &R : Relocs)
210 dbgs() << Prefix << R <<
"\n";
214 MipsELFObjectWriter::MipsELFObjectWriter(uint8_t OSABI,
215 bool HasRelocationAddend,
bool Is64)
221 bool IsPCRel)
const {
227 return ELF::R_MIPS_NONE;
230 "MIPS does not support one byte relocations");
231 return ELF::R_MIPS_NONE;
234 return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
237 return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
244 "MIPS does not support 64-bit PC-relative relocations");
245 return ELF::R_MIPS_NONE;
248 return ELF::R_MIPS_PC16;
250 return ELF::R_MICROMIPS_PC7_S1;
252 return ELF::R_MICROMIPS_PC10_S1;
254 return ELF::R_MICROMIPS_PC16_S1;
256 return ELF::R_MICROMIPS_PC26_S1;
258 return ELF::R_MICROMIPS_PC19_S2;
260 return ELF::R_MICROMIPS_PC18_S3;
262 return ELF::R_MICROMIPS_PC21_S1;
264 return ELF::R_MIPS_PC19_S2;
266 return ELF::R_MIPS_PC18_S3;
268 return ELF::R_MIPS_PC21_S2;
270 return ELF::R_MIPS_PC26_S2;
272 return ELF::R_MIPS_PCHI16;
274 return ELF::R_MIPS_PCLO16;
283 return ELF::R_MIPS_64;
285 return ELF::R_MIPS_TLS_DTPREL32;
287 return ELF::R_MIPS_TLS_DTPREL64;
289 return ELF::R_MIPS_TLS_TPREL32;
291 return ELF::R_MIPS_TLS_TPREL64;
295 Type = setRType((
unsigned)ELF::R_MIPS_GPREL32, Type);
296 Type = setRType2((
unsigned)ELF::R_MIPS_64, Type);
297 Type = setRType3((
unsigned)ELF::R_MIPS_NONE, Type);
300 return ELF::R_MIPS_GPREL32;
302 return ELF::R_MIPS_GPREL16;
304 return ELF::R_MIPS_26;
306 return ELF::R_MIPS_CALL16;
308 return ELF::R_MIPS_GOT16;
310 return ELF::R_MIPS_HI16;
312 return ELF::R_MIPS_LO16;
314 return ELF::R_MIPS_TLS_GD;
316 return ELF::R_MIPS_TLS_GOTTPREL;
318 return ELF::R_MIPS_TLS_TPREL_HI16;
320 return ELF::R_MIPS_TLS_TPREL_LO16;
322 return ELF::R_MIPS_TLS_LDM;
324 return ELF::R_MIPS_TLS_DTPREL_HI16;
326 return ELF::R_MIPS_TLS_DTPREL_LO16;
328 return ELF::R_MIPS_GOT_PAGE;
330 return ELF::R_MIPS_GOT_OFST;
332 return ELF::R_MIPS_GOT_DISP;
335 Type = setRType((
unsigned)ELF::R_MIPS_GPREL16, Type);
336 Type = setRType2((
unsigned)ELF::R_MIPS_SUB, Type);
337 Type = setRType3((
unsigned)ELF::R_MIPS_HI16, Type);
342 Type = setRType((
unsigned)ELF::R_MICROMIPS_GPREL16, Type);
343 Type = setRType2((
unsigned)ELF::R_MICROMIPS_SUB, Type);
344 Type = setRType3((
unsigned)ELF::R_MICROMIPS_HI16, Type);
349 Type = setRType((
unsigned)ELF::R_MIPS_GPREL16, Type);
350 Type = setRType2((
unsigned)ELF::R_MIPS_SUB, Type);
351 Type = setRType3((
unsigned)ELF::R_MIPS_LO16, Type);
356 Type = setRType((
unsigned)ELF::R_MICROMIPS_GPREL16, Type);
357 Type = setRType2((
unsigned)ELF::R_MICROMIPS_SUB, Type);
358 Type = setRType3((
unsigned)ELF::R_MICROMIPS_LO16, Type);
362 return ELF::R_MIPS_HIGHER;
364 return ELF::R_MIPS_HIGHEST;
366 return ELF::R_MIPS_SUB;
368 return ELF::R_MIPS_GOT_HI16;
370 return ELF::R_MIPS_GOT_LO16;
372 return ELF::R_MIPS_CALL_HI16;
374 return ELF::R_MIPS_CALL_LO16;
376 return ELF::R_MICROMIPS_26_S1;
378 return ELF::R_MICROMIPS_HI16;
380 return ELF::R_MICROMIPS_LO16;
382 return ELF::R_MICROMIPS_GOT16;
384 return ELF::R_MICROMIPS_CALL16;
386 return ELF::R_MICROMIPS_GOT_DISP;
388 return ELF::R_MICROMIPS_GOT_PAGE;
390 return ELF::R_MICROMIPS_GOT_OFST;
392 return ELF::R_MICROMIPS_TLS_GD;
394 return ELF::R_MICROMIPS_TLS_LDM;
396 return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
398 return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
400 return ELF::R_MICROMIPS_TLS_GOTTPREL;
402 return ELF::R_MICROMIPS_TLS_TPREL_HI16;
404 return ELF::R_MICROMIPS_TLS_TPREL_LO16;
406 return ELF::R_MICROMIPS_SUB;
408 return ELF::R_MICROMIPS_HIGHER;
410 return ELF::R_MICROMIPS_HIGHEST;
412 return ELF::R_MIPS_JALR;
414 return ELF::R_MICROMIPS_JALR;
456 std::vector<ELFRelocationEntry> &Relocs) {
460 if (hasRelocationAddend())
463 if (Relocs.size() < 2)
472 std::list<MipsRelocationEntry> Sorted;
473 std::list<ELFRelocationEntry> Remainder;
480 copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
485 for (
auto &R : Remainder) {
489 assert(MatchingType != ELF::R_MIPS_NONE &&
490 "Wrong list for reloc that doesn't need a match");
495 auto InsertionPoint =
497 [&R, &MatchingType](
const MipsRelocationEntry &
X) {
516 if (InsertionPoint != Sorted.end())
517 InsertionPoint->Matched =
true;
518 Sorted.insert(InsertionPoint, R)->Matched =
true;
523 assert(Relocs.size() == Sorted.size() &&
"Some relocs were not consumed");
528 for (
const auto &R :
reverse(Sorted))
529 Relocs[CopyTo++] = R.R;
532 bool MipsELFObjectWriter::needsRelocateWithSymbol(
const MCSymbol &Sym,
533 unsigned Type)
const {
537 return needsRelocateWithSymbol(Sym, Type & 0xff) ||
538 needsRelocateWithSymbol(Sym, (Type >> 8) & 0xff) ||
539 needsRelocateWithSymbol(Sym, (Type >> 16) & 0xff);
543 errs() << Type <<
"\n";
548 case ELF::R_MIPS_NONE:
558 case ELF::R_MIPS_GOT16:
559 case ELF::R_MIPS16_GOT16:
560 case ELF::R_MICROMIPS_GOT16:
561 case ELF::R_MIPS_HIGHER:
562 case ELF::R_MIPS_HIGHEST:
563 case ELF::R_MIPS_HI16:
564 case ELF::R_MIPS16_HI16:
565 case ELF::R_MICROMIPS_HI16:
566 case ELF::R_MIPS_LO16:
567 case ELF::R_MIPS16_LO16:
568 case ELF::R_MICROMIPS_LO16:
576 case ELF::R_MIPS_GOT_PAGE:
577 case ELF::R_MICROMIPS_GOT_PAGE:
578 case ELF::R_MIPS_GOT_OFST:
579 case ELF::R_MICROMIPS_GOT_OFST:
582 case ELF::R_MIPS_GPREL32:
588 case ELF::R_MIPS_GPREL16:
589 case ELF::R_MIPS_PC16:
590 case ELF::R_MIPS_SUB:
595 case ELF::R_MIPS_REL32:
596 case ELF::R_MIPS_LITERAL:
597 case ELF::R_MIPS_CALL16:
598 case ELF::R_MIPS_SHIFT5:
599 case ELF::R_MIPS_SHIFT6:
600 case ELF::R_MIPS_GOT_DISP:
601 case ELF::R_MIPS_GOT_HI16:
602 case ELF::R_MIPS_GOT_LO16:
603 case ELF::R_MIPS_INSERT_A:
604 case ELF::R_MIPS_INSERT_B:
605 case ELF::R_MIPS_DELETE:
606 case ELF::R_MIPS_CALL_HI16:
607 case ELF::R_MIPS_CALL_LO16:
608 case ELF::R_MIPS_SCN_DISP:
609 case ELF::R_MIPS_REL16:
610 case ELF::R_MIPS_ADD_IMMEDIATE:
611 case ELF::R_MIPS_PJUMP:
612 case ELF::R_MIPS_RELGOT:
613 case ELF::R_MIPS_JALR:
614 case ELF::R_MIPS_TLS_DTPMOD32:
615 case ELF::R_MIPS_TLS_DTPREL32:
616 case ELF::R_MIPS_TLS_DTPMOD64:
617 case ELF::R_MIPS_TLS_DTPREL64:
618 case ELF::R_MIPS_TLS_GD:
619 case ELF::R_MIPS_TLS_LDM:
620 case ELF::R_MIPS_TLS_DTPREL_HI16:
621 case ELF::R_MIPS_TLS_DTPREL_LO16:
622 case ELF::R_MIPS_TLS_GOTTPREL:
623 case ELF::R_MIPS_TLS_TPREL32:
624 case ELF::R_MIPS_TLS_TPREL64:
625 case ELF::R_MIPS_TLS_TPREL_HI16:
626 case ELF::R_MIPS_TLS_TPREL_LO16:
627 case ELF::R_MIPS_GLOB_DAT:
628 case ELF::R_MIPS_PC21_S2:
629 case ELF::R_MIPS_PC26_S2:
630 case ELF::R_MIPS_PC18_S3:
631 case ELF::R_MIPS_PC19_S2:
632 case ELF::R_MIPS_PCHI16:
633 case ELF::R_MIPS_PCLO16:
634 case ELF::R_MIPS_COPY:
635 case ELF::R_MIPS_JUMP_SLOT:
636 case ELF::R_MIPS_NUM:
637 case ELF::R_MIPS_PC32:
639 case ELF::R_MICROMIPS_26_S1:
640 case ELF::R_MICROMIPS_GPREL16:
641 case ELF::R_MICROMIPS_LITERAL:
642 case ELF::R_MICROMIPS_PC7_S1:
643 case ELF::R_MICROMIPS_PC10_S1:
644 case ELF::R_MICROMIPS_PC16_S1:
645 case ELF::R_MICROMIPS_CALL16:
646 case ELF::R_MICROMIPS_GOT_DISP:
647 case ELF::R_MICROMIPS_GOT_HI16:
648 case ELF::R_MICROMIPS_GOT_LO16:
649 case ELF::R_MICROMIPS_SUB:
650 case ELF::R_MICROMIPS_HIGHER:
651 case ELF::R_MICROMIPS_HIGHEST:
652 case ELF::R_MICROMIPS_CALL_HI16:
653 case ELF::R_MICROMIPS_CALL_LO16:
654 case ELF::R_MICROMIPS_SCN_DISP:
655 case ELF::R_MICROMIPS_JALR:
656 case ELF::R_MICROMIPS_HI0_LO16:
657 case ELF::R_MICROMIPS_TLS_GD:
658 case ELF::R_MICROMIPS_TLS_LDM:
659 case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
660 case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
661 case ELF::R_MICROMIPS_TLS_GOTTPREL:
662 case ELF::R_MICROMIPS_TLS_TPREL_HI16:
663 case ELF::R_MICROMIPS_TLS_TPREL_LO16:
664 case ELF::R_MICROMIPS_GPREL7_S2:
665 case ELF::R_MICROMIPS_PC23_S2:
666 case ELF::R_MICROMIPS_PC21_S1:
667 case ELF::R_MICROMIPS_PC26_S1:
668 case ELF::R_MICROMIPS_PC18_S3:
669 case ELF::R_MICROMIPS_PC19_S2:
674 case ELF::R_MIPS16_26:
675 case ELF::R_MIPS16_GPREL:
676 case ELF::R_MIPS16_CALL16:
677 case ELF::R_MIPS16_TLS_GD:
678 case ELF::R_MIPS16_TLS_LDM:
679 case ELF::R_MIPS16_TLS_DTPREL_HI16:
680 case ELF::R_MIPS16_TLS_DTPREL_LO16:
681 case ELF::R_MIPS16_TLS_GOTTPREL:
682 case ELF::R_MIPS16_TLS_TPREL_HI16:
683 case ELF::R_MIPS16_TLS_TPREL_LO16:
689 std::unique_ptr<MCObjectTargetWriter>
694 return llvm::make_unique<MipsELFObjectWriter>(OSABI, HasRelocationAddend,
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
A eight-byte dtp relative fixup.
This class represents lattice values for constants.
This represents an "assembler immediate".
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
OSType getOS() const
getOS - Get the parsed operating system type of this triple.
std::unique_ptr< MCObjectTargetWriter > createMipsELFObjectWriter(const Triple &TT, bool IsN32)
unsigned getBinding() const
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
A four-byte tp relative fixup.
static unsigned getRelocType(const MCValue &Target, const MCFixupKind FixupKind, const bool IsPCRel)
Translates generic PPC fixup kind to Mach-O/PPC relocation type enum.
Context object for machine code objects.
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
A four-byte gp relative fixup.
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 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).
A four-byte dtp relative fixup.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
constexpr bool isUInt< 8 >(uint64_t x)
The instances of the Type class are immutable: once they are created, they are never changed...
static void dumpRelocs(const char *Prefix, const Container &Relocs)
Print all the relocations.
static bool is64Bit(const char *name)
static bool compareMatchingRelocs(const MipsRelocationEntry &Candidate, const MipsRelocationEntry &PreviousBest)
Determine whether Candidate or PreviousBest is the better match.
void reportError(SMLoc L, const Twine &Msg)
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
A eight-byte tp relative fixup.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
void sort(IteratorTy Start, IteratorTy End)
PowerPC TLS Dynamic Call Fixup
FindBestPredicateResult
The possible results of the Predicate function used by find_best.
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Target - Wrapper for Target specific information.
bool isArch64Bit() const
Test whether the architecture is 64-bit.
void print(raw_ostream &Out) const
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
const MCSymbolELF * OriginalSymbol
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
static unsigned getMatchingLoType(const ELFRelocationEntry &Reloc)
Determine the low relocation that matches the given relocation.
MCFixupKind getKind() const