30#define DEBUG_TYPE "mips-elf-object-writer"
37struct MipsRelocationEntry {
46 MipsELFObjectWriter(
uint8_t OSABI,
bool HasRelocationAddend,
bool Is64);
48 ~MipsELFObjectWriter()
override =
default;
53 unsigned Type)
const override;
55 std::vector<ELFRelocationEntry> &Relocs)
override;
59enum FindBestPredicateResult {
63 FindBest_PerfectMatch,
71template <
class InputIt,
class OutputIt1,
class OutputIt2,
class UnaryPredicate>
73 OutputIt1 d1, OutputIt2 d2,
74 UnaryPredicate Predicate) {
85 return std::make_pair(d1, d2);
97template <
class InputIt,
class UnaryPredicate,
class Comparator>
99 Comparator BetterThan) {
103 unsigned Matched = Predicate(*
I);
104 if (Matched != FindBest_NoMatch) {
105 if (Best ==
Last || BetterThan(*
I, *Best))
108 if (Matched == FindBest_PerfectMatch)
124 if (
Type == ELF::R_MIPS_HI16)
125 return ELF::R_MIPS_LO16;
126 if (
Type == ELF::R_MICROMIPS_HI16)
127 return ELF::R_MICROMIPS_LO16;
128 if (
Type == ELF::R_MIPS16_HI16)
129 return ELF::R_MIPS16_LO16;
132 return ELF::R_MIPS_NONE;
134 if (
Type == ELF::R_MIPS_GOT16)
135 return ELF::R_MIPS_LO16;
136 if (
Type == ELF::R_MICROMIPS_GOT16)
137 return ELF::R_MICROMIPS_LO16;
138 if (
Type == ELF::R_MIPS16_GOT16)
139 return ELF::R_MIPS16_LO16;
141 return ELF::R_MIPS_NONE;
148 return X.Type == MatchingType &&
X.Symbol == R.Symbol &&
X.Addend == R.Addend;
151MipsELFObjectWriter::MipsELFObjectWriter(
uint8_t OSABI,
152 bool HasRelocationAddend,
bool Is64)
155unsigned MipsELFObjectWriter::getRelocType(
MCContext &Ctx,
158 bool IsPCRel)
const {
166 return ELF::R_MIPS_NONE;
169 "MIPS does not support one byte relocations");
170 return ELF::R_MIPS_NONE;
173 return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
176 return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
180 ? setRTypes(ELF::R_MIPS_PC32, ELF::R_MIPS_64, ELF::R_MIPS_NONE)
188 return ELF::R_MIPS_PC16;
190 return ELF::R_MICROMIPS_PC7_S1;
192 return ELF::R_MICROMIPS_PC10_S1;
194 return ELF::R_MICROMIPS_PC16_S1;
196 return ELF::R_MICROMIPS_PC26_S1;
198 return ELF::R_MICROMIPS_PC19_S2;
200 return ELF::R_MICROMIPS_PC18_S3;
202 return ELF::R_MICROMIPS_PC21_S1;
204 return ELF::R_MIPS_PC19_S2;
206 return ELF::R_MIPS_PC18_S3;
208 return ELF::R_MIPS_PC21_S2;
210 return ELF::R_MIPS_PC26_S2;
212 return ELF::R_MIPS_PCHI16;
214 return ELF::R_MIPS_PCLO16;
222 return ELF::R_MIPS_TLS_DTPREL32;
224 return ELF::R_MIPS_TLS_DTPREL64;
226 return ELF::R_MIPS_TLS_TPREL32;
228 return ELF::R_MIPS_TLS_TPREL64;
230 return setRTypes(ELF::R_MIPS_GPREL32,
231 is64Bit() ? ELF::R_MIPS_64 : ELF::R_MIPS_NONE,
234 return ELF::R_MIPS_GPREL16;
236 return ELF::R_MIPS_26;
238 return ELF::R_MIPS_CALL16;
240 return ELF::R_MIPS_GOT16;
242 return ELF::R_MIPS_HI16;
244 return ELF::R_MIPS_LO16;
246 return ELF::R_MIPS_TLS_GD;
248 return ELF::R_MIPS_TLS_GOTTPREL;
250 return ELF::R_MIPS_TLS_TPREL_HI16;
252 return ELF::R_MIPS_TLS_TPREL_LO16;
254 return ELF::R_MIPS_TLS_LDM;
256 return ELF::R_MIPS_TLS_DTPREL_HI16;
258 return ELF::R_MIPS_TLS_DTPREL_LO16;
260 return ELF::R_MIPS_GOT_PAGE;
262 return ELF::R_MIPS_GOT_OFST;
264 return ELF::R_MIPS_GOT_DISP;
266 return setRTypes(ELF::R_MIPS_GPREL16, ELF::R_MIPS_SUB, ELF::R_MIPS_HI16);
268 return setRTypes(ELF::R_MICROMIPS_GPREL16, ELF::R_MICROMIPS_SUB,
269 ELF::R_MICROMIPS_HI16);
271 return setRTypes(ELF::R_MIPS_GPREL16, ELF::R_MIPS_SUB, ELF::R_MIPS_LO16);
273 return setRTypes(ELF::R_MICROMIPS_GPREL16, ELF::R_MICROMIPS_SUB,
274 ELF::R_MICROMIPS_LO16);
276 return ELF::R_MIPS_HIGHER;
278 return ELF::R_MIPS_HIGHEST;
280 return ELF::R_MIPS_SUB;
282 return ELF::R_MIPS_GOT_HI16;
284 return ELF::R_MIPS_GOT_LO16;
286 return ELF::R_MIPS_CALL_HI16;
288 return ELF::R_MIPS_CALL_LO16;
290 return ELF::R_MICROMIPS_26_S1;
292 return ELF::R_MICROMIPS_HI16;
294 return ELF::R_MICROMIPS_LO16;
296 return ELF::R_MICROMIPS_GOT16;
298 return ELF::R_MICROMIPS_CALL16;
300 return ELF::R_MICROMIPS_GOT_DISP;
302 return ELF::R_MICROMIPS_GOT_PAGE;
304 return ELF::R_MICROMIPS_GOT_OFST;
306 return ELF::R_MICROMIPS_TLS_GD;
308 return ELF::R_MICROMIPS_TLS_LDM;
310 return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
312 return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
314 return ELF::R_MICROMIPS_TLS_GOTTPREL;
316 return ELF::R_MICROMIPS_TLS_TPREL_HI16;
318 return ELF::R_MICROMIPS_TLS_TPREL_LO16;
320 return ELF::R_MICROMIPS_SUB;
322 return ELF::R_MICROMIPS_HIGHER;
324 return ELF::R_MICROMIPS_HIGHEST;
326 return ELF::R_MIPS_JALR;
328 return ELF::R_MICROMIPS_JALR;
369void MipsELFObjectWriter::sortRelocs(
const MCAssembler &Asm,
370 std::vector<ELFRelocationEntry> &Relocs) {
374 if (hasRelocationAddend())
380 return A.Offset <
B.Offset;
385 std::list<MipsRelocationEntry> Sorted;
387 for (
auto &R : Relocs) {
393 for (
auto I : Hi16) {
397 if (std::next(
I) != Sorted.end() &&
407 auto Best = Sorted.end();
408 for (
auto J = Sorted.begin(); J != Sorted.end(); ++J) {
410 if (R1.Type == MatchingType &&
R.Symbol == R1.Symbol &&
411 R.Addend <= R1.Addend &&
412 (Best == Sorted.end() || R1.Addend < Best->R.Addend ||
413 (!Best->Matched && R1.Addend == Best->R.Addend)))
416 if (Best != Sorted.end() &&
R.Addend == Best->R.Addend)
417 Best->Matched =
true;
421 Sorted.splice(Best, Sorted,
I);
424 assert(Relocs.size() == Sorted.size() &&
"Some relocs were not consumed");
428 for (
const auto &R : Sorted)
429 Relocs[CopyTo++] =
R.R;
432bool MipsELFObjectWriter::needsRelocateWithSymbol(
const MCValue &Val,
434 unsigned Type)
const {
437 if (!isUInt<8>(
Type))
438 return needsRelocateWithSymbol(Val,
Sym,
Type & 0xff) ||
439 needsRelocateWithSymbol(Val,
Sym, (
Type >> 8) & 0xff) ||
440 needsRelocateWithSymbol(Val,
Sym, (
Type >> 16) & 0xff);
449 case ELF::R_MIPS_NONE:
459 case ELF::R_MIPS_GOT16:
460 case ELF::R_MIPS16_GOT16:
461 case ELF::R_MICROMIPS_GOT16:
462 case ELF::R_MIPS_HIGHER:
463 case ELF::R_MIPS_HIGHEST:
464 case ELF::R_MIPS_HI16:
465 case ELF::R_MIPS16_HI16:
466 case ELF::R_MICROMIPS_HI16:
467 case ELF::R_MIPS_LO16:
468 case ELF::R_MIPS16_LO16:
469 case ELF::R_MICROMIPS_LO16:
477 case ELF::R_MIPS_GOT_PAGE:
478 case ELF::R_MICROMIPS_GOT_PAGE:
479 case ELF::R_MIPS_GOT_OFST:
480 case ELF::R_MICROMIPS_GOT_OFST:
483 case ELF::R_MIPS_GPREL32:
489 case ELF::R_MIPS_GPREL16:
490 case ELF::R_MIPS_PC16:
491 case ELF::R_MIPS_SUB:
496 case ELF::R_MIPS_REL32:
497 case ELF::R_MIPS_LITERAL:
498 case ELF::R_MIPS_CALL16:
499 case ELF::R_MIPS_SHIFT5:
500 case ELF::R_MIPS_SHIFT6:
501 case ELF::R_MIPS_GOT_DISP:
502 case ELF::R_MIPS_GOT_HI16:
503 case ELF::R_MIPS_GOT_LO16:
504 case ELF::R_MIPS_INSERT_A:
505 case ELF::R_MIPS_INSERT_B:
506 case ELF::R_MIPS_DELETE:
507 case ELF::R_MIPS_CALL_HI16:
508 case ELF::R_MIPS_CALL_LO16:
509 case ELF::R_MIPS_SCN_DISP:
510 case ELF::R_MIPS_REL16:
511 case ELF::R_MIPS_ADD_IMMEDIATE:
512 case ELF::R_MIPS_PJUMP:
513 case ELF::R_MIPS_RELGOT:
514 case ELF::R_MIPS_JALR:
515 case ELF::R_MIPS_TLS_DTPMOD32:
516 case ELF::R_MIPS_TLS_DTPREL32:
517 case ELF::R_MIPS_TLS_DTPMOD64:
518 case ELF::R_MIPS_TLS_DTPREL64:
519 case ELF::R_MIPS_TLS_GD:
520 case ELF::R_MIPS_TLS_LDM:
521 case ELF::R_MIPS_TLS_DTPREL_HI16:
522 case ELF::R_MIPS_TLS_DTPREL_LO16:
523 case ELF::R_MIPS_TLS_GOTTPREL:
524 case ELF::R_MIPS_TLS_TPREL32:
525 case ELF::R_MIPS_TLS_TPREL64:
526 case ELF::R_MIPS_TLS_TPREL_HI16:
527 case ELF::R_MIPS_TLS_TPREL_LO16:
528 case ELF::R_MIPS_GLOB_DAT:
529 case ELF::R_MIPS_PC21_S2:
530 case ELF::R_MIPS_PC26_S2:
531 case ELF::R_MIPS_PC18_S3:
532 case ELF::R_MIPS_PC19_S2:
533 case ELF::R_MIPS_PCHI16:
534 case ELF::R_MIPS_PCLO16:
535 case ELF::R_MIPS_COPY:
536 case ELF::R_MIPS_JUMP_SLOT:
537 case ELF::R_MIPS_NUM:
538 case ELF::R_MIPS_PC32:
540 case ELF::R_MICROMIPS_26_S1:
541 case ELF::R_MICROMIPS_GPREL16:
542 case ELF::R_MICROMIPS_LITERAL:
543 case ELF::R_MICROMIPS_PC7_S1:
544 case ELF::R_MICROMIPS_PC10_S1:
545 case ELF::R_MICROMIPS_PC16_S1:
546 case ELF::R_MICROMIPS_CALL16:
547 case ELF::R_MICROMIPS_GOT_DISP:
548 case ELF::R_MICROMIPS_GOT_HI16:
549 case ELF::R_MICROMIPS_GOT_LO16:
550 case ELF::R_MICROMIPS_SUB:
551 case ELF::R_MICROMIPS_HIGHER:
552 case ELF::R_MICROMIPS_HIGHEST:
553 case ELF::R_MICROMIPS_CALL_HI16:
554 case ELF::R_MICROMIPS_CALL_LO16:
555 case ELF::R_MICROMIPS_SCN_DISP:
556 case ELF::R_MICROMIPS_JALR:
557 case ELF::R_MICROMIPS_HI0_LO16:
558 case ELF::R_MICROMIPS_TLS_GD:
559 case ELF::R_MICROMIPS_TLS_LDM:
560 case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
561 case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
562 case ELF::R_MICROMIPS_TLS_GOTTPREL:
563 case ELF::R_MICROMIPS_TLS_TPREL_HI16:
564 case ELF::R_MICROMIPS_TLS_TPREL_LO16:
565 case ELF::R_MICROMIPS_GPREL7_S2:
566 case ELF::R_MICROMIPS_PC23_S2:
567 case ELF::R_MICROMIPS_PC21_S1:
568 case ELF::R_MICROMIPS_PC26_S1:
569 case ELF::R_MICROMIPS_PC18_S3:
570 case ELF::R_MICROMIPS_PC19_S2:
575 case ELF::R_MIPS16_26:
576 case ELF::R_MIPS16_GPREL:
577 case ELF::R_MIPS16_CALL16:
578 case ELF::R_MIPS16_TLS_GD:
579 case ELF::R_MIPS16_TLS_LDM:
580 case ELF::R_MIPS16_TLS_DTPREL_HI16:
581 case ELF::R_MIPS16_TLS_DTPREL_LO16:
582 case ELF::R_MIPS16_TLS_GOTTPREL:
583 case ELF::R_MIPS16_TLS_TPREL_HI16:
584 case ELF::R_MIPS16_TLS_TPREL_LO16:
590std::unique_ptr<MCObjectTargetWriter>
593 bool IsN64 = TT.isArch64Bit() && !IsN32;
594 bool HasRelocationAddend = TT.isArch64Bit();
595 return std::make_unique<MipsELFObjectWriter>(OSABI, HasRelocationAddend,
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 bool isMatchingReloc(unsigned MatchingType, const ELFRelocationEntry &R, const ELFRelocationEntry &X)
static InputIt find_best(InputIt First, InputIt Last, UnaryPredicate Predicate, Comparator BetterThan)
Find the best match in the range [First, Last).
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".
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
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.
#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 * Symbol