LoongArchAsmBackend.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //===-- LoongArchAsmBackend.cpp - LoongArch Assembler Backend -*- C++ -*---===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the LoongArchAsmBackend class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "LoongArchAsmBackend.h"
  13. #include "LoongArchFixupKinds.h"
  14. #include "llvm/MC/MCAsmLayout.h"
  15. #include "llvm/MC/MCAssembler.h"
  16. #include "llvm/MC/MCContext.h"
  17. #include "llvm/MC/MCELFObjectWriter.h"
  18. #include "llvm/MC/MCValue.h"
  19. #include "llvm/Support/Endian.h"
  20. #include "llvm/Support/EndianStream.h"
  21. #define DEBUG_TYPE "loongarch-asmbackend"
  22. using namespace llvm;
  23. std::optional<MCFixupKind>
  24. LoongArchAsmBackend::getFixupKind(StringRef Name) const {
  25. if (STI.getTargetTriple().isOSBinFormatELF()) {
  26. auto Type = llvm::StringSwitch<unsigned>(Name)
  27. #define ELF_RELOC(X, Y) .Case(#X, Y)
  28. #include "llvm/BinaryFormat/ELFRelocs/LoongArch.def"
  29. #undef ELF_RELOC
  30. .Case("BFD_RELOC_NONE", ELF::R_LARCH_NONE)
  31. .Case("BFD_RELOC_32", ELF::R_LARCH_32)
  32. .Case("BFD_RELOC_64", ELF::R_LARCH_64)
  33. .Default(-1u);
  34. if (Type != -1u)
  35. return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
  36. }
  37. return std::nullopt;
  38. }
  39. const MCFixupKindInfo &
  40. LoongArchAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
  41. const static MCFixupKindInfo Infos[] = {
  42. // This table *must* be in the order that the fixup_* kinds are defined in
  43. // LoongArchFixupKinds.h.
  44. //
  45. // {name, offset, bits, flags}
  46. {"fixup_loongarch_b16", 10, 16, MCFixupKindInfo::FKF_IsPCRel},
  47. {"fixup_loongarch_b21", 0, 26, MCFixupKindInfo::FKF_IsPCRel},
  48. {"fixup_loongarch_b26", 0, 26, MCFixupKindInfo::FKF_IsPCRel},
  49. {"fixup_loongarch_abs_hi20", 5, 20, 0},
  50. {"fixup_loongarch_abs_lo12", 10, 12, 0},
  51. {"fixup_loongarch_abs64_lo20", 5, 20, 0},
  52. {"fixup_loongarch_abs64_hi12", 10, 12, 0},
  53. {"fixup_loongarch_tls_le_hi20", 5, 20, 0},
  54. {"fixup_loongarch_tls_le_lo12", 10, 12, 0},
  55. {"fixup_loongarch_tls_le64_lo20", 5, 20, 0},
  56. {"fixup_loongarch_tls_le64_hi12", 10, 12, 0},
  57. // TODO: Add more fixup kinds.
  58. };
  59. static_assert((std::size(Infos)) == LoongArch::NumTargetFixupKinds,
  60. "Not all fixup kinds added to Infos array");
  61. // Fixup kinds from .reloc directive are like R_LARCH_NONE. They
  62. // do not require any extra processing.
  63. if (Kind >= FirstLiteralRelocationKind)
  64. return MCAsmBackend::getFixupKindInfo(FK_NONE);
  65. if (Kind < FirstTargetFixupKind)
  66. return MCAsmBackend::getFixupKindInfo(Kind);
  67. assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
  68. "Invalid kind!");
  69. return Infos[Kind - FirstTargetFixupKind];
  70. }
  71. static void reportOutOfRangeError(MCContext &Ctx, SMLoc Loc, unsigned N) {
  72. Ctx.reportError(Loc, "fixup value out of range [" + Twine(llvm::minIntN(N)) +
  73. ", " + Twine(llvm::maxIntN(N)) + "]");
  74. }
  75. static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
  76. MCContext &Ctx) {
  77. switch (Fixup.getTargetKind()) {
  78. default:
  79. llvm_unreachable("Unknown fixup kind");
  80. case FK_Data_1:
  81. case FK_Data_2:
  82. case FK_Data_4:
  83. case FK_Data_8:
  84. return Value;
  85. case LoongArch::fixup_loongarch_b16: {
  86. if (!isInt<18>(Value))
  87. reportOutOfRangeError(Ctx, Fixup.getLoc(), 18);
  88. if (Value % 4)
  89. Ctx.reportError(Fixup.getLoc(), "fixup value must be 4-byte aligned");
  90. return (Value >> 2) & 0xffff;
  91. }
  92. case LoongArch::fixup_loongarch_b21: {
  93. if (!isInt<23>(Value))
  94. reportOutOfRangeError(Ctx, Fixup.getLoc(), 23);
  95. if (Value % 4)
  96. Ctx.reportError(Fixup.getLoc(), "fixup value must be 4-byte aligned");
  97. return ((Value & 0x3fffc) << 8) | ((Value >> 18) & 0x1f);
  98. }
  99. case LoongArch::fixup_loongarch_b26: {
  100. if (!isInt<28>(Value))
  101. reportOutOfRangeError(Ctx, Fixup.getLoc(), 28);
  102. if (Value % 4)
  103. Ctx.reportError(Fixup.getLoc(), "fixup value must be 4-byte aligned");
  104. return ((Value & 0x3fffc) << 8) | ((Value >> 18) & 0x3ff);
  105. }
  106. case LoongArch::fixup_loongarch_abs_hi20:
  107. case LoongArch::fixup_loongarch_tls_le_hi20:
  108. return (Value >> 12) & 0xfffff;
  109. case LoongArch::fixup_loongarch_abs_lo12:
  110. case LoongArch::fixup_loongarch_tls_le_lo12:
  111. return Value & 0xfff;
  112. case LoongArch::fixup_loongarch_abs64_lo20:
  113. case LoongArch::fixup_loongarch_tls_le64_lo20:
  114. return (Value >> 32) & 0xfffff;
  115. case LoongArch::fixup_loongarch_abs64_hi12:
  116. case LoongArch::fixup_loongarch_tls_le64_hi12:
  117. return (Value >> 52) & 0xfff;
  118. }
  119. }
  120. void LoongArchAsmBackend::applyFixup(const MCAssembler &Asm,
  121. const MCFixup &Fixup,
  122. const MCValue &Target,
  123. MutableArrayRef<char> Data, uint64_t Value,
  124. bool IsResolved,
  125. const MCSubtargetInfo *STI) const {
  126. if (!Value)
  127. return; // Doesn't change encoding.
  128. MCFixupKind Kind = Fixup.getKind();
  129. if (Kind >= FirstLiteralRelocationKind)
  130. return;
  131. MCFixupKindInfo Info = getFixupKindInfo(Kind);
  132. MCContext &Ctx = Asm.getContext();
  133. // Apply any target-specific value adjustments.
  134. Value = adjustFixupValue(Fixup, Value, Ctx);
  135. // Shift the value into position.
  136. Value <<= Info.TargetOffset;
  137. unsigned Offset = Fixup.getOffset();
  138. unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;
  139. assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
  140. // For each byte of the fragment that the fixup touches, mask in the
  141. // bits from the fixup value.
  142. for (unsigned I = 0; I != NumBytes; ++I) {
  143. Data[Offset + I] |= uint8_t((Value >> (I * 8)) & 0xff);
  144. }
  145. }
  146. bool LoongArchAsmBackend::shouldForceRelocation(const MCAssembler &Asm,
  147. const MCFixup &Fixup,
  148. const MCValue &Target) {
  149. if (Fixup.getKind() >= FirstLiteralRelocationKind)
  150. return true;
  151. switch (Fixup.getTargetKind()) {
  152. default:
  153. return false;
  154. case FK_Data_1:
  155. case FK_Data_2:
  156. case FK_Data_4:
  157. case FK_Data_8:
  158. return !Target.isAbsolute();
  159. }
  160. }
  161. bool LoongArchAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
  162. const MCSubtargetInfo *STI) const {
  163. // We mostly follow binutils' convention here: align to 4-byte boundary with a
  164. // 0-fill padding.
  165. OS.write_zeros(Count % 4);
  166. // The remainder is now padded with 4-byte nops.
  167. // nop: andi r0, r0, 0
  168. for (; Count >= 4; Count -= 4)
  169. OS.write("\0\0\x40\x03", 4);
  170. return true;
  171. }
  172. std::unique_ptr<MCObjectTargetWriter>
  173. LoongArchAsmBackend::createObjectTargetWriter() const {
  174. return createLoongArchELFObjectWriter(OSABI, Is64Bit);
  175. }
  176. MCAsmBackend *llvm::createLoongArchAsmBackend(const Target &T,
  177. const MCSubtargetInfo &STI,
  178. const MCRegisterInfo &MRI,
  179. const MCTargetOptions &Options) {
  180. const Triple &TT = STI.getTargetTriple();
  181. uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
  182. return new LoongArchAsmBackend(STI, OSABI, TT.isArch64Bit());
  183. }