MCWinCOFFStreamer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. //===- llvm/MC/MCWinCOFFStreamer.cpp --------------------------------------===//
  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 contains an implementation of a Windows COFF object file streamer.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/MC/MCWinCOFFStreamer.h"
  13. #include "llvm/ADT/SmallString.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/ADT/Triple.h"
  16. #include "llvm/ADT/Twine.h"
  17. #include "llvm/BinaryFormat/COFF.h"
  18. #include "llvm/MC/MCAsmBackend.h"
  19. #include "llvm/MC/MCAssembler.h"
  20. #include "llvm/MC/MCCodeEmitter.h"
  21. #include "llvm/MC/MCContext.h"
  22. #include "llvm/MC/MCExpr.h"
  23. #include "llvm/MC/MCFixup.h"
  24. #include "llvm/MC/MCFragment.h"
  25. #include "llvm/MC/MCObjectFileInfo.h"
  26. #include "llvm/MC/MCObjectStreamer.h"
  27. #include "llvm/MC/MCObjectWriter.h"
  28. #include "llvm/MC/MCSection.h"
  29. #include "llvm/MC/MCSymbolCOFF.h"
  30. #include "llvm/Support/Casting.h"
  31. #include "llvm/Support/ErrorHandling.h"
  32. #include "llvm/Support/MathExtras.h"
  33. #include "llvm/Support/SMLoc.h"
  34. #include "llvm/Support/raw_ostream.h"
  35. #include <algorithm>
  36. #include <cstdint>
  37. using namespace llvm;
  38. #define DEBUG_TYPE "WinCOFFStreamer"
  39. MCWinCOFFStreamer::MCWinCOFFStreamer(MCContext &Context,
  40. std::unique_ptr<MCAsmBackend> MAB,
  41. std::unique_ptr<MCCodeEmitter> CE,
  42. std::unique_ptr<MCObjectWriter> OW)
  43. : MCObjectStreamer(Context, std::move(MAB), std::move(OW), std::move(CE)),
  44. CurSymbol(nullptr) {}
  45. void MCWinCOFFStreamer::emitInstToData(const MCInst &Inst,
  46. const MCSubtargetInfo &STI) {
  47. MCDataFragment *DF = getOrCreateDataFragment();
  48. SmallVector<MCFixup, 4> Fixups;
  49. SmallString<256> Code;
  50. raw_svector_ostream VecOS(Code);
  51. getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
  52. // Add the fixups and data.
  53. for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
  54. Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
  55. DF->getFixups().push_back(Fixups[i]);
  56. }
  57. DF->setHasInstructions(STI);
  58. DF->getContents().append(Code.begin(), Code.end());
  59. }
  60. void MCWinCOFFStreamer::initSections(bool NoExecStack,
  61. const MCSubtargetInfo &STI) {
  62. // FIXME: this is identical to the ELF one.
  63. // This emulates the same behavior of GNU as. This makes it easier
  64. // to compare the output as the major sections are in the same order.
  65. switchSection(getContext().getObjectFileInfo()->getTextSection());
  66. emitCodeAlignment(Align(4), &STI);
  67. switchSection(getContext().getObjectFileInfo()->getDataSection());
  68. emitCodeAlignment(Align(4), &STI);
  69. switchSection(getContext().getObjectFileInfo()->getBSSSection());
  70. emitCodeAlignment(Align(4), &STI);
  71. switchSection(getContext().getObjectFileInfo()->getTextSection());
  72. }
  73. void MCWinCOFFStreamer::emitLabel(MCSymbol *S, SMLoc Loc) {
  74. auto *Symbol = cast<MCSymbolCOFF>(S);
  75. MCObjectStreamer::emitLabel(Symbol, Loc);
  76. }
  77. void MCWinCOFFStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
  78. // Let the target do whatever target specific stuff it needs to do.
  79. getAssembler().getBackend().handleAssemblerFlag(Flag);
  80. switch (Flag) {
  81. // None of these require COFF specific handling.
  82. case MCAF_SyntaxUnified:
  83. case MCAF_Code16:
  84. case MCAF_Code32:
  85. case MCAF_Code64:
  86. break;
  87. case MCAF_SubsectionsViaSymbols:
  88. llvm_unreachable("COFF doesn't support .subsections_via_symbols");
  89. }
  90. }
  91. void MCWinCOFFStreamer::emitThumbFunc(MCSymbol *Func) {
  92. llvm_unreachable("not implemented");
  93. }
  94. bool MCWinCOFFStreamer::emitSymbolAttribute(MCSymbol *S,
  95. MCSymbolAttr Attribute) {
  96. auto *Symbol = cast<MCSymbolCOFF>(S);
  97. getAssembler().registerSymbol(*Symbol);
  98. switch (Attribute) {
  99. default: return false;
  100. case MCSA_WeakReference:
  101. case MCSA_Weak:
  102. Symbol->setIsWeakExternal();
  103. Symbol->setExternal(true);
  104. break;
  105. case MCSA_Global:
  106. Symbol->setExternal(true);
  107. break;
  108. case MCSA_AltEntry:
  109. llvm_unreachable("COFF doesn't support the .alt_entry attribute");
  110. }
  111. return true;
  112. }
  113. void MCWinCOFFStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
  114. llvm_unreachable("not implemented");
  115. }
  116. void MCWinCOFFStreamer::beginCOFFSymbolDef(MCSymbol const *S) {
  117. auto *Symbol = cast<MCSymbolCOFF>(S);
  118. if (CurSymbol)
  119. Error("starting a new symbol definition without completing the "
  120. "previous one");
  121. CurSymbol = Symbol;
  122. }
  123. void MCWinCOFFStreamer::emitCOFFSymbolStorageClass(int StorageClass) {
  124. if (!CurSymbol) {
  125. Error("storage class specified outside of symbol definition");
  126. return;
  127. }
  128. if (StorageClass & ~COFF::SSC_Invalid) {
  129. Error("storage class value '" + Twine(StorageClass) +
  130. "' out of range");
  131. return;
  132. }
  133. getAssembler().registerSymbol(*CurSymbol);
  134. cast<MCSymbolCOFF>(CurSymbol)->setClass((uint16_t)StorageClass);
  135. }
  136. void MCWinCOFFStreamer::emitCOFFSymbolType(int Type) {
  137. if (!CurSymbol) {
  138. Error("symbol type specified outside of a symbol definition");
  139. return;
  140. }
  141. if (Type & ~0xffff) {
  142. Error("type value '" + Twine(Type) + "' out of range");
  143. return;
  144. }
  145. getAssembler().registerSymbol(*CurSymbol);
  146. cast<MCSymbolCOFF>(CurSymbol)->setType((uint16_t)Type);
  147. }
  148. void MCWinCOFFStreamer::endCOFFSymbolDef() {
  149. if (!CurSymbol)
  150. Error("ending symbol definition without starting one");
  151. CurSymbol = nullptr;
  152. }
  153. void MCWinCOFFStreamer::emitCOFFSafeSEH(MCSymbol const *Symbol) {
  154. // SafeSEH is a feature specific to 32-bit x86. It does not exist (and is
  155. // unnecessary) on all platforms which use table-based exception dispatch.
  156. if (getContext().getTargetTriple().getArch() != Triple::x86)
  157. return;
  158. const MCSymbolCOFF *CSymbol = cast<MCSymbolCOFF>(Symbol);
  159. if (CSymbol->isSafeSEH())
  160. return;
  161. MCSection *SXData = getContext().getObjectFileInfo()->getSXDataSection();
  162. getAssembler().registerSection(*SXData);
  163. SXData->ensureMinAlignment(Align(4));
  164. new MCSymbolIdFragment(Symbol, SXData);
  165. getAssembler().registerSymbol(*Symbol);
  166. CSymbol->setIsSafeSEH();
  167. // The Microsoft linker requires that the symbol type of a handler be
  168. // function. Go ahead and oblige it here.
  169. CSymbol->setType(COFF::IMAGE_SYM_DTYPE_FUNCTION
  170. << COFF::SCT_COMPLEX_TYPE_SHIFT);
  171. }
  172. void MCWinCOFFStreamer::emitCOFFSymbolIndex(MCSymbol const *Symbol) {
  173. MCSection *Sec = getCurrentSectionOnly();
  174. getAssembler().registerSection(*Sec);
  175. Sec->ensureMinAlignment(Align(4));
  176. new MCSymbolIdFragment(Symbol, getCurrentSectionOnly());
  177. getAssembler().registerSymbol(*Symbol);
  178. }
  179. void MCWinCOFFStreamer::emitCOFFSectionIndex(const MCSymbol *Symbol) {
  180. visitUsedSymbol(*Symbol);
  181. MCDataFragment *DF = getOrCreateDataFragment();
  182. const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext());
  183. MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_2);
  184. DF->getFixups().push_back(Fixup);
  185. DF->getContents().resize(DF->getContents().size() + 2, 0);
  186. }
  187. void MCWinCOFFStreamer::emitCOFFSecRel32(const MCSymbol *Symbol,
  188. uint64_t Offset) {
  189. visitUsedSymbol(*Symbol);
  190. MCDataFragment *DF = getOrCreateDataFragment();
  191. // Create Symbol A for the relocation relative reference.
  192. const MCExpr *MCE = MCSymbolRefExpr::create(Symbol, getContext());
  193. // Add the constant offset, if given.
  194. if (Offset)
  195. MCE = MCBinaryExpr::createAdd(
  196. MCE, MCConstantExpr::create(Offset, getContext()), getContext());
  197. // Build the secrel32 relocation.
  198. MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_SecRel_4);
  199. // Record the relocation.
  200. DF->getFixups().push_back(Fixup);
  201. // Emit 4 bytes (zeros) to the object file.
  202. DF->getContents().resize(DF->getContents().size() + 4, 0);
  203. }
  204. void MCWinCOFFStreamer::emitCOFFImgRel32(const MCSymbol *Symbol,
  205. int64_t Offset) {
  206. visitUsedSymbol(*Symbol);
  207. MCDataFragment *DF = getOrCreateDataFragment();
  208. // Create Symbol A for the relocation relative reference.
  209. const MCExpr *MCE = MCSymbolRefExpr::create(
  210. Symbol, MCSymbolRefExpr::VK_COFF_IMGREL32, getContext());
  211. // Add the constant offset, if given.
  212. if (Offset)
  213. MCE = MCBinaryExpr::createAdd(
  214. MCE, MCConstantExpr::create(Offset, getContext()), getContext());
  215. // Build the imgrel relocation.
  216. MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_Data_4);
  217. // Record the relocation.
  218. DF->getFixups().push_back(Fixup);
  219. // Emit 4 bytes (zeros) to the object file.
  220. DF->getContents().resize(DF->getContents().size() + 4, 0);
  221. }
  222. void MCWinCOFFStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size,
  223. Align ByteAlignment) {
  224. auto *Symbol = cast<MCSymbolCOFF>(S);
  225. const Triple &T = getContext().getTargetTriple();
  226. if (T.isWindowsMSVCEnvironment()) {
  227. if (ByteAlignment > 32)
  228. report_fatal_error("alignment is limited to 32-bytes");
  229. // Round size up to alignment so that we will honor the alignment request.
  230. Size = std::max(Size, ByteAlignment.value());
  231. }
  232. getAssembler().registerSymbol(*Symbol);
  233. Symbol->setExternal(true);
  234. Symbol->setCommon(Size, ByteAlignment);
  235. if (!T.isWindowsMSVCEnvironment() && ByteAlignment > 1) {
  236. SmallString<128> Directive;
  237. raw_svector_ostream OS(Directive);
  238. const MCObjectFileInfo *MFI = getContext().getObjectFileInfo();
  239. OS << " -aligncomm:\"" << Symbol->getName() << "\","
  240. << Log2_32_Ceil(ByteAlignment.value());
  241. pushSection();
  242. switchSection(MFI->getDrectveSection());
  243. emitBytes(Directive);
  244. popSection();
  245. }
  246. }
  247. void MCWinCOFFStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
  248. Align ByteAlignment) {
  249. auto *Symbol = cast<MCSymbolCOFF>(S);
  250. MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
  251. pushSection();
  252. switchSection(Section);
  253. emitValueToAlignment(ByteAlignment, 0, 1, 0);
  254. emitLabel(Symbol);
  255. Symbol->setExternal(false);
  256. emitZeros(Size);
  257. popSection();
  258. }
  259. void MCWinCOFFStreamer::emitWeakReference(MCSymbol *AliasS,
  260. const MCSymbol *Symbol) {
  261. auto *Alias = cast<MCSymbolCOFF>(AliasS);
  262. emitSymbolAttribute(Alias, MCSA_Weak);
  263. getAssembler().registerSymbol(*Symbol);
  264. Alias->setVariableValue(MCSymbolRefExpr::create(
  265. Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext()));
  266. }
  267. void MCWinCOFFStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
  268. uint64_t Size, Align ByteAlignment,
  269. SMLoc Loc) {
  270. llvm_unreachable("not implemented");
  271. }
  272. void MCWinCOFFStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
  273. uint64_t Size, Align ByteAlignment) {
  274. llvm_unreachable("not implemented");
  275. }
  276. // TODO: Implement this if you want to emit .comment section in COFF obj files.
  277. void MCWinCOFFStreamer::emitIdent(StringRef IdentString) {
  278. llvm_unreachable("not implemented");
  279. }
  280. void MCWinCOFFStreamer::emitWinEHHandlerData(SMLoc Loc) {
  281. llvm_unreachable("not implemented");
  282. }
  283. void MCWinCOFFStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From,
  284. const MCSymbolRefExpr *To,
  285. uint64_t Count) {
  286. // Ignore temporary symbols for now.
  287. if (!From->getSymbol().isTemporary() && !To->getSymbol().isTemporary())
  288. getAssembler().CGProfile.push_back({From, To, Count});
  289. }
  290. void MCWinCOFFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) {
  291. const MCSymbol *S = &SRE->getSymbol();
  292. bool Created;
  293. getAssembler().registerSymbol(*S, &Created);
  294. if (Created)
  295. cast<MCSymbolCOFF>(S)->setExternal(true);
  296. }
  297. void MCWinCOFFStreamer::finalizeCGProfile() {
  298. for (MCAssembler::CGProfileEntry &E : getAssembler().CGProfile) {
  299. finalizeCGProfileEntry(E.From);
  300. finalizeCGProfileEntry(E.To);
  301. }
  302. }
  303. void MCWinCOFFStreamer::finishImpl() {
  304. finalizeCGProfile();
  305. MCObjectStreamer::finishImpl();
  306. }
  307. void MCWinCOFFStreamer::Error(const Twine &Msg) const {
  308. getContext().reportError(SMLoc(), Msg);
  309. }