blob: ec1fa0801a0f8dc425c37fefbafa2804defe4743 [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 {
61 ATFunctionDefinition,
62 ATbfAndefSymbol,
63 ATWeakExternal,
64 ATFile,
65 ATSectionDefinition
66};
Chris Lattner2c52b792010-07-11 22:07:02 +000067
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000068struct AuxSymbol {
Rafael Espindola11e9e212015-05-27 14:37:12 +000069 AuxiliaryType AuxType;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000070 COFF::Auxiliary Aux;
71};
Chris Lattner2c52b792010-07-11 22:07:02 +000072
Michael J. Spencerd6283772010-09-27 08:58:26 +000073class COFFSection;
74
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000075class COFFSymbol {
76public:
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +000077 COFF::symbol Data = {};
Chris Lattner2c52b792010-07-11 22:07:02 +000078
Eugene Zelenko7975b992017-04-26 22:31:39 +000079 using AuxiliarySymbols = SmallVector<AuxSymbol, 1>;
Chris Lattner2c52b792010-07-11 22:07:02 +000080
Rafael Espindola11e9e212015-05-27 14:37:12 +000081 name Name;
82 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000083 AuxiliarySymbols Aux;
Eugene Zelenko1d435522017-02-07 23:02:00 +000084 COFFSymbol *Other = nullptr;
85 COFFSection *Section = nullptr;
86 int Relocations = 0;
87 const MCSymbol *MC = nullptr;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000088
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +000089 COFFSymbol(StringRef Name) : Name(Name) {}
Eugene Zelenko1d435522017-02-07 23:02:00 +000090
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000091 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000092
David Majnemer4eecd302015-05-30 04:56:02 +000093 int64_t getIndex() const { return Index; }
94 void setIndex(int Value) {
95 Index = Value;
96 if (MC)
97 MC->setIndex(static_cast<uint32_t>(Value));
98 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000099};
100
101// This class contains staging data for a COFF relocation entry.
102struct COFFRelocation {
103 COFF::relocation Data;
Eugene Zelenko1d435522017-02-07 23:02:00 +0000104 COFFSymbol *Symb = nullptr;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000105
Eugene Zelenko1d435522017-02-07 23:02:00 +0000106 COFFRelocation() = default;
107
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000108 static size_t size() { return COFF::RelocationSize; }
109};
110
Eugene Zelenko7975b992017-04-26 22:31:39 +0000111using relocations = std::vector<COFFRelocation>;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000112
113class COFFSection {
114public:
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000115 COFF::section Header = {};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000116
Rafael Espindola11e9e212015-05-27 14:37:12 +0000117 std::string Name;
118 int Number;
Eugene Zelenko1d435522017-02-07 23:02:00 +0000119 MCSectionCOFF const *MCSection = nullptr;
120 COFFSymbol *Symbol = nullptr;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000121 relocations Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000122
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000123 COFFSection(StringRef Name) : Name(Name) {}
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000124};
125
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000126class WinCOFFObjectWriter : public MCObjectWriter {
127public:
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
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000148 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000149
Yaron Kerencca43c12014-09-16 21:31:04 +0000150 void reset() override {
151 memset(&Header, 0, sizeof(Header));
152 Header.Machine = TargetObjectWriter->getMachine();
153 Sections.clear();
154 Symbols.clear();
155 Strings.clear();
156 SectionMap.clear();
157 SymbolMap.clear();
158 MCObjectWriter::reset();
159 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000160
Michael J. Spencer17990d52010-10-16 08:25:57 +0000161 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola11e9e212015-05-27 14:37:12 +0000162 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000163 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000164
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000165 void defineSection(MCSectionCOFF const &Sec);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000166
167 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000168 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000169 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000170
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000171 void SetSymbolName(COFFSymbol &S);
172 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000173
Michael J. Spencerd6283772010-09-27 08:58:26 +0000174 bool IsPhysicalSection(COFFSection *S);
175
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000176 // Entity writing methods.
177
178 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000179 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000180 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
Rui Ueyamaac20c172017-02-17 17:32:54 +0000181 void writeSectionHeaders();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000182 void WriteRelocation(const COFF::relocation &R);
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000183 uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout,
184 const MCSection &MCSec);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000185 void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
186 const COFFSection &Sec, const MCSection &MCSec);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000187
188 // MCObjectWriter interface implementation.
189
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000190 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper34a61bc2014-03-08 07:02:02 +0000191 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000192
Jim Grosbach36e60e92015-06-04 22:24:41 +0000193 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000194 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000195 const MCFragment &FB, bool InSet,
196 bool IsPCRel) const override;
197
Jim Grosbach36e60e92015-06-04 22:24:41 +0000198 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000199 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000200 MCValue Target, bool &IsPCRel,
201 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
Jim Grosbach36e60e92015-06-04 22:24:41 +0000207 void 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
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000226WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000227 raw_pwrite_stream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000228 : MCObjectWriter(OS, true), TargetObjectWriter(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) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000475 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
476 writeLE16(0xFFFF);
477 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
478 writeLE16(Header.Machine);
479 writeLE32(Header.TimeDateStamp);
480 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
481 writeLE32(0);
482 writeLE32(0);
483 writeLE32(0);
484 writeLE32(0);
485 writeLE32(Header.NumberOfSections);
486 writeLE32(Header.PointerToSymbolTable);
487 writeLE32(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000488 } else {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000489 writeLE16(Header.Machine);
490 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
491 writeLE32(Header.TimeDateStamp);
492 writeLE32(Header.PointerToSymbolTable);
493 writeLE32(Header.NumberOfSymbols);
494 writeLE16(Header.SizeOfOptionalHeader);
495 writeLE16(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) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000500 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
501 writeLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000502 if (UseBigObj)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000503 writeLE32(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000504 else
Jim Grosbach36e60e92015-06-04 22:24:41 +0000505 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
506 writeLE16(S.Data.Type);
507 write8(S.Data.StorageClass);
508 write8(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 ATFunctionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000517 writeLE32(i.Aux.FunctionDefinition.TagIndex);
518 writeLE32(i.Aux.FunctionDefinition.TotalSize);
519 writeLE32(i.Aux.FunctionDefinition.PointerToLinenumber);
520 writeLE32(i.Aux.FunctionDefinition.PointerToNextFunction);
521 WriteZeros(sizeof(i.Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000522 if (UseBigObj)
523 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000524 break;
525 case ATbfAndefSymbol:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000526 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused1));
527 writeLE16(i.Aux.bfAndefSymbol.Linenumber);
528 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused2));
529 writeLE32(i.Aux.bfAndefSymbol.PointerToNextFunction);
530 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000531 if (UseBigObj)
532 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000533 break;
534 case ATWeakExternal:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000535 writeLE32(i.Aux.WeakExternal.TagIndex);
536 writeLE32(i.Aux.WeakExternal.Characteristics);
537 WriteZeros(sizeof(i.Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000538 if (UseBigObj)
539 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000540 break;
541 case ATFile:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000542 writeBytes(
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000543 StringRef(reinterpret_cast<const char *>(&i.Aux),
David Majnemer4d571592014-09-15 19:42:42 +0000544 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000545 break;
546 case ATSectionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000547 writeLE32(i.Aux.SectionDefinition.Length);
548 writeLE16(i.Aux.SectionDefinition.NumberOfRelocations);
549 writeLE16(i.Aux.SectionDefinition.NumberOfLinenumbers);
550 writeLE32(i.Aux.SectionDefinition.CheckSum);
551 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
552 write8(i.Aux.SectionDefinition.Selection);
553 WriteZeros(sizeof(i.Aux.SectionDefinition.unused));
554 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000555 if (UseBigObj)
556 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000557 break;
558 }
559 }
560}
561
Rui Ueyamaac20c172017-02-17 17:32:54 +0000562// Write the section header.
563void WinCOFFObjectWriter::writeSectionHeaders() {
564 // Section numbers must be monotonically increasing in the section
565 // header, but our Sections array is not sorted by section number,
566 // so make a copy of Sections and sort it.
567 std::vector<COFFSection *> Arr;
568 for (auto &Section : Sections)
569 Arr.push_back(Section.get());
570 std::sort(Arr.begin(), Arr.end(),
571 [](const COFFSection *A, const COFFSection *B) {
572 return A->Number < B->Number;
573 });
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000574
Rui Ueyamaac20c172017-02-17 17:32:54 +0000575 for (auto &Section : Arr) {
576 if (Section->Number == -1)
577 continue;
578
579 COFF::section &S = Section->Header;
580 if (Section->Relocations.size() >= 0xffff)
581 S.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
582 writeBytes(StringRef(S.Name, COFF::NameSize));
583 writeLE32(S.VirtualSize);
584 writeLE32(S.VirtualAddress);
585 writeLE32(S.SizeOfRawData);
586 writeLE32(S.PointerToRawData);
587 writeLE32(S.PointerToRelocations);
588 writeLE32(S.PointerToLineNumbers);
589 writeLE16(S.NumberOfRelocations);
590 writeLE16(S.NumberOfLineNumbers);
591 writeLE32(S.Characteristics);
592 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000593}
594
595void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000596 writeLE32(R.VirtualAddress);
597 writeLE32(R.SymbolTableIndex);
598 writeLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000599}
600
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000601// Write MCSec's contents. What this function does is essentially
602// "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
603// because it needs to compute a CRC.
604uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler &Asm,
605 const MCAsmLayout &Layout,
606 const MCSection &MCSec) {
607 // Save the contents of the section to a temporary buffer, we need this
608 // to CRC the data before we dump it into the object file.
609 SmallVector<char, 128> Buf;
610 raw_svector_ostream VecOS(Buf);
611 raw_pwrite_stream &OldStream = getStream();
612
613 // Redirect the output stream to our buffer and fill our buffer with
614 // the section data.
615 setStream(VecOS);
616 Asm.writeSectionData(&MCSec, Layout);
617
618 // Reset the stream back to what it was before.
619 setStream(OldStream);
620
621 // Write the section contents to the object file.
622 getStream() << Buf;
623
624 // Calculate our CRC with an initial value of '0', this is not how
625 // JamCRC is specified but it aligns with the expected output.
626 JamCRC JC(/*Init=*/0);
627 JC.update(Buf);
628 return JC.getCRC();
629}
630
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000631void WinCOFFObjectWriter::writeSection(MCAssembler &Asm,
632 const MCAsmLayout &Layout,
633 const COFFSection &Sec,
634 const MCSection &MCSec) {
635 if (Sec.Number == -1)
636 return;
637
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000638 // Write the section contents.
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000639 if (Sec.Header.PointerToRawData != 0) {
640 assert(getStream().tell() <= Sec.Header.PointerToRawData &&
641 "Section::PointerToRawData is insane!");
642
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000643 unsigned PaddingSize = Sec.Header.PointerToRawData - getStream().tell();
644 assert(PaddingSize < 4 &&
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000645 "Should only need at most three bytes of padding!");
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000646 WriteZeros(PaddingSize);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000647
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000648 uint32_t CRC = writeSectionContents(Asm, Layout, MCSec);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000649
650 // Update the section definition auxiliary symbol to record the CRC.
651 COFFSection *Sec = SectionMap[&MCSec];
652 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
653 assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
654 AuxSymbol &SecDef = AuxSyms[0];
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000655 SecDef.Aux.SectionDefinition.CheckSum = CRC;
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000656 }
657
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000658 // Write relocations for this section.
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000659 if (Sec.Relocations.empty()) {
660 assert(Sec.Header.PointerToRelocations == 0 &&
661 "Section::PointerToRelocations is insane!");
662 return;
663 }
664
665 assert(getStream().tell() == Sec.Header.PointerToRelocations &&
666 "Section::PointerToRelocations is insane!");
667
668 if (Sec.Relocations.size() >= 0xffff) {
669 // In case of overflow, write actual relocation count as first
670 // relocation. Including the synthetic reloc itself (+ 1).
671 COFF::relocation R;
672 R.VirtualAddress = Sec.Relocations.size() + 1;
673 R.SymbolTableIndex = 0;
674 R.Type = 0;
675 WriteRelocation(R);
676 }
677
678 for (const auto &Relocation : Sec.Relocations)
679 WriteRelocation(Relocation.Data);
680}
681
Chris Lattner2c52b792010-07-11 22:07:02 +0000682////////////////////////////////////////////////////////////////////////////////
683// MCObjectWriter interface implementations
684
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000685void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000686 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000687 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000688 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000689 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000690 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000691
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000692 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000693 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000694 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000695}
696
Jim Grosbach36e60e92015-06-04 22:24:41 +0000697bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000698 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000699 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000700 // MS LINK expects to be able to replace all references to a function with a
701 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
702 // away any relocations to functions.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000703 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
David Majnemer03e2cc32015-12-21 22:09:27 +0000704 if (Asm.isIncrementalLinkerCompatible() &&
705 (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000706 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000707 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000708 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000709}
710
Jim Grosbach36e60e92015-06-04 22:24:41 +0000711void WinCOFFObjectWriter::recordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000712 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
713 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000714 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000715
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000716 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000717 if (!A.isRegistered()) {
718 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000719 Twine("symbol '") + A.getName() +
720 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000721 return;
722 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000723 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000724 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000725 Twine("assembler label '") + A.getName() +
726 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000727 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000728 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000729
Rui Ueyama62376782017-02-16 01:06:45 +0000730 MCSection *MCSec = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000731
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000732 // Mark this symbol as requiring an entry in the symbol table.
Rui Ueyama62376782017-02-16 01:06:45 +0000733 assert(SectionMap.find(MCSec) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000734 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000735
Rui Ueyama62376782017-02-16 01:06:45 +0000736 COFFSection *Sec = SectionMap[MCSec];
Rafael Espindolaed164772011-04-20 14:01:45 +0000737 const MCSymbolRefExpr *SymB = Target.getSymB();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000738
David Majnemera45a1762014-01-06 07:39:46 +0000739 if (SymB) {
740 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000741 if (!B->getFragment()) {
742 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000743 Fixup.getLoc(),
744 Twine("symbol '") + B->getName() +
745 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000746 return;
747 }
David Majnemera45a1762014-01-06 07:39:46 +0000748
Oliver Stannard9be59af2015-11-17 10:00:43 +0000749 if (!A.getFragment()) {
750 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000751 Fixup.getLoc(),
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000752 Twine("symbol '") + A.getName() +
David Majnemera45a1762014-01-06 07:39:46 +0000753 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000754 return;
755 }
David Majnemera45a1762014-01-06 07:39:46 +0000756
Rafael Espindola656669b2017-06-22 20:27:33 +0000757 assert(&A.getSection() != &B->getSection() &&
758 "This doesn't need a relocation");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000759
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000760 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000761 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000762
David Majnemer1de30942015-02-09 06:31:31 +0000763 // Offset of the relocation in the section
764 int64_t OffsetOfRelocation =
765 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
766
Andy Ayers9e5c8512015-05-14 01:10:41 +0000767 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000768 } else {
769 FixedValue = Target.getConstant();
770 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000771
772 COFFRelocation Reloc;
773
Daniel Dunbar727be432010-07-31 21:08:54 +0000774 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000775 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000776
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000777 // Turn relocations for temporary symbols into section relocations.
Rafael Espindola656669b2017-06-22 20:27:33 +0000778 if (A.isTemporary() || SymB) {
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000779 MCSection *TargetSection = &A.getSection();
780 assert(
781 SectionMap.find(TargetSection) != SectionMap.end() &&
782 "Section must already have been defined in executePostLayoutBinding!");
783 Reloc.Symb = SectionMap[TargetSection]->Symbol;
784 FixedValue += Layout.getSymbolOffset(A);
785 } else {
786 assert(
787 SymbolMap.find(&A) != SymbolMap.end() &&
788 "Symbol must already have been defined in executePostLayoutBinding!");
789 Reloc.Symb = SymbolMap[&A];
790 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000791
792 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000793
794 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola656669b2017-06-22 20:27:33 +0000795 Reloc.Data.Type =
796 TargetObjectWriter->getRelocType(Target, Fixup, SymB, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000797
798 // FIXME: Can anyone explain what this does other than adjust for the size
799 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000800 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
801 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
802 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
803 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000804 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000805
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000806 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
807 switch (Reloc.Data.Type) {
808 case COFF::IMAGE_REL_ARM_ABSOLUTE:
809 case COFF::IMAGE_REL_ARM_ADDR32:
810 case COFF::IMAGE_REL_ARM_ADDR32NB:
811 case COFF::IMAGE_REL_ARM_TOKEN:
812 case COFF::IMAGE_REL_ARM_SECTION:
813 case COFF::IMAGE_REL_ARM_SECREL:
814 break;
815 case COFF::IMAGE_REL_ARM_BRANCH11:
816 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000817 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
818 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
819 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000820 case COFF::IMAGE_REL_ARM_BRANCH24:
821 case COFF::IMAGE_REL_ARM_BLX24:
822 case COFF::IMAGE_REL_ARM_MOV32A:
823 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
824 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000825 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000826 // generate the relocations however the rest of the MSVC toolchain is
827 // unable to handle it.
828 llvm_unreachable("unsupported relocation");
829 break;
830 case COFF::IMAGE_REL_ARM_MOV32T:
831 break;
832 case COFF::IMAGE_REL_ARM_BRANCH20T:
833 case COFF::IMAGE_REL_ARM_BRANCH24T:
834 case COFF::IMAGE_REL_ARM_BLX23T:
835 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
836 // perform a 4 byte adjustment to the relocation. Relative branches are
837 // offset by 4 on ARM, however, because there is no RELA relocations, all
838 // branches are offset by 4.
839 FixedValue = FixedValue + 4;
840 break;
841 }
842 }
843
Simon Pilgrimf2fbf432016-11-20 13:47:59 +0000844 // The fixed value never makes sense for section indices, ignore it.
David Majnemer408b5e62016-02-05 01:55:49 +0000845 if (Fixup.getKind() == FK_SecRel_2)
846 FixedValue = 0;
847
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000848 if (TargetObjectWriter->recordRelocation(Fixup))
Rui Ueyama62376782017-02-16 01:06:45 +0000849 Sec->Relocations.push_back(Reloc);
850}
851
852static std::time_t getTime() {
853 std::time_t Now = time(nullptr);
854 if (Now < 0 || !isUInt<32>(Now))
855 return UINT32_MAX;
856 return Now;
Chris Lattner2c52b792010-07-11 22:07:02 +0000857}
858
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000859// Create .file symbols.
860void WinCOFFObjectWriter::createFileSymbols(MCAssembler &Asm) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000861 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000862 // round up to calculate the number of auxiliary symbols required
863 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000864 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000865
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000866 COFFSymbol *File = createSymbol(".file");
867 File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
868 File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
869 File->Aux.resize(Count);
David Majnemer4d571592014-09-15 19:42:42 +0000870
871 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000872 unsigned Length = Name.size();
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000873 for (auto &Aux : File->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000874 Aux.AuxType = ATFile;
875
876 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000877 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000878 Length = Length - SymbolSize;
879 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000880 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000881 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
882 break;
883 }
884
885 Offset += SymbolSize;
886 }
887 }
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000888}
889
Rui Ueyamaac20c172017-02-17 17:32:54 +0000890static bool isAssociative(const COFFSection &Section) {
891 return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
892 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
893}
894
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000895void WinCOFFObjectWriter::assignSectionNumbers() {
896 size_t I = 1;
Rui Ueyamaac20c172017-02-17 17:32:54 +0000897 auto Assign = [&](COFFSection &Section) {
898 Section.Number = I;
899 Section.Symbol->Data.SectionNumber = I;
900 Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I;
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000901 ++I;
Rui Ueyamaac20c172017-02-17 17:32:54 +0000902 };
903
904 // Although it is not explicitly requested by the Microsoft COFF spec,
905 // we should avoid emitting forward associative section references,
906 // because MSVC link.exe as of 2017 cannot handle that.
907 for (const std::unique_ptr<COFFSection> &Section : Sections)
908 if (!isAssociative(*Section))
909 Assign(*Section);
910 for (const std::unique_ptr<COFFSection> &Section : Sections)
911 if (isAssociative(*Section))
912 Assign(*Section);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000913}
914
915// Assign file offsets to COFF object file structures.
916void WinCOFFObjectWriter::assignFileOffsets(MCAssembler &Asm,
917 const MCAsmLayout &Layout) {
918 unsigned Offset = getInitialOffset();
919
920 Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
921 Offset += COFF::SectionSize * Header.NumberOfSections;
922
923 for (const auto &Section : Asm) {
924 COFFSection *Sec = SectionMap[&Section];
925
926 if (Sec->Number == -1)
927 continue;
928
929 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
930
931 if (IsPhysicalSection(Sec)) {
932 // Align the section data to a four byte boundary.
933 Offset = alignTo(Offset, 4);
934 Sec->Header.PointerToRawData = Offset;
935
936 Offset += Sec->Header.SizeOfRawData;
937 }
938
939 if (!Sec->Relocations.empty()) {
940 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
941
942 if (RelocationsOverflow) {
943 // Signal overflow by setting NumberOfRelocations to max value. Actual
944 // size is found in reloc #0. Microsoft tools understand this.
945 Sec->Header.NumberOfRelocations = 0xffff;
946 } else {
947 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
948 }
949 Sec->Header.PointerToRelocations = Offset;
950
951 if (RelocationsOverflow) {
952 // Reloc #0 will contain actual count, so make room for it.
953 Offset += COFF::RelocationSize;
954 }
955
956 Offset += COFF::RelocationSize * Sec->Relocations.size();
957
958 for (auto &Relocation : Sec->Relocations) {
959 assert(Relocation.Symb->getIndex() != -1);
960 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
961 }
962 }
963
964 assert(Sec->Symbol->Aux.size() == 1 &&
965 "Section's symbol must have one aux!");
966 AuxSymbol &Aux = Sec->Symbol->Aux[0];
967 assert(Aux.AuxType == ATSectionDefinition &&
968 "Section's symbol's aux symbol must be a Section Definition!");
969 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
970 Aux.Aux.SectionDefinition.NumberOfRelocations =
971 Sec->Header.NumberOfRelocations;
972 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
973 Sec->Header.NumberOfLineNumbers;
974 }
975
976 Header.PointerToSymbolTable = Offset;
977}
978
979void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
980 const MCAsmLayout &Layout) {
981 if (Sections.size() > INT32_MAX)
982 report_fatal_error(
983 "PE COFF object files can't have more than 2147483647 sections");
984
985 UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
986 Header.NumberOfSections = Sections.size();
987 Header.NumberOfSymbols = 0;
988
989 assignSectionNumbers();
990 createFileSymbols(Asm);
David Majnemer4d571592014-09-15 19:42:42 +0000991
David Majnemer9ab5ff12014-08-28 04:02:50 +0000992 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000993 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000994 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000995 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000996 Symbol->setIndex(Header.NumberOfSymbols++);
997 // Update auxiliary symbol info.
998 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
999 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001000 }
1001
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001002 // Build string table.
1003 for (const auto &S : Sections)
1004 if (S->Name.size() > COFF::NameSize)
1005 Strings.add(S->Name);
1006 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +00001007 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001008 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +00001009 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001010
1011 // Set names.
1012 for (const auto &S : Sections)
1013 SetSectionName(*S);
1014 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +00001015 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001016
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001017 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +00001018 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001019 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +00001020 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001021 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
1022 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001023 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +00001024 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001025 }
1026 }
1027
Nico Riecka37acf72013-07-06 12:13:10 +00001028 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +00001029 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001030 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +00001031 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1032 continue;
1033
Rafael Espindolaf59264f2015-05-27 14:45:54 +00001034 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +00001035
Rafael Espindola0766ae02014-06-06 19:26:12 +00001036 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
1037 assert(COMDAT);
1038 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
1039 assert(COMDATSymbol);
1040 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001041 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +00001042 report_fatal_error(
1043 Twine("Missing associated COMDAT section for section ") +
1044 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +00001045
1046 // Skip this section if the associated section is unused.
1047 if (Assoc->Number == -1)
1048 continue;
1049
David Majnemer4eecd302015-05-30 04:56:02 +00001050 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +00001051 }
1052
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001053 assignFileOffsets(Asm, Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001054
David Majnemer088ba022015-09-01 23:46:11 +00001055 // MS LINK expects to be able to use this timestamp to implement their
1056 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +00001057 if (Asm.isIncrementalLinkerCompatible()) {
Rui Ueyama62376782017-02-16 01:06:45 +00001058 Header.TimeDateStamp = getTime();
David Majnemer03e2cc32015-12-21 22:09:27 +00001059 } else {
Nico Weber891419a2016-01-06 19:05:19 +00001060 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +00001061 Header.TimeDateStamp = 0;
1062 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +00001063
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001064 // Write it all to disk...
1065 WriteFileHeader(Header);
Rui Ueyamaac20c172017-02-17 17:32:54 +00001066 writeSectionHeaders();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001067
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001068 // Write section contents.
1069 sections::iterator I = Sections.begin();
1070 sections::iterator IE = Sections.end();
1071 MCAssembler::iterator J = Asm.begin();
1072 MCAssembler::iterator JE = Asm.end();
1073 for (; I != IE && J != JE; ++I, ++J)
1074 writeSection(Asm, Layout, **I, *J);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001075
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001076 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001077 "Header::PointerToSymbolTable is insane!");
1078
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001079 // Write a symbol table.
Yaron Keren56919ef2014-12-04 08:30:39 +00001080 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001081 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001082 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001083
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001084 // Write a string table, which completes the entire COFF file.
Rafael Espindola39751af2016-10-04 22:43:25 +00001085 Strings.write(getStream());
Chris Lattner2c52b792010-07-11 22:07:02 +00001086}
1087
Rafael Espindola11e9e212015-05-27 14:37:12 +00001088MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1089 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001090
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001091// Pin the vtable to this file.
1092void MCWinCOFFObjectTargetWriter::anchor() {}
1093
Chris Lattner2c52b792010-07-11 22:07:02 +00001094//------------------------------------------------------------------------------
1095// WinCOFFObjectWriter factory function
1096
Rafael Espindola37099d92015-04-09 18:08:15 +00001097MCObjectWriter *
1098llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001099 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001100 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001101}