31#define DEBUG_TYPE "mips-elf-object-writer"
38struct MipsRelocationEntry {
46 Out <<
", Matched=" << Matched;
52 MipsELFObjectWriter(uint8_t OSABI,
bool HasRelocationAddend,
bool Is64);
54 ~MipsELFObjectWriter()
override =
default;
59 unsigned Type)
const override;
61 std::vector<ELFRelocationEntry> &Relocs)
override;
65enum FindBestPredicateResult {
69 FindBest_PerfectMatch,
77template <
class InputIt,
class OutputIt1,
class OutputIt2,
class UnaryPredicate>
79 OutputIt1 d1, OutputIt2 d2,
80 UnaryPredicate Predicate) {
91 return std::make_pair(d1, d2);
103template <
class InputIt,
class UnaryPredicate,
class Comparator>
105 Comparator BetterThan) {
109 unsigned Matched = Predicate(*
I);
110 if (Matched != FindBest_NoMatch) {
111 if (Best ==
Last || BetterThan(*
I, *Best))
114 if (Matched == FindBest_PerfectMatch)
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;
139 return ELF::R_MIPS_NONE;
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;
148 return ELF::R_MIPS_NONE;
167 unsigned MatchingType) {
168 if (
X.R.Type == MatchingType &&
X.R.OriginalSymbol == R.OriginalSymbol) {
169 if (!
X.Matched &&
X.R.Addend == R.Addend)
170 return FindBest_PerfectMatch;
171 else if (
X.R.Addend >= R.Addend)
172 return FindBest_Match;
174 return FindBest_NoMatch;
184 const MipsRelocationEntry &PreviousBest) {
185 if (Candidate.R.Addend != PreviousBest.R.Addend)
186 return Candidate.R.Addend < PreviousBest.R.Addend;
187 return PreviousBest.Matched && !Candidate.Matched;
190MipsELFObjectWriter::MipsELFObjectWriter(uint8_t OSABI,
191 bool HasRelocationAddend,
bool Is64)
194unsigned MipsELFObjectWriter::getRelocType(
MCContext &Ctx,
197 bool IsPCRel)
const {
205 return ELF::R_MIPS_NONE;
208 "MIPS does not support one byte relocations");
209 return ELF::R_MIPS_NONE;
212 return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
215 return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
219 ? setRTypes(ELF::R_MIPS_PC32, ELF::R_MIPS_64, ELF::R_MIPS_NONE)
227 return ELF::R_MIPS_PC16;
229 return ELF::R_MICROMIPS_PC7_S1;
231 return ELF::R_MICROMIPS_PC10_S1;
233 return ELF::R_MICROMIPS_PC16_S1;
235 return ELF::R_MICROMIPS_PC26_S1;
237 return ELF::R_MICROMIPS_PC19_S2;
239 return ELF::R_MICROMIPS_PC18_S3;
241 return ELF::R_MICROMIPS_PC21_S1;
243 return ELF::R_MIPS_PC19_S2;
245 return ELF::R_MIPS_PC18_S3;
247 return ELF::R_MIPS_PC21_S2;
249 return ELF::R_MIPS_PC26_S2;
251 return ELF::R_MIPS_PCHI16;
253 return ELF::R_MIPS_PCLO16;
261 return ELF::R_MIPS_TLS_DTPREL32;
263 return ELF::R_MIPS_TLS_DTPREL64;
265 return ELF::R_MIPS_TLS_TPREL32;
267 return ELF::R_MIPS_TLS_TPREL64;
269 return setRTypes(ELF::R_MIPS_GPREL32,
270 is64Bit() ? ELF::R_MIPS_64 : ELF::R_MIPS_NONE,
273 return ELF::R_MIPS_GPREL16;
275 return ELF::R_MIPS_26;
277 return ELF::R_MIPS_CALL16;
279 return ELF::R_MIPS_GOT16;
281 return ELF::R_MIPS_HI16;
283 return ELF::R_MIPS_LO16;
285 return ELF::R_MIPS_TLS_GD;
287 return ELF::R_MIPS_TLS_GOTTPREL;
289 return ELF::R_MIPS_TLS_TPREL_HI16;
291 return ELF::R_MIPS_TLS_TPREL_LO16;
293 return ELF::R_MIPS_TLS_LDM;
295 return ELF::R_MIPS_TLS_DTPREL_HI16;
297 return ELF::R_MIPS_TLS_DTPREL_LO16;
299 return ELF::R_MIPS_GOT_PAGE;
301 return ELF::R_MIPS_GOT_OFST;
303 return ELF::R_MIPS_GOT_DISP;
305 return setRTypes(ELF::R_MIPS_GPREL16, ELF::R_MIPS_SUB, ELF::R_MIPS_HI16);
307 return setRTypes(ELF::R_MICROMIPS_GPREL16, ELF::R_MICROMIPS_SUB,
308 ELF::R_MICROMIPS_HI16);
310 return setRTypes(ELF::R_MIPS_GPREL16, ELF::R_MIPS_SUB, ELF::R_MIPS_LO16);
312 return setRTypes(ELF::R_MICROMIPS_GPREL16, ELF::R_MICROMIPS_SUB,
313 ELF::R_MICROMIPS_LO16);
315 return ELF::R_MIPS_HIGHER;
317 return ELF::R_MIPS_HIGHEST;
319 return ELF::R_MIPS_SUB;
321 return ELF::R_MIPS_GOT_HI16;
323 return ELF::R_MIPS_GOT_LO16;
325 return ELF::R_MIPS_CALL_HI16;
327 return ELF::R_MIPS_CALL_LO16;
329 return ELF::R_MICROMIPS_26_S1;
331 return ELF::R_MICROMIPS_HI16;
333 return ELF::R_MICROMIPS_LO16;
335 return ELF::R_MICROMIPS_GOT16;
337 return ELF::R_MICROMIPS_CALL16;
339 return ELF::R_MICROMIPS_GOT_DISP;
341 return ELF::R_MICROMIPS_GOT_PAGE;
343 return ELF::R_MICROMIPS_GOT_OFST;
345 return ELF::R_MICROMIPS_TLS_GD;
347 return ELF::R_MICROMIPS_TLS_LDM;
349 return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
351 return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
353 return ELF::R_MICROMIPS_TLS_GOTTPREL;
355 return ELF::R_MICROMIPS_TLS_TPREL_HI16;
357 return ELF::R_MICROMIPS_TLS_TPREL_LO16;
359 return ELF::R_MICROMIPS_SUB;
361 return ELF::R_MICROMIPS_HIGHER;
363 return ELF::R_MICROMIPS_HIGHEST;
365 return ELF::R_MIPS_JALR;
367 return ELF::R_MICROMIPS_JALR;
408void MipsELFObjectWriter::sortRelocs(
const MCAssembler &Asm,
409 std::vector<ELFRelocationEntry> &Relocs) {
413 if (hasRelocationAddend())
416 if (Relocs.size() < 2)
422 return A.Offset <
B.Offset;
425 std::list<MipsRelocationEntry> Sorted;
426 std::list<ELFRelocationEntry> Remainder;
431 copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
433 return getMatchingLoType(Reloc) != ELF::R_MIPS_NONE;
436 for (
auto &R : Remainder) {
438 assert(MatchingType != ELF::R_MIPS_NONE &&
439 "Wrong list for reloc that doesn't need a match");
444 auto InsertionPoint =
446 [&R, &MatchingType](
const MipsRelocationEntry &
X) {
447 return isMatchingReloc(X, R, MatchingType);
465 if (InsertionPoint != Sorted.end())
466 InsertionPoint->Matched =
true;
467 Sorted.insert(InsertionPoint, R)->Matched =
true;
470 assert(Relocs.size() == Sorted.size() &&
"Some relocs were not consumed");
474 for (
const auto &R : Sorted)
475 Relocs[CopyTo++] =
R.R;
478bool MipsELFObjectWriter::needsRelocateWithSymbol(
const MCValue &Val,
480 unsigned Type)
const {
483 if (!isUInt<8>(
Type))
484 return needsRelocateWithSymbol(Val,
Sym,
Type & 0xff) ||
485 needsRelocateWithSymbol(Val,
Sym, (
Type >> 8) & 0xff) ||
486 needsRelocateWithSymbol(Val,
Sym, (
Type >> 16) & 0xff);
495 case ELF::R_MIPS_NONE:
505 case ELF::R_MIPS_GOT16:
506 case ELF::R_MIPS16_GOT16:
507 case ELF::R_MICROMIPS_GOT16:
508 case ELF::R_MIPS_HIGHER:
509 case ELF::R_MIPS_HIGHEST:
510 case ELF::R_MIPS_HI16:
511 case ELF::R_MIPS16_HI16:
512 case ELF::R_MICROMIPS_HI16:
513 case ELF::R_MIPS_LO16:
514 case ELF::R_MIPS16_LO16:
515 case ELF::R_MICROMIPS_LO16:
523 case ELF::R_MIPS_GOT_PAGE:
524 case ELF::R_MICROMIPS_GOT_PAGE:
525 case ELF::R_MIPS_GOT_OFST:
526 case ELF::R_MICROMIPS_GOT_OFST:
529 case ELF::R_MIPS_GPREL32:
535 case ELF::R_MIPS_GPREL16:
536 case ELF::R_MIPS_PC16:
537 case ELF::R_MIPS_SUB:
542 case ELF::R_MIPS_REL32:
543 case ELF::R_MIPS_LITERAL:
544 case ELF::R_MIPS_CALL16:
545 case ELF::R_MIPS_SHIFT5:
546 case ELF::R_MIPS_SHIFT6:
547 case ELF::R_MIPS_GOT_DISP:
548 case ELF::R_MIPS_GOT_HI16:
549 case ELF::R_MIPS_GOT_LO16:
550 case ELF::R_MIPS_INSERT_A:
551 case ELF::R_MIPS_INSERT_B:
552 case ELF::R_MIPS_DELETE:
553 case ELF::R_MIPS_CALL_HI16:
554 case ELF::R_MIPS_CALL_LO16:
555 case ELF::R_MIPS_SCN_DISP:
556 case ELF::R_MIPS_REL16:
557 case ELF::R_MIPS_ADD_IMMEDIATE:
558 case ELF::R_MIPS_PJUMP:
559 case ELF::R_MIPS_RELGOT:
560 case ELF::R_MIPS_JALR:
561 case ELF::R_MIPS_TLS_DTPMOD32:
562 case ELF::R_MIPS_TLS_DTPREL32:
563 case ELF::R_MIPS_TLS_DTPMOD64:
564 case ELF::R_MIPS_TLS_DTPREL64:
565 case ELF::R_MIPS_TLS_GD:
566 case ELF::R_MIPS_TLS_LDM:
567 case ELF::R_MIPS_TLS_DTPREL_HI16:
568 case ELF::R_MIPS_TLS_DTPREL_LO16:
569 case ELF::R_MIPS_TLS_GOTTPREL:
570 case ELF::R_MIPS_TLS_TPREL32:
571 case ELF::R_MIPS_TLS_TPREL64:
572 case ELF::R_MIPS_TLS_TPREL_HI16:
573 case ELF::R_MIPS_TLS_TPREL_LO16:
574 case ELF::R_MIPS_GLOB_DAT:
575 case ELF::R_MIPS_PC21_S2:
576 case ELF::R_MIPS_PC26_S2:
577 case ELF::R_MIPS_PC18_S3:
578 case ELF::R_MIPS_PC19_S2:
579 case ELF::R_MIPS_PCHI16:
580 case ELF::R_MIPS_PCLO16:
581 case ELF::R_MIPS_COPY:
582 case ELF::R_MIPS_JUMP_SLOT:
583 case ELF::R_MIPS_NUM:
584 case ELF::R_MIPS_PC32:
586 case ELF::R_MICROMIPS_26_S1:
587 case ELF::R_MICROMIPS_GPREL16:
588 case ELF::R_MICROMIPS_LITERAL:
589 case ELF::R_MICROMIPS_PC7_S1:
590 case ELF::R_MICROMIPS_PC10_S1:
591 case ELF::R_MICROMIPS_PC16_S1:
592 case ELF::R_MICROMIPS_CALL16:
593 case ELF::R_MICROMIPS_GOT_DISP:
594 case ELF::R_MICROMIPS_GOT_HI16:
595 case ELF::R_MICROMIPS_GOT_LO16:
596 case ELF::R_MICROMIPS_SUB:
597 case ELF::R_MICROMIPS_HIGHER:
598 case ELF::R_MICROMIPS_HIGHEST:
599 case ELF::R_MICROMIPS_CALL_HI16:
600 case ELF::R_MICROMIPS_CALL_LO16:
601 case ELF::R_MICROMIPS_SCN_DISP:
602 case ELF::R_MICROMIPS_JALR:
603 case ELF::R_MICROMIPS_HI0_LO16:
604 case ELF::R_MICROMIPS_TLS_GD:
605 case ELF::R_MICROMIPS_TLS_LDM:
606 case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
607 case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
608 case ELF::R_MICROMIPS_TLS_GOTTPREL:
609 case ELF::R_MICROMIPS_TLS_TPREL_HI16:
610 case ELF::R_MICROMIPS_TLS_TPREL_LO16:
611 case ELF::R_MICROMIPS_GPREL7_S2:
612 case ELF::R_MICROMIPS_PC23_S2:
613 case ELF::R_MICROMIPS_PC21_S1:
614 case ELF::R_MICROMIPS_PC26_S1:
615 case ELF::R_MICROMIPS_PC18_S3:
616 case ELF::R_MICROMIPS_PC19_S2:
621 case ELF::R_MIPS16_26:
622 case ELF::R_MIPS16_GPREL:
623 case ELF::R_MIPS16_CALL16:
624 case ELF::R_MIPS16_TLS_GD:
625 case ELF::R_MIPS16_TLS_LDM:
626 case ELF::R_MIPS16_TLS_DTPREL_HI16:
627 case ELF::R_MIPS16_TLS_DTPREL_LO16:
628 case ELF::R_MIPS16_TLS_GOTTPREL:
629 case ELF::R_MIPS16_TLS_TPREL_HI16:
630 case ELF::R_MIPS16_TLS_TPREL_LO16:
636std::unique_ptr<MCObjectTargetWriter>
639 bool IsN64 = TT.isArch64Bit() && !IsN32;
640 bool HasRelocationAddend = TT.isArch64Bit();
641 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 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.
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)
@ 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.
const MCSymbolELF * OriginalSymbol