31#define DEBUG_TYPE "mips-elf-object-writer"
38struct MipsRelocationEntry {
46 Out <<
", Matched=" << Matched;
59 MipsELFObjectWriter(uint8_t OSABI,
bool HasRelocationAddend,
bool Is64);
61 ~MipsELFObjectWriter()
override =
default;
66 unsigned Type)
const override;
68 std::vector<ELFRelocationEntry> &Relocs)
override;
72enum FindBestPredicateResult {
76 FindBest_PerfectMatch,
84template <
class InputIt,
class OutputIt1,
class OutputIt2,
class UnaryPredicate>
86 OutputIt1 d1, OutputIt2 d2,
87 UnaryPredicate Predicate) {
98 return std::make_pair(d1, d2);
110template <
class InputIt,
class UnaryPredicate,
class Comparator>
112 Comparator BetterThan) {
116 unsigned Matched = Predicate(*
I);
117 if (Matched != FindBest_NoMatch) {
120 if (Best ==
Last || BetterThan(*
I, *Best)) {
125 if (Matched == FindBest_PerfectMatch) {
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;
152 return ELF::R_MIPS_NONE;
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;
161 return ELF::R_MIPS_NONE;
180 unsigned MatchingType) {
181 if (
X.R.Type == MatchingType &&
X.R.OriginalSymbol == R.OriginalSymbol) {
183 X.R.OriginalAddend == R.OriginalAddend)
184 return FindBest_PerfectMatch;
185 else if (
X.R.OriginalAddend >= R.OriginalAddend)
186 return FindBest_Match;
188 return FindBest_NoMatch;
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;
206template <
class Container>
207static void dumpRelocs(
const char *Prefix,
const Container &Relocs) {
208 for (
const auto &R : Relocs)
209 dbgs() << Prefix << R <<
"\n";
213MipsELFObjectWriter::MipsELFObjectWriter(uint8_t OSABI,
214 bool HasRelocationAddend,
bool Is64)
217unsigned MipsELFObjectWriter::getRelocType(
MCContext &Ctx,
220 bool IsPCRel)
const {
228 return ELF::R_MIPS_NONE;
231 "MIPS does not support one byte relocations");
232 return ELF::R_MIPS_NONE;
235 return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
238 return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
242 ? setRTypes(ELF::R_MIPS_PC32, ELF::R_MIPS_64, ELF::R_MIPS_NONE)
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;
284 return ELF::R_MIPS_TLS_DTPREL32;
286 return ELF::R_MIPS_TLS_DTPREL64;
288 return ELF::R_MIPS_TLS_TPREL32;
290 return ELF::R_MIPS_TLS_TPREL64;
292 return setRTypes(ELF::R_MIPS_GPREL32,
293 is64Bit() ? ELF::R_MIPS_64 : 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;
431void MipsELFObjectWriter::sortRelocs(
const MCAssembler &Asm,
432 std::vector<ELFRelocationEntry> &Relocs) {
436 if (hasRelocationAddend())
439 if (Relocs.size() < 2)
445 return A.Offset <
B.Offset;
448 std::list<MipsRelocationEntry> Sorted;
449 std::list<ELFRelocationEntry> Remainder;
456 copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
458 return getMatchingLoType(Reloc) != ELF::R_MIPS_NONE;
461 for (
auto &R : Remainder) {
465 assert(MatchingType != ELF::R_MIPS_NONE &&
466 "Wrong list for reloc that doesn't need a match");
471 auto InsertionPoint =
473 [&R, &MatchingType](
const MipsRelocationEntry &
X) {
474 return isMatchingReloc(X, R, MatchingType);
492 if (InsertionPoint != Sorted.end())
493 InsertionPoint->Matched =
true;
494 Sorted.insert(InsertionPoint, R)->Matched =
true;
499 assert(Relocs.size() == Sorted.size() &&
"Some relocs were not consumed");
503 for (
const auto &R : Sorted)
504 Relocs[CopyTo++] =
R.R;
507bool MipsELFObjectWriter::needsRelocateWithSymbol(
const MCValue &Val,
509 unsigned Type)
const {
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);
524 case ELF::R_MIPS_NONE:
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:
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:
558 case ELF::R_MIPS_GPREL32:
564 case ELF::R_MIPS_GPREL16:
565 case ELF::R_MIPS_PC16:
566 case ELF::R_MIPS_SUB:
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:
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:
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:
665std::unique_ptr<MCObjectTargetWriter>
668 bool IsN64 = TT.isArch64Bit() && !IsN32;
669 bool HasRelocationAddend = TT.isArch64Bit();
670 return std::make_unique<MipsELFObjectWriter>(OSABI, HasRelocationAddend,
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")
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
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())
static bool is64Bit(const char *name)
Context object for machine code objects.
void reportError(SMLoc L, const Twine &Msg)
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)...
unsigned getBinding() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
This represents an "assembler immediate".
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
The instances of the Type class are immutable: once they are created, they are never changed.
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ fixup_MICROMIPS_TLS_TPREL_LO16
@ fixup_MICROMIPS_GOT_PAGE
@ fixup_MICROMIPS_PC16_S1
@ fixup_MICROMIPS_TLS_TPREL_HI16
@ fixup_MICROMIPS_PC21_S1
@ fixup_MICROMIPS_GPOFF_LO
@ fixup_MICROMIPS_PC19_S2
@ fixup_MICROMIPS_TLS_LDM
@ fixup_MICROMIPS_GOT_OFST
@ fixup_MICROMIPS_TLS_DTPREL_HI16
@ fixup_MICROMIPS_PC10_S1
@ fixup_MICROMIPS_HIGHEST
@ fixup_MICROMIPS_GOT_DISP
@ 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.
std::unique_ptr< MCObjectTargetWriter > createMipsELFObjectWriter(const Triple &TT, bool IsN32)
void sort(IteratorTy Start, IteratorTy End)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
@ FirstLiteralRelocationKind
The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for relocations coming from ....
@ FK_Data_8
A eight-byte fixup.
@ FK_Data_1
A one-byte fixup.
@ FK_Data_4
A four-byte fixup.
@ FK_DTPRel_4
A four-byte dtp relative fixup.
@ FK_DTPRel_8
A eight-byte dtp relative fixup.
@ FK_TPRel_4
A four-byte tp relative fixup.
@ FK_GPRel_4
A four-byte gp relative fixup.
@ FK_TPRel_8
A eight-byte tp relative fixup.
@ FK_Data_2
A two-byte fixup.
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)
const MCSymbolELF * OriginalSymbol