blob: 4f6103c6d684b3ffab244c03c17dfddd2dbeeb55 [file] [log] [blame]
Eugene Zelenko1d435522017-02-07 23:02:00 +00001//===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
Chris Lattner2c52b792010-07-11 22:07:02 +00002//
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
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000014#include "llvm/ADT/DenseMap.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/ADT/STLExtras.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/SmallVector.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000018#include "llvm/ADT/StringRef.h"
Nico Riecka37acf72013-07-06 12:13:10 +000019#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000020#include "llvm/BinaryFormat/COFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCAsmLayout.h"
22#include "llvm/MC/MCAssembler.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCExpr.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000025#include "llvm/MC/MCFixup.h"
26#include "llvm/MC/MCFragment.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/MC/MCObjectWriter.h"
28#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCSectionCOFF.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000030#include "llvm/MC/MCSymbol.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000031#include "llvm/MC/MCSymbolCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/MC/MCValue.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000033#include "llvm/MC/MCWinCOFFObjectWriter.h"
Hans Wennborgf26bfc12014-09-29 22:43:20 +000034#include "llvm/MC/StringTableBuilder.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000035#include "llvm/Support/Casting.h"
Hans Wennborgba80b5d2014-09-28 00:22:27 +000036#include "llvm/Support/Endian.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000037#include "llvm/Support/ErrorHandling.h"
David Majnemer6ddc6362015-09-01 21:23:58 +000038#include "llvm/Support/JamCRC.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000039#include "llvm/Support/MathExtras.h"
40#include "llvm/Support/raw_ostream.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000041#include <algorithm>
Eugene Zelenko1d435522017-02-07 23:02:00 +000042#include <cassert>
43#include <cstddef>
44#include <cstdint>
45#include <cstring>
David Majnemer088ba022015-09-01 23:46:11 +000046#include <ctime>
Eugene Zelenko1d435522017-02-07 23:02:00 +000047#include <memory>
48#include <string>
49#include <vector>
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000050
Chris Lattner2c52b792010-07-11 22:07:02 +000051using namespace llvm;
Rui Ueyama86e3ef92017-02-14 23:28:19 +000052using llvm::support::endian::write32le;
Chris Lattner2c52b792010-07-11 22:07:02 +000053
Chandler Carruthf58e3762014-04-22 03:04:17 +000054#define DEBUG_TYPE "WinCOFFObjectWriter"
55
Chris Lattner2c52b792010-07-11 22:07:02 +000056namespace {
Eugene Zelenko1d435522017-02-07 23:02:00 +000057
Eugene Zelenko7975b992017-04-26 22:31:39 +000058using name = SmallString<COFF::NameSize>;
Chris Lattner2c52b792010-07-11 22:07:02 +000059
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000060enum AuxiliaryType {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000061 ATWeakExternal,
62 ATFile,
63 ATSectionDefinition
64};
Chris Lattner2c52b792010-07-11 22:07:02 +000065
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000066struct AuxSymbol {
Rafael Espindola11e9e212015-05-27 14:37:12 +000067 AuxiliaryType AuxType;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000068 COFF::Auxiliary Aux;
69};
Chris Lattner2c52b792010-07-11 22:07:02 +000070
Michael J. Spencerd6283772010-09-27 08:58:26 +000071class COFFSection;
72
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000073class COFFSymbol {
74public:
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +000075 COFF::symbol Data = {};
Chris Lattner2c52b792010-07-11 22:07:02 +000076
Eugene Zelenko7975b992017-04-26 22:31:39 +000077 using AuxiliarySymbols = SmallVector<AuxSymbol, 1>;
Chris Lattner2c52b792010-07-11 22:07:02 +000078
Rafael Espindola11e9e212015-05-27 14:37:12 +000079 name Name;
80 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000081 AuxiliarySymbols Aux;
Eugene Zelenko1d435522017-02-07 23:02:00 +000082 COFFSymbol *Other = nullptr;
83 COFFSection *Section = nullptr;
84 int Relocations = 0;
85 const MCSymbol *MC = nullptr;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000086
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +000087 COFFSymbol(StringRef Name) : Name(Name) {}
Eugene Zelenko1d435522017-02-07 23:02:00 +000088
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000089 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000090
David Majnemer4eecd302015-05-30 04:56:02 +000091 int64_t getIndex() const { return Index; }
92 void setIndex(int Value) {
93 Index = Value;
94 if (MC)
95 MC->setIndex(static_cast<uint32_t>(Value));
96 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000097};
98
99// This class contains staging data for a COFF relocation entry.
100struct COFFRelocation {
101 COFF::relocation Data;
Eugene Zelenko1d435522017-02-07 23:02:00 +0000102 COFFSymbol *Symb = nullptr;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000103
Eugene Zelenko1d435522017-02-07 23:02:00 +0000104 COFFRelocation() = default;
105
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000106 static size_t size() { return COFF::RelocationSize; }
107};
108
Eugene Zelenko7975b992017-04-26 22:31:39 +0000109using relocations = std::vector<COFFRelocation>;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000110
111class COFFSection {
112public:
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000113 COFF::section Header = {};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000114
Rafael Espindola11e9e212015-05-27 14:37:12 +0000115 std::string Name;
116 int Number;
Eugene Zelenko1d435522017-02-07 23:02:00 +0000117 MCSectionCOFF const *MCSection = nullptr;
118 COFFSymbol *Symbol = nullptr;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000119 relocations Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000120
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000121 COFFSection(StringRef Name) : Name(Name) {}
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000122};
123
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000124class WinCOFFObjectWriter : public MCObjectWriter {
125public:
Peter Collingbournef17b1492018-05-21 18:17:42 +0000126 support::endian::Writer W;
127
Eugene Zelenko7975b992017-04-26 22:31:39 +0000128 using symbols = std::vector<std::unique_ptr<COFFSymbol>>;
129 using sections = std::vector<std::unique_ptr<COFFSection>>;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000130
Eugene Zelenko7975b992017-04-26 22:31:39 +0000131 using symbol_map = DenseMap<MCSymbol const *, COFFSymbol *>;
132 using section_map = DenseMap<MCSection const *, COFFSection *>;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000133
Ahmed Charles56440fd2014-03-06 05:51:42 +0000134 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000135
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000136 // Root level file contents.
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000137 COFF::header Header = {};
Rafael Espindola11e9e212015-05-27 14:37:12 +0000138 sections Sections;
139 symbols Symbols;
Rafael Espindola21956e42015-10-23 21:48:05 +0000140 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000141
142 // Maps used during object file creation.
143 section_map SectionMap;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000144 symbol_map SymbolMap;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000145
David Majnemer4d571592014-09-15 19:42:42 +0000146 bool UseBigObj;
147
Lang Hames77dff392017-10-10 00:50:29 +0000148 WinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
149 raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000150
Yaron Kerencca43c12014-09-16 21:31:04 +0000151 void reset() override {
152 memset(&Header, 0, sizeof(Header));
153 Header.Machine = TargetObjectWriter->getMachine();
154 Sections.clear();
155 Symbols.clear();
156 Strings.clear();
157 SectionMap.clear();
158 SymbolMap.clear();
159 MCObjectWriter::reset();
160 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000161
Michael J. Spencer17990d52010-10-16 08:25:57 +0000162 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola11e9e212015-05-27 14:37:12 +0000163 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000164 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000165
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000166 void defineSection(MCSectionCOFF const &Sec);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000167
168 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000169 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000170 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000171
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000172 void SetSymbolName(COFFSymbol &S);
173 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000174
Michael J. Spencerd6283772010-09-27 08:58:26 +0000175 bool IsPhysicalSection(COFFSection *S);
176
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000177 // Entity writing methods.
178
179 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000180 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000181 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
Rui Ueyamaac20c172017-02-17 17:32:54 +0000182 void writeSectionHeaders();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000183 void WriteRelocation(const COFF::relocation &R);
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000184 uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout,
185 const MCSection &MCSec);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000186 void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
187 const COFFSection &Sec, const MCSection &MCSec);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000188
189 // MCObjectWriter interface implementation.
190
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000191 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper34a61bc2014-03-08 07:02:02 +0000192 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000193
Jim Grosbach36e60e92015-06-04 22:24:41 +0000194 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000195 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000196 const MCFragment &FB, bool InSet,
197 bool IsPCRel) const override;
198
Jim Grosbach36e60e92015-06-04 22:24:41 +0000199 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000200 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindolaceecfe5b2017-07-11 23:56:10 +0000201 MCValue Target, uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000202
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000203 void createFileSymbols(MCAssembler &Asm);
204 void assignSectionNumbers();
205 void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout);
206
Peter Collingbourne438390f2018-05-21 18:23:50 +0000207 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000208};
Eugene Zelenko1d435522017-02-07 23:02:00 +0000209
210} // end anonymous namespace
Chris Lattner2c52b792010-07-11 22:07:02 +0000211
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000212//------------------------------------------------------------------------------
213// Symbol class implementation
214
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000215// In the case that the name does not fit within 8 bytes, the offset
216// into the string table is stored in the last 4 bytes instead, leaving
217// the first 4 bytes as 0.
218void COFFSymbol::set_name_offset(uint32_t Offset) {
Rui Ueyama86e3ef92017-02-14 23:28:19 +0000219 write32le(Data.Name + 0, 0);
220 write32le(Data.Name + 4, Offset);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000221}
222
223//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000224// WinCOFFObjectWriter class implementation
225
Lang Hames77dff392017-10-10 00:50:29 +0000226WinCOFFObjectWriter::WinCOFFObjectWriter(
227 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS)
Peter Collingbourne59a6fc42018-05-21 18:28:57 +0000228 : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000229 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000230}
231
Michael J. Spencer17990d52010-10-16 08:25:57 +0000232COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000233 Symbols.push_back(make_unique<COFFSymbol>(Name));
234 return Symbols.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000235}
236
Yaron Keren56919ef2014-12-04 08:30:39 +0000237COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Rui Ueyama0fcdb482017-02-14 23:47:34 +0000238 COFFSymbol *&Ret = SymbolMap[Symbol];
239 if (!Ret)
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000240 Ret = createSymbol(Symbol->getName());
Rui Ueyama0fcdb482017-02-14 23:47:34 +0000241 return Ret;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000242}
243
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000244COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000245 Sections.emplace_back(make_unique<COFFSection>(Name));
246 return Sections.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000247}
248
Rui Ueyama24e27b42017-02-15 00:15:54 +0000249static uint32_t getAlignment(const MCSectionCOFF &Sec) {
250 switch (Sec.getAlignment()) {
251 case 1:
252 return COFF::IMAGE_SCN_ALIGN_1BYTES;
253 case 2:
254 return COFF::IMAGE_SCN_ALIGN_2BYTES;
255 case 4:
256 return COFF::IMAGE_SCN_ALIGN_4BYTES;
257 case 8:
258 return COFF::IMAGE_SCN_ALIGN_8BYTES;
259 case 16:
260 return COFF::IMAGE_SCN_ALIGN_16BYTES;
261 case 32:
262 return COFF::IMAGE_SCN_ALIGN_32BYTES;
263 case 64:
264 return COFF::IMAGE_SCN_ALIGN_64BYTES;
265 case 128:
266 return COFF::IMAGE_SCN_ALIGN_128BYTES;
267 case 256:
268 return COFF::IMAGE_SCN_ALIGN_256BYTES;
269 case 512:
270 return COFF::IMAGE_SCN_ALIGN_512BYTES;
271 case 1024:
272 return COFF::IMAGE_SCN_ALIGN_1024BYTES;
273 case 2048:
274 return COFF::IMAGE_SCN_ALIGN_2048BYTES;
275 case 4096:
276 return COFF::IMAGE_SCN_ALIGN_4096BYTES;
277 case 8192:
278 return COFF::IMAGE_SCN_ALIGN_8192BYTES;
279 }
280 llvm_unreachable("unsupported section alignment");
281}
282
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000283/// This function takes a section data object from the assembler
284/// and creates the associated COFF section staging object.
Rui Ueyama09786c42017-02-15 00:28:48 +0000285void WinCOFFObjectWriter::defineSection(const MCSectionCOFF &MCSec) {
286 COFFSection *Section = createSection(MCSec.getSectionName());
287 COFFSymbol *Symbol = createSymbol(MCSec.getSectionName());
288 Section->Symbol = Symbol;
289 Symbol->Section = Section;
290 Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Rui Ueyama24e27b42017-02-15 00:15:54 +0000291
292 // Create a COMDAT symbol if needed.
Rui Ueyama09786c42017-02-15 00:28:48 +0000293 if (MCSec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
294 if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
Rafael Espindola0766ae02014-06-06 19:26:12 +0000295 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
296 if (COMDATSymbol->Section)
297 report_fatal_error("two sections have the same comdat");
Rui Ueyama09786c42017-02-15 00:28:48 +0000298 COMDATSymbol->Section = Section;
Rafael Espindola0766ae02014-06-06 19:26:12 +0000299 }
300 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000301
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000302 // In this case the auxiliary symbol is a Section Definition.
Rui Ueyama09786c42017-02-15 00:28:48 +0000303 Symbol->Aux.resize(1);
304 Symbol->Aux[0] = {};
305 Symbol->Aux[0].AuxType = ATSectionDefinition;
306 Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000307
Rui Ueyama09786c42017-02-15 00:28:48 +0000308 // Set section alignment.
309 Section->Header.Characteristics = MCSec.getCharacteristics();
310 Section->Header.Characteristics |= getAlignment(MCSec);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000311
312 // Bind internal COFF section to MC section.
Rui Ueyama09786c42017-02-15 00:28:48 +0000313 Section->MCSection = &MCSec;
314 SectionMap[&MCSec] = Section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000315}
316
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000317static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000318 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000319 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000320 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000321
322 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000323 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000324 return 0;
325
326 return Res;
327}
328
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000329COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
330 if (!Symbol.isVariable())
331 return nullptr;
332
333 const MCSymbolRefExpr *SymRef =
334 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
335 if (!SymRef)
336 return nullptr;
337
338 const MCSymbol &Aliasee = SymRef->getSymbol();
339 if (!Aliasee.isUndefined())
340 return nullptr;
341 return GetOrCreateCOFFSymbol(&Aliasee);
342}
343
David Majnemera9bdb322014-04-08 22:33:40 +0000344/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000345/// and creates the associated COFF symbol staging object.
Rui Ueyama789c4222017-02-15 01:09:01 +0000346void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000347 MCAssembler &Assembler,
348 const MCAsmLayout &Layout) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000349 COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
350 const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000351 COFFSection *Sec = nullptr;
352 if (Base && Base->getFragment()) {
353 Sec = SectionMap[Base->getFragment()->getParent()];
Rui Ueyama789c4222017-02-15 01:09:01 +0000354 if (Sym->Section && Sym->Section != Sec)
Rafael Espindola30c080a2016-05-26 18:48:23 +0000355 report_fatal_error("conflicting sections for symbol");
356 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000357
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000358 COFFSymbol *Local = nullptr;
Rui Ueyama789c4222017-02-15 01:09:01 +0000359 if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
360 Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000361
Rui Ueyama789c4222017-02-15 01:09:01 +0000362 COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000363 if (!WeakDefault) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000364 std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000365 WeakDefault = createSymbol(WeakName);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000366 if (!Sec)
367 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
368 else
369 WeakDefault->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000370 Local = WeakDefault;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000371 }
372
Rui Ueyama789c4222017-02-15 01:09:01 +0000373 Sym->Other = WeakDefault;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000374
Michael J. Spencer17990d52010-10-16 08:25:57 +0000375 // Setup the Weak External auxiliary symbol.
Rui Ueyama789c4222017-02-15 01:09:01 +0000376 Sym->Aux.resize(1);
377 memset(&Sym->Aux[0], 0, sizeof(Sym->Aux[0]));
378 Sym->Aux[0].AuxType = ATWeakExternal;
379 Sym->Aux[0].Aux.WeakExternal.TagIndex = 0;
380 Sym->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000381 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000382 } else {
Rafael Espindola30c080a2016-05-26 18:48:23 +0000383 if (!Base)
Rui Ueyama789c4222017-02-15 01:09:01 +0000384 Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola30c080a2016-05-26 18:48:23 +0000385 else
Rui Ueyama789c4222017-02-15 01:09:01 +0000386 Sym->Section = Sec;
387 Local = Sym;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000388 }
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000389
390 if (Local) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000391 Local->Data.Value = getSymbolValue(MCSym, Layout);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000392
Rui Ueyama789c4222017-02-15 01:09:01 +0000393 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000394 Local->Data.Type = SymbolCOFF.getType();
395 Local->Data.StorageClass = SymbolCOFF.getClass();
396
397 // If no storage class was specified in the streamer, define it here.
398 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000399 bool IsExternal = MCSym.isExternal() ||
400 (!MCSym.getFragment() && !MCSym.isVariable());
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000401
402 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
403 : COFF::IMAGE_SYM_CLASS_STATIC;
404 }
405 }
406
Rui Ueyama789c4222017-02-15 01:09:01 +0000407 Sym->MC = &MCSym;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000408}
409
Nico Rieck01143f92014-02-25 09:50:40 +0000410// Maximum offsets for different string table entry encodings.
David Majnemerf088f522016-01-24 20:46:11 +0000411enum : unsigned { Max7DecimalOffset = 9999999U };
412enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
Nico Rieck01143f92014-02-25 09:50:40 +0000413
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000414// Encode a string table entry offset in base 64, padded to 6 chars, and
415// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
416// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000417static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000418 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000419 "Illegal section name encoding for value");
420
421 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
422 "abcdefghijklmnopqrstuvwxyz"
423 "0123456789+/";
424
425 Buffer[0] = '/';
426 Buffer[1] = '/';
427
Rafael Espindola11e9e212015-05-27 14:37:12 +0000428 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000429 for (unsigned i = 0; i < 6; ++i) {
430 unsigned Rem = Value % 64;
431 Value /= 64;
432 *(Ptr--) = Alphabet[Rem];
433 }
434}
435
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000436void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000437 if (S.Name.size() <= COFF::NameSize) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000438 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000439 return;
David Majnemerf088f522016-01-24 20:46:11 +0000440 }
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000441
442 uint64_t StringTableEntry = Strings.getOffset(S.Name);
443 if (StringTableEntry <= Max7DecimalOffset) {
Rui Ueyama4b58f5772017-02-15 01:48:33 +0000444 SmallVector<char, COFF::NameSize> Buffer;
445 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
446 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
447 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000448 return;
449 }
450 if (StringTableEntry <= MaxBase64Offset) {
451 // Starting with 10,000,000, offsets are encoded as base64.
452 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
453 return;
454 }
455 report_fatal_error("COFF string table is greater than 64 GB.");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000456}
457
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000458void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
459 if (S.Name.size() > COFF::NameSize)
460 S.set_name_offset(Strings.getOffset(S.Name));
461 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000462 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000463}
464
Michael J. Spencerd6283772010-09-27 08:58:26 +0000465bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000466 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
467 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000468}
469
470//------------------------------------------------------------------------------
471// entity writing methods
472
473void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000474 if (UseBigObj) {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000475 W.write<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
476 W.write<uint16_t>(0xFFFF);
477 W.write<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion);
478 W.write<uint16_t>(Header.Machine);
479 W.write<uint32_t>(Header.TimeDateStamp);
480 W.OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic));
481 W.write<uint32_t>(0);
482 W.write<uint32_t>(0);
483 W.write<uint32_t>(0);
484 W.write<uint32_t>(0);
485 W.write<uint32_t>(Header.NumberOfSections);
486 W.write<uint32_t>(Header.PointerToSymbolTable);
487 W.write<uint32_t>(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000488 } else {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000489 W.write<uint16_t>(Header.Machine);
490 W.write<uint16_t>(static_cast<int16_t>(Header.NumberOfSections));
491 W.write<uint32_t>(Header.TimeDateStamp);
492 W.write<uint32_t>(Header.PointerToSymbolTable);
493 W.write<uint32_t>(Header.NumberOfSymbols);
494 W.write<uint16_t>(Header.SizeOfOptionalHeader);
495 W.write<uint16_t>(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000496 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000497}
498
David Blaikief564ab62014-04-15 05:25:03 +0000499void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000500 W.OS.write(S.Data.Name, COFF::NameSize);
501 W.write<uint32_t>(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000502 if (UseBigObj)
Peter Collingbournef17b1492018-05-21 18:17:42 +0000503 W.write<uint32_t>(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000504 else
Peter Collingbournef17b1492018-05-21 18:17:42 +0000505 W.write<uint16_t>(static_cast<int16_t>(S.Data.SectionNumber));
506 W.write<uint16_t>(S.Data.Type);
507 W.OS << char(S.Data.StorageClass);
508 W.OS << char(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000509 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000510}
511
512void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000513 const COFFSymbol::AuxiliarySymbols &S) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000514 for (const AuxSymbol &i : S) {
515 switch (i.AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000516 case ATWeakExternal:
Peter Collingbournef17b1492018-05-21 18:17:42 +0000517 W.write<uint32_t>(i.Aux.WeakExternal.TagIndex);
518 W.write<uint32_t>(i.Aux.WeakExternal.Characteristics);
519 W.OS.write_zeros(sizeof(i.Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000520 if (UseBigObj)
Peter Collingbournef17b1492018-05-21 18:17:42 +0000521 W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000522 break;
523 case ATFile:
Peter Collingbournef17b1492018-05-21 18:17:42 +0000524 W.OS.write(reinterpret_cast<const char *>(&i.Aux),
525 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000526 break;
527 case ATSectionDefinition:
Peter Collingbournef17b1492018-05-21 18:17:42 +0000528 W.write<uint32_t>(i.Aux.SectionDefinition.Length);
529 W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfRelocations);
530 W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfLinenumbers);
531 W.write<uint32_t>(i.Aux.SectionDefinition.CheckSum);
532 W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
533 W.OS << char(i.Aux.SectionDefinition.Selection);
534 W.OS.write_zeros(sizeof(i.Aux.SectionDefinition.unused));
535 W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000536 if (UseBigObj)
Peter Collingbournef17b1492018-05-21 18:17:42 +0000537 W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000538 break;
539 }
540 }
541}
542
Rui Ueyamaac20c172017-02-17 17:32:54 +0000543// Write the section header.
544void WinCOFFObjectWriter::writeSectionHeaders() {
545 // Section numbers must be monotonically increasing in the section
546 // header, but our Sections array is not sorted by section number,
547 // so make a copy of Sections and sort it.
548 std::vector<COFFSection *> Arr;
549 for (auto &Section : Sections)
550 Arr.push_back(Section.get());
Mandeep Singh Grangdb456ef2018-04-13 19:47:01 +0000551 llvm::sort(Arr.begin(), Arr.end(),
552 [](const COFFSection *A, const COFFSection *B) {
553 return A->Number < B->Number;
554 });
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000555
Rui Ueyamaac20c172017-02-17 17:32:54 +0000556 for (auto &Section : Arr) {
557 if (Section->Number == -1)
558 continue;
559
560 COFF::section &S = Section->Header;
561 if (Section->Relocations.size() >= 0xffff)
562 S.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Peter Collingbournef17b1492018-05-21 18:17:42 +0000563 W.OS.write(S.Name, COFF::NameSize);
564 W.write<uint32_t>(S.VirtualSize);
565 W.write<uint32_t>(S.VirtualAddress);
566 W.write<uint32_t>(S.SizeOfRawData);
567 W.write<uint32_t>(S.PointerToRawData);
568 W.write<uint32_t>(S.PointerToRelocations);
569 W.write<uint32_t>(S.PointerToLineNumbers);
570 W.write<uint16_t>(S.NumberOfRelocations);
571 W.write<uint16_t>(S.NumberOfLineNumbers);
572 W.write<uint32_t>(S.Characteristics);
Rui Ueyamaac20c172017-02-17 17:32:54 +0000573 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000574}
575
576void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000577 W.write<uint32_t>(R.VirtualAddress);
578 W.write<uint32_t>(R.SymbolTableIndex);
579 W.write<uint16_t>(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000580}
581
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000582// Write MCSec's contents. What this function does is essentially
583// "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
584// because it needs to compute a CRC.
585uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler &Asm,
586 const MCAsmLayout &Layout,
587 const MCSection &MCSec) {
588 // Save the contents of the section to a temporary buffer, we need this
589 // to CRC the data before we dump it into the object file.
590 SmallVector<char, 128> Buf;
591 raw_svector_ostream VecOS(Buf);
Peter Collingbourne147db3e2018-05-21 18:11:35 +0000592 Asm.writeSectionData(VecOS, &MCSec, Layout);
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000593
594 // Write the section contents to the object file.
Peter Collingbournef17b1492018-05-21 18:17:42 +0000595 W.OS << Buf;
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000596
597 // Calculate our CRC with an initial value of '0', this is not how
598 // JamCRC is specified but it aligns with the expected output.
599 JamCRC JC(/*Init=*/0);
600 JC.update(Buf);
601 return JC.getCRC();
602}
603
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000604void WinCOFFObjectWriter::writeSection(MCAssembler &Asm,
605 const MCAsmLayout &Layout,
606 const COFFSection &Sec,
607 const MCSection &MCSec) {
608 if (Sec.Number == -1)
609 return;
610
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000611 // Write the section contents.
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000612 if (Sec.Header.PointerToRawData != 0) {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000613 assert(W.OS.tell() <= Sec.Header.PointerToRawData &&
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000614 "Section::PointerToRawData is insane!");
615
Peter Collingbournef17b1492018-05-21 18:17:42 +0000616 unsigned PaddingSize = Sec.Header.PointerToRawData - W.OS.tell();
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000617 assert(PaddingSize < 4 &&
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000618 "Should only need at most three bytes of padding!");
Peter Collingbournef17b1492018-05-21 18:17:42 +0000619 W.OS.write_zeros(PaddingSize);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000620
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000621 uint32_t CRC = writeSectionContents(Asm, Layout, MCSec);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000622
623 // Update the section definition auxiliary symbol to record the CRC.
624 COFFSection *Sec = SectionMap[&MCSec];
625 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
626 assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
627 AuxSymbol &SecDef = AuxSyms[0];
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000628 SecDef.Aux.SectionDefinition.CheckSum = CRC;
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000629 }
630
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000631 // Write relocations for this section.
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000632 if (Sec.Relocations.empty()) {
633 assert(Sec.Header.PointerToRelocations == 0 &&
634 "Section::PointerToRelocations is insane!");
635 return;
636 }
637
Peter Collingbournef17b1492018-05-21 18:17:42 +0000638 assert(W.OS.tell() == Sec.Header.PointerToRelocations &&
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000639 "Section::PointerToRelocations is insane!");
640
641 if (Sec.Relocations.size() >= 0xffff) {
642 // In case of overflow, write actual relocation count as first
643 // relocation. Including the synthetic reloc itself (+ 1).
644 COFF::relocation R;
645 R.VirtualAddress = Sec.Relocations.size() + 1;
646 R.SymbolTableIndex = 0;
647 R.Type = 0;
648 WriteRelocation(R);
649 }
650
651 for (const auto &Relocation : Sec.Relocations)
652 WriteRelocation(Relocation.Data);
653}
654
Chris Lattner2c52b792010-07-11 22:07:02 +0000655////////////////////////////////////////////////////////////////////////////////
656// MCObjectWriter interface implementations
657
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000658void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000659 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000660 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000661 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000662 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000663 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000664
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000665 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000666 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000667 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000668}
669
Jim Grosbach36e60e92015-06-04 22:24:41 +0000670bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000671 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000672 bool InSet, bool IsPCRel) const {
Reid Kleckner5a5ac652018-03-14 19:24:32 +0000673 // Don't drop relocations between functions, even if they are in the same text
674 // section. Multiple Visual C++ linker features depend on having the
675 // relocations present. The /INCREMENTAL flag will cause these relocations to
676 // point to thunks, and the /GUARD:CF flag assumes that it can use relocations
677 // to approximate the set of all address taken functions. LLD's implementation
678 // of /GUARD:CF also relies on the existance of these relocations.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000679 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
Reid Kleckner5a5ac652018-03-14 19:24:32 +0000680 if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000681 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000682 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000683 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000684}
685
Rafael Espindolaceecfe5b2017-07-11 23:56:10 +0000686void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
687 const MCAsmLayout &Layout,
688 const MCFragment *Fragment,
689 const MCFixup &Fixup, MCValue Target,
690 uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000691 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000692
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000693 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000694 if (!A.isRegistered()) {
695 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000696 Twine("symbol '") + A.getName() +
697 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000698 return;
699 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000700 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000701 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000702 Twine("assembler label '") + A.getName() +
703 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000704 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000705 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000706
Rui Ueyama62376782017-02-16 01:06:45 +0000707 MCSection *MCSec = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000708
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000709 // Mark this symbol as requiring an entry in the symbol table.
Rui Ueyama62376782017-02-16 01:06:45 +0000710 assert(SectionMap.find(MCSec) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000711 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000712
Rui Ueyama62376782017-02-16 01:06:45 +0000713 COFFSection *Sec = SectionMap[MCSec];
Rafael Espindolaed164772011-04-20 14:01:45 +0000714 const MCSymbolRefExpr *SymB = Target.getSymB();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000715
David Majnemera45a1762014-01-06 07:39:46 +0000716 if (SymB) {
717 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000718 if (!B->getFragment()) {
719 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000720 Fixup.getLoc(),
721 Twine("symbol '") + B->getName() +
722 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000723 return;
724 }
David Majnemera45a1762014-01-06 07:39:46 +0000725
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000726 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000727 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000728
David Majnemer1de30942015-02-09 06:31:31 +0000729 // Offset of the relocation in the section
730 int64_t OffsetOfRelocation =
731 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
732
Andy Ayers9e5c8512015-05-14 01:10:41 +0000733 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000734 } else {
735 FixedValue = Target.getConstant();
736 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000737
738 COFFRelocation Reloc;
739
Daniel Dunbar727be432010-07-31 21:08:54 +0000740 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000741 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000742
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000743 // Turn relocations for temporary symbols into section relocations.
Rafael Espindolad2edd132017-06-22 21:57:04 +0000744 if (A.isTemporary()) {
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000745 MCSection *TargetSection = &A.getSection();
746 assert(
747 SectionMap.find(TargetSection) != SectionMap.end() &&
748 "Section must already have been defined in executePostLayoutBinding!");
749 Reloc.Symb = SectionMap[TargetSection]->Symbol;
750 FixedValue += Layout.getSymbolOffset(A);
751 } else {
752 assert(
753 SymbolMap.find(&A) != SymbolMap.end() &&
754 "Symbol must already have been defined in executePostLayoutBinding!");
755 Reloc.Symb = SymbolMap[&A];
756 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000757
758 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000759
760 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola58173b92017-06-23 04:07:44 +0000761 Reloc.Data.Type = TargetObjectWriter->getRelocType(
762 Asm.getContext(), Target, Fixup, SymB, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000763
764 // FIXME: Can anyone explain what this does other than adjust for the size
765 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000766 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
767 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
768 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
769 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000770 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000771
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000772 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
773 switch (Reloc.Data.Type) {
774 case COFF::IMAGE_REL_ARM_ABSOLUTE:
775 case COFF::IMAGE_REL_ARM_ADDR32:
776 case COFF::IMAGE_REL_ARM_ADDR32NB:
777 case COFF::IMAGE_REL_ARM_TOKEN:
778 case COFF::IMAGE_REL_ARM_SECTION:
779 case COFF::IMAGE_REL_ARM_SECREL:
780 break;
781 case COFF::IMAGE_REL_ARM_BRANCH11:
782 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000783 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
784 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
785 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000786 case COFF::IMAGE_REL_ARM_BRANCH24:
787 case COFF::IMAGE_REL_ARM_BLX24:
788 case COFF::IMAGE_REL_ARM_MOV32A:
789 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
790 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000791 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000792 // generate the relocations however the rest of the MSVC toolchain is
793 // unable to handle it.
794 llvm_unreachable("unsupported relocation");
795 break;
796 case COFF::IMAGE_REL_ARM_MOV32T:
797 break;
798 case COFF::IMAGE_REL_ARM_BRANCH20T:
799 case COFF::IMAGE_REL_ARM_BRANCH24T:
800 case COFF::IMAGE_REL_ARM_BLX23T:
801 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
802 // perform a 4 byte adjustment to the relocation. Relative branches are
803 // offset by 4 on ARM, however, because there is no RELA relocations, all
804 // branches are offset by 4.
805 FixedValue = FixedValue + 4;
806 break;
807 }
808 }
809
Simon Pilgrimf2fbf432016-11-20 13:47:59 +0000810 // The fixed value never makes sense for section indices, ignore it.
David Majnemer408b5e62016-02-05 01:55:49 +0000811 if (Fixup.getKind() == FK_SecRel_2)
812 FixedValue = 0;
813
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000814 if (TargetObjectWriter->recordRelocation(Fixup))
Rui Ueyama62376782017-02-16 01:06:45 +0000815 Sec->Relocations.push_back(Reloc);
816}
817
818static std::time_t getTime() {
819 std::time_t Now = time(nullptr);
820 if (Now < 0 || !isUInt<32>(Now))
821 return UINT32_MAX;
822 return Now;
Chris Lattner2c52b792010-07-11 22:07:02 +0000823}
824
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000825// Create .file symbols.
826void WinCOFFObjectWriter::createFileSymbols(MCAssembler &Asm) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000827 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000828 // round up to calculate the number of auxiliary symbols required
829 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000830 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000831
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000832 COFFSymbol *File = createSymbol(".file");
833 File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
834 File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
835 File->Aux.resize(Count);
David Majnemer4d571592014-09-15 19:42:42 +0000836
837 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000838 unsigned Length = Name.size();
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000839 for (auto &Aux : File->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000840 Aux.AuxType = ATFile;
841
842 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000843 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000844 Length = Length - SymbolSize;
845 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000846 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000847 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
848 break;
849 }
850
851 Offset += SymbolSize;
852 }
853 }
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000854}
855
Rui Ueyamaac20c172017-02-17 17:32:54 +0000856static bool isAssociative(const COFFSection &Section) {
857 return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
858 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
859}
860
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000861void WinCOFFObjectWriter::assignSectionNumbers() {
862 size_t I = 1;
Rui Ueyamaac20c172017-02-17 17:32:54 +0000863 auto Assign = [&](COFFSection &Section) {
864 Section.Number = I;
865 Section.Symbol->Data.SectionNumber = I;
866 Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I;
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000867 ++I;
Rui Ueyamaac20c172017-02-17 17:32:54 +0000868 };
869
870 // Although it is not explicitly requested by the Microsoft COFF spec,
871 // we should avoid emitting forward associative section references,
872 // because MSVC link.exe as of 2017 cannot handle that.
873 for (const std::unique_ptr<COFFSection> &Section : Sections)
874 if (!isAssociative(*Section))
875 Assign(*Section);
876 for (const std::unique_ptr<COFFSection> &Section : Sections)
877 if (isAssociative(*Section))
878 Assign(*Section);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000879}
880
881// Assign file offsets to COFF object file structures.
882void WinCOFFObjectWriter::assignFileOffsets(MCAssembler &Asm,
883 const MCAsmLayout &Layout) {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000884 unsigned Offset = W.OS.tell();
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000885
886 Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
887 Offset += COFF::SectionSize * Header.NumberOfSections;
888
889 for (const auto &Section : Asm) {
890 COFFSection *Sec = SectionMap[&Section];
891
892 if (Sec->Number == -1)
893 continue;
894
895 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
896
897 if (IsPhysicalSection(Sec)) {
898 // Align the section data to a four byte boundary.
899 Offset = alignTo(Offset, 4);
900 Sec->Header.PointerToRawData = Offset;
901
902 Offset += Sec->Header.SizeOfRawData;
903 }
904
905 if (!Sec->Relocations.empty()) {
906 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
907
908 if (RelocationsOverflow) {
909 // Signal overflow by setting NumberOfRelocations to max value. Actual
910 // size is found in reloc #0. Microsoft tools understand this.
911 Sec->Header.NumberOfRelocations = 0xffff;
912 } else {
913 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
914 }
915 Sec->Header.PointerToRelocations = Offset;
916
917 if (RelocationsOverflow) {
918 // Reloc #0 will contain actual count, so make room for it.
919 Offset += COFF::RelocationSize;
920 }
921
922 Offset += COFF::RelocationSize * Sec->Relocations.size();
923
924 for (auto &Relocation : Sec->Relocations) {
925 assert(Relocation.Symb->getIndex() != -1);
926 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
927 }
928 }
929
930 assert(Sec->Symbol->Aux.size() == 1 &&
931 "Section's symbol must have one aux!");
932 AuxSymbol &Aux = Sec->Symbol->Aux[0];
933 assert(Aux.AuxType == ATSectionDefinition &&
934 "Section's symbol's aux symbol must be a Section Definition!");
935 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
936 Aux.Aux.SectionDefinition.NumberOfRelocations =
937 Sec->Header.NumberOfRelocations;
938 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
939 Sec->Header.NumberOfLineNumbers;
940 }
941
942 Header.PointerToSymbolTable = Offset;
943}
944
Peter Collingbourne438390f2018-05-21 18:23:50 +0000945uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
946 const MCAsmLayout &Layout) {
947 uint64_t StartOffset = W.OS.tell();
948
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000949 if (Sections.size() > INT32_MAX)
950 report_fatal_error(
951 "PE COFF object files can't have more than 2147483647 sections");
952
953 UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
954 Header.NumberOfSections = Sections.size();
955 Header.NumberOfSymbols = 0;
956
957 assignSectionNumbers();
958 createFileSymbols(Asm);
David Majnemer4d571592014-09-15 19:42:42 +0000959
David Majnemer9ab5ff12014-08-28 04:02:50 +0000960 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000961 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000962 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000963 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000964 Symbol->setIndex(Header.NumberOfSymbols++);
965 // Update auxiliary symbol info.
966 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
967 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000968 }
969
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000970 // Build string table.
971 for (const auto &S : Sections)
972 if (S->Name.size() > COFF::NameSize)
973 Strings.add(S->Name);
974 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000975 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000976 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000977 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000978
979 // Set names.
980 for (const auto &S : Sections)
981 SetSectionName(*S);
982 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000983 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000984
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000985 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000986 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000987 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000988 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000989 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
990 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000991 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000992 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000993 }
994 }
995
Nico Riecka37acf72013-07-06 12:13:10 +0000996 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000997 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000998 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000999 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1000 continue;
1001
Rafael Espindolaf59264f2015-05-27 14:45:54 +00001002 const MCSectionCOFF &MCSec = *Section->MCSection;
Reid Kleckner602c0da2018-08-16 21:34:41 +00001003 const MCSymbol *AssocMCSym = MCSec.getCOMDATSymbol();
1004 assert(AssocMCSym);
Nico Riecka37acf72013-07-06 12:13:10 +00001005
Reid Kleckner602c0da2018-08-16 21:34:41 +00001006 // It's an error to try to associate with an undefined symbol or a symbol
1007 // without a section.
1008 if (!AssocMCSym->isInSection()) {
1009 Asm.getContext().reportError(
1010 SMLoc(), Twine("cannot make section ") + MCSec.getSectionName() +
1011 Twine(" associative with sectionless symbol ") +
1012 AssocMCSym->getName());
1013 continue;
1014 }
1015
1016 const auto *AssocMCSec = cast<MCSectionCOFF>(&AssocMCSym->getSection());
1017 assert(SectionMap.count(AssocMCSec));
1018 COFFSection *AssocSec = SectionMap[AssocMCSec];
Nico Riecka37acf72013-07-06 12:13:10 +00001019
1020 // Skip this section if the associated section is unused.
Reid Kleckner602c0da2018-08-16 21:34:41 +00001021 if (AssocSec->Number == -1)
Nico Riecka37acf72013-07-06 12:13:10 +00001022 continue;
1023
Reid Kleckner602c0da2018-08-16 21:34:41 +00001024 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = AssocSec->Number;
Nico Riecka37acf72013-07-06 12:13:10 +00001025 }
1026
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001027 assignFileOffsets(Asm, Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001028
David Majnemer088ba022015-09-01 23:46:11 +00001029 // MS LINK expects to be able to use this timestamp to implement their
1030 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +00001031 if (Asm.isIncrementalLinkerCompatible()) {
Rui Ueyama62376782017-02-16 01:06:45 +00001032 Header.TimeDateStamp = getTime();
David Majnemer03e2cc32015-12-21 22:09:27 +00001033 } else {
Nico Weber891419a2016-01-06 19:05:19 +00001034 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +00001035 Header.TimeDateStamp = 0;
1036 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +00001037
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001038 // Write it all to disk...
1039 WriteFileHeader(Header);
Rui Ueyamaac20c172017-02-17 17:32:54 +00001040 writeSectionHeaders();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001041
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001042 // Write section contents.
1043 sections::iterator I = Sections.begin();
1044 sections::iterator IE = Sections.end();
1045 MCAssembler::iterator J = Asm.begin();
1046 MCAssembler::iterator JE = Asm.end();
1047 for (; I != IE && J != JE; ++I, ++J)
1048 writeSection(Asm, Layout, **I, *J);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001049
Peter Collingbournef17b1492018-05-21 18:17:42 +00001050 assert(W.OS.tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001051 "Header::PointerToSymbolTable is insane!");
1052
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001053 // Write a symbol table.
Yaron Keren56919ef2014-12-04 08:30:39 +00001054 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001055 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001056 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001057
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001058 // Write a string table, which completes the entire COFF file.
Peter Collingbournef17b1492018-05-21 18:17:42 +00001059 Strings.write(W.OS);
Peter Collingbourne438390f2018-05-21 18:23:50 +00001060
1061 return W.OS.tell() - StartOffset;
Chris Lattner2c52b792010-07-11 22:07:02 +00001062}
1063
Rafael Espindola11e9e212015-05-27 14:37:12 +00001064MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1065 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001066
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001067// Pin the vtable to this file.
1068void MCWinCOFFObjectTargetWriter::anchor() {}
1069
Chris Lattner2c52b792010-07-11 22:07:02 +00001070//------------------------------------------------------------------------------
1071// WinCOFFObjectWriter factory function
1072
Lang Hames60fbc7c2017-10-10 16:28:07 +00001073std::unique_ptr<MCObjectWriter> llvm::createWinCOFFObjectWriter(
Lang Hames77dff392017-10-10 00:50:29 +00001074 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) {
Lang Hames60fbc7c2017-10-10 16:28:07 +00001075 return llvm::make_unique<WinCOFFObjectWriter>(std::move(MOTW), OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001076}