26 #define DEBUG_TYPE "mips-elf-object-writer"
32 struct MipsRelocationEntry {
40 Out <<
", Matched=" << Matched;
53 MipsELFObjectWriter(
bool _is64Bit, uint8_t OSABI,
bool _isN64,
56 ~MipsELFObjectWriter()
override;
60 bool needsRelocateWithSymbol(
const MCSymbol &Sym,
61 unsigned Type)
const override;
63 std::vector<ELFRelocationEntry> &Relocs)
override;
69 template <
class InputIt,
class OutputIt1,
class OutputIt2,
class UnaryPredicate>
70 std::pair<OutputIt1, OutputIt2> copy_if_else(InputIt First, InputIt Last,
71 OutputIt1 d1, OutputIt2 d2,
73 for (InputIt
I = First;
I != Last; ++
I) {
83 return std::make_pair(d1, d2);
91 FindBest_PerfectMatch,
103 template <
class InputIt,
class UnaryPredicate,
class Comparator>
104 InputIt find_best(InputIt First, InputIt Last, UnaryPredicate Predicate,
105 Comparator BetterThan) {
108 for (InputIt
I = First;
I != Last; ++
I) {
110 if (Matched != FindBest_NoMatch) {
111 DEBUG(
dbgs() << std::distance(First,
I) <<
" is a match (";
113 if (Best == Last || BetterThan(*
I, *Best)) {
114 DEBUG(
dbgs() <<
".. and it beats the last one\n");
118 if (Matched == FindBest_PerfectMatch) {
119 DEBUG(
dbgs() <<
".. and it is unbeatable\n");
136 if (Type == ELF::R_MIPS_HI16)
137 return ELF::R_MIPS_LO16;
138 if (Type == ELF::R_MICROMIPS_HI16)
139 return ELF::R_MICROMIPS_LO16;
140 if (Type == ELF::R_MIPS16_HI16)
141 return ELF::R_MIPS16_LO16;
144 return ELF::R_MIPS_NONE;
146 if (Type == ELF::R_MIPS_GOT16)
147 return ELF::R_MIPS_LO16;
148 if (Type == ELF::R_MICROMIPS_GOT16)
149 return ELF::R_MICROMIPS_LO16;
150 if (Type == ELF::R_MIPS16_GOT16)
151 return ELF::R_MIPS16_LO16;
153 return ELF::R_MIPS_NONE;
172 unsigned MatchingType) {
173 if (X.R.Type == MatchingType && X.R.OriginalSymbol == R.
OriginalSymbol) {
176 return FindBest_PerfectMatch;
178 return FindBest_Match;
180 return FindBest_NoMatch;
189 static bool compareMatchingRelocs(
const MipsRelocationEntry &Candidate,
190 const MipsRelocationEntry &PreviousBest) {
191 if (Candidate.R.OriginalAddend != PreviousBest.R.OriginalAddend)
192 return Candidate.R.OriginalAddend < PreviousBest.R.OriginalAddend;
193 return PreviousBest.Matched && !Candidate.Matched;
198 template <
class Container>
199 static void dumpRelocs(
const char *
Prefix,
const Container &Relocs) {
200 for (
const auto &R : Relocs)
201 dbgs() << Prefix << R <<
"\n";
207 MipsELFObjectWriter::MipsELFObjectWriter(
bool _is64Bit, uint8_t OSABI,
208 bool _isN64,
bool IsLittleEndian)
213 MipsELFObjectWriter::~MipsELFObjectWriter() {}
218 bool IsPCRel)
const {
224 return ELF::R_MIPS_NONE;
227 return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
230 return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
237 return ELF::R_MIPS_PC16;
239 return ELF::R_MICROMIPS_PC7_S1;
241 return ELF::R_MICROMIPS_PC10_S1;
243 return ELF::R_MICROMIPS_PC16_S1;
245 return ELF::R_MICROMIPS_PC26_S1;
247 return ELF::R_MICROMIPS_PC19_S2;
249 return ELF::R_MICROMIPS_PC18_S3;
251 return ELF::R_MICROMIPS_PC21_S1;
253 return ELF::R_MIPS_PC19_S2;
255 return ELF::R_MIPS_PC18_S3;
257 return ELF::R_MIPS_PC21_S2;
259 return ELF::R_MIPS_PC26_S2;
261 return ELF::R_MIPS_PCHI16;
263 return ELF::R_MIPS_PCLO16;
272 return ELF::R_MIPS_64;
274 return ELF::R_MIPS_TLS_DTPREL32;
276 return ELF::R_MIPS_TLS_DTPREL64;
278 return ELF::R_MIPS_TLS_TPREL32;
280 return ELF::R_MIPS_TLS_TPREL64;
283 unsigned Type = (
unsigned)ELF::R_MIPS_NONE;
284 Type = setRType((
unsigned)ELF::R_MIPS_GPREL32, Type);
285 Type = setRType2((
unsigned)ELF::R_MIPS_64, Type);
286 Type = setRType3((
unsigned)ELF::R_MIPS_NONE, Type);
289 return ELF::R_MIPS_GPREL32;
291 return ELF::R_MIPS_GPREL16;
293 return ELF::R_MIPS_26;
295 return ELF::R_MIPS_CALL16;
297 return ELF::R_MIPS_GOT16;
299 return ELF::R_MIPS_HI16;
301 return ELF::R_MIPS_LO16;
303 return ELF::R_MIPS_TLS_GD;
305 return ELF::R_MIPS_TLS_GOTTPREL;
307 return ELF::R_MIPS_TLS_TPREL_HI16;
309 return ELF::R_MIPS_TLS_TPREL_LO16;
311 return ELF::R_MIPS_TLS_LDM;
313 return ELF::R_MIPS_TLS_DTPREL_HI16;
315 return ELF::R_MIPS_TLS_DTPREL_LO16;
317 return ELF::R_MIPS_GOT_PAGE;
319 return ELF::R_MIPS_GOT_OFST;
321 return ELF::R_MIPS_GOT_DISP;
323 unsigned Type = (
unsigned)ELF::R_MIPS_NONE;
324 Type = setRType((
unsigned)ELF::R_MIPS_GPREL16, Type);
325 Type = setRType2((
unsigned)ELF::R_MIPS_SUB, Type);
326 Type = setRType3((
unsigned)ELF::R_MIPS_HI16, Type);
330 unsigned Type = (
unsigned)ELF::R_MIPS_NONE;
331 Type = setRType((
unsigned)ELF::R_MIPS_GPREL16, Type);
332 Type = setRType2((
unsigned)ELF::R_MIPS_SUB, Type);
333 Type = setRType3((
unsigned)ELF::R_MIPS_LO16, Type);
337 return ELF::R_MIPS_HIGHER;
339 return ELF::R_MIPS_HIGHEST;
341 return ELF::R_MIPS_SUB;
343 return ELF::R_MIPS_GOT_HI16;
345 return ELF::R_MIPS_GOT_LO16;
347 return ELF::R_MIPS_CALL_HI16;
349 return ELF::R_MIPS_CALL_LO16;
351 return ELF::R_MICROMIPS_26_S1;
353 return ELF::R_MICROMIPS_HI16;
355 return ELF::R_MICROMIPS_LO16;
357 return ELF::R_MICROMIPS_GOT16;
359 return ELF::R_MICROMIPS_CALL16;
361 return ELF::R_MICROMIPS_GOT_DISP;
363 return ELF::R_MICROMIPS_GOT_PAGE;
365 return ELF::R_MICROMIPS_GOT_OFST;
367 return ELF::R_MICROMIPS_TLS_GD;
369 return ELF::R_MICROMIPS_TLS_LDM;
371 return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
373 return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
375 return ELF::R_MICROMIPS_TLS_TPREL_HI16;
377 return ELF::R_MICROMIPS_TLS_TPREL_LO16;
379 return ELF::R_MICROMIPS_SUB;
421 std::vector<ELFRelocationEntry> &Relocs) {
426 if (hasRelocationAddend())
429 if (Relocs.size() < 2)
433 std::sort(Relocs.begin(), Relocs.end(),
438 std::list<MipsRelocationEntry> Sorted;
439 std::list<ELFRelocationEntry> Remainder;
441 DEBUG(dumpRelocs(
"R: ", Relocs));
446 copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
448 return getMatchingLoType(Reloc) != ELF::R_MIPS_NONE;
451 for (
auto &R : Remainder) {
452 DEBUG(
dbgs() <<
"Matching: " << R <<
"\n");
454 unsigned MatchingType = getMatchingLoType(R);
455 assert(MatchingType != ELF::R_MIPS_NONE &&
456 "Wrong list for reloc that doesn't need a match");
461 auto InsertionPoint =
462 find_best(Sorted.begin(), Sorted.end(),
463 [&R, &MatchingType](
const MipsRelocationEntry &
X) {
464 return isMatchingReloc(X, R, MatchingType);
466 compareMatchingRelocs);
482 if (InsertionPoint != Sorted.end())
483 InsertionPoint->Matched =
true;
484 Sorted.insert(InsertionPoint, R)->Matched =
true;
487 DEBUG(dumpRelocs(
"S: ", Sorted));
489 assert(Relocs.size() == Sorted.size() &&
"Some relocs were not consumed");
494 for (
const auto &R :
reverse(Sorted))
495 Relocs[CopyTo++] = R.R;
498 bool MipsELFObjectWriter::needsRelocateWithSymbol(
const MCSymbol &Sym,
499 unsigned Type)
const {
503 return needsRelocateWithSymbol(Sym, Type & 0xff) ||
504 needsRelocateWithSymbol(Sym, (Type >> 8) & 0xff) ||
505 needsRelocateWithSymbol(Sym, (Type >> 16) & 0xff);
509 errs() << Type <<
"\n";
514 case ELF::R_MIPS_NONE:
524 case ELF::R_MIPS_GOT16:
525 case ELF::R_MIPS16_GOT16:
526 case ELF::R_MICROMIPS_GOT16:
527 case ELF::R_MIPS_HI16:
528 case ELF::R_MIPS16_HI16:
529 case ELF::R_MICROMIPS_HI16:
530 case ELF::R_MIPS_LO16:
531 case ELF::R_MIPS16_LO16:
532 case ELF::R_MICROMIPS_LO16:
540 case ELF::R_MIPS_GOT_PAGE:
541 case ELF::R_MICROMIPS_GOT_PAGE:
542 case ELF::R_MIPS_GOT_OFST:
543 case ELF::R_MICROMIPS_GOT_OFST:
546 case ELF::R_MIPS_GPREL32:
552 case ELF::R_MIPS_GPREL16:
553 case ELF::R_MIPS_PC16:
554 case ELF::R_MIPS_SUB:
559 case ELF::R_MIPS_REL32:
560 case ELF::R_MIPS_LITERAL:
561 case ELF::R_MIPS_CALL16:
562 case ELF::R_MIPS_SHIFT5:
563 case ELF::R_MIPS_SHIFT6:
564 case ELF::R_MIPS_GOT_DISP:
565 case ELF::R_MIPS_GOT_HI16:
566 case ELF::R_MIPS_GOT_LO16:
567 case ELF::R_MIPS_INSERT_A:
568 case ELF::R_MIPS_INSERT_B:
569 case ELF::R_MIPS_DELETE:
570 case ELF::R_MIPS_HIGHER:
571 case ELF::R_MIPS_HIGHEST:
572 case ELF::R_MIPS_CALL_HI16:
573 case ELF::R_MIPS_CALL_LO16:
574 case ELF::R_MIPS_SCN_DISP:
575 case ELF::R_MIPS_REL16:
576 case ELF::R_MIPS_ADD_IMMEDIATE:
577 case ELF::R_MIPS_PJUMP:
578 case ELF::R_MIPS_RELGOT:
579 case ELF::R_MIPS_JALR:
580 case ELF::R_MIPS_TLS_DTPMOD32:
581 case ELF::R_MIPS_TLS_DTPREL32:
582 case ELF::R_MIPS_TLS_DTPMOD64:
583 case ELF::R_MIPS_TLS_DTPREL64:
584 case ELF::R_MIPS_TLS_GD:
585 case ELF::R_MIPS_TLS_LDM:
586 case ELF::R_MIPS_TLS_DTPREL_HI16:
587 case ELF::R_MIPS_TLS_DTPREL_LO16:
588 case ELF::R_MIPS_TLS_GOTTPREL:
589 case ELF::R_MIPS_TLS_TPREL32:
590 case ELF::R_MIPS_TLS_TPREL64:
591 case ELF::R_MIPS_TLS_TPREL_HI16:
592 case ELF::R_MIPS_TLS_TPREL_LO16:
593 case ELF::R_MIPS_GLOB_DAT:
594 case ELF::R_MIPS_PC21_S2:
595 case ELF::R_MIPS_PC26_S2:
596 case ELF::R_MIPS_PC18_S3:
597 case ELF::R_MIPS_PC19_S2:
598 case ELF::R_MIPS_PCHI16:
599 case ELF::R_MIPS_PCLO16:
600 case ELF::R_MIPS_COPY:
601 case ELF::R_MIPS_JUMP_SLOT:
602 case ELF::R_MIPS_NUM:
603 case ELF::R_MIPS_PC32:
605 case ELF::R_MICROMIPS_26_S1:
606 case ELF::R_MICROMIPS_GPREL16:
607 case ELF::R_MICROMIPS_LITERAL:
608 case ELF::R_MICROMIPS_PC7_S1:
609 case ELF::R_MICROMIPS_PC10_S1:
610 case ELF::R_MICROMIPS_PC16_S1:
611 case ELF::R_MICROMIPS_CALL16:
612 case ELF::R_MICROMIPS_GOT_DISP:
613 case ELF::R_MICROMIPS_GOT_HI16:
614 case ELF::R_MICROMIPS_GOT_LO16:
615 case ELF::R_MICROMIPS_SUB:
616 case ELF::R_MICROMIPS_HIGHER:
617 case ELF::R_MICROMIPS_HIGHEST:
618 case ELF::R_MICROMIPS_CALL_HI16:
619 case ELF::R_MICROMIPS_CALL_LO16:
620 case ELF::R_MICROMIPS_SCN_DISP:
621 case ELF::R_MICROMIPS_JALR:
622 case ELF::R_MICROMIPS_HI0_LO16:
623 case ELF::R_MICROMIPS_TLS_GD:
624 case ELF::R_MICROMIPS_TLS_LDM:
625 case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
626 case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
627 case ELF::R_MICROMIPS_TLS_GOTTPREL:
628 case ELF::R_MICROMIPS_TLS_TPREL_HI16:
629 case ELF::R_MICROMIPS_TLS_TPREL_LO16:
630 case ELF::R_MICROMIPS_GPREL7_S2:
631 case ELF::R_MICROMIPS_PC23_S2:
632 case ELF::R_MICROMIPS_PC21_S1:
633 case ELF::R_MICROMIPS_PC26_S1:
634 case ELF::R_MICROMIPS_PC18_S3:
635 case ELF::R_MICROMIPS_PC19_S2:
640 case ELF::R_MIPS16_26:
641 case ELF::R_MIPS16_GPREL:
642 case ELF::R_MIPS16_CALL16:
643 case ELF::R_MIPS16_TLS_GD:
644 case ELF::R_MIPS16_TLS_LDM:
645 case ELF::R_MIPS16_TLS_DTPREL_HI16:
646 case ELF::R_MIPS16_TLS_DTPREL_LO16:
647 case ELF::R_MIPS16_TLS_GOTTPREL:
648 case ELF::R_MIPS16_TLS_TPREL_HI16:
649 case ELF::R_MIPS16_TLS_TPREL_LO16:
660 new MipsELFObjectWriter(Is64Bit, OSABI, Is64Bit, IsLittleEndian);
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
A eight-byte dtp relative fixup.
This represents an "assembler immediate".
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
void print(raw_ostream &Out) const
Defines the object file and target independent interfaces used by the assembler backend to write nati...
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 GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
A four-byte dtp relative fixup.
constexpr bool isUInt< 8 >(uint64_t x)
The instances of the Type class are immutable: once they are created, they are never changed...
unsigned getBinding() const
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
A eight-byte tp relative fixup.
MCFixupKind getKind() const
MCObjectWriter * createMipsELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI, bool IsLittleEndian, bool Is64Bit)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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.
MCObjectWriter * createELFObjectWriter(MCELFObjectTargetWriter *MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
An abstract base class for streams implementations that also support a pwrite operation.
const MCSymbolELF * OriginalSymbol
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
#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 GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")