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;
231 return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
234 return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
241 return ELF::R_MIPS_PC16;
243 return ELF::R_MICROMIPS_PC7_S1;
245 return ELF::R_MICROMIPS_PC10_S1;
247 return ELF::R_MICROMIPS_PC16_S1;
249 return ELF::R_MICROMIPS_PC26_S1;
251 return ELF::R_MICROMIPS_PC19_S2;
253 return ELF::R_MICROMIPS_PC18_S3;
255 return ELF::R_MICROMIPS_PC21_S1;
257 return ELF::R_MIPS_PC19_S2;
259 return ELF::R_MIPS_PC18_S3;
261 return ELF::R_MIPS_PC21_S2;
263 return ELF::R_MIPS_PC26_S2;
265 return ELF::R_MIPS_PCHI16;
267 return ELF::R_MIPS_PCLO16;
276 return ELF::R_MIPS_64;
278 return ELF::R_MIPS_TLS_DTPREL32;
280 return ELF::R_MIPS_TLS_DTPREL64;
282 return ELF::R_MIPS_TLS_TPREL32;
284 return ELF::R_MIPS_TLS_TPREL64;
288 Type = setRType((
unsigned)ELF::R_MIPS_GPREL32, Type);
289 Type = setRType2((
unsigned)ELF::R_MIPS_64, Type);
290 Type = setRType3((
unsigned)ELF::R_MIPS_NONE, Type);
293 return ELF::R_MIPS_GPREL32;
295 return ELF::R_MIPS_GPREL16;
297 return ELF::R_MIPS_26;
299 return ELF::R_MIPS_CALL16;
301 return ELF::R_MIPS_GOT16;
303 return ELF::R_MIPS_HI16;
305 return ELF::R_MIPS_LO16;
307 return ELF::R_MIPS_TLS_GD;
309 return ELF::R_MIPS_TLS_GOTTPREL;
311 return ELF::R_MIPS_TLS_TPREL_HI16;
313 return ELF::R_MIPS_TLS_TPREL_LO16;
315 return ELF::R_MIPS_TLS_LDM;
317 return ELF::R_MIPS_TLS_DTPREL_HI16;
319 return ELF::R_MIPS_TLS_DTPREL_LO16;
321 return ELF::R_MIPS_GOT_PAGE;
323 return ELF::R_MIPS_GOT_OFST;
325 return ELF::R_MIPS_GOT_DISP;
328 Type = setRType((
unsigned)ELF::R_MIPS_GPREL16, Type);
329 Type = setRType2((
unsigned)ELF::R_MIPS_SUB, Type);
330 Type = setRType3((
unsigned)ELF::R_MIPS_HI16, Type);
335 Type = setRType((
unsigned)ELF::R_MICROMIPS_GPREL16, Type);
336 Type = setRType2((
unsigned)ELF::R_MICROMIPS_SUB, Type);
337 Type = setRType3((
unsigned)ELF::R_MICROMIPS_HI16, Type);
342 Type = setRType((
unsigned)ELF::R_MIPS_GPREL16, Type);
343 Type = setRType2((
unsigned)ELF::R_MIPS_SUB, Type);
344 Type = setRType3((
unsigned)ELF::R_MIPS_LO16, Type);
349 Type = setRType((
unsigned)ELF::R_MICROMIPS_GPREL16, Type);
350 Type = setRType2((
unsigned)ELF::R_MICROMIPS_SUB, Type);
351 Type = setRType3((
unsigned)ELF::R_MICROMIPS_LO16, Type);
355 return ELF::R_MIPS_HIGHER;
357 return ELF::R_MIPS_HIGHEST;
359 return ELF::R_MIPS_SUB;
361 return ELF::R_MIPS_GOT_HI16;
363 return ELF::R_MIPS_GOT_LO16;
365 return ELF::R_MIPS_CALL_HI16;
367 return ELF::R_MIPS_CALL_LO16;
369 return ELF::R_MICROMIPS_26_S1;
371 return ELF::R_MICROMIPS_HI16;
373 return ELF::R_MICROMIPS_LO16;
375 return ELF::R_MICROMIPS_GOT16;
377 return ELF::R_MICROMIPS_CALL16;
379 return ELF::R_MICROMIPS_GOT_DISP;
381 return ELF::R_MICROMIPS_GOT_PAGE;
383 return ELF::R_MICROMIPS_GOT_OFST;
385 return ELF::R_MICROMIPS_TLS_GD;
387 return ELF::R_MICROMIPS_TLS_LDM;
389 return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
391 return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
393 return ELF::R_MICROMIPS_TLS_GOTTPREL;
395 return ELF::R_MICROMIPS_TLS_TPREL_HI16;
397 return ELF::R_MICROMIPS_TLS_TPREL_LO16;
399 return ELF::R_MICROMIPS_SUB;
401 return ELF::R_MICROMIPS_HIGHER;
403 return ELF::R_MICROMIPS_HIGHEST;
445 std::vector<ELFRelocationEntry> &Relocs) {
449 if (hasRelocationAddend())
452 if (Relocs.size() < 2)
461 std::list<MipsRelocationEntry> Sorted;
462 std::list<ELFRelocationEntry> Remainder;
469 copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
474 for (
auto &R : Remainder) {
478 assert(MatchingType != ELF::R_MIPS_NONE &&
479 "Wrong list for reloc that doesn't need a match");
484 auto InsertionPoint =
486 [&R, &MatchingType](
const MipsRelocationEntry &
X) {
505 if (InsertionPoint != Sorted.end())
506 InsertionPoint->Matched =
true;
507 Sorted.insert(InsertionPoint, R)->Matched =
true;
512 assert(Relocs.size() == Sorted.size() &&
"Some relocs were not consumed");
517 for (
const auto &R :
reverse(Sorted))
518 Relocs[CopyTo++] = R.R;
521 bool MipsELFObjectWriter::needsRelocateWithSymbol(
const MCSymbol &Sym,
522 unsigned Type)
const {
526 return needsRelocateWithSymbol(Sym, Type & 0xff) ||
527 needsRelocateWithSymbol(Sym, (Type >> 8) & 0xff) ||
528 needsRelocateWithSymbol(Sym, (Type >> 16) & 0xff);
532 errs() << Type <<
"\n";
537 case ELF::R_MIPS_NONE:
547 case ELF::R_MIPS_GOT16:
548 case ELF::R_MIPS16_GOT16:
549 case ELF::R_MICROMIPS_GOT16:
550 case ELF::R_MIPS_HIGHER:
551 case ELF::R_MIPS_HIGHEST:
552 case ELF::R_MIPS_HI16:
553 case ELF::R_MIPS16_HI16:
554 case ELF::R_MICROMIPS_HI16:
555 case ELF::R_MIPS_LO16:
556 case ELF::R_MIPS16_LO16:
557 case ELF::R_MICROMIPS_LO16:
565 case ELF::R_MIPS_GOT_PAGE:
566 case ELF::R_MICROMIPS_GOT_PAGE:
567 case ELF::R_MIPS_GOT_OFST:
568 case ELF::R_MICROMIPS_GOT_OFST:
571 case ELF::R_MIPS_GPREL32:
577 case ELF::R_MIPS_GPREL16:
578 case ELF::R_MIPS_PC16:
579 case ELF::R_MIPS_SUB:
584 case ELF::R_MIPS_REL32:
585 case ELF::R_MIPS_LITERAL:
586 case ELF::R_MIPS_CALL16:
587 case ELF::R_MIPS_SHIFT5:
588 case ELF::R_MIPS_SHIFT6:
589 case ELF::R_MIPS_GOT_DISP:
590 case ELF::R_MIPS_GOT_HI16:
591 case ELF::R_MIPS_GOT_LO16:
592 case ELF::R_MIPS_INSERT_A:
593 case ELF::R_MIPS_INSERT_B:
594 case ELF::R_MIPS_DELETE:
595 case ELF::R_MIPS_CALL_HI16:
596 case ELF::R_MIPS_CALL_LO16:
597 case ELF::R_MIPS_SCN_DISP:
598 case ELF::R_MIPS_REL16:
599 case ELF::R_MIPS_ADD_IMMEDIATE:
600 case ELF::R_MIPS_PJUMP:
601 case ELF::R_MIPS_RELGOT:
602 case ELF::R_MIPS_JALR:
603 case ELF::R_MIPS_TLS_DTPMOD32:
604 case ELF::R_MIPS_TLS_DTPREL32:
605 case ELF::R_MIPS_TLS_DTPMOD64:
606 case ELF::R_MIPS_TLS_DTPREL64:
607 case ELF::R_MIPS_TLS_GD:
608 case ELF::R_MIPS_TLS_LDM:
609 case ELF::R_MIPS_TLS_DTPREL_HI16:
610 case ELF::R_MIPS_TLS_DTPREL_LO16:
611 case ELF::R_MIPS_TLS_GOTTPREL:
612 case ELF::R_MIPS_TLS_TPREL32:
613 case ELF::R_MIPS_TLS_TPREL64:
614 case ELF::R_MIPS_TLS_TPREL_HI16:
615 case ELF::R_MIPS_TLS_TPREL_LO16:
616 case ELF::R_MIPS_GLOB_DAT:
617 case ELF::R_MIPS_PC21_S2:
618 case ELF::R_MIPS_PC26_S2:
619 case ELF::R_MIPS_PC18_S3:
620 case ELF::R_MIPS_PC19_S2:
621 case ELF::R_MIPS_PCHI16:
622 case ELF::R_MIPS_PCLO16:
623 case ELF::R_MIPS_COPY:
624 case ELF::R_MIPS_JUMP_SLOT:
625 case ELF::R_MIPS_NUM:
626 case ELF::R_MIPS_PC32:
628 case ELF::R_MICROMIPS_26_S1:
629 case ELF::R_MICROMIPS_GPREL16:
630 case ELF::R_MICROMIPS_LITERAL:
631 case ELF::R_MICROMIPS_PC7_S1:
632 case ELF::R_MICROMIPS_PC10_S1:
633 case ELF::R_MICROMIPS_PC16_S1:
634 case ELF::R_MICROMIPS_CALL16:
635 case ELF::R_MICROMIPS_GOT_DISP:
636 case ELF::R_MICROMIPS_GOT_HI16:
637 case ELF::R_MICROMIPS_GOT_LO16:
638 case ELF::R_MICROMIPS_SUB:
639 case ELF::R_MICROMIPS_HIGHER:
640 case ELF::R_MICROMIPS_HIGHEST:
641 case ELF::R_MICROMIPS_CALL_HI16:
642 case ELF::R_MICROMIPS_CALL_LO16:
643 case ELF::R_MICROMIPS_SCN_DISP:
644 case ELF::R_MICROMIPS_JALR:
645 case ELF::R_MICROMIPS_HI0_LO16:
646 case ELF::R_MICROMIPS_TLS_GD:
647 case ELF::R_MICROMIPS_TLS_LDM:
648 case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
649 case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
650 case ELF::R_MICROMIPS_TLS_GOTTPREL:
651 case ELF::R_MICROMIPS_TLS_TPREL_HI16:
652 case ELF::R_MICROMIPS_TLS_TPREL_LO16:
653 case ELF::R_MICROMIPS_GPREL7_S2:
654 case ELF::R_MICROMIPS_PC23_S2:
655 case ELF::R_MICROMIPS_PC21_S1:
656 case ELF::R_MICROMIPS_PC26_S1:
657 case ELF::R_MICROMIPS_PC18_S3:
658 case ELF::R_MICROMIPS_PC19_S2:
663 case ELF::R_MIPS16_26:
664 case ELF::R_MIPS16_GPREL:
665 case ELF::R_MIPS16_CALL16:
666 case ELF::R_MIPS16_TLS_GD:
667 case ELF::R_MIPS16_TLS_LDM:
668 case ELF::R_MIPS16_TLS_DTPREL_HI16:
669 case ELF::R_MIPS16_TLS_DTPREL_LO16:
670 case ELF::R_MIPS16_TLS_GOTTPREL:
671 case ELF::R_MIPS16_TLS_TPREL_HI16:
672 case ELF::R_MIPS16_TLS_TPREL_LO16:
678 std::unique_ptr<MCObjectTargetWriter>
683 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.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Compute iterated dominance frontiers using a linear time algorithm.
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.
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...
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
static unsigned getMatchingLoType(const ELFRelocationEntry &Reloc)
Determine the low relocation that matches the given relocation.
MCFixupKind getKind() const