31 #define DEBUG_TYPE "mips-elf-object-writer" 38 struct MipsRelocationEntry {
46 Out <<
", Matched=" << Matched;
59 MipsELFObjectWriter(uint8_t OSABI,
bool HasRelocationAddend,
bool Is64);
61 ~MipsELFObjectWriter()
override =
default;
65 bool needsRelocateWithSymbol(
const MCSymbol &Sym,
66 unsigned Type)
const override;
68 std::vector<ELFRelocationEntry> &Relocs)
override;
76 FindBest_PerfectMatch,
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,
88 for (InputIt
I = First;
I != Last; ++
I) {
98 return std::make_pair(d1, d2);
110 template <
class InputIt,
class UnaryPredicate,
class Comparator>
112 Comparator BetterThan) {
115 for (InputIt
I = First;
I != Last; ++
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) {
184 return FindBest_PerfectMatch;
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;
206 template <
class Container>
208 for (
const auto &R : Relocs)
209 dbgs() << Prefix << R <<
"\n";
213 MipsELFObjectWriter::MipsELFObjectWriter(uint8_t OSABI,
214 bool HasRelocationAddend,
bool Is64)
220 bool IsPCRel)
const {
226 return ELF::R_MIPS_NONE;
229 "MIPS does not support one byte relocations");
230 return ELF::R_MIPS_NONE;
233 return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
236 return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
243 "MIPS does not support 64-bit PC-relative relocations");
244 return ELF::R_MIPS_NONE;
247 return ELF::R_MIPS_PC16;
249 return ELF::R_MICROMIPS_PC7_S1;
251 return ELF::R_MICROMIPS_PC10_S1;
253 return ELF::R_MICROMIPS_PC16_S1;
255 return ELF::R_MICROMIPS_PC26_S1;
257 return ELF::R_MICROMIPS_PC19_S2;
259 return ELF::R_MICROMIPS_PC18_S3;
261 return ELF::R_MICROMIPS_PC21_S1;
263 return ELF::R_MIPS_PC19_S2;
265 return ELF::R_MIPS_PC18_S3;
267 return ELF::R_MIPS_PC21_S2;
269 return ELF::R_MIPS_PC26_S2;
271 return ELF::R_MIPS_PCHI16;
273 return ELF::R_MIPS_PCLO16;
282 return ELF::R_MIPS_64;
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;
294 Type = setRType((
unsigned)ELF::R_MIPS_GPREL32, Type);
295 Type = setRType2((
unsigned)ELF::R_MIPS_64, Type);
296 Type = setRType3((
unsigned)ELF::R_MIPS_NONE, Type);
299 return ELF::R_MIPS_GPREL32;
301 return ELF::R_MIPS_GPREL16;
303 return ELF::R_MIPS_26;
305 return ELF::R_MIPS_CALL16;
307 return ELF::R_MIPS_GOT16;
309 return ELF::R_MIPS_HI16;
311 return ELF::R_MIPS_LO16;
313 return ELF::R_MIPS_TLS_GD;
315 return ELF::R_MIPS_TLS_GOTTPREL;
317 return ELF::R_MIPS_TLS_TPREL_HI16;
319 return ELF::R_MIPS_TLS_TPREL_LO16;
321 return ELF::R_MIPS_TLS_LDM;
323 return ELF::R_MIPS_TLS_DTPREL_HI16;
325 return ELF::R_MIPS_TLS_DTPREL_LO16;
327 return ELF::R_MIPS_GOT_PAGE;
329 return ELF::R_MIPS_GOT_OFST;
331 return ELF::R_MIPS_GOT_DISP;
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_HI16, Type);
341 Type = setRType((
unsigned)ELF::R_MICROMIPS_GPREL16, Type);
342 Type = setRType2((
unsigned)ELF::R_MICROMIPS_SUB, Type);
343 Type = setRType3((
unsigned)ELF::R_MICROMIPS_HI16, Type);
348 Type = setRType((
unsigned)ELF::R_MIPS_GPREL16, Type);
349 Type = setRType2((
unsigned)ELF::R_MIPS_SUB, Type);
350 Type = setRType3((
unsigned)ELF::R_MIPS_LO16, Type);
355 Type = setRType((
unsigned)ELF::R_MICROMIPS_GPREL16, Type);
356 Type = setRType2((
unsigned)ELF::R_MICROMIPS_SUB, Type);
357 Type = setRType3((
unsigned)ELF::R_MICROMIPS_LO16, Type);
361 return ELF::R_MIPS_HIGHER;
363 return ELF::R_MIPS_HIGHEST;
365 return ELF::R_MIPS_SUB;
367 return ELF::R_MIPS_GOT_HI16;
369 return ELF::R_MIPS_GOT_LO16;
371 return ELF::R_MIPS_CALL_HI16;
373 return ELF::R_MIPS_CALL_LO16;
375 return ELF::R_MICROMIPS_26_S1;
377 return ELF::R_MICROMIPS_HI16;
379 return ELF::R_MICROMIPS_LO16;
381 return ELF::R_MICROMIPS_GOT16;
383 return ELF::R_MICROMIPS_CALL16;
385 return ELF::R_MICROMIPS_GOT_DISP;
387 return ELF::R_MICROMIPS_GOT_PAGE;
389 return ELF::R_MICROMIPS_GOT_OFST;
391 return ELF::R_MICROMIPS_TLS_GD;
393 return ELF::R_MICROMIPS_TLS_LDM;
395 return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
397 return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
399 return ELF::R_MICROMIPS_TLS_GOTTPREL;
401 return ELF::R_MICROMIPS_TLS_TPREL_HI16;
403 return ELF::R_MICROMIPS_TLS_TPREL_LO16;
405 return ELF::R_MICROMIPS_SUB;
407 return ELF::R_MICROMIPS_HIGHER;
409 return ELF::R_MICROMIPS_HIGHEST;
411 return ELF::R_MIPS_JALR;
413 return ELF::R_MICROMIPS_JALR;
455 std::vector<ELFRelocationEntry> &Relocs) {
459 if (hasRelocationAddend())
462 if (Relocs.size() < 2)
471 std::list<MipsRelocationEntry> Sorted;
472 std::list<ELFRelocationEntry> Remainder;
479 copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
484 for (
auto &R : Remainder) {
488 assert(MatchingType != ELF::R_MIPS_NONE &&
489 "Wrong list for reloc that doesn't need a match");
494 auto InsertionPoint =
496 [&R, &MatchingType](
const MipsRelocationEntry &
X) {
515 if (InsertionPoint != Sorted.end())
516 InsertionPoint->Matched =
true;
517 Sorted.insert(InsertionPoint, R)->Matched =
true;
522 assert(Relocs.size() == Sorted.size() &&
"Some relocs were not consumed");
527 for (
const auto &R :
reverse(Sorted))
528 Relocs[CopyTo++] = R.R;
531 bool MipsELFObjectWriter::needsRelocateWithSymbol(
const MCSymbol &Sym,
532 unsigned Type)
const {
536 return needsRelocateWithSymbol(Sym, Type & 0xff) ||
537 needsRelocateWithSymbol(Sym, (Type >> 8) & 0xff) ||
538 needsRelocateWithSymbol(Sym, (Type >> 16) & 0xff);
542 errs() << Type <<
"\n";
547 case ELF::R_MIPS_NONE:
557 case ELF::R_MIPS_GOT16:
558 case ELF::R_MIPS16_GOT16:
559 case ELF::R_MICROMIPS_GOT16:
560 case ELF::R_MIPS_HIGHER:
561 case ELF::R_MIPS_HIGHEST:
562 case ELF::R_MIPS_HI16:
563 case ELF::R_MIPS16_HI16:
564 case ELF::R_MICROMIPS_HI16:
565 case ELF::R_MIPS_LO16:
566 case ELF::R_MIPS16_LO16:
567 case ELF::R_MICROMIPS_LO16:
575 case ELF::R_MIPS_GOT_PAGE:
576 case ELF::R_MICROMIPS_GOT_PAGE:
577 case ELF::R_MIPS_GOT_OFST:
578 case ELF::R_MICROMIPS_GOT_OFST:
581 case ELF::R_MIPS_GPREL32:
587 case ELF::R_MIPS_GPREL16:
588 case ELF::R_MIPS_PC16:
589 case ELF::R_MIPS_SUB:
594 case ELF::R_MIPS_REL32:
595 case ELF::R_MIPS_LITERAL:
596 case ELF::R_MIPS_CALL16:
597 case ELF::R_MIPS_SHIFT5:
598 case ELF::R_MIPS_SHIFT6:
599 case ELF::R_MIPS_GOT_DISP:
600 case ELF::R_MIPS_GOT_HI16:
601 case ELF::R_MIPS_GOT_LO16:
602 case ELF::R_MIPS_INSERT_A:
603 case ELF::R_MIPS_INSERT_B:
604 case ELF::R_MIPS_DELETE:
605 case ELF::R_MIPS_CALL_HI16:
606 case ELF::R_MIPS_CALL_LO16:
607 case ELF::R_MIPS_SCN_DISP:
608 case ELF::R_MIPS_REL16:
609 case ELF::R_MIPS_ADD_IMMEDIATE:
610 case ELF::R_MIPS_PJUMP:
611 case ELF::R_MIPS_RELGOT:
612 case ELF::R_MIPS_JALR:
613 case ELF::R_MIPS_TLS_DTPMOD32:
614 case ELF::R_MIPS_TLS_DTPREL32:
615 case ELF::R_MIPS_TLS_DTPMOD64:
616 case ELF::R_MIPS_TLS_DTPREL64:
617 case ELF::R_MIPS_TLS_GD:
618 case ELF::R_MIPS_TLS_LDM:
619 case ELF::R_MIPS_TLS_DTPREL_HI16:
620 case ELF::R_MIPS_TLS_DTPREL_LO16:
621 case ELF::R_MIPS_TLS_GOTTPREL:
622 case ELF::R_MIPS_TLS_TPREL32:
623 case ELF::R_MIPS_TLS_TPREL64:
624 case ELF::R_MIPS_TLS_TPREL_HI16:
625 case ELF::R_MIPS_TLS_TPREL_LO16:
626 case ELF::R_MIPS_GLOB_DAT:
627 case ELF::R_MIPS_PC21_S2:
628 case ELF::R_MIPS_PC26_S2:
629 case ELF::R_MIPS_PC18_S3:
630 case ELF::R_MIPS_PC19_S2:
631 case ELF::R_MIPS_PCHI16:
632 case ELF::R_MIPS_PCLO16:
633 case ELF::R_MIPS_COPY:
634 case ELF::R_MIPS_JUMP_SLOT:
635 case ELF::R_MIPS_NUM:
636 case ELF::R_MIPS_PC32:
638 case ELF::R_MICROMIPS_26_S1:
639 case ELF::R_MICROMIPS_GPREL16:
640 case ELF::R_MICROMIPS_LITERAL:
641 case ELF::R_MICROMIPS_PC7_S1:
642 case ELF::R_MICROMIPS_PC10_S1:
643 case ELF::R_MICROMIPS_PC16_S1:
644 case ELF::R_MICROMIPS_CALL16:
645 case ELF::R_MICROMIPS_GOT_DISP:
646 case ELF::R_MICROMIPS_GOT_HI16:
647 case ELF::R_MICROMIPS_GOT_LO16:
648 case ELF::R_MICROMIPS_SUB:
649 case ELF::R_MICROMIPS_HIGHER:
650 case ELF::R_MICROMIPS_HIGHEST:
651 case ELF::R_MICROMIPS_CALL_HI16:
652 case ELF::R_MICROMIPS_CALL_LO16:
653 case ELF::R_MICROMIPS_SCN_DISP:
654 case ELF::R_MICROMIPS_JALR:
655 case ELF::R_MICROMIPS_HI0_LO16:
656 case ELF::R_MICROMIPS_TLS_GD:
657 case ELF::R_MICROMIPS_TLS_LDM:
658 case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
659 case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
660 case ELF::R_MICROMIPS_TLS_GOTTPREL:
661 case ELF::R_MICROMIPS_TLS_TPREL_HI16:
662 case ELF::R_MICROMIPS_TLS_TPREL_LO16:
663 case ELF::R_MICROMIPS_GPREL7_S2:
664 case ELF::R_MICROMIPS_PC23_S2:
665 case ELF::R_MICROMIPS_PC21_S1:
666 case ELF::R_MICROMIPS_PC26_S1:
667 case ELF::R_MICROMIPS_PC18_S3:
668 case ELF::R_MICROMIPS_PC19_S2:
673 case ELF::R_MIPS16_26:
674 case ELF::R_MIPS16_GPREL:
675 case ELF::R_MIPS16_CALL16:
676 case ELF::R_MIPS16_TLS_GD:
677 case ELF::R_MIPS16_TLS_LDM:
678 case ELF::R_MIPS16_TLS_DTPREL_HI16:
679 case ELF::R_MIPS16_TLS_DTPREL_LO16:
680 case ELF::R_MIPS16_TLS_GOTTPREL:
681 case ELF::R_MIPS16_TLS_TPREL_HI16:
682 case ELF::R_MIPS16_TLS_TPREL_LO16:
688 std::unique_ptr<MCObjectTargetWriter>
693 return std::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 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())
unsigned getTargetKind() const
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
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.
static bool is64Bit(const char *name)