PPCELFStreamer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //===-------- PPCELFStreamer.cpp - ELF Object Output ---------------------===//
  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 is a custom MCELFStreamer for PowerPC.
  10. //
  11. // The purpose of the custom ELF streamer is to allow us to intercept
  12. // instructions as they are being emitted and align all 8 byte instructions
  13. // to a 64 byte boundary if required (by adding a 4 byte nop). This is important
  14. // because 8 byte instructions are not allowed to cross 64 byte boundaries
  15. // and by aliging anything that is within 4 bytes of the boundary we can
  16. // guarantee that the 8 byte instructions do not cross that boundary.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include "PPCELFStreamer.h"
  20. #include "PPCFixupKinds.h"
  21. #include "PPCInstrInfo.h"
  22. #include "PPCMCCodeEmitter.h"
  23. #include "llvm/BinaryFormat/ELF.h"
  24. #include "llvm/MC/MCAsmBackend.h"
  25. #include "llvm/MC/MCAssembler.h"
  26. #include "llvm/MC/MCCodeEmitter.h"
  27. #include "llvm/MC/MCContext.h"
  28. #include "llvm/MC/MCInst.h"
  29. #include "llvm/MC/MCInstrDesc.h"
  30. #include "llvm/MC/MCObjectWriter.h"
  31. #include "llvm/MC/MCSymbolELF.h"
  32. #include "llvm/Support/Casting.h"
  33. #include "llvm/Support/SourceMgr.h"
  34. using namespace llvm;
  35. PPCELFStreamer::PPCELFStreamer(MCContext &Context,
  36. std::unique_ptr<MCAsmBackend> MAB,
  37. std::unique_ptr<MCObjectWriter> OW,
  38. std::unique_ptr<MCCodeEmitter> Emitter)
  39. : MCELFStreamer(Context, std::move(MAB), std::move(OW), std::move(Emitter)),
  40. LastLabel(nullptr) {}
  41. void PPCELFStreamer::emitPrefixedInstruction(const MCInst &Inst,
  42. const MCSubtargetInfo &STI) {
  43. // Prefixed instructions must not cross a 64-byte boundary (i.e. prefix is
  44. // before the boundary and the remaining 4-bytes are after the boundary). In
  45. // order to achieve this, a nop is added prior to any such boundary-crossing
  46. // prefixed instruction. Align to 64 bytes if possible but add a maximum of 4
  47. // bytes when trying to do that. If alignment requires adding more than 4
  48. // bytes then the instruction won't be aligned. When emitting a code alignment
  49. // a new fragment is created for this alignment. This fragment will contain
  50. // all of the nops required as part of the alignment operation. In the cases
  51. // when no nops are added then The fragment is still created but it remains
  52. // empty.
  53. emitCodeAlignment(64, &STI, 4);
  54. // Emit the instruction.
  55. // Since the previous emit created a new fragment then adding this instruction
  56. // also forces the addition of a new fragment. Inst is now the first
  57. // instruction in that new fragment.
  58. MCELFStreamer::emitInstruction(Inst, STI);
  59. // The above instruction is forced to start a new fragment because it
  60. // comes after a code alignment fragment. Get that new fragment.
  61. MCFragment *InstructionFragment = getCurrentFragment();
  62. SMLoc InstLoc = Inst.getLoc();
  63. // Check if there was a last label emitted.
  64. if (LastLabel && !LastLabel->isUnset() && LastLabelLoc.isValid() &&
  65. InstLoc.isValid()) {
  66. const SourceMgr *SourceManager = getContext().getSourceManager();
  67. unsigned InstLine = SourceManager->FindLineNumber(InstLoc);
  68. unsigned LabelLine = SourceManager->FindLineNumber(LastLabelLoc);
  69. // If the Label and the Instruction are on the same line then move the
  70. // label to the top of the fragment containing the aligned instruction that
  71. // was just added.
  72. if (InstLine == LabelLine) {
  73. AssignFragment(LastLabel, InstructionFragment);
  74. LastLabel->setOffset(0);
  75. }
  76. }
  77. }
  78. void PPCELFStreamer::emitInstruction(const MCInst &Inst,
  79. const MCSubtargetInfo &STI) {
  80. PPCMCCodeEmitter *Emitter =
  81. static_cast<PPCMCCodeEmitter*>(getAssembler().getEmitterPtr());
  82. // If the instruction is a part of the GOT to PC-Rel link time optimization
  83. // instruction pair, return a value, otherwise return None. A true returned
  84. // value means the instruction is the PLDpc and a false value means it is
  85. // the user instruction.
  86. Optional<bool> IsPartOfGOTToPCRelPair = isPartOfGOTToPCRelPair(Inst, STI);
  87. // User of the GOT-indirect address.
  88. // For example, the load that will get the relocation as follows:
  89. // .reloc .Lpcrel1-8,R_PPC64_PCREL_OPT,.-(.Lpcrel1-8)
  90. // lwa 3, 4(3)
  91. if (IsPartOfGOTToPCRelPair.hasValue() && !IsPartOfGOTToPCRelPair.getValue())
  92. emitGOTToPCRelReloc(Inst);
  93. // Special handling is only for prefixed instructions.
  94. if (!Emitter->isPrefixedInstruction(Inst)) {
  95. MCELFStreamer::emitInstruction(Inst, STI);
  96. return;
  97. }
  98. emitPrefixedInstruction(Inst, STI);
  99. // Producer of the GOT-indirect address.
  100. // For example, the prefixed load from the got that will get the label as
  101. // follows:
  102. // pld 3, vec@got@pcrel(0), 1
  103. // .Lpcrel1:
  104. if (IsPartOfGOTToPCRelPair.hasValue() && IsPartOfGOTToPCRelPair.getValue())
  105. emitGOTToPCRelLabel(Inst);
  106. }
  107. void PPCELFStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
  108. LastLabel = Symbol;
  109. LastLabelLoc = Loc;
  110. MCELFStreamer::emitLabel(Symbol);
  111. }
  112. // This linker time GOT PC Relative optimization relocation will look like this:
  113. // pld <reg> symbol@got@pcrel
  114. // <Label###>:
  115. // .reloc Label###-8,R_PPC64_PCREL_OPT,.-(Label###-8)
  116. // load <loadedreg>, 0(<reg>)
  117. // The reason we place the label after the PLDpc instruction is that there
  118. // may be an alignment nop before it since prefixed instructions must not
  119. // cross a 64-byte boundary (please see
  120. // PPCELFStreamer::emitPrefixedInstruction()). When referring to the
  121. // label, we subtract the width of a prefixed instruction (8 bytes) to ensure
  122. // we refer to the PLDpc.
  123. void PPCELFStreamer::emitGOTToPCRelReloc(const MCInst &Inst) {
  124. // Get the last operand which contains the symbol.
  125. const MCOperand &Operand = Inst.getOperand(Inst.getNumOperands() - 1);
  126. assert(Operand.isExpr() && "Expecting an MCExpr.");
  127. // Cast the last operand to MCSymbolRefExpr to get the symbol.
  128. const MCExpr *Expr = Operand.getExpr();
  129. const MCSymbolRefExpr *SymExpr = static_cast<const MCSymbolRefExpr *>(Expr);
  130. assert(SymExpr->getKind() == MCSymbolRefExpr::VK_PPC_PCREL_OPT &&
  131. "Expecting a symbol of type VK_PPC_PCREL_OPT");
  132. MCSymbol *LabelSym =
  133. getContext().getOrCreateSymbol(SymExpr->getSymbol().getName());
  134. const MCExpr *LabelExpr = MCSymbolRefExpr::create(LabelSym, getContext());
  135. const MCExpr *Eight = MCConstantExpr::create(8, getContext());
  136. // SubExpr is just Label###-8
  137. const MCExpr *SubExpr =
  138. MCBinaryExpr::createSub(LabelExpr, Eight, getContext());
  139. MCSymbol *CurrentLocation = getContext().createTempSymbol();
  140. const MCExpr *CurrentLocationExpr =
  141. MCSymbolRefExpr::create(CurrentLocation, getContext());
  142. // SubExpr2 is .-(Label###-8)
  143. const MCExpr *SubExpr2 =
  144. MCBinaryExpr::createSub(CurrentLocationExpr, SubExpr, getContext());
  145. MCDataFragment *DF = static_cast<MCDataFragment *>(LabelSym->getFragment());
  146. assert(DF && "Expecting a valid data fragment.");
  147. MCFixupKind FixupKind = static_cast<MCFixupKind>(FirstLiteralRelocationKind +
  148. ELF::R_PPC64_PCREL_OPT);
  149. DF->getFixups().push_back(
  150. MCFixup::create(LabelSym->getOffset() - 8, SubExpr2,
  151. FixupKind, Inst.getLoc()));
  152. emitLabel(CurrentLocation, Inst.getLoc());
  153. }
  154. // Emit the label that immediately follows the PLDpc for a link time GOT PC Rel
  155. // optimization.
  156. void PPCELFStreamer::emitGOTToPCRelLabel(const MCInst &Inst) {
  157. // Get the last operand which contains the symbol.
  158. const MCOperand &Operand = Inst.getOperand(Inst.getNumOperands() - 1);
  159. assert(Operand.isExpr() && "Expecting an MCExpr.");
  160. // Cast the last operand to MCSymbolRefExpr to get the symbol.
  161. const MCExpr *Expr = Operand.getExpr();
  162. const MCSymbolRefExpr *SymExpr = static_cast<const MCSymbolRefExpr *>(Expr);
  163. assert(SymExpr->getKind() == MCSymbolRefExpr::VK_PPC_PCREL_OPT &&
  164. "Expecting a symbol of type VK_PPC_PCREL_OPT");
  165. MCSymbol *LabelSym =
  166. getContext().getOrCreateSymbol(SymExpr->getSymbol().getName());
  167. emitLabel(LabelSym, Inst.getLoc());
  168. }
  169. // This funciton checks if the parameter Inst is part of the setup for a link
  170. // time GOT PC Relative optimization. For example in this situation:
  171. // <MCInst PLDpc <MCOperand Reg:282> <MCOperand Expr:(glob_double@got@pcrel)>
  172. // <MCOperand Imm:0> <MCOperand Expr:(.Lpcrel@<<invalid>>)>>
  173. // <MCInst SOME_LOAD <MCOperand Reg:22> <MCOperand Imm:0> <MCOperand Reg:282>
  174. // <MCOperand Expr:(.Lpcrel@<<invalid>>)>>
  175. // The above is a pair of such instructions and this function will not return
  176. // None for either one of them. In both cases we are looking for the last
  177. // operand <MCOperand Expr:(.Lpcrel@<<invalid>>)> which needs to be an MCExpr
  178. // and has the flag MCSymbolRefExpr::VK_PPC_PCREL_OPT. After that we just look
  179. // at the opcode and in the case of PLDpc we will return true. For the load
  180. // (or store) this function will return false indicating it has found the second
  181. // instruciton in the pair.
  182. Optional<bool> llvm::isPartOfGOTToPCRelPair(const MCInst &Inst,
  183. const MCSubtargetInfo &STI) {
  184. // Need at least two operands.
  185. if (Inst.getNumOperands() < 2)
  186. return None;
  187. unsigned LastOp = Inst.getNumOperands() - 1;
  188. // The last operand needs to be an MCExpr and it needs to have a variant kind
  189. // of VK_PPC_PCREL_OPT. If it does not satisfy these conditions it is not a
  190. // link time GOT PC Rel opt instruction and we can ignore it and return None.
  191. const MCOperand &Operand = Inst.getOperand(LastOp);
  192. if (!Operand.isExpr())
  193. return None;
  194. // Check for the variant kind VK_PPC_PCREL_OPT in this expression.
  195. const MCExpr *Expr = Operand.getExpr();
  196. const MCSymbolRefExpr *SymExpr = static_cast<const MCSymbolRefExpr *>(Expr);
  197. if (!SymExpr || SymExpr->getKind() != MCSymbolRefExpr::VK_PPC_PCREL_OPT)
  198. return None;
  199. return (Inst.getOpcode() == PPC::PLDpc);
  200. }
  201. MCELFStreamer *llvm::createPPCELFStreamer(
  202. MCContext &Context, std::unique_ptr<MCAsmBackend> MAB,
  203. std::unique_ptr<MCObjectWriter> OW,
  204. std::unique_ptr<MCCodeEmitter> Emitter) {
  205. return new PPCELFStreamer(Context, std::move(MAB), std::move(OW),
  206. std::move(Emitter));
  207. }