blob: da8fe73f823bff7a222a113b4df44ad2818a5126 [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"
Eugene Zelenko1d435522017-02-07 23:02:00 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/SmallVector.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/ADT/STLExtras.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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCAsmLayout.h"
21#include "llvm/MC/MCAssembler.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000024#include "llvm/MC/MCFixup.h"
25#include "llvm/MC/MCFragment.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/MC/MCObjectWriter.h"
27#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCSectionCOFF.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000029#include "llvm/MC/MCSymbol.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000030#include "llvm/MC/MCSymbolCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/MC/MCValue.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000032#include "llvm/MC/MCWinCOFFObjectWriter.h"
Hans Wennborgf26bfc12014-09-29 22:43:20 +000033#include "llvm/MC/StringTableBuilder.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000034#include "llvm/Support/Casting.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000035#include "llvm/Support/COFF.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"
41#include <cassert>
42#include <cstddef>
43#include <cstdint>
44#include <cstring>
David Majnemer088ba022015-09-01 23:46:11 +000045#include <ctime>
Eugene Zelenko1d435522017-02-07 23:02:00 +000046#include <memory>
47#include <string>
48#include <vector>
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000049
Chris Lattner2c52b792010-07-11 22:07:02 +000050using namespace llvm;
Rui Ueyama86e3ef92017-02-14 23:28:19 +000051using llvm::support::endian::write32le;
Chris Lattner2c52b792010-07-11 22:07:02 +000052
Chandler Carruthf58e3762014-04-22 03:04:17 +000053#define DEBUG_TYPE "WinCOFFObjectWriter"
54
Chris Lattner2c52b792010-07-11 22:07:02 +000055namespace {
Eugene Zelenko1d435522017-02-07 23:02:00 +000056
Dmitri Gribenko226fea52013-01-13 16:01:15 +000057typedef SmallString<COFF::NameSize> name;
Chris Lattner2c52b792010-07-11 22:07:02 +000058
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000059enum AuxiliaryType {
60 ATFunctionDefinition,
61 ATbfAndefSymbol,
62 ATWeakExternal,
63 ATFile,
64 ATSectionDefinition
65};
Chris Lattner2c52b792010-07-11 22:07:02 +000066
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000067struct AuxSymbol {
Rafael Espindola11e9e212015-05-27 14:37:12 +000068 AuxiliaryType AuxType;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000069 COFF::Auxiliary Aux;
70};
Chris Lattner2c52b792010-07-11 22:07:02 +000071
Michael J. Spencerd6283772010-09-27 08:58:26 +000072class COFFSection;
73
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000074class COFFSymbol {
75public:
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +000076 COFF::symbol Data = {};
Chris Lattner2c52b792010-07-11 22:07:02 +000077
Dmitri Gribenko226fea52013-01-13 16:01:15 +000078 typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
Chris Lattner2c52b792010-07-11 22:07:02 +000079
Rafael Espindola11e9e212015-05-27 14:37:12 +000080 name Name;
81 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000082 AuxiliarySymbols Aux;
Eugene Zelenko1d435522017-02-07 23:02:00 +000083 COFFSymbol *Other = nullptr;
84 COFFSection *Section = nullptr;
85 int Relocations = 0;
86 const MCSymbol *MC = nullptr;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000087
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +000088 COFFSymbol(StringRef Name) : Name(Name) {}
Eugene Zelenko1d435522017-02-07 23:02:00 +000089
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000090 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000091
David Majnemer4eecd302015-05-30 04:56:02 +000092 int64_t getIndex() const { return Index; }
93 void setIndex(int Value) {
94 Index = Value;
95 if (MC)
96 MC->setIndex(static_cast<uint32_t>(Value));
97 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000098};
99
100// This class contains staging data for a COFF relocation entry.
101struct COFFRelocation {
102 COFF::relocation Data;
Eugene Zelenko1d435522017-02-07 23:02:00 +0000103 COFFSymbol *Symb = nullptr;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000104
Eugene Zelenko1d435522017-02-07 23:02:00 +0000105 COFFRelocation() = default;
106
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000107 static size_t size() { return COFF::RelocationSize; }
108};
109
110typedef std::vector<COFFRelocation> relocations;
111
112class COFFSection {
113public:
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000114 COFF::section Header = {};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000115
Rafael Espindola11e9e212015-05-27 14:37:12 +0000116 std::string Name;
117 int Number;
Eugene Zelenko1d435522017-02-07 23:02:00 +0000118 MCSectionCOFF const *MCSection = nullptr;
119 COFFSymbol *Symbol = nullptr;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000120 relocations Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000121
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000122 COFFSection(StringRef Name) : Name(Name) {}
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000123};
124
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000125class WinCOFFObjectWriter : public MCObjectWriter {
126public:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000127 typedef std::vector<std::unique_ptr<COFFSymbol>> symbols;
David Blaikief564ab62014-04-15 05:25:03 +0000128 typedef std::vector<std::unique_ptr<COFFSection>> sections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000129
Rafael Espindola11e9e212015-05-27 14:37:12 +0000130 typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000131 typedef DenseMap<MCSection const *, COFFSection *> section_map;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000132
Ahmed Charles56440fd2014-03-06 05:51:42 +0000133 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000134
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000135 // Root level file contents.
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000136 COFF::header Header = {};
Rafael Espindola11e9e212015-05-27 14:37:12 +0000137 sections Sections;
138 symbols Symbols;
Rafael Espindola21956e42015-10-23 21:48:05 +0000139 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000140
141 // Maps used during object file creation.
142 section_map SectionMap;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000143 symbol_map SymbolMap;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000144
David Majnemer4d571592014-09-15 19:42:42 +0000145 bool UseBigObj;
146
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000147 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000148
Yaron Kerencca43c12014-09-16 21:31:04 +0000149 void reset() override {
150 memset(&Header, 0, sizeof(Header));
151 Header.Machine = TargetObjectWriter->getMachine();
152 Sections.clear();
153 Symbols.clear();
154 Strings.clear();
155 SectionMap.clear();
156 SymbolMap.clear();
157 MCObjectWriter::reset();
158 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000159
Michael J. Spencer17990d52010-10-16 08:25:57 +0000160 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola11e9e212015-05-27 14:37:12 +0000161 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000162 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000163
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000164 void defineSection(MCSectionCOFF const &Sec);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000165
166 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000167 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000168 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000169
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000170 void SetSymbolName(COFFSymbol &S);
171 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000172
Michael J. Spencerd6283772010-09-27 08:58:26 +0000173 bool IsPhysicalSection(COFFSection *S);
174
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000175 // Entity writing methods.
176
177 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000178 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000179 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
Rui Ueyamaac20c172017-02-17 17:32:54 +0000180 void writeSectionHeaders();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000181 void WriteRelocation(const COFF::relocation &R);
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000182 uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout,
183 const MCSection &MCSec);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000184 void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
185 const COFFSection &Sec, const MCSection &MCSec);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000186
187 // MCObjectWriter interface implementation.
188
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000189 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper34a61bc2014-03-08 07:02:02 +0000190 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000191
Jim Grosbach36e60e92015-06-04 22:24:41 +0000192 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000193 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000194 const MCFragment &FB, bool InSet,
195 bool IsPCRel) const override;
196
Jim Grosbach36e60e92015-06-04 22:24:41 +0000197 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000198 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000199 MCValue Target, bool &IsPCRel,
200 uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000201
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000202 void createFileSymbols(MCAssembler &Asm);
203 void assignSectionNumbers();
204 void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout);
205
Jim Grosbach36e60e92015-06-04 22:24:41 +0000206 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000207};
Eugene Zelenko1d435522017-02-07 23:02:00 +0000208
209} // end anonymous namespace
Chris Lattner2c52b792010-07-11 22:07:02 +0000210
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000211//------------------------------------------------------------------------------
212// Symbol class implementation
213
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000214// In the case that the name does not fit within 8 bytes, the offset
215// into the string table is stored in the last 4 bytes instead, leaving
216// the first 4 bytes as 0.
217void COFFSymbol::set_name_offset(uint32_t Offset) {
Rui Ueyama86e3ef92017-02-14 23:28:19 +0000218 write32le(Data.Name + 0, 0);
219 write32le(Data.Name + 4, Offset);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000220}
221
222//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000223// WinCOFFObjectWriter class implementation
224
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000225WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000226 raw_pwrite_stream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000227 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000228 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000229}
230
Michael J. Spencer17990d52010-10-16 08:25:57 +0000231COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000232 Symbols.push_back(make_unique<COFFSymbol>(Name));
233 return Symbols.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000234}
235
Yaron Keren56919ef2014-12-04 08:30:39 +0000236COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Rui Ueyama0fcdb482017-02-14 23:47:34 +0000237 COFFSymbol *&Ret = SymbolMap[Symbol];
238 if (!Ret)
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000239 Ret = createSymbol(Symbol->getName());
Rui Ueyama0fcdb482017-02-14 23:47:34 +0000240 return Ret;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000241}
242
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000243COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000244 Sections.emplace_back(make_unique<COFFSection>(Name));
245 return Sections.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000246}
247
Rui Ueyama24e27b42017-02-15 00:15:54 +0000248static uint32_t getAlignment(const MCSectionCOFF &Sec) {
249 switch (Sec.getAlignment()) {
250 case 1:
251 return COFF::IMAGE_SCN_ALIGN_1BYTES;
252 case 2:
253 return COFF::IMAGE_SCN_ALIGN_2BYTES;
254 case 4:
255 return COFF::IMAGE_SCN_ALIGN_4BYTES;
256 case 8:
257 return COFF::IMAGE_SCN_ALIGN_8BYTES;
258 case 16:
259 return COFF::IMAGE_SCN_ALIGN_16BYTES;
260 case 32:
261 return COFF::IMAGE_SCN_ALIGN_32BYTES;
262 case 64:
263 return COFF::IMAGE_SCN_ALIGN_64BYTES;
264 case 128:
265 return COFF::IMAGE_SCN_ALIGN_128BYTES;
266 case 256:
267 return COFF::IMAGE_SCN_ALIGN_256BYTES;
268 case 512:
269 return COFF::IMAGE_SCN_ALIGN_512BYTES;
270 case 1024:
271 return COFF::IMAGE_SCN_ALIGN_1024BYTES;
272 case 2048:
273 return COFF::IMAGE_SCN_ALIGN_2048BYTES;
274 case 4096:
275 return COFF::IMAGE_SCN_ALIGN_4096BYTES;
276 case 8192:
277 return COFF::IMAGE_SCN_ALIGN_8192BYTES;
278 }
279 llvm_unreachable("unsupported section alignment");
280}
281
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000282/// This function takes a section data object from the assembler
283/// and creates the associated COFF section staging object.
Rui Ueyama09786c42017-02-15 00:28:48 +0000284void WinCOFFObjectWriter::defineSection(const MCSectionCOFF &MCSec) {
285 COFFSection *Section = createSection(MCSec.getSectionName());
286 COFFSymbol *Symbol = createSymbol(MCSec.getSectionName());
287 Section->Symbol = Symbol;
288 Symbol->Section = Section;
289 Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Rui Ueyama24e27b42017-02-15 00:15:54 +0000290
291 // Create a COMDAT symbol if needed.
Rui Ueyama09786c42017-02-15 00:28:48 +0000292 if (MCSec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
293 if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
Rafael Espindola0766ae02014-06-06 19:26:12 +0000294 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
295 if (COMDATSymbol->Section)
296 report_fatal_error("two sections have the same comdat");
Rui Ueyama09786c42017-02-15 00:28:48 +0000297 COMDATSymbol->Section = Section;
Rafael Espindola0766ae02014-06-06 19:26:12 +0000298 }
299 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000300
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000301 // In this case the auxiliary symbol is a Section Definition.
Rui Ueyama09786c42017-02-15 00:28:48 +0000302 Symbol->Aux.resize(1);
303 Symbol->Aux[0] = {};
304 Symbol->Aux[0].AuxType = ATSectionDefinition;
305 Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000306
Rui Ueyama09786c42017-02-15 00:28:48 +0000307 // Set section alignment.
308 Section->Header.Characteristics = MCSec.getCharacteristics();
309 Section->Header.Characteristics |= getAlignment(MCSec);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000310
311 // Bind internal COFF section to MC section.
Rui Ueyama09786c42017-02-15 00:28:48 +0000312 Section->MCSection = &MCSec;
313 SectionMap[&MCSec] = Section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000314}
315
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000316static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000317 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000318 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000319 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000320
321 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000322 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000323 return 0;
324
325 return Res;
326}
327
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000328COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
329 if (!Symbol.isVariable())
330 return nullptr;
331
332 const MCSymbolRefExpr *SymRef =
333 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
334 if (!SymRef)
335 return nullptr;
336
337 const MCSymbol &Aliasee = SymRef->getSymbol();
338 if (!Aliasee.isUndefined())
339 return nullptr;
340 return GetOrCreateCOFFSymbol(&Aliasee);
341}
342
David Majnemera9bdb322014-04-08 22:33:40 +0000343/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000344/// and creates the associated COFF symbol staging object.
Rui Ueyama789c4222017-02-15 01:09:01 +0000345void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000346 MCAssembler &Assembler,
347 const MCAsmLayout &Layout) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000348 COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
349 const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000350 COFFSection *Sec = nullptr;
351 if (Base && Base->getFragment()) {
352 Sec = SectionMap[Base->getFragment()->getParent()];
Rui Ueyama789c4222017-02-15 01:09:01 +0000353 if (Sym->Section && Sym->Section != Sec)
Rafael Espindola30c080a2016-05-26 18:48:23 +0000354 report_fatal_error("conflicting sections for symbol");
355 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000356
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000357 COFFSymbol *Local = nullptr;
Rui Ueyama789c4222017-02-15 01:09:01 +0000358 if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
359 Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000360
Rui Ueyama789c4222017-02-15 01:09:01 +0000361 COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000362 if (!WeakDefault) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000363 std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000364 WeakDefault = createSymbol(WeakName);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000365 if (!Sec)
366 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
367 else
368 WeakDefault->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000369 Local = WeakDefault;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000370 }
371
Rui Ueyama789c4222017-02-15 01:09:01 +0000372 Sym->Other = WeakDefault;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000373
Michael J. Spencer17990d52010-10-16 08:25:57 +0000374 // Setup the Weak External auxiliary symbol.
Rui Ueyama789c4222017-02-15 01:09:01 +0000375 Sym->Aux.resize(1);
376 memset(&Sym->Aux[0], 0, sizeof(Sym->Aux[0]));
377 Sym->Aux[0].AuxType = ATWeakExternal;
378 Sym->Aux[0].Aux.WeakExternal.TagIndex = 0;
379 Sym->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000380 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000381 } else {
Rafael Espindola30c080a2016-05-26 18:48:23 +0000382 if (!Base)
Rui Ueyama789c4222017-02-15 01:09:01 +0000383 Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola30c080a2016-05-26 18:48:23 +0000384 else
Rui Ueyama789c4222017-02-15 01:09:01 +0000385 Sym->Section = Sec;
386 Local = Sym;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000387 }
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000388
389 if (Local) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000390 Local->Data.Value = getSymbolValue(MCSym, Layout);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000391
Rui Ueyama789c4222017-02-15 01:09:01 +0000392 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000393 Local->Data.Type = SymbolCOFF.getType();
394 Local->Data.StorageClass = SymbolCOFF.getClass();
395
396 // If no storage class was specified in the streamer, define it here.
397 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000398 bool IsExternal = MCSym.isExternal() ||
399 (!MCSym.getFragment() && !MCSym.isVariable());
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000400
401 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
402 : COFF::IMAGE_SYM_CLASS_STATIC;
403 }
404 }
405
Rui Ueyama789c4222017-02-15 01:09:01 +0000406 Sym->MC = &MCSym;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000407}
408
Nico Rieck01143f92014-02-25 09:50:40 +0000409// Maximum offsets for different string table entry encodings.
David Majnemerf088f522016-01-24 20:46:11 +0000410enum : unsigned { Max7DecimalOffset = 9999999U };
411enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
Nico Rieck01143f92014-02-25 09:50:40 +0000412
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000413// Encode a string table entry offset in base 64, padded to 6 chars, and
414// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
415// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000416static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000417 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000418 "Illegal section name encoding for value");
419
420 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
421 "abcdefghijklmnopqrstuvwxyz"
422 "0123456789+/";
423
424 Buffer[0] = '/';
425 Buffer[1] = '/';
426
Rafael Espindola11e9e212015-05-27 14:37:12 +0000427 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000428 for (unsigned i = 0; i < 6; ++i) {
429 unsigned Rem = Value % 64;
430 Value /= 64;
431 *(Ptr--) = Alphabet[Rem];
432 }
433}
434
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000435void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000436 if (S.Name.size() <= COFF::NameSize) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000437 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000438 return;
David Majnemerf088f522016-01-24 20:46:11 +0000439 }
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000440
441 uint64_t StringTableEntry = Strings.getOffset(S.Name);
442 if (StringTableEntry <= Max7DecimalOffset) {
Rui Ueyama4b58f5772017-02-15 01:48:33 +0000443 SmallVector<char, COFF::NameSize> Buffer;
444 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
445 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
446 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000447 return;
448 }
449 if (StringTableEntry <= MaxBase64Offset) {
450 // Starting with 10,000,000, offsets are encoded as base64.
451 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
452 return;
453 }
454 report_fatal_error("COFF string table is greater than 64 GB.");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000455}
456
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000457void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
458 if (S.Name.size() > COFF::NameSize)
459 S.set_name_offset(Strings.getOffset(S.Name));
460 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000461 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000462}
463
Michael J. Spencerd6283772010-09-27 08:58:26 +0000464bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000465 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
466 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000467}
468
469//------------------------------------------------------------------------------
470// entity writing methods
471
472void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000473 if (UseBigObj) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000474 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
475 writeLE16(0xFFFF);
476 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
477 writeLE16(Header.Machine);
478 writeLE32(Header.TimeDateStamp);
479 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
480 writeLE32(0);
481 writeLE32(0);
482 writeLE32(0);
483 writeLE32(0);
484 writeLE32(Header.NumberOfSections);
485 writeLE32(Header.PointerToSymbolTable);
486 writeLE32(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000487 } else {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000488 writeLE16(Header.Machine);
489 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
490 writeLE32(Header.TimeDateStamp);
491 writeLE32(Header.PointerToSymbolTable);
492 writeLE32(Header.NumberOfSymbols);
493 writeLE16(Header.SizeOfOptionalHeader);
494 writeLE16(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000495 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000496}
497
David Blaikief564ab62014-04-15 05:25:03 +0000498void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000499 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
500 writeLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000501 if (UseBigObj)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000502 writeLE32(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000503 else
Jim Grosbach36e60e92015-06-04 22:24:41 +0000504 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
505 writeLE16(S.Data.Type);
506 write8(S.Data.StorageClass);
507 write8(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000508 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000509}
510
511void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000512 const COFFSymbol::AuxiliarySymbols &S) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000513 for (const AuxSymbol &i : S) {
514 switch (i.AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000515 case ATFunctionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000516 writeLE32(i.Aux.FunctionDefinition.TagIndex);
517 writeLE32(i.Aux.FunctionDefinition.TotalSize);
518 writeLE32(i.Aux.FunctionDefinition.PointerToLinenumber);
519 writeLE32(i.Aux.FunctionDefinition.PointerToNextFunction);
520 WriteZeros(sizeof(i.Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000521 if (UseBigObj)
522 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000523 break;
524 case ATbfAndefSymbol:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000525 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused1));
526 writeLE16(i.Aux.bfAndefSymbol.Linenumber);
527 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused2));
528 writeLE32(i.Aux.bfAndefSymbol.PointerToNextFunction);
529 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000530 if (UseBigObj)
531 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000532 break;
533 case ATWeakExternal:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000534 writeLE32(i.Aux.WeakExternal.TagIndex);
535 writeLE32(i.Aux.WeakExternal.Characteristics);
536 WriteZeros(sizeof(i.Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000537 if (UseBigObj)
538 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000539 break;
540 case ATFile:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000541 writeBytes(
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000542 StringRef(reinterpret_cast<const char *>(&i.Aux),
David Majnemer4d571592014-09-15 19:42:42 +0000543 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000544 break;
545 case ATSectionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000546 writeLE32(i.Aux.SectionDefinition.Length);
547 writeLE16(i.Aux.SectionDefinition.NumberOfRelocations);
548 writeLE16(i.Aux.SectionDefinition.NumberOfLinenumbers);
549 writeLE32(i.Aux.SectionDefinition.CheckSum);
550 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
551 write8(i.Aux.SectionDefinition.Selection);
552 WriteZeros(sizeof(i.Aux.SectionDefinition.unused));
553 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000554 if (UseBigObj)
555 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000556 break;
557 }
558 }
559}
560
Rui Ueyamaac20c172017-02-17 17:32:54 +0000561// Write the section header.
562void WinCOFFObjectWriter::writeSectionHeaders() {
563 // Section numbers must be monotonically increasing in the section
564 // header, but our Sections array is not sorted by section number,
565 // so make a copy of Sections and sort it.
566 std::vector<COFFSection *> Arr;
567 for (auto &Section : Sections)
568 Arr.push_back(Section.get());
569 std::sort(Arr.begin(), Arr.end(),
570 [](const COFFSection *A, const COFFSection *B) {
571 return A->Number < B->Number;
572 });
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000573
Rui Ueyamaac20c172017-02-17 17:32:54 +0000574 for (auto &Section : Arr) {
575 if (Section->Number == -1)
576 continue;
577
578 COFF::section &S = Section->Header;
579 if (Section->Relocations.size() >= 0xffff)
580 S.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
581 writeBytes(StringRef(S.Name, COFF::NameSize));
582 writeLE32(S.VirtualSize);
583 writeLE32(S.VirtualAddress);
584 writeLE32(S.SizeOfRawData);
585 writeLE32(S.PointerToRawData);
586 writeLE32(S.PointerToRelocations);
587 writeLE32(S.PointerToLineNumbers);
588 writeLE16(S.NumberOfRelocations);
589 writeLE16(S.NumberOfLineNumbers);
590 writeLE32(S.Characteristics);
591 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000592}
593
594void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000595 writeLE32(R.VirtualAddress);
596 writeLE32(R.SymbolTableIndex);
597 writeLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000598}
599
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000600// Write MCSec's contents. What this function does is essentially
601// "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
602// because it needs to compute a CRC.
603uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler &Asm,
604 const MCAsmLayout &Layout,
605 const MCSection &MCSec) {
606 // Save the contents of the section to a temporary buffer, we need this
607 // to CRC the data before we dump it into the object file.
608 SmallVector<char, 128> Buf;
609 raw_svector_ostream VecOS(Buf);
610 raw_pwrite_stream &OldStream = getStream();
611
612 // Redirect the output stream to our buffer and fill our buffer with
613 // the section data.
614 setStream(VecOS);
615 Asm.writeSectionData(&MCSec, Layout);
616
617 // Reset the stream back to what it was before.
618 setStream(OldStream);
619
620 // Write the section contents to the object file.
621 getStream() << Buf;
622
623 // Calculate our CRC with an initial value of '0', this is not how
624 // JamCRC is specified but it aligns with the expected output.
625 JamCRC JC(/*Init=*/0);
626 JC.update(Buf);
627 return JC.getCRC();
628}
629
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000630void WinCOFFObjectWriter::writeSection(MCAssembler &Asm,
631 const MCAsmLayout &Layout,
632 const COFFSection &Sec,
633 const MCSection &MCSec) {
634 if (Sec.Number == -1)
635 return;
636
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000637 // Write the section contents.
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000638 if (Sec.Header.PointerToRawData != 0) {
639 assert(getStream().tell() <= Sec.Header.PointerToRawData &&
640 "Section::PointerToRawData is insane!");
641
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000642 unsigned PaddingSize = Sec.Header.PointerToRawData - getStream().tell();
643 assert(PaddingSize < 4 &&
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000644 "Should only need at most three bytes of padding!");
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000645 WriteZeros(PaddingSize);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000646
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000647 uint32_t CRC = writeSectionContents(Asm, Layout, MCSec);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000648
649 // Update the section definition auxiliary symbol to record the CRC.
650 COFFSection *Sec = SectionMap[&MCSec];
651 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
652 assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
653 AuxSymbol &SecDef = AuxSyms[0];
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000654 SecDef.Aux.SectionDefinition.CheckSum = CRC;
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000655 }
656
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000657 // Write relocations for this section.
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000658 if (Sec.Relocations.empty()) {
659 assert(Sec.Header.PointerToRelocations == 0 &&
660 "Section::PointerToRelocations is insane!");
661 return;
662 }
663
664 assert(getStream().tell() == Sec.Header.PointerToRelocations &&
665 "Section::PointerToRelocations is insane!");
666
667 if (Sec.Relocations.size() >= 0xffff) {
668 // In case of overflow, write actual relocation count as first
669 // relocation. Including the synthetic reloc itself (+ 1).
670 COFF::relocation R;
671 R.VirtualAddress = Sec.Relocations.size() + 1;
672 R.SymbolTableIndex = 0;
673 R.Type = 0;
674 WriteRelocation(R);
675 }
676
677 for (const auto &Relocation : Sec.Relocations)
678 WriteRelocation(Relocation.Data);
679}
680
Chris Lattner2c52b792010-07-11 22:07:02 +0000681////////////////////////////////////////////////////////////////////////////////
682// MCObjectWriter interface implementations
683
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000684void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000685 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000686 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000687 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000688 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000689 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000690
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000691 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000692 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000693 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000694}
695
Jim Grosbach36e60e92015-06-04 22:24:41 +0000696bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000697 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000698 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000699 // MS LINK expects to be able to replace all references to a function with a
700 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
701 // away any relocations to functions.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000702 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
David Majnemer03e2cc32015-12-21 22:09:27 +0000703 if (Asm.isIncrementalLinkerCompatible() &&
704 (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000705 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000706 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000707 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000708}
709
Jim Grosbach36e60e92015-06-04 22:24:41 +0000710void WinCOFFObjectWriter::recordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000711 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
712 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000713 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000714
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000715 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000716 if (!A.isRegistered()) {
717 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000718 Twine("symbol '") + A.getName() +
719 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000720 return;
721 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000722 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000723 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000724 Twine("assembler label '") + A.getName() +
725 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000726 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000727 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000728
Rui Ueyama62376782017-02-16 01:06:45 +0000729 MCSection *MCSec = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000730
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000731 // Mark this symbol as requiring an entry in the symbol table.
Rui Ueyama62376782017-02-16 01:06:45 +0000732 assert(SectionMap.find(MCSec) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000733 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000734
Rui Ueyama62376782017-02-16 01:06:45 +0000735 COFFSection *Sec = SectionMap[MCSec];
Rafael Espindolaed164772011-04-20 14:01:45 +0000736 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000737 bool CrossSection = false;
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
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000757 CrossSection = &A.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000758
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000759 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000760 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000761
762 // In the case where we have SymbA and SymB, we just need to store the delta
763 // between the two symbols. Update FixedValue to account for the delta, and
764 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000765 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000766 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000767 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000768 return;
David Majnemer1de30942015-02-09 06:31:31 +0000769 }
770
771 // Offset of the relocation in the section
772 int64_t OffsetOfRelocation =
773 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
774
Andy Ayers9e5c8512015-05-14 01:10:41 +0000775 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000776 } else {
777 FixedValue = Target.getConstant();
778 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000779
780 COFFRelocation Reloc;
781
Daniel Dunbar727be432010-07-31 21:08:54 +0000782 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000783 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000784
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000785 // Turn relocations for temporary symbols into section relocations.
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000786 if (A.isTemporary() || CrossSection) {
787 MCSection *TargetSection = &A.getSection();
788 assert(
789 SectionMap.find(TargetSection) != SectionMap.end() &&
790 "Section must already have been defined in executePostLayoutBinding!");
791 Reloc.Symb = SectionMap[TargetSection]->Symbol;
792 FixedValue += Layout.getSymbolOffset(A);
793 } else {
794 assert(
795 SymbolMap.find(&A) != SymbolMap.end() &&
796 "Symbol must already have been defined in executePostLayoutBinding!");
797 Reloc.Symb = SymbolMap[&A];
798 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000799
800 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000801
802 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000803 Reloc.Data.Type = TargetObjectWriter->getRelocType(
804 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000805
806 // FIXME: Can anyone explain what this does other than adjust for the size
807 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000808 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
809 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
810 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
811 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000812 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000813
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000814 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
815 switch (Reloc.Data.Type) {
816 case COFF::IMAGE_REL_ARM_ABSOLUTE:
817 case COFF::IMAGE_REL_ARM_ADDR32:
818 case COFF::IMAGE_REL_ARM_ADDR32NB:
819 case COFF::IMAGE_REL_ARM_TOKEN:
820 case COFF::IMAGE_REL_ARM_SECTION:
821 case COFF::IMAGE_REL_ARM_SECREL:
822 break;
823 case COFF::IMAGE_REL_ARM_BRANCH11:
824 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000825 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
826 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
827 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000828 case COFF::IMAGE_REL_ARM_BRANCH24:
829 case COFF::IMAGE_REL_ARM_BLX24:
830 case COFF::IMAGE_REL_ARM_MOV32A:
831 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
832 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000833 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000834 // generate the relocations however the rest of the MSVC toolchain is
835 // unable to handle it.
836 llvm_unreachable("unsupported relocation");
837 break;
838 case COFF::IMAGE_REL_ARM_MOV32T:
839 break;
840 case COFF::IMAGE_REL_ARM_BRANCH20T:
841 case COFF::IMAGE_REL_ARM_BRANCH24T:
842 case COFF::IMAGE_REL_ARM_BLX23T:
843 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
844 // perform a 4 byte adjustment to the relocation. Relative branches are
845 // offset by 4 on ARM, however, because there is no RELA relocations, all
846 // branches are offset by 4.
847 FixedValue = FixedValue + 4;
848 break;
849 }
850 }
851
Simon Pilgrimf2fbf432016-11-20 13:47:59 +0000852 // The fixed value never makes sense for section indices, ignore it.
David Majnemer408b5e62016-02-05 01:55:49 +0000853 if (Fixup.getKind() == FK_SecRel_2)
854 FixedValue = 0;
855
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000856 if (TargetObjectWriter->recordRelocation(Fixup))
Rui Ueyama62376782017-02-16 01:06:45 +0000857 Sec->Relocations.push_back(Reloc);
858}
859
860static std::time_t getTime() {
861 std::time_t Now = time(nullptr);
862 if (Now < 0 || !isUInt<32>(Now))
863 return UINT32_MAX;
864 return Now;
Chris Lattner2c52b792010-07-11 22:07:02 +0000865}
866
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000867// Create .file symbols.
868void WinCOFFObjectWriter::createFileSymbols(MCAssembler &Asm) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000869 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000870 // round up to calculate the number of auxiliary symbols required
871 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000872 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000873
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000874 COFFSymbol *File = createSymbol(".file");
875 File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
876 File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
877 File->Aux.resize(Count);
David Majnemer4d571592014-09-15 19:42:42 +0000878
879 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000880 unsigned Length = Name.size();
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000881 for (auto &Aux : File->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000882 Aux.AuxType = ATFile;
883
884 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000885 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000886 Length = Length - SymbolSize;
887 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000888 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000889 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
890 break;
891 }
892
893 Offset += SymbolSize;
894 }
895 }
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000896}
897
Rui Ueyamaac20c172017-02-17 17:32:54 +0000898static bool isAssociative(const COFFSection &Section) {
899 return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
900 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
901}
902
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000903void WinCOFFObjectWriter::assignSectionNumbers() {
904 size_t I = 1;
Rui Ueyamaac20c172017-02-17 17:32:54 +0000905 auto Assign = [&](COFFSection &Section) {
906 Section.Number = I;
907 Section.Symbol->Data.SectionNumber = I;
908 Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I;
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000909 ++I;
Rui Ueyamaac20c172017-02-17 17:32:54 +0000910 };
911
912 // Although it is not explicitly requested by the Microsoft COFF spec,
913 // we should avoid emitting forward associative section references,
914 // because MSVC link.exe as of 2017 cannot handle that.
915 for (const std::unique_ptr<COFFSection> &Section : Sections)
916 if (!isAssociative(*Section))
917 Assign(*Section);
918 for (const std::unique_ptr<COFFSection> &Section : Sections)
919 if (isAssociative(*Section))
920 Assign(*Section);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000921}
922
923// Assign file offsets to COFF object file structures.
924void WinCOFFObjectWriter::assignFileOffsets(MCAssembler &Asm,
925 const MCAsmLayout &Layout) {
926 unsigned Offset = getInitialOffset();
927
928 Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
929 Offset += COFF::SectionSize * Header.NumberOfSections;
930
931 for (const auto &Section : Asm) {
932 COFFSection *Sec = SectionMap[&Section];
933
934 if (Sec->Number == -1)
935 continue;
936
937 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
938
939 if (IsPhysicalSection(Sec)) {
940 // Align the section data to a four byte boundary.
941 Offset = alignTo(Offset, 4);
942 Sec->Header.PointerToRawData = Offset;
943
944 Offset += Sec->Header.SizeOfRawData;
945 }
946
947 if (!Sec->Relocations.empty()) {
948 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
949
950 if (RelocationsOverflow) {
951 // Signal overflow by setting NumberOfRelocations to max value. Actual
952 // size is found in reloc #0. Microsoft tools understand this.
953 Sec->Header.NumberOfRelocations = 0xffff;
954 } else {
955 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
956 }
957 Sec->Header.PointerToRelocations = Offset;
958
959 if (RelocationsOverflow) {
960 // Reloc #0 will contain actual count, so make room for it.
961 Offset += COFF::RelocationSize;
962 }
963
964 Offset += COFF::RelocationSize * Sec->Relocations.size();
965
966 for (auto &Relocation : Sec->Relocations) {
967 assert(Relocation.Symb->getIndex() != -1);
968 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
969 }
970 }
971
972 assert(Sec->Symbol->Aux.size() == 1 &&
973 "Section's symbol must have one aux!");
974 AuxSymbol &Aux = Sec->Symbol->Aux[0];
975 assert(Aux.AuxType == ATSectionDefinition &&
976 "Section's symbol's aux symbol must be a Section Definition!");
977 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
978 Aux.Aux.SectionDefinition.NumberOfRelocations =
979 Sec->Header.NumberOfRelocations;
980 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
981 Sec->Header.NumberOfLineNumbers;
982 }
983
984 Header.PointerToSymbolTable = Offset;
985}
986
987void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
988 const MCAsmLayout &Layout) {
989 if (Sections.size() > INT32_MAX)
990 report_fatal_error(
991 "PE COFF object files can't have more than 2147483647 sections");
992
993 UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
994 Header.NumberOfSections = Sections.size();
995 Header.NumberOfSymbols = 0;
996
997 assignSectionNumbers();
998 createFileSymbols(Asm);
David Majnemer4d571592014-09-15 19:42:42 +0000999
David Majnemer9ab5ff12014-08-28 04:02:50 +00001000 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001001 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +00001002 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001003 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +00001004 Symbol->setIndex(Header.NumberOfSymbols++);
1005 // Update auxiliary symbol info.
1006 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
1007 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001008 }
1009
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001010 // Build string table.
1011 for (const auto &S : Sections)
1012 if (S->Name.size() > COFF::NameSize)
1013 Strings.add(S->Name);
1014 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +00001015 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001016 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +00001017 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001018
1019 // Set names.
1020 for (const auto &S : Sections)
1021 SetSectionName(*S);
1022 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +00001023 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001024
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001025 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +00001026 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001027 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +00001028 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001029 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
1030 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001031 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +00001032 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001033 }
1034 }
1035
Nico Riecka37acf72013-07-06 12:13:10 +00001036 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +00001037 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001038 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +00001039 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1040 continue;
1041
Rafael Espindolaf59264f2015-05-27 14:45:54 +00001042 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +00001043
Rafael Espindola0766ae02014-06-06 19:26:12 +00001044 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
1045 assert(COMDAT);
1046 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
1047 assert(COMDATSymbol);
1048 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001049 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +00001050 report_fatal_error(
1051 Twine("Missing associated COMDAT section for section ") +
1052 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +00001053
1054 // Skip this section if the associated section is unused.
1055 if (Assoc->Number == -1)
1056 continue;
1057
David Majnemer4eecd302015-05-30 04:56:02 +00001058 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +00001059 }
1060
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001061 assignFileOffsets(Asm, Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001062
David Majnemer088ba022015-09-01 23:46:11 +00001063 // MS LINK expects to be able to use this timestamp to implement their
1064 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +00001065 if (Asm.isIncrementalLinkerCompatible()) {
Rui Ueyama62376782017-02-16 01:06:45 +00001066 Header.TimeDateStamp = getTime();
David Majnemer03e2cc32015-12-21 22:09:27 +00001067 } else {
Nico Weber891419a2016-01-06 19:05:19 +00001068 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +00001069 Header.TimeDateStamp = 0;
1070 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +00001071
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001072 // Write it all to disk...
1073 WriteFileHeader(Header);
Rui Ueyamaac20c172017-02-17 17:32:54 +00001074 writeSectionHeaders();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001075
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001076 // Write section contents.
1077 sections::iterator I = Sections.begin();
1078 sections::iterator IE = Sections.end();
1079 MCAssembler::iterator J = Asm.begin();
1080 MCAssembler::iterator JE = Asm.end();
1081 for (; I != IE && J != JE; ++I, ++J)
1082 writeSection(Asm, Layout, **I, *J);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001083
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001084 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001085 "Header::PointerToSymbolTable is insane!");
1086
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001087 // Write a symbol table.
Yaron Keren56919ef2014-12-04 08:30:39 +00001088 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001089 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001090 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001091
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001092 // Write a string table, which completes the entire COFF file.
Rafael Espindola39751af2016-10-04 22:43:25 +00001093 Strings.write(getStream());
Chris Lattner2c52b792010-07-11 22:07:02 +00001094}
1095
Rafael Espindola11e9e212015-05-27 14:37:12 +00001096MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1097 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001098
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001099// Pin the vtable to this file.
1100void MCWinCOFFObjectTargetWriter::anchor() {}
1101
Chris Lattner2c52b792010-07-11 22:07:02 +00001102//------------------------------------------------------------------------------
1103// WinCOFFObjectWriter factory function
1104
Rafael Espindola37099d92015-04-09 18:08:15 +00001105MCObjectWriter *
1106llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001107 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001108 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001109}