AArch64ELFStreamer.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //===- lib/MC/AArch64ELFStreamer.cpp - ELF Object Output for AArch64 ------===//
  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 assembles .s files and emits AArch64 ELF .o object files. Different
  10. // from generic ELF streamer in emitting mapping symbols ($x and $d) to delimit
  11. // regions of data and code.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "AArch64ELFStreamer.h"
  15. #include "AArch64MCTargetDesc.h"
  16. #include "AArch64TargetStreamer.h"
  17. #include "AArch64WinCOFFStreamer.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/ADT/Triple.h"
  21. #include "llvm/ADT/Twine.h"
  22. #include "llvm/BinaryFormat/ELF.h"
  23. #include "llvm/MC/MCAsmBackend.h"
  24. #include "llvm/MC/MCAssembler.h"
  25. #include "llvm/MC/MCCodeEmitter.h"
  26. #include "llvm/MC/MCContext.h"
  27. #include "llvm/MC/MCELFStreamer.h"
  28. #include "llvm/MC/MCExpr.h"
  29. #include "llvm/MC/MCInst.h"
  30. #include "llvm/MC/MCObjectWriter.h"
  31. #include "llvm/MC/MCSection.h"
  32. #include "llvm/MC/MCStreamer.h"
  33. #include "llvm/MC/MCSubtargetInfo.h"
  34. #include "llvm/MC/MCSymbolELF.h"
  35. #include "llvm/MC/MCWinCOFFStreamer.h"
  36. #include "llvm/Support/Casting.h"
  37. #include "llvm/Support/FormattedStream.h"
  38. #include "llvm/Support/raw_ostream.h"
  39. using namespace llvm;
  40. namespace {
  41. class AArch64ELFStreamer;
  42. class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
  43. formatted_raw_ostream &OS;
  44. void emitInst(uint32_t Inst) override;
  45. void emitDirectiveVariantPCS(MCSymbol *Symbol) override {
  46. OS << "\t.variant_pcs\t" << Symbol->getName() << "\n";
  47. }
  48. void emitARM64WinCFIAllocStack(unsigned Size) override {
  49. OS << "\t.seh_stackalloc\t" << Size << "\n";
  50. }
  51. void emitARM64WinCFISaveR19R20X(int Offset) override {
  52. OS << "\t.seh_save_r19r20_x\t" << Offset << "\n";
  53. }
  54. void emitARM64WinCFISaveFPLR(int Offset) override {
  55. OS << "\t.seh_save_fplr\t" << Offset << "\n";
  56. }
  57. void emitARM64WinCFISaveFPLRX(int Offset) override {
  58. OS << "\t.seh_save_fplr_x\t" << Offset << "\n";
  59. }
  60. void emitARM64WinCFISaveReg(unsigned Reg, int Offset) override {
  61. OS << "\t.seh_save_reg\tx" << Reg << ", " << Offset << "\n";
  62. }
  63. void emitARM64WinCFISaveRegX(unsigned Reg, int Offset) override {
  64. OS << "\t.seh_save_reg_x\tx" << Reg << ", " << Offset << "\n";
  65. }
  66. void emitARM64WinCFISaveRegP(unsigned Reg, int Offset) override {
  67. OS << "\t.seh_save_regp\tx" << Reg << ", " << Offset << "\n";
  68. }
  69. void emitARM64WinCFISaveRegPX(unsigned Reg, int Offset) override {
  70. OS << "\t.seh_save_regp_x\tx" << Reg << ", " << Offset << "\n";
  71. }
  72. void emitARM64WinCFISaveLRPair(unsigned Reg, int Offset) override {
  73. OS << "\t.seh_save_lrpair\tx" << Reg << ", " << Offset << "\n";
  74. }
  75. void emitARM64WinCFISaveFReg(unsigned Reg, int Offset) override {
  76. OS << "\t.seh_save_freg\td" << Reg << ", " << Offset << "\n";
  77. }
  78. void emitARM64WinCFISaveFRegX(unsigned Reg, int Offset) override {
  79. OS << "\t.seh_save_freg_x\td" << Reg << ", " << Offset << "\n";
  80. }
  81. void emitARM64WinCFISaveFRegP(unsigned Reg, int Offset) override {
  82. OS << "\t.seh_save_fregp\td" << Reg << ", " << Offset << "\n";
  83. }
  84. void emitARM64WinCFISaveFRegPX(unsigned Reg, int Offset) override {
  85. OS << "\t.seh_save_fregp_x\td" << Reg << ", " << Offset << "\n";
  86. }
  87. void emitARM64WinCFISetFP() override { OS << "\t.seh_set_fp\n"; }
  88. void emitARM64WinCFIAddFP(unsigned Size) override {
  89. OS << "\t.seh_add_fp\t" << Size << "\n";
  90. }
  91. void emitARM64WinCFINop() override { OS << "\t.seh_nop\n"; }
  92. void emitARM64WinCFISaveNext() override { OS << "\t.seh_save_next\n"; }
  93. void emitARM64WinCFIPrologEnd() override { OS << "\t.seh_endprologue\n"; }
  94. void emitARM64WinCFIEpilogStart() override { OS << "\t.seh_startepilogue\n"; }
  95. void emitARM64WinCFIEpilogEnd() override { OS << "\t.seh_endepilogue\n"; }
  96. void emitARM64WinCFITrapFrame() override { OS << "\t.seh_trap_frame\n"; }
  97. void emitARM64WinCFIMachineFrame() override { OS << "\t.seh_pushframe\n"; }
  98. void emitARM64WinCFIContext() override { OS << "\t.seh_context\n"; }
  99. void emitARM64WinCFIClearUnwoundToCall() override {
  100. OS << "\t.seh_clear_unwound_to_call\n";
  101. }
  102. public:
  103. AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
  104. };
  105. AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
  106. formatted_raw_ostream &OS)
  107. : AArch64TargetStreamer(S), OS(OS) {}
  108. void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
  109. OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
  110. }
  111. /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
  112. /// the appropriate points in the object files. These symbols are defined in the
  113. /// AArch64 ELF ABI:
  114. /// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
  115. ///
  116. /// In brief: $x or $d should be emitted at the start of each contiguous region
  117. /// of A64 code or data in a section. In practice, this emission does not rely
  118. /// on explicit assembler directives but on inherent properties of the
  119. /// directives doing the emission (e.g. ".byte" is data, "add x0, x0, x0" an
  120. /// instruction).
  121. ///
  122. /// As a result this system is orthogonal to the DataRegion infrastructure used
  123. /// by MachO. Beware!
  124. class AArch64ELFStreamer : public MCELFStreamer {
  125. public:
  126. AArch64ELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
  127. std::unique_ptr<MCObjectWriter> OW,
  128. std::unique_ptr<MCCodeEmitter> Emitter)
  129. : MCELFStreamer(Context, std::move(TAB), std::move(OW),
  130. std::move(Emitter)),
  131. MappingSymbolCounter(0), LastEMS(EMS_None) {}
  132. void changeSection(MCSection *Section, const MCExpr *Subsection) override {
  133. // We have to keep track of the mapping symbol state of any sections we
  134. // use. Each one should start off as EMS_None, which is provided as the
  135. // default constructor by DenseMap::lookup.
  136. LastMappingSymbols[getPreviousSection().first] = LastEMS;
  137. LastEMS = LastMappingSymbols.lookup(Section);
  138. MCELFStreamer::changeSection(Section, Subsection);
  139. }
  140. // Reset state between object emissions
  141. void reset() override {
  142. MappingSymbolCounter = 0;
  143. MCELFStreamer::reset();
  144. LastMappingSymbols.clear();
  145. LastEMS = EMS_None;
  146. }
  147. /// This function is the one used to emit instruction data into the ELF
  148. /// streamer. We override it to add the appropriate mapping symbol if
  149. /// necessary.
  150. void emitInstruction(const MCInst &Inst,
  151. const MCSubtargetInfo &STI) override {
  152. emitA64MappingSymbol();
  153. MCELFStreamer::emitInstruction(Inst, STI);
  154. }
  155. /// Emit a 32-bit value as an instruction. This is only used for the .inst
  156. /// directive, EmitInstruction should be used in other cases.
  157. void emitInst(uint32_t Inst) {
  158. char Buffer[4];
  159. // We can't just use EmitIntValue here, as that will emit a data mapping
  160. // symbol, and swap the endianness on big-endian systems (instructions are
  161. // always little-endian).
  162. for (char &C : Buffer) {
  163. C = uint8_t(Inst);
  164. Inst >>= 8;
  165. }
  166. emitA64MappingSymbol();
  167. MCELFStreamer::emitBytes(StringRef(Buffer, 4));
  168. }
  169. /// This is one of the functions used to emit data into an ELF section, so the
  170. /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
  171. /// if necessary.
  172. void emitBytes(StringRef Data) override {
  173. emitDataMappingSymbol();
  174. MCELFStreamer::emitBytes(Data);
  175. }
  176. /// This is one of the functions used to emit data into an ELF section, so the
  177. /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
  178. /// if necessary.
  179. void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
  180. emitDataMappingSymbol();
  181. MCELFStreamer::emitValueImpl(Value, Size, Loc);
  182. }
  183. void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
  184. SMLoc Loc) override {
  185. emitDataMappingSymbol();
  186. MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
  187. }
  188. private:
  189. enum ElfMappingSymbol {
  190. EMS_None,
  191. EMS_A64,
  192. EMS_Data
  193. };
  194. void emitDataMappingSymbol() {
  195. if (LastEMS == EMS_Data)
  196. return;
  197. emitMappingSymbol("$d");
  198. LastEMS = EMS_Data;
  199. }
  200. void emitA64MappingSymbol() {
  201. if (LastEMS == EMS_A64)
  202. return;
  203. emitMappingSymbol("$x");
  204. LastEMS = EMS_A64;
  205. }
  206. void emitMappingSymbol(StringRef Name) {
  207. auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
  208. Name + "." + Twine(MappingSymbolCounter++)));
  209. emitLabel(Symbol);
  210. Symbol->setType(ELF::STT_NOTYPE);
  211. Symbol->setBinding(ELF::STB_LOCAL);
  212. Symbol->setExternal(false);
  213. }
  214. int64_t MappingSymbolCounter;
  215. DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
  216. ElfMappingSymbol LastEMS;
  217. };
  218. } // end anonymous namespace
  219. AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
  220. return static_cast<AArch64ELFStreamer &>(Streamer);
  221. }
  222. void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
  223. getStreamer().emitInst(Inst);
  224. }
  225. void AArch64TargetELFStreamer::emitDirectiveVariantPCS(MCSymbol *Symbol) {
  226. getStreamer().getAssembler().registerSymbol(*Symbol);
  227. cast<MCSymbolELF>(Symbol)->setOther(ELF::STO_AARCH64_VARIANT_PCS);
  228. }
  229. MCTargetStreamer *
  230. llvm::createAArch64AsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS,
  231. MCInstPrinter *InstPrint,
  232. bool isVerboseAsm) {
  233. return new AArch64TargetAsmStreamer(S, OS);
  234. }
  235. MCELFStreamer *llvm::createAArch64ELFStreamer(
  236. MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
  237. std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
  238. bool RelaxAll) {
  239. AArch64ELFStreamer *S = new AArch64ELFStreamer(
  240. Context, std::move(TAB), std::move(OW), std::move(Emitter));
  241. if (RelaxAll)
  242. S->getAssembler().setRelaxAll(true);
  243. return S;
  244. }