WinCOFFObjectWriter.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. //===-- llvm/MC/WinCOFFObjectWriter.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 Win32 COFF object file writer.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/MC/MCWinCOFFObjectWriter.h"
  14. #include "llvm/ADT/DenseMap.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/StringMap.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/ADT/Twine.h"
  19. #include "llvm/MC/MCAsmLayout.h"
  20. #include "llvm/MC/MCAssembler.h"
  21. #include "llvm/MC/MCContext.h"
  22. #include "llvm/MC/MCExpr.h"
  23. #include "llvm/MC/MCObjectFileInfo.h"
  24. #include "llvm/MC/MCObjectWriter.h"
  25. #include "llvm/MC/MCSection.h"
  26. #include "llvm/MC/MCSectionCOFF.h"
  27. #include "llvm/MC/MCSymbolCOFF.h"
  28. #include "llvm/MC/MCValue.h"
  29. #include "llvm/MC/StringTableBuilder.h"
  30. #include "llvm/Support/COFF.h"
  31. #include "llvm/Support/Debug.h"
  32. #include "llvm/Support/Endian.h"
  33. #include "llvm/Support/ErrorHandling.h"
  34. #include "llvm/Support/TimeValue.h"
  35. #include <cstdio>
  36. using namespace llvm;
  37. #define DEBUG_TYPE "WinCOFFObjectWriter"
  38. namespace {
  39. typedef SmallString<COFF::NameSize> name;
  40. enum AuxiliaryType {
  41. ATFunctionDefinition,
  42. ATbfAndefSymbol,
  43. ATWeakExternal,
  44. ATFile,
  45. ATSectionDefinition
  46. };
  47. struct AuxSymbol {
  48. AuxiliaryType AuxType;
  49. COFF::Auxiliary Aux;
  50. };
  51. class COFFSymbol;
  52. class COFFSection;
  53. class COFFSymbol {
  54. public:
  55. COFF::symbol Data;
  56. typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
  57. name Name;
  58. int Index;
  59. AuxiliarySymbols Aux;
  60. COFFSymbol *Other;
  61. COFFSection *Section;
  62. int Relocations;
  63. const MCSymbol *MC;
  64. COFFSymbol(StringRef name);
  65. void set_name_offset(uint32_t Offset);
  66. bool should_keep() const;
  67. int64_t getIndex() const { return Index; }
  68. void setIndex(int Value) {
  69. Index = Value;
  70. if (MC)
  71. MC->setIndex(static_cast<uint32_t>(Value));
  72. }
  73. };
  74. // This class contains staging data for a COFF relocation entry.
  75. struct COFFRelocation {
  76. COFF::relocation Data;
  77. COFFSymbol *Symb;
  78. COFFRelocation() : Symb(nullptr) {}
  79. static size_t size() { return COFF::RelocationSize; }
  80. };
  81. typedef std::vector<COFFRelocation> relocations;
  82. class COFFSection {
  83. public:
  84. COFF::section Header;
  85. std::string Name;
  86. int Number;
  87. MCSectionCOFF const *MCSection;
  88. COFFSymbol *Symbol;
  89. relocations Relocations;
  90. COFFSection(StringRef name);
  91. static size_t size();
  92. };
  93. class WinCOFFObjectWriter : public MCObjectWriter {
  94. public:
  95. typedef std::vector<std::unique_ptr<COFFSymbol>> symbols;
  96. typedef std::vector<std::unique_ptr<COFFSection>> sections;
  97. typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
  98. typedef DenseMap<MCSection const *, COFFSection *> section_map;
  99. std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
  100. // Root level file contents.
  101. COFF::header Header;
  102. sections Sections;
  103. symbols Symbols;
  104. StringTableBuilder Strings;
  105. // Maps used during object file creation.
  106. section_map SectionMap;
  107. symbol_map SymbolMap;
  108. bool UseBigObj;
  109. WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
  110. void reset() override {
  111. memset(&Header, 0, sizeof(Header));
  112. Header.Machine = TargetObjectWriter->getMachine();
  113. Sections.clear();
  114. Symbols.clear();
  115. Strings.clear();
  116. SectionMap.clear();
  117. SymbolMap.clear();
  118. MCObjectWriter::reset();
  119. }
  120. COFFSymbol *createSymbol(StringRef Name);
  121. COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
  122. COFFSection *createSection(StringRef Name);
  123. template <typename object_t, typename list_t>
  124. object_t *createCOFFEntity(StringRef Name, list_t &List);
  125. void defineSection(MCSectionCOFF const &Sec);
  126. void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
  127. const MCAsmLayout &Layout);
  128. void SetSymbolName(COFFSymbol &S);
  129. void SetSectionName(COFFSection &S);
  130. bool ExportSymbol(const MCSymbol &Symbol, MCAssembler &Asm);
  131. bool IsPhysicalSection(COFFSection *S);
  132. // Entity writing methods.
  133. void WriteFileHeader(const COFF::header &Header);
  134. void WriteSymbol(const COFFSymbol &S);
  135. void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
  136. void writeSectionHeader(const COFF::section &S);
  137. void WriteRelocation(const COFF::relocation &R);
  138. // MCObjectWriter interface implementation.
  139. void executePostLayoutBinding(MCAssembler &Asm,
  140. const MCAsmLayout &Layout) override;
  141. bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
  142. const MCSymbol &SymA,
  143. const MCFragment &FB, bool InSet,
  144. bool IsPCRel) const override;
  145. bool isWeak(const MCSymbol &Sym) const override;
  146. void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
  147. const MCFragment *Fragment, const MCFixup &Fixup,
  148. MCValue Target, bool &IsPCRel,
  149. uint64_t &FixedValue) override;
  150. void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
  151. };
  152. }
  153. static inline void write_uint32_le(void *Data, uint32_t Value) {
  154. support::endian::write<uint32_t, support::little, support::unaligned>(Data,
  155. Value);
  156. }
  157. //------------------------------------------------------------------------------
  158. // Symbol class implementation
  159. COFFSymbol::COFFSymbol(StringRef name)
  160. : Name(name.begin(), name.end()), Other(nullptr), Section(nullptr),
  161. Relocations(0), MC(nullptr) {
  162. memset(&Data, 0, sizeof(Data));
  163. }
  164. // In the case that the name does not fit within 8 bytes, the offset
  165. // into the string table is stored in the last 4 bytes instead, leaving
  166. // the first 4 bytes as 0.
  167. void COFFSymbol::set_name_offset(uint32_t Offset) {
  168. write_uint32_le(Data.Name + 0, 0);
  169. write_uint32_le(Data.Name + 4, Offset);
  170. }
  171. /// logic to decide if the symbol should be reported in the symbol table
  172. bool COFFSymbol::should_keep() const {
  173. // no section means its external, keep it
  174. if (!Section)
  175. return true;
  176. // if it has relocations pointing at it, keep it
  177. if (Relocations > 0) {
  178. assert(Section->Number != -1 && "Sections with relocations must be real!");
  179. return true;
  180. }
  181. // if this is a safeseh handler, keep it
  182. if (MC && (cast<MCSymbolCOFF>(MC)->isSafeSEH()))
  183. return true;
  184. // if the section its in is being droped, drop it
  185. if (Section->Number == -1)
  186. return false;
  187. // if it is the section symbol, keep it
  188. if (Section->Symbol == this)
  189. return true;
  190. // if its temporary, drop it
  191. if (MC && MC->isTemporary())
  192. return false;
  193. // otherwise, keep it
  194. return true;
  195. }
  196. //------------------------------------------------------------------------------
  197. // Section class implementation
  198. COFFSection::COFFSection(StringRef name)
  199. : Name(name), MCSection(nullptr), Symbol(nullptr) {
  200. memset(&Header, 0, sizeof(Header));
  201. }
  202. size_t COFFSection::size() { return COFF::SectionSize; }
  203. //------------------------------------------------------------------------------
  204. // WinCOFFObjectWriter class implementation
  205. WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
  206. raw_pwrite_stream &OS)
  207. : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
  208. memset(&Header, 0, sizeof(Header));
  209. Header.Machine = TargetObjectWriter->getMachine();
  210. }
  211. COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
  212. return createCOFFEntity<COFFSymbol>(Name, Symbols);
  213. }
  214. COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
  215. symbol_map::iterator i = SymbolMap.find(Symbol);
  216. if (i != SymbolMap.end())
  217. return i->second;
  218. COFFSymbol *RetSymbol =
  219. createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
  220. SymbolMap[Symbol] = RetSymbol;
  221. return RetSymbol;
  222. }
  223. COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
  224. return createCOFFEntity<COFFSection>(Name, Sections);
  225. }
  226. /// A template used to lookup or create a symbol/section, and initialize it if
  227. /// needed.
  228. template <typename object_t, typename list_t>
  229. object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name, list_t &List) {
  230. List.push_back(make_unique<object_t>(Name));
  231. return List.back().get();
  232. }
  233. /// This function takes a section data object from the assembler
  234. /// and creates the associated COFF section staging object.
  235. void WinCOFFObjectWriter::defineSection(MCSectionCOFF const &Sec) {
  236. COFFSection *coff_section = createSection(Sec.getSectionName());
  237. COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
  238. if (Sec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
  239. if (const MCSymbol *S = Sec.getCOMDATSymbol()) {
  240. COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
  241. if (COMDATSymbol->Section)
  242. report_fatal_error("two sections have the same comdat");
  243. COMDATSymbol->Section = coff_section;
  244. }
  245. }
  246. coff_section->Symbol = coff_symbol;
  247. coff_symbol->Section = coff_section;
  248. coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
  249. // In this case the auxiliary symbol is a Section Definition.
  250. coff_symbol->Aux.resize(1);
  251. memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
  252. coff_symbol->Aux[0].AuxType = ATSectionDefinition;
  253. coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
  254. coff_section->Header.Characteristics = Sec.getCharacteristics();
  255. uint32_t &Characteristics = coff_section->Header.Characteristics;
  256. switch (Sec.getAlignment()) {
  257. case 1:
  258. Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES;
  259. break;
  260. case 2:
  261. Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES;
  262. break;
  263. case 4:
  264. Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES;
  265. break;
  266. case 8:
  267. Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES;
  268. break;
  269. case 16:
  270. Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES;
  271. break;
  272. case 32:
  273. Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES;
  274. break;
  275. case 64:
  276. Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES;
  277. break;
  278. case 128:
  279. Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES;
  280. break;
  281. case 256:
  282. Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES;
  283. break;
  284. case 512:
  285. Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES;
  286. break;
  287. case 1024:
  288. Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES;
  289. break;
  290. case 2048:
  291. Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES;
  292. break;
  293. case 4096:
  294. Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES;
  295. break;
  296. case 8192:
  297. Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES;
  298. break;
  299. default:
  300. llvm_unreachable("unsupported section alignment");
  301. }
  302. // Bind internal COFF section to MC section.
  303. coff_section->MCSection = &Sec;
  304. SectionMap[&Sec] = coff_section;
  305. }
  306. static uint64_t getSymbolValue(const MCSymbol &Symbol,
  307. const MCAsmLayout &Layout) {
  308. if (Symbol.isCommon() && Symbol.isExternal())
  309. return Symbol.getCommonSize();
  310. uint64_t Res;
  311. if (!Layout.getSymbolOffset(Symbol, Res))
  312. return 0;
  313. return Res;
  314. }
  315. /// This function takes a symbol data object from the assembler
  316. /// and creates the associated COFF symbol staging object.
  317. void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol,
  318. MCAssembler &Assembler,
  319. const MCAsmLayout &Layout) {
  320. COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
  321. SymbolMap[&Symbol] = coff_symbol;
  322. if (cast<MCSymbolCOFF>(Symbol).isWeakExternal()) {
  323. coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
  324. if (Symbol.isVariable()) {
  325. const MCSymbolRefExpr *SymRef =
  326. dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
  327. if (!SymRef)
  328. report_fatal_error("Weak externals may only alias symbols");
  329. coff_symbol->Other = GetOrCreateCOFFSymbol(&SymRef->getSymbol());
  330. } else {
  331. std::string WeakName = (".weak." + Symbol.getName() + ".default").str();
  332. COFFSymbol *WeakDefault = createSymbol(WeakName);
  333. WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
  334. WeakDefault->Data.StorageClass = COFF::IMAGE_SYM_CLASS_EXTERNAL;
  335. WeakDefault->Data.Type = 0;
  336. WeakDefault->Data.Value = 0;
  337. coff_symbol->Other = WeakDefault;
  338. }
  339. // Setup the Weak External auxiliary symbol.
  340. coff_symbol->Aux.resize(1);
  341. memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
  342. coff_symbol->Aux[0].AuxType = ATWeakExternal;
  343. coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
  344. coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
  345. COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
  346. coff_symbol->MC = &Symbol;
  347. } else {
  348. const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
  349. coff_symbol->Data.Value = getSymbolValue(Symbol, Layout);
  350. const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(Symbol);
  351. coff_symbol->Data.Type = SymbolCOFF.getType();
  352. coff_symbol->Data.StorageClass = SymbolCOFF.getClass();
  353. // If no storage class was specified in the streamer, define it here.
  354. if (coff_symbol->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
  355. bool IsExternal = Symbol.isExternal() ||
  356. (!Symbol.getFragment() && !Symbol.isVariable());
  357. coff_symbol->Data.StorageClass = IsExternal
  358. ? COFF::IMAGE_SYM_CLASS_EXTERNAL
  359. : COFF::IMAGE_SYM_CLASS_STATIC;
  360. }
  361. if (!Base) {
  362. coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
  363. } else {
  364. if (Base->getFragment()) {
  365. COFFSection *Sec = SectionMap[Base->getFragment()->getParent()];
  366. if (coff_symbol->Section && coff_symbol->Section != Sec)
  367. report_fatal_error("conflicting sections for symbol");
  368. coff_symbol->Section = Sec;
  369. }
  370. }
  371. coff_symbol->MC = &Symbol;
  372. }
  373. }
  374. // Maximum offsets for different string table entry encodings.
  375. static const unsigned Max6DecimalOffset = 999999;
  376. static const unsigned Max7DecimalOffset = 9999999;
  377. static const uint64_t MaxBase64Offset = 0xFFFFFFFFFULL; // 64^6, including 0
  378. // Encode a string table entry offset in base 64, padded to 6 chars, and
  379. // prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
  380. // Buffer must be at least 8 bytes large. No terminating null appended.
  381. static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
  382. assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
  383. "Illegal section name encoding for value");
  384. static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  385. "abcdefghijklmnopqrstuvwxyz"
  386. "0123456789+/";
  387. Buffer[0] = '/';
  388. Buffer[1] = '/';
  389. char *Ptr = Buffer + 7;
  390. for (unsigned i = 0; i < 6; ++i) {
  391. unsigned Rem = Value % 64;
  392. Value /= 64;
  393. *(Ptr--) = Alphabet[Rem];
  394. }
  395. }
  396. void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
  397. if (S.Name.size() > COFF::NameSize) {
  398. uint64_t StringTableEntry = Strings.getOffset(S.Name);
  399. if (StringTableEntry <= Max6DecimalOffset) {
  400. //std::sprintf(S.Header.Name, "/%d", unsigned(StringTableEntry)); // HLSL Change - use sprintf_s
  401. sprintf_s(S.Header.Name, _countof(S.Header.Name), "/%d", unsigned(StringTableEntry));
  402. } else if (StringTableEntry <= Max7DecimalOffset) {
  403. // With seven digits, we have to skip the terminating null. Because
  404. // sprintf always appends it, we use a larger temporary buffer.
  405. char buffer[9] = { 0 }; // HLSL Change - initialize first element with 0
  406. //std::sprintf(buffer, "/%d", unsigned(StringTableEntry)); // HLSL Change - use sprintf_s instead
  407. sprintf_s(buffer, _countof(buffer), "/%d", unsigned(StringTableEntry));
  408. std::memcpy(S.Header.Name, buffer, 8);
  409. } else if (StringTableEntry <= MaxBase64Offset) {
  410. // Starting with 10,000,000, offsets are encoded as base64.
  411. encodeBase64StringEntry(S.Header.Name, StringTableEntry);
  412. } else {
  413. report_fatal_error("COFF string table is greater than 64 GB.");
  414. }
  415. } else
  416. std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
  417. }
  418. void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
  419. if (S.Name.size() > COFF::NameSize)
  420. S.set_name_offset(Strings.getOffset(S.Name));
  421. else
  422. std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
  423. }
  424. bool WinCOFFObjectWriter::ExportSymbol(const MCSymbol &Symbol,
  425. MCAssembler &Asm) {
  426. // This doesn't seem to be right. Strings referred to from the .data section
  427. // need symbols so they can be linked to code in the .text section right?
  428. // return Asm.isSymbolLinkerVisible(Symbol);
  429. // Non-temporary labels should always be visible to the linker.
  430. if (!Symbol.isTemporary())
  431. return true;
  432. // Temporary variable symbols are invisible.
  433. if (Symbol.isVariable())
  434. return false;
  435. // Absolute temporary labels are never visible.
  436. return !Symbol.isAbsolute();
  437. }
  438. bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
  439. return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
  440. 0;
  441. }
  442. //------------------------------------------------------------------------------
  443. // entity writing methods
  444. void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
  445. if (UseBigObj) {
  446. writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
  447. writeLE16(0xFFFF);
  448. writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
  449. writeLE16(Header.Machine);
  450. writeLE32(Header.TimeDateStamp);
  451. writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
  452. writeLE32(0);
  453. writeLE32(0);
  454. writeLE32(0);
  455. writeLE32(0);
  456. writeLE32(Header.NumberOfSections);
  457. writeLE32(Header.PointerToSymbolTable);
  458. writeLE32(Header.NumberOfSymbols);
  459. } else {
  460. writeLE16(Header.Machine);
  461. writeLE16(static_cast<int16_t>(Header.NumberOfSections));
  462. writeLE32(Header.TimeDateStamp);
  463. writeLE32(Header.PointerToSymbolTable);
  464. writeLE32(Header.NumberOfSymbols);
  465. writeLE16(Header.SizeOfOptionalHeader);
  466. writeLE16(Header.Characteristics);
  467. }
  468. }
  469. void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
  470. writeBytes(StringRef(S.Data.Name, COFF::NameSize));
  471. writeLE32(S.Data.Value);
  472. if (UseBigObj)
  473. writeLE32(S.Data.SectionNumber);
  474. else
  475. writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
  476. writeLE16(S.Data.Type);
  477. write8(S.Data.StorageClass);
  478. write8(S.Data.NumberOfAuxSymbols);
  479. WriteAuxiliarySymbols(S.Aux);
  480. }
  481. void WinCOFFObjectWriter::WriteAuxiliarySymbols(
  482. const COFFSymbol::AuxiliarySymbols &S) {
  483. for (COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
  484. i != e; ++i) {
  485. switch (i->AuxType) {
  486. case ATFunctionDefinition:
  487. writeLE32(i->Aux.FunctionDefinition.TagIndex);
  488. writeLE32(i->Aux.FunctionDefinition.TotalSize);
  489. writeLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
  490. writeLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
  491. WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
  492. if (UseBigObj)
  493. WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
  494. break;
  495. case ATbfAndefSymbol:
  496. WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
  497. writeLE16(i->Aux.bfAndefSymbol.Linenumber);
  498. WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
  499. writeLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
  500. WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
  501. if (UseBigObj)
  502. WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
  503. break;
  504. case ATWeakExternal:
  505. writeLE32(i->Aux.WeakExternal.TagIndex);
  506. writeLE32(i->Aux.WeakExternal.Characteristics);
  507. WriteZeros(sizeof(i->Aux.WeakExternal.unused));
  508. if (UseBigObj)
  509. WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
  510. break;
  511. case ATFile:
  512. writeBytes(
  513. StringRef(reinterpret_cast<const char *>(&i->Aux),
  514. UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
  515. break;
  516. case ATSectionDefinition:
  517. writeLE32(i->Aux.SectionDefinition.Length);
  518. writeLE16(i->Aux.SectionDefinition.NumberOfRelocations);
  519. writeLE16(i->Aux.SectionDefinition.NumberOfLinenumbers);
  520. writeLE32(i->Aux.SectionDefinition.CheckSum);
  521. writeLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number));
  522. write8(i->Aux.SectionDefinition.Selection);
  523. WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
  524. writeLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number >> 16));
  525. if (UseBigObj)
  526. WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
  527. break;
  528. }
  529. }
  530. }
  531. void WinCOFFObjectWriter::writeSectionHeader(const COFF::section &S) {
  532. writeBytes(StringRef(S.Name, COFF::NameSize));
  533. writeLE32(S.VirtualSize);
  534. writeLE32(S.VirtualAddress);
  535. writeLE32(S.SizeOfRawData);
  536. writeLE32(S.PointerToRawData);
  537. writeLE32(S.PointerToRelocations);
  538. writeLE32(S.PointerToLineNumbers);
  539. writeLE16(S.NumberOfRelocations);
  540. writeLE16(S.NumberOfLineNumbers);
  541. writeLE32(S.Characteristics);
  542. }
  543. void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
  544. writeLE32(R.VirtualAddress);
  545. writeLE32(R.SymbolTableIndex);
  546. writeLE16(R.Type);
  547. }
  548. ////////////////////////////////////////////////////////////////////////////////
  549. // MCObjectWriter interface implementations
  550. void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
  551. const MCAsmLayout &Layout) {
  552. // "Define" each section & symbol. This creates section & symbol
  553. // entries in the staging area.
  554. for (const auto &Section : Asm)
  555. defineSection(static_cast<const MCSectionCOFF &>(Section));
  556. for (const MCSymbol &Symbol : Asm.symbols())
  557. if (ExportSymbol(Symbol, Asm))
  558. DefineSymbol(Symbol, Asm, Layout);
  559. }
  560. bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
  561. const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
  562. bool InSet, bool IsPCRel) const {
  563. // MS LINK expects to be able to replace all references to a function with a
  564. // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
  565. // away any relocations to functions.
  566. uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
  567. if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
  568. return false;
  569. return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
  570. InSet, IsPCRel);
  571. }
  572. bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
  573. if (!Sym.isExternal())
  574. return false;
  575. if (!Sym.isInSection())
  576. return false;
  577. const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
  578. if (!Sec.getCOMDATSymbol())
  579. return false;
  580. // It looks like for COFF it is invalid to replace a reference to a global
  581. // in a comdat with a reference to a local.
  582. // FIXME: Add a specification reference if available.
  583. return true;
  584. }
  585. void WinCOFFObjectWriter::recordRelocation(
  586. MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
  587. const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
  588. assert(Target.getSymA() && "Relocation must reference a symbol!");
  589. const MCSymbol &Symbol = Target.getSymA()->getSymbol();
  590. const MCSymbol &A = Symbol;
  591. if (!A.isRegistered())
  592. Asm.getContext().reportFatalError(Fixup.getLoc(),
  593. Twine("symbol '") + A.getName() +
  594. "' can not be undefined");
  595. MCSection *Section = Fragment->getParent();
  596. // Mark this symbol as requiring an entry in the symbol table.
  597. assert(SectionMap.find(Section) != SectionMap.end() &&
  598. "Section must already have been defined in executePostLayoutBinding!");
  599. assert(SymbolMap.find(&A) != SymbolMap.end() &&
  600. "Symbol must already have been defined in executePostLayoutBinding!");
  601. COFFSection *coff_section = SectionMap[Section];
  602. COFFSymbol *coff_symbol = SymbolMap[&A];
  603. const MCSymbolRefExpr *SymB = Target.getSymB();
  604. bool CrossSection = false;
  605. if (SymB) {
  606. const MCSymbol *B = &SymB->getSymbol();
  607. if (!B->getFragment())
  608. Asm.getContext().reportFatalError(
  609. Fixup.getLoc(),
  610. Twine("symbol '") + B->getName() +
  611. "' can not be undefined in a subtraction expression");
  612. if (!A.getFragment())
  613. Asm.getContext().reportFatalError(
  614. Fixup.getLoc(),
  615. Twine("symbol '") + Symbol.getName() +
  616. "' can not be undefined in a subtraction expression");
  617. CrossSection = &Symbol.getSection() != &B->getSection();
  618. // Offset of the symbol in the section
  619. int64_t OffsetOfB = Layout.getSymbolOffset(*B);
  620. // In the case where we have SymbA and SymB, we just need to store the delta
  621. // between the two symbols. Update FixedValue to account for the delta, and
  622. // skip recording the relocation.
  623. if (!CrossSection) {
  624. int64_t OffsetOfA = Layout.getSymbolOffset(A);
  625. FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
  626. return;
  627. }
  628. // Offset of the relocation in the section
  629. int64_t OffsetOfRelocation =
  630. Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
  631. FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
  632. } else {
  633. FixedValue = Target.getConstant();
  634. }
  635. COFFRelocation Reloc;
  636. Reloc.Data.SymbolTableIndex = 0;
  637. Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
  638. // Turn relocations for temporary symbols into section relocations.
  639. if (coff_symbol->MC->isTemporary() || CrossSection) {
  640. Reloc.Symb = coff_symbol->Section->Symbol;
  641. FixedValue += Layout.getFragmentOffset(coff_symbol->MC->getFragment()) +
  642. coff_symbol->MC->getOffset();
  643. } else
  644. Reloc.Symb = coff_symbol;
  645. ++Reloc.Symb->Relocations;
  646. Reloc.Data.VirtualAddress += Fixup.getOffset();
  647. Reloc.Data.Type = TargetObjectWriter->getRelocType(
  648. Target, Fixup, CrossSection, Asm.getBackend());
  649. // FIXME: Can anyone explain what this does other than adjust for the size
  650. // of the offset?
  651. if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
  652. Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
  653. (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
  654. Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
  655. FixedValue += 4;
  656. if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
  657. switch (Reloc.Data.Type) {
  658. case COFF::IMAGE_REL_ARM_ABSOLUTE:
  659. case COFF::IMAGE_REL_ARM_ADDR32:
  660. case COFF::IMAGE_REL_ARM_ADDR32NB:
  661. case COFF::IMAGE_REL_ARM_TOKEN:
  662. case COFF::IMAGE_REL_ARM_SECTION:
  663. case COFF::IMAGE_REL_ARM_SECREL:
  664. break;
  665. case COFF::IMAGE_REL_ARM_BRANCH11:
  666. case COFF::IMAGE_REL_ARM_BLX11:
  667. // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
  668. // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
  669. // for Windows CE).
  670. case COFF::IMAGE_REL_ARM_BRANCH24:
  671. case COFF::IMAGE_REL_ARM_BLX24:
  672. case COFF::IMAGE_REL_ARM_MOV32A:
  673. // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
  674. // only used for ARM mode code, which is documented as being unsupported
  675. // by Windows on ARM. Empirical proof indicates that masm is able to
  676. // generate the relocations however the rest of the MSVC toolchain is
  677. // unable to handle it.
  678. llvm_unreachable("unsupported relocation");
  679. break;
  680. case COFF::IMAGE_REL_ARM_MOV32T:
  681. break;
  682. case COFF::IMAGE_REL_ARM_BRANCH20T:
  683. case COFF::IMAGE_REL_ARM_BRANCH24T:
  684. case COFF::IMAGE_REL_ARM_BLX23T:
  685. // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
  686. // perform a 4 byte adjustment to the relocation. Relative branches are
  687. // offset by 4 on ARM, however, because there is no RELA relocations, all
  688. // branches are offset by 4.
  689. FixedValue = FixedValue + 4;
  690. break;
  691. }
  692. }
  693. if (TargetObjectWriter->recordRelocation(Fixup))
  694. coff_section->Relocations.push_back(Reloc);
  695. }
  696. void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
  697. const MCAsmLayout &Layout) {
  698. size_t SectionsSize = Sections.size();
  699. if (SectionsSize > static_cast<size_t>(INT32_MAX))
  700. report_fatal_error(
  701. "PE COFF object files can't have more than 2147483647 sections");
  702. // Assign symbol and section indexes and offsets.
  703. int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
  704. UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
  705. // Assign section numbers.
  706. size_t Number = 1;
  707. for (const auto &Section : Sections) {
  708. Section->Number = Number;
  709. Section->Symbol->Data.SectionNumber = Number;
  710. Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
  711. ++Number;
  712. }
  713. Header.NumberOfSections = NumberOfSections;
  714. Header.NumberOfSymbols = 0;
  715. for (const std::string &Name : Asm.getFileNames()) {
  716. // round up to calculate the number of auxiliary symbols required
  717. unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
  718. unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
  719. COFFSymbol *file = createSymbol(".file");
  720. file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
  721. file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
  722. file->Aux.resize(Count);
  723. unsigned Offset = 0;
  724. unsigned Length = Name.size();
  725. for (auto &Aux : file->Aux) {
  726. Aux.AuxType = ATFile;
  727. if (Length > SymbolSize) {
  728. memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
  729. Length = Length - SymbolSize;
  730. } else {
  731. memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
  732. memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
  733. break;
  734. }
  735. Offset += SymbolSize;
  736. }
  737. }
  738. for (auto &Symbol : Symbols) {
  739. // Update section number & offset for symbols that have them.
  740. if (Symbol->Section)
  741. Symbol->Data.SectionNumber = Symbol->Section->Number;
  742. if (Symbol->should_keep()) {
  743. Symbol->setIndex(Header.NumberOfSymbols++);
  744. // Update auxiliary symbol info.
  745. Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
  746. Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
  747. } else {
  748. Symbol->setIndex(-1);
  749. }
  750. }
  751. // Build string table.
  752. for (const auto &S : Sections)
  753. if (S->Name.size() > COFF::NameSize)
  754. Strings.add(S->Name);
  755. for (const auto &S : Symbols)
  756. if (S->should_keep() && S->Name.size() > COFF::NameSize)
  757. Strings.add(S->Name);
  758. Strings.finalize(StringTableBuilder::WinCOFF);
  759. // Set names.
  760. for (const auto &S : Sections)
  761. SetSectionName(*S);
  762. for (auto &S : Symbols)
  763. if (S->should_keep())
  764. SetSymbolName(*S);
  765. // Fixup weak external references.
  766. for (auto &Symbol : Symbols) {
  767. if (Symbol->Other) {
  768. assert(Symbol->getIndex() != -1);
  769. assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
  770. assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
  771. "Symbol's aux symbol must be a Weak External!");
  772. Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
  773. }
  774. }
  775. // Fixup associative COMDAT sections.
  776. for (auto &Section : Sections) {
  777. if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
  778. COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
  779. continue;
  780. const MCSectionCOFF &MCSec = *Section->MCSection;
  781. const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
  782. assert(COMDAT);
  783. COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
  784. assert(COMDATSymbol);
  785. COFFSection *Assoc = COMDATSymbol->Section;
  786. if (!Assoc)
  787. report_fatal_error(
  788. Twine("Missing associated COMDAT section for section ") +
  789. MCSec.getSectionName());
  790. // Skip this section if the associated section is unused.
  791. if (Assoc->Number == -1)
  792. continue;
  793. Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
  794. }
  795. // Assign file offsets to COFF object file structures.
  796. unsigned offset = 0;
  797. if (UseBigObj)
  798. offset += COFF::Header32Size;
  799. else
  800. offset += COFF::Header16Size;
  801. offset += COFF::SectionSize * Header.NumberOfSections;
  802. for (const auto &Section : Asm) {
  803. COFFSection *Sec = SectionMap[&Section];
  804. if (Sec->Number == -1)
  805. continue;
  806. Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
  807. if (IsPhysicalSection(Sec)) {
  808. // Align the section data to a four byte boundary.
  809. offset = RoundUpToAlignment(offset, 4);
  810. Sec->Header.PointerToRawData = offset;
  811. offset += Sec->Header.SizeOfRawData;
  812. }
  813. if (Sec->Relocations.size() > 0) {
  814. bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
  815. if (RelocationsOverflow) {
  816. // Signal overflow by setting NumberOfRelocations to max value. Actual
  817. // size is found in reloc #0. Microsoft tools understand this.
  818. Sec->Header.NumberOfRelocations = 0xffff;
  819. } else {
  820. Sec->Header.NumberOfRelocations = Sec->Relocations.size();
  821. }
  822. Sec->Header.PointerToRelocations = offset;
  823. if (RelocationsOverflow) {
  824. // Reloc #0 will contain actual count, so make room for it.
  825. offset += COFF::RelocationSize;
  826. }
  827. offset += COFF::RelocationSize * Sec->Relocations.size();
  828. for (auto &Relocation : Sec->Relocations) {
  829. assert(Relocation.Symb->getIndex() != -1);
  830. Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
  831. }
  832. }
  833. assert(Sec->Symbol->Aux.size() == 1 &&
  834. "Section's symbol must have one aux!");
  835. AuxSymbol &Aux = Sec->Symbol->Aux[0];
  836. assert(Aux.AuxType == ATSectionDefinition &&
  837. "Section's symbol's aux symbol must be a Section Definition!");
  838. Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
  839. Aux.Aux.SectionDefinition.NumberOfRelocations =
  840. Sec->Header.NumberOfRelocations;
  841. Aux.Aux.SectionDefinition.NumberOfLinenumbers =
  842. Sec->Header.NumberOfLineNumbers;
  843. }
  844. Header.PointerToSymbolTable = offset;
  845. // We want a deterministic output. It looks like GNU as also writes 0 in here.
  846. Header.TimeDateStamp = 0;
  847. // Write it all to disk...
  848. WriteFileHeader(Header);
  849. {
  850. sections::iterator i, ie;
  851. MCAssembler::iterator j, je;
  852. for (auto &Section : Sections) {
  853. if (Section->Number != -1) {
  854. if (Section->Relocations.size() >= 0xffff)
  855. Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
  856. writeSectionHeader(Section->Header);
  857. }
  858. }
  859. for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
  860. je = Asm.end();
  861. (i != ie) && (j != je); ++i, ++j) {
  862. if ((*i)->Number == -1)
  863. continue;
  864. if ((*i)->Header.PointerToRawData != 0) {
  865. assert(OS.tell() <= (*i)->Header.PointerToRawData &&
  866. "Section::PointerToRawData is insane!");
  867. unsigned SectionDataPadding = (*i)->Header.PointerToRawData - OS.tell();
  868. assert(SectionDataPadding < 4 &&
  869. "Should only need at most three bytes of padding!");
  870. WriteZeros(SectionDataPadding);
  871. Asm.writeSectionData(&*j, Layout);
  872. }
  873. if ((*i)->Relocations.size() > 0) {
  874. assert(OS.tell() == (*i)->Header.PointerToRelocations &&
  875. "Section::PointerToRelocations is insane!");
  876. if ((*i)->Relocations.size() >= 0xffff) {
  877. // In case of overflow, write actual relocation count as first
  878. // relocation. Including the synthetic reloc itself (+ 1).
  879. COFF::relocation r;
  880. r.VirtualAddress = (*i)->Relocations.size() + 1;
  881. r.SymbolTableIndex = 0;
  882. r.Type = 0;
  883. WriteRelocation(r);
  884. }
  885. for (const auto &Relocation : (*i)->Relocations)
  886. WriteRelocation(Relocation.Data);
  887. } else
  888. assert((*i)->Header.PointerToRelocations == 0 &&
  889. "Section::PointerToRelocations is insane!");
  890. }
  891. }
  892. assert(OS.tell() == Header.PointerToSymbolTable &&
  893. "Header::PointerToSymbolTable is insane!");
  894. for (auto &Symbol : Symbols)
  895. if (Symbol->getIndex() != -1)
  896. WriteSymbol(*Symbol);
  897. OS.write(Strings.data().data(), Strings.data().size());
  898. }
  899. MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
  900. : Machine(Machine_) {}
  901. // Pin the vtable to this file.
  902. void MCWinCOFFObjectTargetWriter::anchor() {}
  903. //------------------------------------------------------------------------------
  904. // WinCOFFObjectWriter factory function
  905. MCObjectWriter *
  906. llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
  907. raw_pwrite_stream &OS) {
  908. return new WinCOFFObjectWriter(MOTW, OS);
  909. }