PPCAsmBackend.cpp 9.2 KB

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