PPCAsmBackend.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //===-- PPCAsmBackend.cpp - PPC Assembler Backend -------------------------===//
  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. #include "MCTargetDesc/PPCFixupKinds.h"
  9. #include "MCTargetDesc/PPCMCTargetDesc.h"
  10. #include "llvm/BinaryFormat/ELF.h"
  11. #include "llvm/BinaryFormat/MachO.h"
  12. #include "llvm/MC/MCAsmBackend.h"
  13. #include "llvm/MC/MCAssembler.h"
  14. #include "llvm/MC/MCELFObjectWriter.h"
  15. #include "llvm/MC/MCFixupKindInfo.h"
  16. #include "llvm/MC/MCMachObjectWriter.h"
  17. #include "llvm/MC/MCObjectWriter.h"
  18. #include "llvm/MC/MCSectionMachO.h"
  19. #include "llvm/MC/MCSubtargetInfo.h"
  20. #include "llvm/MC/MCSymbolELF.h"
  21. #include "llvm/MC/MCValue.h"
  22. #include "llvm/MC/TargetRegistry.h"
  23. #include "llvm/Support/ErrorHandling.h"
  24. using namespace llvm;
  25. static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
  26. switch (Kind) {
  27. default:
  28. llvm_unreachable("Unknown fixup kind!");
  29. case FK_Data_1:
  30. case FK_Data_2:
  31. case FK_Data_4:
  32. case FK_Data_8:
  33. case PPC::fixup_ppc_nofixup:
  34. return Value;
  35. case PPC::fixup_ppc_brcond14:
  36. case PPC::fixup_ppc_brcond14abs:
  37. return Value & 0xfffc;
  38. case PPC::fixup_ppc_br24:
  39. case PPC::fixup_ppc_br24abs:
  40. case PPC::fixup_ppc_br24_notoc:
  41. return Value & 0x3fffffc;
  42. case PPC::fixup_ppc_half16:
  43. return Value & 0xffff;
  44. case PPC::fixup_ppc_half16ds:
  45. case PPC::fixup_ppc_half16dq:
  46. return Value & 0xfffc;
  47. case PPC::fixup_ppc_pcrel34:
  48. case PPC::fixup_ppc_imm34:
  49. return Value & 0x3ffffffff;
  50. }
  51. }
  52. static unsigned getFixupKindNumBytes(unsigned Kind) {
  53. switch (Kind) {
  54. default:
  55. llvm_unreachable("Unknown fixup kind!");
  56. case FK_Data_1:
  57. return 1;
  58. case FK_Data_2:
  59. case PPC::fixup_ppc_half16:
  60. case PPC::fixup_ppc_half16ds:
  61. case PPC::fixup_ppc_half16dq:
  62. return 2;
  63. case FK_Data_4:
  64. case PPC::fixup_ppc_brcond14:
  65. case PPC::fixup_ppc_brcond14abs:
  66. case PPC::fixup_ppc_br24:
  67. case PPC::fixup_ppc_br24abs:
  68. case PPC::fixup_ppc_br24_notoc:
  69. return 4;
  70. case PPC::fixup_ppc_pcrel34:
  71. case PPC::fixup_ppc_imm34:
  72. case FK_Data_8:
  73. return 8;
  74. case PPC::fixup_ppc_nofixup:
  75. return 0;
  76. }
  77. }
  78. namespace {
  79. class PPCAsmBackend : public MCAsmBackend {
  80. protected:
  81. Triple TT;
  82. public:
  83. PPCAsmBackend(const Target &T, const Triple &TT)
  84. : MCAsmBackend(TT.isLittleEndian() ? support::little : support::big),
  85. TT(TT) {}
  86. unsigned getNumFixupKinds() const override {
  87. return PPC::NumTargetFixupKinds;
  88. }
  89. const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
  90. const static MCFixupKindInfo InfosBE[PPC::NumTargetFixupKinds] = {
  91. // name offset bits flags
  92. { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
  93. { "fixup_ppc_br24_notoc", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
  94. { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
  95. { "fixup_ppc_br24abs", 6, 24, 0 },
  96. { "fixup_ppc_brcond14abs", 16, 14, 0 },
  97. { "fixup_ppc_half16", 0, 16, 0 },
  98. { "fixup_ppc_half16ds", 0, 14, 0 },
  99. { "fixup_ppc_pcrel34", 0, 34, MCFixupKindInfo::FKF_IsPCRel },
  100. { "fixup_ppc_imm34", 0, 34, 0 },
  101. { "fixup_ppc_nofixup", 0, 0, 0 }
  102. };
  103. const static MCFixupKindInfo InfosLE[PPC::NumTargetFixupKinds] = {
  104. // name offset bits flags
  105. { "fixup_ppc_br24", 2, 24, MCFixupKindInfo::FKF_IsPCRel },
  106. { "fixup_ppc_br24_notoc", 2, 24, MCFixupKindInfo::FKF_IsPCRel },
  107. { "fixup_ppc_brcond14", 2, 14, MCFixupKindInfo::FKF_IsPCRel },
  108. { "fixup_ppc_br24abs", 2, 24, 0 },
  109. { "fixup_ppc_brcond14abs", 2, 14, 0 },
  110. { "fixup_ppc_half16", 0, 16, 0 },
  111. { "fixup_ppc_half16ds", 2, 14, 0 },
  112. { "fixup_ppc_pcrel34", 0, 34, MCFixupKindInfo::FKF_IsPCRel },
  113. { "fixup_ppc_imm34", 0, 34, 0 },
  114. { "fixup_ppc_nofixup", 0, 0, 0 }
  115. };
  116. // Fixup kinds from .reloc directive are like R_PPC_NONE/R_PPC64_NONE. They
  117. // do not require any extra processing.
  118. if (Kind >= FirstLiteralRelocationKind)
  119. return MCAsmBackend::getFixupKindInfo(FK_NONE);
  120. if (Kind < FirstTargetFixupKind)
  121. return MCAsmBackend::getFixupKindInfo(Kind);
  122. assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
  123. "Invalid kind!");
  124. return (Endian == support::little
  125. ? InfosLE
  126. : InfosBE)[Kind - FirstTargetFixupKind];
  127. }
  128. void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
  129. const MCValue &Target, MutableArrayRef<char> Data,
  130. uint64_t Value, bool IsResolved,
  131. const MCSubtargetInfo *STI) const override {
  132. MCFixupKind Kind = Fixup.getKind();
  133. if (Kind >= FirstLiteralRelocationKind)
  134. return;
  135. Value = adjustFixupValue(Kind, Value);
  136. if (!Value) return; // Doesn't change encoding.
  137. unsigned Offset = Fixup.getOffset();
  138. unsigned NumBytes = getFixupKindNumBytes(Kind);
  139. // For each byte of the fragment that the fixup touches, mask in the bits
  140. // from the fixup value. The Value has been "split up" into the appropriate
  141. // bitfields above.
  142. for (unsigned i = 0; i != NumBytes; ++i) {
  143. unsigned Idx = Endian == support::little ? i : (NumBytes - 1 - i);
  144. Data[Offset + i] |= uint8_t((Value >> (Idx * 8)) & 0xff);
  145. }
  146. }
  147. bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
  148. const MCValue &Target) override {
  149. MCFixupKind Kind = Fixup.getKind();
  150. switch ((unsigned)Kind) {
  151. default:
  152. return Kind >= FirstLiteralRelocationKind;
  153. case PPC::fixup_ppc_br24:
  154. case PPC::fixup_ppc_br24abs:
  155. case PPC::fixup_ppc_br24_notoc:
  156. // If the target symbol has a local entry point we must not attempt
  157. // to resolve the fixup directly. Emit a relocation and leave
  158. // resolution of the final target address to the linker.
  159. if (const MCSymbolRefExpr *A = Target.getSymA()) {
  160. if (const auto *S = dyn_cast<MCSymbolELF>(&A->getSymbol())) {
  161. // The "other" values are stored in the last 6 bits of the second
  162. // byte. The traditional defines for STO values assume the full byte
  163. // and thus the shift to pack it.
  164. unsigned Other = S->getOther() << 2;
  165. if ((Other & ELF::STO_PPC64_LOCAL_MASK) != 0)
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. }
  172. bool fixupNeedsRelaxation(const MCFixup &Fixup,
  173. uint64_t Value,
  174. const MCRelaxableFragment *DF,
  175. const MCAsmLayout &Layout) const override {
  176. // FIXME.
  177. llvm_unreachable("relaxInstruction() unimplemented");
  178. }
  179. void relaxInstruction(MCInst &Inst,
  180. const MCSubtargetInfo &STI) const override {
  181. // FIXME.
  182. llvm_unreachable("relaxInstruction() unimplemented");
  183. }
  184. bool writeNopData(raw_ostream &OS, uint64_t Count,
  185. const MCSubtargetInfo *STI) const override {
  186. uint64_t NumNops = Count / 4;
  187. for (uint64_t i = 0; i != NumNops; ++i)
  188. support::endian::write<uint32_t>(OS, 0x60000000, Endian);
  189. OS.write_zeros(Count % 4);
  190. return true;
  191. }
  192. };
  193. } // end anonymous namespace
  194. // FIXME: This should be in a separate file.
  195. namespace {
  196. class ELFPPCAsmBackend : public PPCAsmBackend {
  197. public:
  198. ELFPPCAsmBackend(const Target &T, const Triple &TT) : PPCAsmBackend(T, TT) {}
  199. std::unique_ptr<MCObjectTargetWriter>
  200. createObjectTargetWriter() const override {
  201. uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
  202. bool Is64 = TT.isPPC64();
  203. return createPPCELFObjectWriter(Is64, OSABI);
  204. }
  205. std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
  206. };
  207. class XCOFFPPCAsmBackend : public PPCAsmBackend {
  208. public:
  209. XCOFFPPCAsmBackend(const Target &T, const Triple &TT)
  210. : PPCAsmBackend(T, TT) {}
  211. std::unique_ptr<MCObjectTargetWriter>
  212. createObjectTargetWriter() const override {
  213. return createPPCXCOFFObjectWriter(TT.isArch64Bit());
  214. }
  215. };
  216. } // end anonymous namespace
  217. std::optional<MCFixupKind>
  218. ELFPPCAsmBackend::getFixupKind(StringRef Name) const {
  219. if (TT.isOSBinFormatELF()) {
  220. unsigned Type;
  221. if (TT.isPPC64()) {
  222. Type = llvm::StringSwitch<unsigned>(Name)
  223. #define ELF_RELOC(X, Y) .Case(#X, Y)
  224. #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
  225. #undef ELF_RELOC
  226. .Case("BFD_RELOC_NONE", ELF::R_PPC64_NONE)
  227. .Case("BFD_RELOC_16", ELF::R_PPC64_ADDR16)
  228. .Case("BFD_RELOC_32", ELF::R_PPC64_ADDR32)
  229. .Case("BFD_RELOC_64", ELF::R_PPC64_ADDR64)
  230. .Default(-1u);
  231. } else {
  232. Type = llvm::StringSwitch<unsigned>(Name)
  233. #define ELF_RELOC(X, Y) .Case(#X, Y)
  234. #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
  235. #undef ELF_RELOC
  236. .Case("BFD_RELOC_NONE", ELF::R_PPC_NONE)
  237. .Case("BFD_RELOC_16", ELF::R_PPC_ADDR16)
  238. .Case("BFD_RELOC_32", ELF::R_PPC_ADDR32)
  239. .Default(-1u);
  240. }
  241. if (Type != -1u)
  242. return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
  243. }
  244. return std::nullopt;
  245. }
  246. MCAsmBackend *llvm::createPPCAsmBackend(const Target &T,
  247. const MCSubtargetInfo &STI,
  248. const MCRegisterInfo &MRI,
  249. const MCTargetOptions &Options) {
  250. const Triple &TT = STI.getTargetTriple();
  251. if (TT.isOSBinFormatXCOFF())
  252. return new XCOFFPPCAsmBackend(T, TT);
  253. return new ELFPPCAsmBackend(T, TT);
  254. }