WinCOFFStreamer.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //===-- llvm/MC/WinCOFFStreamer.cpp -----------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains an implementation of a Windows COFF object file streamer.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/MC/MCAsmBackend.h"
  14. #include "llvm/MC/MCAsmLayout.h"
  15. #include "llvm/MC/MCAssembler.h"
  16. #include "llvm/MC/MCCodeEmitter.h"
  17. #include "llvm/MC/MCContext.h"
  18. #include "llvm/MC/MCExpr.h"
  19. #include "llvm/MC/MCObjectFileInfo.h"
  20. #include "llvm/MC/MCObjectStreamer.h"
  21. #include "llvm/MC/MCSection.h"
  22. #include "llvm/MC/MCSectionCOFF.h"
  23. #include "llvm/MC/MCStreamer.h"
  24. #include "llvm/MC/MCSymbolCOFF.h"
  25. #include "llvm/MC/MCValue.h"
  26. #include "llvm/MC/MCWinCOFFStreamer.h"
  27. #include "llvm/Support/COFF.h"
  28. #include "llvm/Support/Debug.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/MathExtras.h"
  31. #include "llvm/Support/TargetRegistry.h"
  32. #include "llvm/Support/raw_ostream.h"
  33. using namespace llvm;
  34. #define DEBUG_TYPE "WinCOFFStreamer"
  35. namespace llvm {
  36. MCWinCOFFStreamer::MCWinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
  37. MCCodeEmitter &CE, raw_pwrite_stream &OS)
  38. : MCObjectStreamer(Context, MAB, OS, &CE), CurSymbol(nullptr) {}
  39. void MCWinCOFFStreamer::EmitInstToData(const MCInst &Inst,
  40. const MCSubtargetInfo &STI) {
  41. MCDataFragment *DF = getOrCreateDataFragment();
  42. SmallVector<MCFixup, 4> Fixups;
  43. SmallString<256> Code;
  44. raw_svector_ostream VecOS(Code);
  45. getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
  46. VecOS.flush();
  47. // Add the fixups and data.
  48. for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
  49. Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
  50. DF->getFixups().push_back(Fixups[i]);
  51. }
  52. DF->getContents().append(Code.begin(), Code.end());
  53. }
  54. void MCWinCOFFStreamer::InitSections(bool NoExecStack) {
  55. // FIXME: this is identical to the ELF one.
  56. // This emulates the same behavior of GNU as. This makes it easier
  57. // to compare the output as the major sections are in the same order.
  58. SwitchSection(getContext().getObjectFileInfo()->getTextSection());
  59. EmitCodeAlignment(4);
  60. SwitchSection(getContext().getObjectFileInfo()->getDataSection());
  61. EmitCodeAlignment(4);
  62. SwitchSection(getContext().getObjectFileInfo()->getBSSSection());
  63. EmitCodeAlignment(4);
  64. SwitchSection(getContext().getObjectFileInfo()->getTextSection());
  65. }
  66. void MCWinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
  67. assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
  68. MCObjectStreamer::EmitLabel(Symbol);
  69. }
  70. void MCWinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
  71. llvm_unreachable("not implemented");
  72. }
  73. void MCWinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
  74. llvm_unreachable("not implemented");
  75. }
  76. bool MCWinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
  77. MCSymbolAttr Attribute) {
  78. assert(Symbol && "Symbol must be non-null!");
  79. assert((!Symbol->isInSection() ||
  80. Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
  81. "Got non-COFF section in the COFF backend!");
  82. getAssembler().registerSymbol(*Symbol);
  83. switch (Attribute) {
  84. default: return false;
  85. case MCSA_WeakReference:
  86. case MCSA_Weak:
  87. cast<MCSymbolCOFF>(Symbol)->setIsWeakExternal();
  88. Symbol->setExternal(true);
  89. break;
  90. case MCSA_Global:
  91. Symbol->setExternal(true);
  92. break;
  93. }
  94. return true;
  95. }
  96. void MCWinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
  97. llvm_unreachable("not implemented");
  98. }
  99. void MCWinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
  100. assert((!Symbol->isInSection() ||
  101. Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
  102. "Got non-COFF section in the COFF backend!");
  103. if (CurSymbol)
  104. FatalError("starting a new symbol definition without completing the "
  105. "previous one");
  106. CurSymbol = Symbol;
  107. }
  108. void MCWinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
  109. if (!CurSymbol)
  110. FatalError("storage class specified outside of symbol definition");
  111. if (StorageClass & ~COFF::SSC_Invalid)
  112. FatalError("storage class value '" + Twine(StorageClass) +
  113. "' out of range");
  114. getAssembler().registerSymbol(*CurSymbol);
  115. cast<MCSymbolCOFF>(CurSymbol)->setClass((uint16_t)StorageClass);
  116. }
  117. void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) {
  118. if (!CurSymbol)
  119. FatalError("symbol type specified outside of a symbol definition");
  120. if (Type & ~0xffff)
  121. FatalError("type value '" + Twine(Type) + "' out of range");
  122. getAssembler().registerSymbol(*CurSymbol);
  123. cast<MCSymbolCOFF>(CurSymbol)->setType((uint16_t)Type);
  124. }
  125. void MCWinCOFFStreamer::EndCOFFSymbolDef() {
  126. if (!CurSymbol)
  127. FatalError("ending symbol definition without starting one");
  128. CurSymbol = nullptr;
  129. }
  130. void MCWinCOFFStreamer::EmitCOFFSafeSEH(MCSymbol const *Symbol) {
  131. // SafeSEH is a feature specific to 32-bit x86. It does not exist (and is
  132. // unnecessary) on all platforms which use table-based exception dispatch.
  133. if (getContext().getObjectFileInfo()->getTargetTriple().getArch() !=
  134. Triple::x86)
  135. return;
  136. const MCSymbolCOFF *CSymbol = cast<MCSymbolCOFF>(Symbol);
  137. if (CSymbol->isSafeSEH())
  138. return;
  139. MCSection *SXData = getContext().getObjectFileInfo()->getSXDataSection();
  140. getAssembler().registerSection(*SXData);
  141. if (SXData->getAlignment() < 4)
  142. SXData->setAlignment(4);
  143. new MCSafeSEHFragment(Symbol, SXData);
  144. getAssembler().registerSymbol(*Symbol);
  145. CSymbol->setIsSafeSEH();
  146. // The Microsoft linker requires that the symbol type of a handler be
  147. // function. Go ahead and oblige it here.
  148. CSymbol->setType(COFF::IMAGE_SYM_DTYPE_FUNCTION
  149. << COFF::SCT_COMPLEX_TYPE_SHIFT);
  150. }
  151. void MCWinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
  152. MCDataFragment *DF = getOrCreateDataFragment();
  153. const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext());
  154. MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_2);
  155. DF->getFixups().push_back(Fixup);
  156. DF->getContents().resize(DF->getContents().size() + 2, 0);
  157. }
  158. void MCWinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
  159. MCDataFragment *DF = getOrCreateDataFragment();
  160. const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext());
  161. MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_4);
  162. DF->getFixups().push_back(Fixup);
  163. DF->getContents().resize(DF->getContents().size() + 4, 0);
  164. }
  165. void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  166. unsigned ByteAlignment) {
  167. assert((!Symbol->isInSection() ||
  168. Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
  169. "Got non-COFF section in the COFF backend!");
  170. const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
  171. if (T.isKnownWindowsMSVCEnvironment()) {
  172. if (ByteAlignment > 32)
  173. report_fatal_error("alignment is limited to 32-bytes");
  174. // Round size up to alignment so that we will honor the alignment request.
  175. Size = std::max(Size, static_cast<uint64_t>(ByteAlignment));
  176. }
  177. AssignSection(Symbol, nullptr);
  178. getAssembler().registerSymbol(*Symbol);
  179. Symbol->setExternal(true);
  180. Symbol->setCommon(Size, ByteAlignment);
  181. if (!T.isKnownWindowsMSVCEnvironment() && ByteAlignment > 1) {
  182. SmallString<128> Directive;
  183. raw_svector_ostream OS(Directive);
  184. const MCObjectFileInfo *MFI = getContext().getObjectFileInfo();
  185. OS << " -aligncomm:\"" << Symbol->getName() << "\","
  186. << Log2_32_Ceil(ByteAlignment);
  187. OS.flush();
  188. PushSection();
  189. SwitchSection(MFI->getDrectveSection());
  190. EmitBytes(Directive);
  191. PopSection();
  192. }
  193. }
  194. void MCWinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  195. unsigned ByteAlignment) {
  196. assert(!Symbol->isInSection() && "Symbol must not already have a section!");
  197. MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
  198. getAssembler().registerSection(*Section);
  199. if (Section->getAlignment() < ByteAlignment)
  200. Section->setAlignment(ByteAlignment);
  201. getAssembler().registerSymbol(*Symbol);
  202. Symbol->setExternal(false);
  203. AssignSection(Symbol, Section);
  204. if (ByteAlignment != 1)
  205. new MCAlignFragment(ByteAlignment, /*Value=*/0, /*ValueSize=*/0,
  206. ByteAlignment, Section);
  207. MCFillFragment *Fragment = new MCFillFragment(
  208. /*Value=*/0, /*ValueSize=*/0, Size, Section);
  209. Symbol->setFragment(Fragment);
  210. }
  211. void MCWinCOFFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
  212. uint64_t Size, unsigned ByteAlignment) {
  213. llvm_unreachable("not implemented");
  214. }
  215. void MCWinCOFFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
  216. uint64_t Size, unsigned ByteAlignment) {
  217. llvm_unreachable("not implemented");
  218. }
  219. void MCWinCOFFStreamer::EmitFileDirective(StringRef Filename) {
  220. getAssembler().addFileName(Filename);
  221. }
  222. // TODO: Implement this if you want to emit .comment section in COFF obj files.
  223. void MCWinCOFFStreamer::EmitIdent(StringRef IdentString) {
  224. llvm_unreachable("not implemented");
  225. }
  226. void MCWinCOFFStreamer::EmitWinEHHandlerData() {
  227. llvm_unreachable("not implemented");
  228. }
  229. void MCWinCOFFStreamer::FinishImpl() {
  230. MCObjectStreamer::FinishImpl();
  231. }
  232. LLVM_ATTRIBUTE_NORETURN
  233. void MCWinCOFFStreamer::FatalError(const Twine &Msg) const {
  234. getContext().reportFatalError(SMLoc(), Msg);
  235. }
  236. }