30 #define DEBUG_TYPE "mips-elf-object-writer"
37 struct MipsRelocationEntry {
45 Out <<
", Matched=" << Matched;
58 MipsELFObjectWriter(
bool _is64Bit, uint8_t OSABI,
bool _isN64,
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) {
118 DEBUG(
dbgs() << std::distance(First,
I) <<
" is a match (";
120 if (Best == Last || BetterThan(*
I, *Best)) {
121 DEBUG(
dbgs() <<
".. and it beats the last one\n");
125 if (Matched == FindBest_PerfectMatch) {
126 DEBUG(
dbgs() <<
".. and it is unbeatable\n");
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;
151 return ELF::R_MIPS_NONE;
153 if (Type == ELF::R_MIPS_GOT16)
154 return ELF::R_MIPS_LO16;
155 if (Type == ELF::R_MICROMIPS_GOT16)
156 return ELF::R_MICROMIPS_LO16;
157 if (Type == ELF::R_MIPS16_GOT16)
158 return ELF::R_MIPS16_LO16;
160 return ELF::R_MIPS_NONE;
179 unsigned MatchingType) {
180 if (X.R.Type == MatchingType && X.R.OriginalSymbol == R.
OriginalSymbol) {
183 return FindBest_PerfectMatch;
185 return FindBest_Match;
187 return FindBest_NoMatch;
197 const MipsRelocationEntry &PreviousBest) {
198 if (Candidate.R.OriginalAddend != PreviousBest.R.OriginalAddend)
199 return Candidate.R.OriginalAddend < PreviousBest.R.OriginalAddend;
200 return PreviousBest.Matched && !Candidate.Matched;
205 template <
class Container>
207 for (
const auto &R : Relocs)
208 dbgs() << Prefix << R <<
"\n";
212 MipsELFObjectWriter::MipsELFObjectWriter(
bool _is64Bit, uint8_t OSABI,
213 bool _isN64,
bool IsLittleEndian)
221 bool IsPCRel)
const {
227 return ELF::R_MIPS_NONE;
230 return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
233 return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
240 return ELF::R_MIPS_PC16;
242 return ELF::R_MICROMIPS_PC7_S1;
244 return ELF::R_MICROMIPS_PC10_S1;
246 return ELF::R_MICROMIPS_PC16_S1;
248 return ELF::R_MICROMIPS_PC26_S1;
250 return ELF::R_MICROMIPS_PC19_S2;
252 return ELF::R_MICROMIPS_PC18_S3;
254 return ELF::R_MICROMIPS_PC21_S1;
256 return ELF::R_MIPS_PC19_S2;
258 return ELF::R_MIPS_PC18_S3;
260 return ELF::R_MIPS_PC21_S2;
262 return ELF::R_MIPS_PC26_S2;
264 return ELF::R_MIPS_PCHI16;
266 return ELF::R_MIPS_PCLO16;
275 return ELF::R_MIPS_64;
277 return ELF::R_MIPS_TLS_DTPREL32;
279 return ELF::R_MIPS_TLS_DTPREL64;
281 return ELF::R_MIPS_TLS_TPREL32;
283 return ELF::R_MIPS_TLS_TPREL64;
287 Type = setRType((
unsigned)ELF::R_MIPS_GPREL32, Type);
288 Type = setRType2((
unsigned)ELF::R_MIPS_64, Type);
289 Type = setRType3((
unsigned)ELF::R_MIPS_NONE, Type);
292 return ELF::R_MIPS_GPREL32;
294 return ELF::R_MIPS_GPREL16;
296 return ELF::R_MIPS_26;
298 return ELF::R_MIPS_CALL16;
300 return ELF::R_MIPS_GOT16;
302 return ELF::R_MIPS_HI16;
304 return ELF::R_MIPS_LO16;
306 return ELF::R_MIPS_TLS_GD;
308 return ELF::R_MIPS_TLS_GOTTPREL;
310 return ELF::R_MIPS_TLS_TPREL_HI16;
312 return ELF::R_MIPS_TLS_TPREL_LO16;
314 return ELF::R_MIPS_TLS_LDM;
316 return ELF::R_MIPS_TLS_DTPREL_HI16;
318 return ELF::R_MIPS_TLS_DTPREL_LO16;
320 return ELF::R_MIPS_GOT_PAGE;
322 return ELF::R_MIPS_GOT_OFST;
324 return ELF::R_MIPS_GOT_DISP;
326 unsigned Type = (
unsigned)ELF::R_MIPS_NONE;
327 Type = setRType((
unsigned)ELF::R_MIPS_GPREL16, Type);
328 Type = setRType2((
unsigned)ELF::R_MIPS_SUB, Type);
329 Type = setRType3((
unsigned)ELF::R_MIPS_HI16, Type);
333 unsigned Type = (
unsigned)ELF::R_MIPS_NONE;
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_LO16, Type);
340 return ELF::R_MIPS_HIGHER;
342 return ELF::R_MIPS_HIGHEST;
344 return ELF::R_MIPS_SUB;
346 return ELF::R_MIPS_GOT_HI16;
348 return ELF::R_MIPS_GOT_LO16;
350 return ELF::R_MIPS_CALL_HI16;
352 return ELF::R_MIPS_CALL_LO16;
354 return ELF::R_MICROMIPS_26_S1;
356 return ELF::R_MICROMIPS_HI16;
358 return ELF::R_MICROMIPS_LO16;
360 return ELF::R_MICROMIPS_GOT16;
362 return ELF::R_MICROMIPS_CALL16;
364 return ELF::R_MICROMIPS_GOT_DISP;
366 return ELF::R_MICROMIPS_GOT_PAGE;
368 return ELF::R_MICROMIPS_GOT_OFST;
370 return ELF::R_MICROMIPS_TLS_GD;
372 return ELF::R_MICROMIPS_TLS_LDM;
374 return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
376 return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
378 return ELF::R_MICROMIPS_TLS_GOTTPREL;
380 return ELF::R_MICROMIPS_TLS_TPREL_HI16;
382 return ELF::R_MICROMIPS_TLS_TPREL_LO16;
384 return ELF::R_MICROMIPS_SUB;
426 std::vector<ELFRelocationEntry> &Relocs) {
430 if (hasRelocationAddend())
433 if (Relocs.size() < 2)
442 std::list<MipsRelocationEntry> Sorted;
443 std::list<ELFRelocationEntry> Remainder;
450 copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
455 for (
auto &R : Remainder) {
456 DEBUG(
dbgs() <<
"Matching: " << R <<
"\n");
459 assert(MatchingType != ELF::R_MIPS_NONE &&
460 "Wrong list for reloc that doesn't need a match");
465 auto InsertionPoint =
467 [&R, &MatchingType](
const MipsRelocationEntry &
X) {
486 if (InsertionPoint != Sorted.end())
487 InsertionPoint->Matched =
true;
488 Sorted.insert(InsertionPoint, R)->Matched =
true;
493 assert(Relocs.size() == Sorted.size() &&
"Some relocs were not consumed");
498 for (
const auto &R :
reverse(Sorted))
499 Relocs[CopyTo++] = R.R;
502 bool MipsELFObjectWriter::needsRelocateWithSymbol(
const MCSymbol &Sym,
503 unsigned Type)
const {
507 return needsRelocateWithSymbol(Sym, Type & 0xff) ||
508 needsRelocateWithSymbol(Sym, (Type >> 8) & 0xff) ||
509 needsRelocateWithSymbol(Sym, (Type >> 16) & 0xff);
513 errs() << Type <<
"\n";
518 case ELF::R_MIPS_NONE:
528 case ELF::R_MIPS_GOT16:
529 case ELF::R_MIPS16_GOT16:
530 case ELF::R_MICROMIPS_GOT16:
531 case ELF::R_MIPS_HIGHER:
532 case ELF::R_MIPS_HIGHEST:
533 case ELF::R_MIPS_HI16:
534 case ELF::R_MIPS16_HI16:
535 case ELF::R_MICROMIPS_HI16:
536 case ELF::R_MIPS_LO16:
537 case ELF::R_MIPS16_LO16:
538 case ELF::R_MICROMIPS_LO16:
546 case ELF::R_MIPS_GOT_PAGE:
547 case ELF::R_MICROMIPS_GOT_PAGE:
548 case ELF::R_MIPS_GOT_OFST:
549 case ELF::R_MICROMIPS_GOT_OFST:
552 case ELF::R_MIPS_GPREL32:
558 case ELF::R_MIPS_GPREL16:
559 case ELF::R_MIPS_PC16:
560 case ELF::R_MIPS_SUB:
565 case ELF::R_MIPS_REL32:
566 case ELF::R_MIPS_LITERAL:
567 case ELF::R_MIPS_CALL16:
568 case ELF::R_MIPS_SHIFT5:
569 case ELF::R_MIPS_SHIFT6:
570 case ELF::R_MIPS_GOT_DISP:
571 case ELF::R_MIPS_GOT_HI16:
572 case ELF::R_MIPS_GOT_LO16:
573 case ELF::R_MIPS_INSERT_A:
574 case ELF::R_MIPS_INSERT_B:
575 case ELF::R_MIPS_DELETE:
576 case ELF::R_MIPS_CALL_HI16:
577 case ELF::R_MIPS_CALL_LO16:
578 case ELF::R_MIPS_SCN_DISP:
579 case ELF::R_MIPS_REL16:
580 case ELF::R_MIPS_ADD_IMMEDIATE:
581 case ELF::R_MIPS_PJUMP:
582 case ELF::R_MIPS_RELGOT:
583 case ELF::R_MIPS_JALR:
584 case ELF::R_MIPS_TLS_DTPMOD32:
585 case ELF::R_MIPS_TLS_DTPREL32:
586 case ELF::R_MIPS_TLS_DTPMOD64:
587 case ELF::R_MIPS_TLS_DTPREL64:
588 case ELF::R_MIPS_TLS_GD:
589 case ELF::R_MIPS_TLS_LDM:
590 case ELF::R_MIPS_TLS_DTPREL_HI16:
591 case ELF::R_MIPS_TLS_DTPREL_LO16:
592 case ELF::R_MIPS_TLS_GOTTPREL:
593 case ELF::R_MIPS_TLS_TPREL32:
594 case ELF::R_MIPS_TLS_TPREL64:
595 case ELF::R_MIPS_TLS_TPREL_HI16:
596 case ELF::R_MIPS_TLS_TPREL_LO16:
597 case ELF::R_MIPS_GLOB_DAT:
598 case ELF::R_MIPS_PC21_S2:
599 case ELF::R_MIPS_PC26_S2:
600 case ELF::R_MIPS_PC18_S3:
601 case ELF::R_MIPS_PC19_S2:
602 case ELF::R_MIPS_PCHI16:
603 case ELF::R_MIPS_PCLO16:
604 case ELF::R_MIPS_COPY:
605 case ELF::R_MIPS_JUMP_SLOT:
606 case ELF::R_MIPS_NUM:
607 case ELF::R_MIPS_PC32:
609 case ELF::R_MICROMIPS_26_S1:
610 case ELF::R_MICROMIPS_GPREL16:
611 case ELF::R_MICROMIPS_LITERAL:
612 case ELF::R_MICROMIPS_PC7_S1:
613 case ELF::R_MICROMIPS_PC10_S1:
614 case ELF::R_MICROMIPS_PC16_S1:
615 case ELF::R_MICROMIPS_CALL16:
616 case ELF::R_MICROMIPS_GOT_DISP:
617 case ELF::R_MICROMIPS_GOT_HI16:
618 case ELF::R_MICROMIPS_GOT_LO16:
619 case ELF::R_MICROMIPS_SUB:
620 case ELF::R_MICROMIPS_HIGHER:
621 case ELF::R_MICROMIPS_HIGHEST:
622 case ELF::R_MICROMIPS_CALL_HI16:
623 case ELF::R_MICROMIPS_CALL_LO16:
624 case ELF::R_MICROMIPS_SCN_DISP:
625 case ELF::R_MICROMIPS_JALR:
626 case ELF::R_MICROMIPS_HI0_LO16:
627 case ELF::R_MICROMIPS_TLS_GD:
628 case ELF::R_MICROMIPS_TLS_LDM:
629 case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
630 case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
631 case ELF::R_MICROMIPS_TLS_GOTTPREL:
632 case ELF::R_MICROMIPS_TLS_TPREL_HI16:
633 case ELF::R_MICROMIPS_TLS_TPREL_LO16:
634 case ELF::R_MICROMIPS_GPREL7_S2:
635 case ELF::R_MICROMIPS_PC23_S2:
636 case ELF::R_MICROMIPS_PC21_S1:
637 case ELF::R_MICROMIPS_PC26_S1:
638 case ELF::R_MICROMIPS_PC18_S3:
639 case ELF::R_MICROMIPS_PC19_S2:
644 case ELF::R_MIPS16_26:
645 case ELF::R_MIPS16_GPREL:
646 case ELF::R_MIPS16_CALL16:
647 case ELF::R_MIPS16_TLS_GD:
648 case ELF::R_MIPS16_TLS_LDM:
649 case ELF::R_MIPS16_TLS_DTPREL_HI16:
650 case ELF::R_MIPS16_TLS_DTPREL_LO16:
651 case ELF::R_MIPS16_TLS_GOTTPREL:
652 case ELF::R_MIPS16_TLS_TPREL_HI16:
653 case ELF::R_MIPS16_TLS_TPREL_LO16:
664 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 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.
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.
unsigned getBinding() const
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
static bool compareMatchingRelocs(const MipsRelocationEntry &Candidate, const MipsRelocationEntry &PreviousBest)
Determine whether Candidate or PreviousBest is the better match.
A eight-byte tp relative fixup.
MCFixupKind getKind() const
MCObjectWriter * createMipsELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI, bool IsLittleEndian, bool Is64Bit)
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.
llvm_unreachable("Unknown RegBank!")
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 unsigned getMatchingLoType(const ELFRelocationEntry &Reloc)
Determine the low relocation that matches the given relocation.
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())