blob: b774852eabe6b8d895ba2c2044d0ea6707a823c3 [file] [log] [blame]
Eugene Zelenkof31871c2017-02-07 23:02:00 +00001//===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
Chris Lattnerb1622902010-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. Spencer801a3592010-07-26 02:17:32 +000014#include "llvm/ADT/DenseMap.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000015#include "llvm/ADT/STLExtras.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/SmallVector.h"
Michael J. Spencer801a3592010-07-26 02:17:32 +000018#include "llvm/ADT/StringRef.h"
Nico Rieck80646282013-07-06 12:13:10 +000019#include "llvm/ADT/Twine.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000020#include "llvm/BinaryFormat/COFF.h"
Chandler Carruthd04a8d42012-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 Zelenkof31871c2017-02-07 23:02:00 +000025#include "llvm/MC/MCFixup.h"
26#include "llvm/MC/MCFragment.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/MC/MCObjectWriter.h"
28#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCSectionCOFF.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000030#include "llvm/MC/MCSymbol.h"
Pete Cooperf560b882015-06-08 17:17:12 +000031#include "llvm/MC/MCSymbolCOFF.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000032#include "llvm/MC/MCValue.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000033#include "llvm/MC/MCWinCOFFObjectWriter.h"
Hans Wennborg4edcbae2014-09-29 22:43:20 +000034#include "llvm/MC/StringTableBuilder.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000035#include "llvm/Support/Casting.h"
Hans Wennborge05d3b92014-09-28 00:22:27 +000036#include "llvm/Support/Endian.h"
Michael J. Spencer801a3592010-07-26 02:17:32 +000037#include "llvm/Support/ErrorHandling.h"
David Majnemer3d58bd62015-09-01 21:23:58 +000038#include "llvm/Support/JamCRC.h"
Peter Collingbourne38b60af2018-08-22 23:58:16 +000039#include "llvm/Support/LEB128.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000040#include "llvm/Support/MathExtras.h"
41#include "llvm/Support/raw_ostream.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000042#include <algorithm>
Eugene Zelenkof31871c2017-02-07 23:02:00 +000043#include <cassert>
44#include <cstddef>
45#include <cstdint>
46#include <cstring>
David Majnemer6ad37ec2015-09-01 23:46:11 +000047#include <ctime>
Eugene Zelenkof31871c2017-02-07 23:02:00 +000048#include <memory>
49#include <string>
50#include <vector>
Michael J. Spencer801a3592010-07-26 02:17:32 +000051
Chris Lattnerb1622902010-07-11 22:07:02 +000052using namespace llvm;
Rui Ueyamad5265b302017-02-14 23:28:19 +000053using llvm::support::endian::write32le;
Chris Lattnerb1622902010-07-11 22:07:02 +000054
Chandler Carruth0d338a52014-04-22 03:04:17 +000055#define DEBUG_TYPE "WinCOFFObjectWriter"
56
Chris Lattnerb1622902010-07-11 22:07:02 +000057namespace {
Eugene Zelenkof31871c2017-02-07 23:02:00 +000058
Eugene Zelenkof49f90e2017-04-26 22:31:39 +000059using name = SmallString<COFF::NameSize>;
Chris Lattnerb1622902010-07-11 22:07:02 +000060
Michael J. Spencer801a3592010-07-26 02:17:32 +000061enum AuxiliaryType {
Michael J. Spencer801a3592010-07-26 02:17:32 +000062 ATWeakExternal,
63 ATFile,
64 ATSectionDefinition
65};
Chris Lattnerb1622902010-07-11 22:07:02 +000066
Michael J. Spencer801a3592010-07-26 02:17:32 +000067struct AuxSymbol {
Rafael Espindola1da0f912015-05-27 14:37:12 +000068 AuxiliaryType AuxType;
Michael J. Spencer801a3592010-07-26 02:17:32 +000069 COFF::Auxiliary Aux;
70};
Chris Lattnerb1622902010-07-11 22:07:02 +000071
Michael J. Spencera72d8782010-09-27 08:58:26 +000072class COFFSection;
73
Michael J. Spencer801a3592010-07-26 02:17:32 +000074class COFFSymbol {
75public:
Rui Ueyama3afdebf2017-02-14 23:28:01 +000076 COFF::symbol Data = {};
Chris Lattnerb1622902010-07-11 22:07:02 +000077
Eugene Zelenkof49f90e2017-04-26 22:31:39 +000078 using AuxiliarySymbols = SmallVector<AuxSymbol, 1>;
Chris Lattnerb1622902010-07-11 22:07:02 +000079
Rafael Espindola1da0f912015-05-27 14:37:12 +000080 name Name;
81 int Index;
Michael J. Spencer801a3592010-07-26 02:17:32 +000082 AuxiliarySymbols Aux;
Eugene Zelenkof31871c2017-02-07 23:02:00 +000083 COFFSymbol *Other = nullptr;
84 COFFSection *Section = nullptr;
85 int Relocations = 0;
86 const MCSymbol *MC = nullptr;
Michael J. Spencer801a3592010-07-26 02:17:32 +000087
Rui Ueyama3afdebf2017-02-14 23:28:01 +000088 COFFSymbol(StringRef Name) : Name(Name) {}
Eugene Zelenkof31871c2017-02-07 23:02:00 +000089
Michael J. Spencer801a3592010-07-26 02:17:32 +000090 void set_name_offset(uint32_t Offset);
Michael J. Spencera72d8782010-09-27 08:58:26 +000091
David Majnemere0d2a292015-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. Spencer801a3592010-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 Zelenkof31871c2017-02-07 23:02:00 +0000103 COFFSymbol *Symb = nullptr;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000104
Eugene Zelenkof31871c2017-02-07 23:02:00 +0000105 COFFRelocation() = default;
106
Michael J. Spencer801a3592010-07-26 02:17:32 +0000107 static size_t size() { return COFF::RelocationSize; }
108};
109
Eugene Zelenkof49f90e2017-04-26 22:31:39 +0000110using relocations = std::vector<COFFRelocation>;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000111
112class COFFSection {
113public:
Rui Ueyama3afdebf2017-02-14 23:28:01 +0000114 COFF::section Header = {};
Michael J. Spencer801a3592010-07-26 02:17:32 +0000115
Rafael Espindola1da0f912015-05-27 14:37:12 +0000116 std::string Name;
117 int Number;
Eugene Zelenkof31871c2017-02-07 23:02:00 +0000118 MCSectionCOFF const *MCSection = nullptr;
119 COFFSymbol *Symbol = nullptr;
Rafael Espindola1da0f912015-05-27 14:37:12 +0000120 relocations Relocations;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000121
Rui Ueyama3afdebf2017-02-14 23:28:01 +0000122 COFFSection(StringRef Name) : Name(Name) {}
Michael J. Spencer801a3592010-07-26 02:17:32 +0000123};
124
Michael J. Spencer801a3592010-07-26 02:17:32 +0000125class WinCOFFObjectWriter : public MCObjectWriter {
126public:
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000127 support::endian::Writer W;
128
Eugene Zelenkof49f90e2017-04-26 22:31:39 +0000129 using symbols = std::vector<std::unique_ptr<COFFSymbol>>;
130 using sections = std::vector<std::unique_ptr<COFFSection>>;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000131
Eugene Zelenkof49f90e2017-04-26 22:31:39 +0000132 using symbol_map = DenseMap<MCSymbol const *, COFFSymbol *>;
133 using section_map = DenseMap<MCSection const *, COFFSection *>;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000134
Ahmed Charlesf4ccd112014-03-06 05:51:42 +0000135 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindoladf092702011-12-24 02:14:02 +0000136
Michael J. Spencer801a3592010-07-26 02:17:32 +0000137 // Root level file contents.
Rui Ueyama3afdebf2017-02-14 23:28:01 +0000138 COFF::header Header = {};
Rafael Espindola1da0f912015-05-27 14:37:12 +0000139 sections Sections;
140 symbols Symbols;
Rafael Espindola715bcca2015-10-23 21:48:05 +0000141 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
Michael J. Spencer801a3592010-07-26 02:17:32 +0000142
143 // Maps used during object file creation.
144 section_map SectionMap;
Rafael Espindola1da0f912015-05-27 14:37:12 +0000145 symbol_map SymbolMap;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000146
David Majnemerf1198da2014-09-15 19:42:42 +0000147 bool UseBigObj;
148
Peter Collingbourne38b60af2018-08-22 23:58:16 +0000149 bool EmitAddrsigSection = false;
150 MCSectionCOFF *AddrsigSection;
151 std::vector<const MCSymbol *> AddrsigSyms;
152
Lang Hames1d8cdbb2017-10-10 00:50:29 +0000153 WinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
154 raw_pwrite_stream &OS);
Rafael Espindola8ee55c72015-04-09 18:08:15 +0000155
Yaron Kerenca7da162014-09-16 21:31:04 +0000156 void reset() override {
157 memset(&Header, 0, sizeof(Header));
158 Header.Machine = TargetObjectWriter->getMachine();
159 Sections.clear();
160 Symbols.clear();
161 Strings.clear();
162 SectionMap.clear();
163 SymbolMap.clear();
164 MCObjectWriter::reset();
165 }
Michael J. Spencer801a3592010-07-26 02:17:32 +0000166
Michael J. Spencer4cee2892010-10-16 08:25:57 +0000167 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola1da0f912015-05-27 14:37:12 +0000168 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer4cee2892010-10-16 08:25:57 +0000169 COFFSection *createSection(StringRef Name);
Michael J. Spencer801a3592010-07-26 02:17:32 +0000170
Rafael Espindola7e5614e2015-05-27 14:45:54 +0000171 void defineSection(MCSectionCOFF const &Sec);
Rafael Espindola746036c2016-05-26 20:31:00 +0000172
173 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
Duncan P. N. Exon Smith96273f62015-05-20 19:34:08 +0000174 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Kleckner28860822013-09-17 23:18:05 +0000175 const MCAsmLayout &Layout);
Michael J. Spencer801a3592010-07-26 02:17:32 +0000176
Hans Wennborg4edcbae2014-09-29 22:43:20 +0000177 void SetSymbolName(COFFSymbol &S);
178 void SetSectionName(COFFSection &S);
Michael J. Spencera72d8782010-09-27 08:58:26 +0000179
Michael J. Spencera72d8782010-09-27 08:58:26 +0000180 bool IsPhysicalSection(COFFSection *S);
181
Michael J. Spencer801a3592010-07-26 02:17:32 +0000182 // Entity writing methods.
183
184 void WriteFileHeader(const COFF::header &Header);
David Blaikiea016c1d2014-04-15 05:25:03 +0000185 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencer801a3592010-07-26 02:17:32 +0000186 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
Rui Ueyamaa24db232017-02-17 17:32:54 +0000187 void writeSectionHeaders();
Michael J. Spencer801a3592010-07-26 02:17:32 +0000188 void WriteRelocation(const COFF::relocation &R);
Rui Ueyama458e2422017-02-16 02:56:06 +0000189 uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout,
190 const MCSection &MCSec);
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000191 void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
192 const COFFSection &Sec, const MCSection &MCSec);
Michael J. Spencer801a3592010-07-26 02:17:32 +0000193
194 // MCObjectWriter interface implementation.
195
Jim Grosbacheafe4652015-06-04 23:25:54 +0000196 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper5747a142014-03-08 07:02:02 +0000197 const MCAsmLayout &Layout) override;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000198
Jim Grosbachbc812862015-06-04 22:24:41 +0000199 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smith9e6378d2015-05-16 01:01:55 +0000200 const MCSymbol &SymA,
David Majnemer16d80312014-11-11 08:43:57 +0000201 const MCFragment &FB, bool InSet,
202 bool IsPCRel) const override;
203
Jim Grosbachbc812862015-06-04 22:24:41 +0000204 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper5747a142014-03-08 07:02:02 +0000205 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola210f5222017-07-11 23:56:10 +0000206 MCValue Target, uint64_t &FixedValue) override;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000207
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000208 void createFileSymbols(MCAssembler &Asm);
209 void assignSectionNumbers();
210 void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout);
211
Peter Collingbourne38b60af2018-08-22 23:58:16 +0000212 void emitAddrsigSection() override { EmitAddrsigSection = true; }
213 void addAddrsigSymbol(const MCSymbol *Sym) override {
214 AddrsigSyms.push_back(Sym);
215 }
216
Peter Collingbourne0d4292f2018-05-21 18:23:50 +0000217 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000218};
Eugene Zelenkof31871c2017-02-07 23:02:00 +0000219
220} // end anonymous namespace
Chris Lattnerb1622902010-07-11 22:07:02 +0000221
Michael J. Spencer801a3592010-07-26 02:17:32 +0000222//------------------------------------------------------------------------------
223// Symbol class implementation
224
Michael J. Spencer801a3592010-07-26 02:17:32 +0000225// In the case that the name does not fit within 8 bytes, the offset
226// into the string table is stored in the last 4 bytes instead, leaving
227// the first 4 bytes as 0.
228void COFFSymbol::set_name_offset(uint32_t Offset) {
Rui Ueyamad5265b302017-02-14 23:28:19 +0000229 write32le(Data.Name + 0, 0);
230 write32le(Data.Name + 4, Offset);
Michael J. Spencer801a3592010-07-26 02:17:32 +0000231}
232
233//------------------------------------------------------------------------------
Michael J. Spencer801a3592010-07-26 02:17:32 +0000234// WinCOFFObjectWriter class implementation
235
Lang Hames1d8cdbb2017-10-10 00:50:29 +0000236WinCOFFObjectWriter::WinCOFFObjectWriter(
237 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS)
Peter Collingbourne2e125e82018-05-21 18:28:57 +0000238 : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {
Rafael Espindoladf092702011-12-24 02:14:02 +0000239 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencer801a3592010-07-26 02:17:32 +0000240}
241
Michael J. Spencer4cee2892010-10-16 08:25:57 +0000242COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Rui Ueyama6b607dc2017-02-14 23:58:19 +0000243 Symbols.push_back(make_unique<COFFSymbol>(Name));
244 return Symbols.back().get();
Michael J. Spencer801a3592010-07-26 02:17:32 +0000245}
246
Yaron Kerenba8180b2014-12-04 08:30:39 +0000247COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Rui Ueyama7c1b5b02017-02-14 23:47:34 +0000248 COFFSymbol *&Ret = SymbolMap[Symbol];
249 if (!Ret)
Rui Ueyama6b607dc2017-02-14 23:58:19 +0000250 Ret = createSymbol(Symbol->getName());
Rui Ueyama7c1b5b02017-02-14 23:47:34 +0000251 return Ret;
Michael J. Spencer4cee2892010-10-16 08:25:57 +0000252}
253
Dmitri Gribenko96f498b2013-01-13 16:01:15 +0000254COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Rui Ueyama6b607dc2017-02-14 23:58:19 +0000255 Sections.emplace_back(make_unique<COFFSection>(Name));
256 return Sections.back().get();
Michael J. Spencer801a3592010-07-26 02:17:32 +0000257}
258
Rui Ueyama766de4d2017-02-15 00:15:54 +0000259static uint32_t getAlignment(const MCSectionCOFF &Sec) {
260 switch (Sec.getAlignment()) {
261 case 1:
262 return COFF::IMAGE_SCN_ALIGN_1BYTES;
263 case 2:
264 return COFF::IMAGE_SCN_ALIGN_2BYTES;
265 case 4:
266 return COFF::IMAGE_SCN_ALIGN_4BYTES;
267 case 8:
268 return COFF::IMAGE_SCN_ALIGN_8BYTES;
269 case 16:
270 return COFF::IMAGE_SCN_ALIGN_16BYTES;
271 case 32:
272 return COFF::IMAGE_SCN_ALIGN_32BYTES;
273 case 64:
274 return COFF::IMAGE_SCN_ALIGN_64BYTES;
275 case 128:
276 return COFF::IMAGE_SCN_ALIGN_128BYTES;
277 case 256:
278 return COFF::IMAGE_SCN_ALIGN_256BYTES;
279 case 512:
280 return COFF::IMAGE_SCN_ALIGN_512BYTES;
281 case 1024:
282 return COFF::IMAGE_SCN_ALIGN_1024BYTES;
283 case 2048:
284 return COFF::IMAGE_SCN_ALIGN_2048BYTES;
285 case 4096:
286 return COFF::IMAGE_SCN_ALIGN_4096BYTES;
287 case 8192:
288 return COFF::IMAGE_SCN_ALIGN_8192BYTES;
289 }
290 llvm_unreachable("unsupported section alignment");
291}
292
Michael J. Spencer801a3592010-07-26 02:17:32 +0000293/// This function takes a section data object from the assembler
294/// and creates the associated COFF section staging object.
Rui Ueyama52b91822017-02-15 00:28:48 +0000295void WinCOFFObjectWriter::defineSection(const MCSectionCOFF &MCSec) {
296 COFFSection *Section = createSection(MCSec.getSectionName());
297 COFFSymbol *Symbol = createSymbol(MCSec.getSectionName());
298 Section->Symbol = Symbol;
299 Symbol->Section = Section;
300 Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Rui Ueyama766de4d2017-02-15 00:15:54 +0000301
302 // Create a COMDAT symbol if needed.
Rui Ueyama52b91822017-02-15 00:28:48 +0000303 if (MCSec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
304 if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
Rafael Espindola013321a2014-06-06 19:26:12 +0000305 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
306 if (COMDATSymbol->Section)
307 report_fatal_error("two sections have the same comdat");
Rui Ueyama52b91822017-02-15 00:28:48 +0000308 COMDATSymbol->Section = Section;
Rafael Espindola013321a2014-06-06 19:26:12 +0000309 }
310 }
Michael J. Spencer801a3592010-07-26 02:17:32 +0000311
Michael J. Spencer801a3592010-07-26 02:17:32 +0000312 // In this case the auxiliary symbol is a Section Definition.
Rui Ueyama52b91822017-02-15 00:28:48 +0000313 Symbol->Aux.resize(1);
314 Symbol->Aux[0] = {};
315 Symbol->Aux[0].AuxType = ATSectionDefinition;
316 Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection();
Michael J. Spencer801a3592010-07-26 02:17:32 +0000317
Rui Ueyama52b91822017-02-15 00:28:48 +0000318 // Set section alignment.
319 Section->Header.Characteristics = MCSec.getCharacteristics();
320 Section->Header.Characteristics |= getAlignment(MCSec);
Michael J. Spencer801a3592010-07-26 02:17:32 +0000321
322 // Bind internal COFF section to MC section.
Rui Ueyama52b91822017-02-15 00:28:48 +0000323 Section->MCSection = &MCSec;
324 SectionMap[&MCSec] = Section;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000325}
326
Duncan P. N. Exon Smith96273f62015-05-20 19:34:08 +0000327static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolab77f8912014-05-01 00:10:17 +0000328 const MCAsmLayout &Layout) {
Rafael Espindolacfac75a2015-05-29 21:45:01 +0000329 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola82fdcc02015-05-29 17:48:04 +0000330 return Symbol.getCommonSize();
Rafael Espindolab77f8912014-05-01 00:10:17 +0000331
332 uint64_t Res;
Duncan P. N. Exon Smith96273f62015-05-20 19:34:08 +0000333 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolab77f8912014-05-01 00:10:17 +0000334 return 0;
335
336 return Res;
337}
338
Rafael Espindola746036c2016-05-26 20:31:00 +0000339COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
340 if (!Symbol.isVariable())
341 return nullptr;
342
343 const MCSymbolRefExpr *SymRef =
344 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
345 if (!SymRef)
346 return nullptr;
347
348 const MCSymbol &Aliasee = SymRef->getSymbol();
349 if (!Aliasee.isUndefined())
350 return nullptr;
351 return GetOrCreateCOFFSymbol(&Aliasee);
352}
353
David Majnemere4d89ec2014-04-08 22:33:40 +0000354/// This function takes a symbol data object from the assembler
Michael J. Spencer801a3592010-07-26 02:17:32 +0000355/// and creates the associated COFF symbol staging object.
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000356void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
Reid Kleckner28860822013-09-17 23:18:05 +0000357 MCAssembler &Assembler,
358 const MCAsmLayout &Layout) {
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000359 COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
360 const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
Rafael Espindola8d48aba2016-05-26 18:48:23 +0000361 COFFSection *Sec = nullptr;
362 if (Base && Base->getFragment()) {
363 Sec = SectionMap[Base->getFragment()->getParent()];
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000364 if (Sym->Section && Sym->Section != Sec)
Rafael Espindola8d48aba2016-05-26 18:48:23 +0000365 report_fatal_error("conflicting sections for symbol");
366 }
Michael J. Spencer801a3592010-07-26 02:17:32 +0000367
Rafael Espindola746036c2016-05-26 20:31:00 +0000368 COFFSymbol *Local = nullptr;
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000369 if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
370 Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
Michael J. Spencer4cee2892010-10-16 08:25:57 +0000371
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000372 COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
Rafael Espindola746036c2016-05-26 20:31:00 +0000373 if (!WeakDefault) {
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000374 std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
Rafael Espindola746036c2016-05-26 20:31:00 +0000375 WeakDefault = createSymbol(WeakName);
Rafael Espindola8d48aba2016-05-26 18:48:23 +0000376 if (!Sec)
377 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
378 else
379 WeakDefault->Section = Sec;
Rafael Espindola746036c2016-05-26 20:31:00 +0000380 Local = WeakDefault;
Michael J. Spencer4cee2892010-10-16 08:25:57 +0000381 }
382
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000383 Sym->Other = WeakDefault;
Rafael Espindola746036c2016-05-26 20:31:00 +0000384
Michael J. Spencer4cee2892010-10-16 08:25:57 +0000385 // Setup the Weak External auxiliary symbol.
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000386 Sym->Aux.resize(1);
387 memset(&Sym->Aux[0], 0, sizeof(Sym->Aux[0]));
388 Sym->Aux[0].AuxType = ATWeakExternal;
389 Sym->Aux[0].Aux.WeakExternal.TagIndex = 0;
390 Sym->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola1da0f912015-05-27 14:37:12 +0000391 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne4974b972013-04-22 18:48:56 +0000392 } else {
Rafael Espindola8d48aba2016-05-26 18:48:23 +0000393 if (!Base)
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000394 Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola8d48aba2016-05-26 18:48:23 +0000395 else
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000396 Sym->Section = Sec;
397 Local = Sym;
Michael J. Spencer4cee2892010-10-16 08:25:57 +0000398 }
Rafael Espindola746036c2016-05-26 20:31:00 +0000399
400 if (Local) {
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000401 Local->Data.Value = getSymbolValue(MCSym, Layout);
Rafael Espindola746036c2016-05-26 20:31:00 +0000402
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000403 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
Rafael Espindola746036c2016-05-26 20:31:00 +0000404 Local->Data.Type = SymbolCOFF.getType();
405 Local->Data.StorageClass = SymbolCOFF.getClass();
406
407 // If no storage class was specified in the streamer, define it here.
408 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000409 bool IsExternal = MCSym.isExternal() ||
410 (!MCSym.getFragment() && !MCSym.isVariable());
Rafael Espindola746036c2016-05-26 20:31:00 +0000411
412 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
413 : COFF::IMAGE_SYM_CLASS_STATIC;
414 }
415 }
416
Rui Ueyamafb44f2a2017-02-15 01:09:01 +0000417 Sym->MC = &MCSym;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000418}
419
Nico Rieck6c35c952014-02-25 09:50:40 +0000420// Maximum offsets for different string table entry encodings.
David Majnemera4c381a2016-01-24 20:46:11 +0000421enum : unsigned { Max7DecimalOffset = 9999999U };
422enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
Nico Rieck6c35c952014-02-25 09:50:40 +0000423
Nico Rieckfa3089b2014-02-22 16:12:20 +0000424// Encode a string table entry offset in base 64, padded to 6 chars, and
425// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
426// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola1da0f912015-05-27 14:37:12 +0000427static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck6c35c952014-02-25 09:50:40 +0000428 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieckfa3089b2014-02-22 16:12:20 +0000429 "Illegal section name encoding for value");
430
431 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
432 "abcdefghijklmnopqrstuvwxyz"
433 "0123456789+/";
434
435 Buffer[0] = '/';
436 Buffer[1] = '/';
437
Rafael Espindola1da0f912015-05-27 14:37:12 +0000438 char *Ptr = Buffer + 7;
Nico Rieckfa3089b2014-02-22 16:12:20 +0000439 for (unsigned i = 0; i < 6; ++i) {
440 unsigned Rem = Value % 64;
441 Value /= 64;
442 *(Ptr--) = Alphabet[Rem];
443 }
444}
445
Hans Wennborg4edcbae2014-09-29 22:43:20 +0000446void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Rui Ueyama070aca82017-02-15 01:09:20 +0000447 if (S.Name.size() <= COFF::NameSize) {
Michael J. Spencera72d8782010-09-27 08:58:26 +0000448 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Rui Ueyama070aca82017-02-15 01:09:20 +0000449 return;
David Majnemera4c381a2016-01-24 20:46:11 +0000450 }
Rui Ueyama070aca82017-02-15 01:09:20 +0000451
452 uint64_t StringTableEntry = Strings.getOffset(S.Name);
453 if (StringTableEntry <= Max7DecimalOffset) {
Rui Ueyamada87ca02017-02-15 01:48:33 +0000454 SmallVector<char, COFF::NameSize> Buffer;
455 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
456 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
457 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
Rui Ueyama070aca82017-02-15 01:09:20 +0000458 return;
459 }
460 if (StringTableEntry <= MaxBase64Offset) {
461 // Starting with 10,000,000, offsets are encoded as base64.
462 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
463 return;
464 }
465 report_fatal_error("COFF string table is greater than 64 GB.");
Michael J. Spencera72d8782010-09-27 08:58:26 +0000466}
467
Hans Wennborg4edcbae2014-09-29 22:43:20 +0000468void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
469 if (S.Name.size() > COFF::NameSize)
470 S.set_name_offset(Strings.getOffset(S.Name));
471 else
Michael J. Spencera72d8782010-09-27 08:58:26 +0000472 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencera72d8782010-09-27 08:58:26 +0000473}
474
Michael J. Spencera72d8782010-09-27 08:58:26 +0000475bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola1da0f912015-05-27 14:37:12 +0000476 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
477 0;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000478}
479
480//------------------------------------------------------------------------------
481// entity writing methods
482
483void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemerf1198da2014-09-15 19:42:42 +0000484 if (UseBigObj) {
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000485 W.write<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
486 W.write<uint16_t>(0xFFFF);
487 W.write<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion);
488 W.write<uint16_t>(Header.Machine);
489 W.write<uint32_t>(Header.TimeDateStamp);
490 W.OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic));
491 W.write<uint32_t>(0);
492 W.write<uint32_t>(0);
493 W.write<uint32_t>(0);
494 W.write<uint32_t>(0);
495 W.write<uint32_t>(Header.NumberOfSections);
496 W.write<uint32_t>(Header.PointerToSymbolTable);
497 W.write<uint32_t>(Header.NumberOfSymbols);
David Majnemerf1198da2014-09-15 19:42:42 +0000498 } else {
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000499 W.write<uint16_t>(Header.Machine);
500 W.write<uint16_t>(static_cast<int16_t>(Header.NumberOfSections));
501 W.write<uint32_t>(Header.TimeDateStamp);
502 W.write<uint32_t>(Header.PointerToSymbolTable);
503 W.write<uint32_t>(Header.NumberOfSymbols);
504 W.write<uint16_t>(Header.SizeOfOptionalHeader);
505 W.write<uint16_t>(Header.Characteristics);
David Majnemerf1198da2014-09-15 19:42:42 +0000506 }
Michael J. Spencer801a3592010-07-26 02:17:32 +0000507}
508
David Blaikiea016c1d2014-04-15 05:25:03 +0000509void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000510 W.OS.write(S.Data.Name, COFF::NameSize);
511 W.write<uint32_t>(S.Data.Value);
David Majnemerf1198da2014-09-15 19:42:42 +0000512 if (UseBigObj)
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000513 W.write<uint32_t>(S.Data.SectionNumber);
David Majnemerf1198da2014-09-15 19:42:42 +0000514 else
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000515 W.write<uint16_t>(static_cast<int16_t>(S.Data.SectionNumber));
516 W.write<uint16_t>(S.Data.Type);
517 W.OS << char(S.Data.StorageClass);
518 W.OS << char(S.Data.NumberOfAuxSymbols);
David Blaikiea016c1d2014-04-15 05:25:03 +0000519 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencer801a3592010-07-26 02:17:32 +0000520}
521
522void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola1da0f912015-05-27 14:37:12 +0000523 const COFFSymbol::AuxiliarySymbols &S) {
Benjamin Kramerdd1d7a42016-06-26 14:49:00 +0000524 for (const AuxSymbol &i : S) {
525 switch (i.AuxType) {
Michael J. Spencer801a3592010-07-26 02:17:32 +0000526 case ATWeakExternal:
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000527 W.write<uint32_t>(i.Aux.WeakExternal.TagIndex);
528 W.write<uint32_t>(i.Aux.WeakExternal.Characteristics);
529 W.OS.write_zeros(sizeof(i.Aux.WeakExternal.unused));
David Majnemerf1198da2014-09-15 19:42:42 +0000530 if (UseBigObj)
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000531 W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencer801a3592010-07-26 02:17:32 +0000532 break;
533 case ATFile:
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000534 W.OS.write(reinterpret_cast<const char *>(&i.Aux),
535 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size);
Michael J. Spencer801a3592010-07-26 02:17:32 +0000536 break;
537 case ATSectionDefinition:
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000538 W.write<uint32_t>(i.Aux.SectionDefinition.Length);
539 W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfRelocations);
540 W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfLinenumbers);
541 W.write<uint32_t>(i.Aux.SectionDefinition.CheckSum);
542 W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
543 W.OS << char(i.Aux.SectionDefinition.Selection);
544 W.OS.write_zeros(sizeof(i.Aux.SectionDefinition.unused));
545 W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
David Majnemerf1198da2014-09-15 19:42:42 +0000546 if (UseBigObj)
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000547 W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencer801a3592010-07-26 02:17:32 +0000548 break;
549 }
550 }
551}
552
Rui Ueyamaa24db232017-02-17 17:32:54 +0000553// Write the section header.
554void WinCOFFObjectWriter::writeSectionHeaders() {
555 // Section numbers must be monotonically increasing in the section
556 // header, but our Sections array is not sorted by section number,
557 // so make a copy of Sections and sort it.
558 std::vector<COFFSection *> Arr;
559 for (auto &Section : Sections)
560 Arr.push_back(Section.get());
Fangrui Song3b35e172018-09-27 02:13:45 +0000561 llvm::sort(Arr, [](const COFFSection *A, const COFFSection *B) {
562 return A->Number < B->Number;
563 });
Michael J. Spencer801a3592010-07-26 02:17:32 +0000564
Rui Ueyamaa24db232017-02-17 17:32:54 +0000565 for (auto &Section : Arr) {
566 if (Section->Number == -1)
567 continue;
568
569 COFF::section &S = Section->Header;
570 if (Section->Relocations.size() >= 0xffff)
571 S.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000572 W.OS.write(S.Name, COFF::NameSize);
573 W.write<uint32_t>(S.VirtualSize);
574 W.write<uint32_t>(S.VirtualAddress);
575 W.write<uint32_t>(S.SizeOfRawData);
576 W.write<uint32_t>(S.PointerToRawData);
577 W.write<uint32_t>(S.PointerToRelocations);
578 W.write<uint32_t>(S.PointerToLineNumbers);
579 W.write<uint16_t>(S.NumberOfRelocations);
580 W.write<uint16_t>(S.NumberOfLineNumbers);
581 W.write<uint32_t>(S.Characteristics);
Rui Ueyamaa24db232017-02-17 17:32:54 +0000582 }
Michael J. Spencer801a3592010-07-26 02:17:32 +0000583}
584
585void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000586 W.write<uint32_t>(R.VirtualAddress);
587 W.write<uint32_t>(R.SymbolTableIndex);
588 W.write<uint16_t>(R.Type);
Chris Lattnerb1622902010-07-11 22:07:02 +0000589}
590
Rui Ueyama458e2422017-02-16 02:56:06 +0000591// Write MCSec's contents. What this function does is essentially
592// "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
593// because it needs to compute a CRC.
594uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler &Asm,
595 const MCAsmLayout &Layout,
596 const MCSection &MCSec) {
597 // Save the contents of the section to a temporary buffer, we need this
598 // to CRC the data before we dump it into the object file.
599 SmallVector<char, 128> Buf;
600 raw_svector_ostream VecOS(Buf);
Peter Collingbourne74bda0b2018-05-21 18:11:35 +0000601 Asm.writeSectionData(VecOS, &MCSec, Layout);
Rui Ueyama458e2422017-02-16 02:56:06 +0000602
603 // Write the section contents to the object file.
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000604 W.OS << Buf;
Rui Ueyama458e2422017-02-16 02:56:06 +0000605
606 // Calculate our CRC with an initial value of '0', this is not how
607 // JamCRC is specified but it aligns with the expected output.
608 JamCRC JC(/*Init=*/0);
609 JC.update(Buf);
610 return JC.getCRC();
611}
612
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000613void WinCOFFObjectWriter::writeSection(MCAssembler &Asm,
614 const MCAsmLayout &Layout,
615 const COFFSection &Sec,
616 const MCSection &MCSec) {
617 if (Sec.Number == -1)
618 return;
619
Rui Ueyama458e2422017-02-16 02:56:06 +0000620 // Write the section contents.
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000621 if (Sec.Header.PointerToRawData != 0) {
Peter Collingbourne18532182018-08-23 05:39:36 +0000622 assert(W.OS.tell() == Sec.Header.PointerToRawData &&
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000623 "Section::PointerToRawData is insane!");
624
Rui Ueyama458e2422017-02-16 02:56:06 +0000625 uint32_t CRC = writeSectionContents(Asm, Layout, MCSec);
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000626
627 // Update the section definition auxiliary symbol to record the CRC.
628 COFFSection *Sec = SectionMap[&MCSec];
629 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
630 assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
631 AuxSymbol &SecDef = AuxSyms[0];
Rui Ueyama458e2422017-02-16 02:56:06 +0000632 SecDef.Aux.SectionDefinition.CheckSum = CRC;
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000633 }
634
Rui Ueyama458e2422017-02-16 02:56:06 +0000635 // Write relocations for this section.
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000636 if (Sec.Relocations.empty()) {
637 assert(Sec.Header.PointerToRelocations == 0 &&
638 "Section::PointerToRelocations is insane!");
639 return;
640 }
641
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000642 assert(W.OS.tell() == Sec.Header.PointerToRelocations &&
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000643 "Section::PointerToRelocations is insane!");
644
645 if (Sec.Relocations.size() >= 0xffff) {
646 // In case of overflow, write actual relocation count as first
647 // relocation. Including the synthetic reloc itself (+ 1).
648 COFF::relocation R;
649 R.VirtualAddress = Sec.Relocations.size() + 1;
650 R.SymbolTableIndex = 0;
651 R.Type = 0;
652 WriteRelocation(R);
653 }
654
655 for (const auto &Relocation : Sec.Relocations)
656 WriteRelocation(Relocation.Data);
657}
658
Chris Lattnerb1622902010-07-11 22:07:02 +0000659////////////////////////////////////////////////////////////////////////////////
660// MCObjectWriter interface implementations
661
Jim Grosbacheafe4652015-06-04 23:25:54 +0000662void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000663 const MCAsmLayout &Layout) {
Peter Collingbourne38b60af2018-08-22 23:58:16 +0000664 if (EmitAddrsigSection) {
665 AddrsigSection = Asm.getContext().getCOFFSection(
666 ".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE,
667 SectionKind::getMetadata());
668 Asm.registerSection(*AddrsigSection);
669 }
670
Michael J. Spencer801a3592010-07-26 02:17:32 +0000671 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencera72d8782010-09-27 08:58:26 +0000672 // entries in the staging area.
Yaron Kerenba8180b2014-12-04 08:30:39 +0000673 for (const auto &Section : Asm)
Rafael Espindola7e5614e2015-05-27 14:45:54 +0000674 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencer801a3592010-07-26 02:17:32 +0000675
Duncan P. N. Exon Smitha5bb8422015-05-16 00:35:24 +0000676 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne41984d32015-11-26 23:29:27 +0000677 if (!Symbol.isTemporary())
Duncan P. N. Exon Smith96273f62015-05-20 19:34:08 +0000678 DefineSymbol(Symbol, Asm, Layout);
Chris Lattnerb1622902010-07-11 22:07:02 +0000679}
680
Jim Grosbachbc812862015-06-04 22:24:41 +0000681bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smith9e6378d2015-05-16 01:01:55 +0000682 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindolafb118bd2015-04-17 21:15:17 +0000683 bool InSet, bool IsPCRel) const {
Reid Kleckner92329722018-03-14 19:24:32 +0000684 // Don't drop relocations between functions, even if they are in the same text
685 // section. Multiple Visual C++ linker features depend on having the
686 // relocations present. The /INCREMENTAL flag will cause these relocations to
687 // point to thunks, and the /GUARD:CF flag assumes that it can use relocations
688 // to approximate the set of all address taken functions. LLD's implementation
689 // of /GUARD:CF also relies on the existance of these relocations.
Pete Cooperf560b882015-06-08 17:17:12 +0000690 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
Reid Kleckner92329722018-03-14 19:24:32 +0000691 if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer16d80312014-11-11 08:43:57 +0000692 return false;
Jim Grosbachbc812862015-06-04 22:24:41 +0000693 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindolafb118bd2015-04-17 21:15:17 +0000694 InSet, IsPCRel);
David Majnemer16d80312014-11-11 08:43:57 +0000695}
696
Rafael Espindola210f5222017-07-11 23:56:10 +0000697void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
698 const MCAsmLayout &Layout,
699 const MCFragment *Fragment,
700 const MCFixup &Fixup, MCValue Target,
701 uint64_t &FixedValue) {
Craig Topper4266ae82014-04-13 04:57:38 +0000702 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencer82c84fd2010-08-24 21:04:52 +0000703
Peter Collingbourne41984d32015-11-26 23:29:27 +0000704 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard99ab1122015-11-17 10:00:43 +0000705 if (!A.isRegistered()) {
706 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola1da0f912015-05-27 14:37:12 +0000707 Twine("symbol '") + A.getName() +
708 "' can not be undefined");
Oliver Stannard99ab1122015-11-17 10:00:43 +0000709 return;
710 }
Reid Klecknercba458e2015-09-16 16:26:29 +0000711 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard99ab1122015-11-17 10:00:43 +0000712 Asm.getContext().reportError(Fixup.getLoc(),
Reid Klecknercba458e2015-09-16 16:26:29 +0000713 Twine("assembler label '") + A.getName() +
714 "' can not be undefined");
Oliver Stannard99ab1122015-11-17 10:00:43 +0000715 return;
Reid Klecknercba458e2015-09-16 16:26:29 +0000716 }
Timur Iskhodzhanov4e54e6f2014-01-30 21:13:05 +0000717
Rui Ueyama5ececce2017-02-16 01:06:45 +0000718 MCSection *MCSec = Fragment->getParent();
Michael J. Spencer801a3592010-07-26 02:17:32 +0000719
Michael J. Spencer82c84fd2010-08-24 21:04:52 +0000720 // Mark this symbol as requiring an entry in the symbol table.
Rui Ueyama5ececce2017-02-16 01:06:45 +0000721 assert(SectionMap.find(MCSec) != SectionMap.end() &&
Jim Grosbacheafe4652015-06-04 23:25:54 +0000722 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencer801a3592010-07-26 02:17:32 +0000723
Rui Ueyama5ececce2017-02-16 01:06:45 +0000724 COFFSection *Sec = SectionMap[MCSec];
Rafael Espindola3660a842011-04-20 14:01:45 +0000725 const MCSymbolRefExpr *SymB = Target.getSymB();
Michael J. Spencer801a3592010-07-26 02:17:32 +0000726
David Majnemerba754832014-01-06 07:39:46 +0000727 if (SymB) {
728 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard99ab1122015-11-17 10:00:43 +0000729 if (!B->getFragment()) {
730 Asm.getContext().reportError(
David Majnemerba754832014-01-06 07:39:46 +0000731 Fixup.getLoc(),
732 Twine("symbol '") + B->getName() +
733 "' can not be undefined in a subtraction expression");
Oliver Stannard99ab1122015-11-17 10:00:43 +0000734 return;
735 }
David Majnemerba754832014-01-06 07:39:46 +0000736
Rafael Espindola1ac7fe02011-04-21 18:36:50 +0000737 // Offset of the symbol in the section
Duncan P. N. Exon Smithe1fce862015-05-19 23:53:20 +0000738 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencer82c84fd2010-08-24 21:04:52 +0000739
David Majnemer3c1b9d52015-02-09 06:31:31 +0000740 // Offset of the relocation in the section
741 int64_t OffsetOfRelocation =
742 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
743
Andy Ayersedd2bb82015-05-14 01:10:41 +0000744 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencer82c84fd2010-08-24 21:04:52 +0000745 } else {
746 FixedValue = Target.getConstant();
747 }
Michael J. Spencer801a3592010-07-26 02:17:32 +0000748
749 COFFRelocation Reloc;
750
Daniel Dunbar425f6342010-07-31 21:08:54 +0000751 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000752 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencera72d8782010-09-27 08:58:26 +0000753
Michael J. Spencerea1104a2010-10-05 19:48:12 +0000754 // Turn relocations for temporary symbols into section relocations.
Rafael Espindolaf2891be2017-06-22 21:57:04 +0000755 if (A.isTemporary()) {
Peter Collingbourne41984d32015-11-26 23:29:27 +0000756 MCSection *TargetSection = &A.getSection();
757 assert(
758 SectionMap.find(TargetSection) != SectionMap.end() &&
759 "Section must already have been defined in executePostLayoutBinding!");
760 Reloc.Symb = SectionMap[TargetSection]->Symbol;
761 FixedValue += Layout.getSymbolOffset(A);
762 } else {
763 assert(
764 SymbolMap.find(&A) != SymbolMap.end() &&
765 "Symbol must already have been defined in executePostLayoutBinding!");
766 Reloc.Symb = SymbolMap[&A];
767 }
Michael J. Spencera72d8782010-09-27 08:58:26 +0000768
769 ++Reloc.Symb->Relocations;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000770
771 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindolaef429082017-06-23 04:07:44 +0000772 Reloc.Data.Type = TargetObjectWriter->getRelocType(
773 Asm.getContext(), Target, Fixup, SymB, Asm.getBackend());
Rafael Espindolab156c5d2011-12-22 22:21:47 +0000774
775 // FIXME: Can anyone explain what this does other than adjust for the size
776 // of the offset?
Saleem Abdulrasool42ad5102014-04-13 20:47:55 +0000777 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
778 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
779 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
780 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencer82c84fd2010-08-24 21:04:52 +0000781 FixedValue += 4;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000782
Saleem Abdulrasool2d0d7fd2014-04-27 03:48:22 +0000783 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
784 switch (Reloc.Data.Type) {
785 case COFF::IMAGE_REL_ARM_ABSOLUTE:
786 case COFF::IMAGE_REL_ARM_ADDR32:
787 case COFF::IMAGE_REL_ARM_ADDR32NB:
788 case COFF::IMAGE_REL_ARM_TOKEN:
789 case COFF::IMAGE_REL_ARM_SECTION:
790 case COFF::IMAGE_REL_ARM_SECREL:
791 break;
792 case COFF::IMAGE_REL_ARM_BRANCH11:
793 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola1da0f912015-05-27 14:37:12 +0000794 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
795 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
796 // for Windows CE).
Saleem Abdulrasool2d0d7fd2014-04-27 03:48:22 +0000797 case COFF::IMAGE_REL_ARM_BRANCH24:
798 case COFF::IMAGE_REL_ARM_BLX24:
799 case COFF::IMAGE_REL_ARM_MOV32A:
800 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
801 // only used for ARM mode code, which is documented as being unsupported
Alp Toker727273b2014-05-15 01:52:21 +0000802 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool2d0d7fd2014-04-27 03:48:22 +0000803 // generate the relocations however the rest of the MSVC toolchain is
804 // unable to handle it.
805 llvm_unreachable("unsupported relocation");
806 break;
807 case COFF::IMAGE_REL_ARM_MOV32T:
808 break;
809 case COFF::IMAGE_REL_ARM_BRANCH20T:
810 case COFF::IMAGE_REL_ARM_BRANCH24T:
811 case COFF::IMAGE_REL_ARM_BLX23T:
812 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
813 // perform a 4 byte adjustment to the relocation. Relative branches are
814 // offset by 4 on ARM, however, because there is no RELA relocations, all
815 // branches are offset by 4.
816 FixedValue = FixedValue + 4;
817 break;
818 }
819 }
820
Simon Pilgrim84d1e912016-11-20 13:47:59 +0000821 // The fixed value never makes sense for section indices, ignore it.
David Majnemer7ddc5472016-02-05 01:55:49 +0000822 if (Fixup.getKind() == FK_SecRel_2)
823 FixedValue = 0;
824
Saleem Abdulrasool159ccc82014-05-21 23:17:50 +0000825 if (TargetObjectWriter->recordRelocation(Fixup))
Rui Ueyama5ececce2017-02-16 01:06:45 +0000826 Sec->Relocations.push_back(Reloc);
827}
828
829static std::time_t getTime() {
830 std::time_t Now = time(nullptr);
831 if (Now < 0 || !isUInt<32>(Now))
832 return UINT32_MAX;
833 return Now;
Chris Lattnerb1622902010-07-11 22:07:02 +0000834}
835
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000836// Create .file symbols.
837void WinCOFFObjectWriter::createFileSymbols(MCAssembler &Asm) {
Rafael Espindolafd64e0f2015-05-28 18:03:20 +0000838 for (const std::string &Name : Asm.getFileNames()) {
David Majnemerf1198da2014-09-15 19:42:42 +0000839 // round up to calculate the number of auxiliary symbols required
840 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindolafd64e0f2015-05-28 18:03:20 +0000841 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemerf1198da2014-09-15 19:42:42 +0000842
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000843 COFFSymbol *File = createSymbol(".file");
844 File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
845 File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
846 File->Aux.resize(Count);
David Majnemerf1198da2014-09-15 19:42:42 +0000847
848 unsigned Offset = 0;
Rafael Espindolafd64e0f2015-05-28 18:03:20 +0000849 unsigned Length = Name.size();
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000850 for (auto &Aux : File->Aux) {
David Majnemerf1198da2014-09-15 19:42:42 +0000851 Aux.AuxType = ATFile;
852
853 if (Length > SymbolSize) {
Rafael Espindolafd64e0f2015-05-28 18:03:20 +0000854 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemerf1198da2014-09-15 19:42:42 +0000855 Length = Length - SymbolSize;
856 } else {
Rafael Espindolafd64e0f2015-05-28 18:03:20 +0000857 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemerf1198da2014-09-15 19:42:42 +0000858 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
859 break;
860 }
861
862 Offset += SymbolSize;
863 }
864 }
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000865}
866
Rui Ueyamaa24db232017-02-17 17:32:54 +0000867static bool isAssociative(const COFFSection &Section) {
868 return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
869 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
870}
871
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000872void WinCOFFObjectWriter::assignSectionNumbers() {
873 size_t I = 1;
Rui Ueyamaa24db232017-02-17 17:32:54 +0000874 auto Assign = [&](COFFSection &Section) {
875 Section.Number = I;
876 Section.Symbol->Data.SectionNumber = I;
877 Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I;
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000878 ++I;
Rui Ueyamaa24db232017-02-17 17:32:54 +0000879 };
880
881 // Although it is not explicitly requested by the Microsoft COFF spec,
882 // we should avoid emitting forward associative section references,
883 // because MSVC link.exe as of 2017 cannot handle that.
884 for (const std::unique_ptr<COFFSection> &Section : Sections)
885 if (!isAssociative(*Section))
886 Assign(*Section);
887 for (const std::unique_ptr<COFFSection> &Section : Sections)
888 if (isAssociative(*Section))
889 Assign(*Section);
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000890}
891
892// Assign file offsets to COFF object file structures.
893void WinCOFFObjectWriter::assignFileOffsets(MCAssembler &Asm,
894 const MCAsmLayout &Layout) {
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000895 unsigned Offset = W.OS.tell();
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000896
897 Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
898 Offset += COFF::SectionSize * Header.NumberOfSections;
899
900 for (const auto &Section : Asm) {
901 COFFSection *Sec = SectionMap[&Section];
902
903 if (Sec->Number == -1)
904 continue;
905
906 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
907
908 if (IsPhysicalSection(Sec)) {
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000909 Sec->Header.PointerToRawData = Offset;
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000910 Offset += Sec->Header.SizeOfRawData;
911 }
912
913 if (!Sec->Relocations.empty()) {
914 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
915
916 if (RelocationsOverflow) {
917 // Signal overflow by setting NumberOfRelocations to max value. Actual
918 // size is found in reloc #0. Microsoft tools understand this.
919 Sec->Header.NumberOfRelocations = 0xffff;
920 } else {
921 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
922 }
923 Sec->Header.PointerToRelocations = Offset;
924
925 if (RelocationsOverflow) {
926 // Reloc #0 will contain actual count, so make room for it.
927 Offset += COFF::RelocationSize;
928 }
929
930 Offset += COFF::RelocationSize * Sec->Relocations.size();
931
932 for (auto &Relocation : Sec->Relocations) {
933 assert(Relocation.Symb->getIndex() != -1);
934 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
935 }
936 }
937
938 assert(Sec->Symbol->Aux.size() == 1 &&
939 "Section's symbol must have one aux!");
940 AuxSymbol &Aux = Sec->Symbol->Aux[0];
941 assert(Aux.AuxType == ATSectionDefinition &&
942 "Section's symbol's aux symbol must be a Section Definition!");
943 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
944 Aux.Aux.SectionDefinition.NumberOfRelocations =
945 Sec->Header.NumberOfRelocations;
946 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
947 Sec->Header.NumberOfLineNumbers;
948 }
949
950 Header.PointerToSymbolTable = Offset;
951}
952
Peter Collingbourne0d4292f2018-05-21 18:23:50 +0000953uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
954 const MCAsmLayout &Layout) {
955 uint64_t StartOffset = W.OS.tell();
956
Rui Ueyama1e5304f2017-02-16 02:35:48 +0000957 if (Sections.size() > INT32_MAX)
958 report_fatal_error(
959 "PE COFF object files can't have more than 2147483647 sections");
960
961 UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
962 Header.NumberOfSections = Sections.size();
963 Header.NumberOfSymbols = 0;
964
965 assignSectionNumbers();
966 createFileSymbols(Asm);
David Majnemerf1198da2014-09-15 19:42:42 +0000967
David Majnemer52838292014-08-28 04:02:50 +0000968 for (auto &Symbol : Symbols) {
Michael J. Spencer801a3592010-07-26 02:17:32 +0000969 // Update section number & offset for symbols that have them.
Rafael Espindola23d27592014-05-01 13:37:57 +0000970 if (Symbol->Section)
Saleem Abdulrasool1c5e4e52014-04-28 03:34:48 +0000971 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne41984d32015-11-26 23:29:27 +0000972 Symbol->setIndex(Header.NumberOfSymbols++);
973 // Update auxiliary symbol info.
974 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
975 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencer801a3592010-07-26 02:17:32 +0000976 }
977
Hans Wennborg4edcbae2014-09-29 22:43:20 +0000978 // Build string table.
979 for (const auto &S : Sections)
980 if (S->Name.size() > COFF::NameSize)
981 Strings.add(S->Name);
982 for (const auto &S : Symbols)
Peter Collingbourne41984d32015-11-26 23:29:27 +0000983 if (S->Name.size() > COFF::NameSize)
Hans Wennborg4edcbae2014-09-29 22:43:20 +0000984 Strings.add(S->Name);
Rafael Espindola715bcca2015-10-23 21:48:05 +0000985 Strings.finalize();
Hans Wennborg4edcbae2014-09-29 22:43:20 +0000986
987 // Set names.
988 for (const auto &S : Sections)
989 SetSectionName(*S);
990 for (auto &S : Symbols)
Peter Collingbourne41984d32015-11-26 23:29:27 +0000991 SetSymbolName(*S);
Hans Wennborg4edcbae2014-09-29 22:43:20 +0000992
Michael J. Spencer801a3592010-07-26 02:17:32 +0000993 // Fixup weak external references.
Yaron Kerenba8180b2014-12-04 08:30:39 +0000994 for (auto &Symbol : Symbols) {
Saleem Abdulrasool1c5e4e52014-04-28 03:34:48 +0000995 if (Symbol->Other) {
David Majnemere0d2a292015-05-30 04:56:02 +0000996 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool1c5e4e52014-04-28 03:34:48 +0000997 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
998 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencer801a3592010-07-26 02:17:32 +0000999 "Symbol's aux symbol must be a Weak External!");
David Majnemere0d2a292015-05-30 04:56:02 +00001000 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencer801a3592010-07-26 02:17:32 +00001001 }
1002 }
1003
Nico Rieck80646282013-07-06 12:13:10 +00001004 // Fixup associative COMDAT sections.
Yaron Kerenba8180b2014-12-04 08:30:39 +00001005 for (auto &Section : Sections) {
Saleem Abdulrasool1c5e4e52014-04-28 03:34:48 +00001006 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Rieck80646282013-07-06 12:13:10 +00001007 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1008 continue;
1009
Rafael Espindola7e5614e2015-05-27 14:45:54 +00001010 const MCSectionCOFF &MCSec = *Section->MCSection;
Reid Klecknera9c19262018-08-16 21:34:41 +00001011 const MCSymbol *AssocMCSym = MCSec.getCOMDATSymbol();
1012 assert(AssocMCSym);
Nico Rieck80646282013-07-06 12:13:10 +00001013
Reid Klecknera9c19262018-08-16 21:34:41 +00001014 // It's an error to try to associate with an undefined symbol or a symbol
1015 // without a section.
1016 if (!AssocMCSym->isInSection()) {
1017 Asm.getContext().reportError(
1018 SMLoc(), Twine("cannot make section ") + MCSec.getSectionName() +
1019 Twine(" associative with sectionless symbol ") +
1020 AssocMCSym->getName());
1021 continue;
1022 }
1023
1024 const auto *AssocMCSec = cast<MCSectionCOFF>(&AssocMCSym->getSection());
1025 assert(SectionMap.count(AssocMCSec));
1026 COFFSection *AssocSec = SectionMap[AssocMCSec];
Nico Rieck80646282013-07-06 12:13:10 +00001027
1028 // Skip this section if the associated section is unused.
Reid Klecknera9c19262018-08-16 21:34:41 +00001029 if (AssocSec->Number == -1)
Nico Rieck80646282013-07-06 12:13:10 +00001030 continue;
1031
Reid Klecknera9c19262018-08-16 21:34:41 +00001032 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = AssocSec->Number;
Nico Rieck80646282013-07-06 12:13:10 +00001033 }
1034
Peter Collingbourne38b60af2018-08-22 23:58:16 +00001035 // Create the contents of the .llvm_addrsig section.
1036 if (EmitAddrsigSection) {
1037 auto Frag = new MCDataFragment(AddrsigSection);
Peter Collingbourne4662b472018-08-23 06:57:49 +00001038 Frag->setLayoutOrder(0);
Peter Collingbourne38b60af2018-08-22 23:58:16 +00001039 raw_svector_ostream OS(Frag->getContents());
1040 for (const MCSymbol *S : AddrsigSyms) {
1041 if (!S->isTemporary()) {
1042 encodeULEB128(S->getIndex(), OS);
1043 continue;
1044 }
1045
1046 MCSection *TargetSection = &S->getSection();
1047 assert(SectionMap.find(TargetSection) != SectionMap.end() &&
1048 "Section must already have been defined in "
1049 "executePostLayoutBinding!");
1050 encodeULEB128(SectionMap[TargetSection]->Symbol->getIndex(), OS);
1051 }
1052 }
1053
Rui Ueyama1e5304f2017-02-16 02:35:48 +00001054 assignFileOffsets(Asm, Layout);
Michael J. Spencer801a3592010-07-26 02:17:32 +00001055
David Majnemer6ad37ec2015-09-01 23:46:11 +00001056 // MS LINK expects to be able to use this timestamp to implement their
1057 // /INCREMENTAL feature.
David Majnemer56afa6e2015-12-21 22:09:27 +00001058 if (Asm.isIncrementalLinkerCompatible()) {
Rui Ueyama5ececce2017-02-16 01:06:45 +00001059 Header.TimeDateStamp = getTime();
David Majnemer56afa6e2015-12-21 22:09:27 +00001060 } else {
Nico Weberd8f6d5c2016-01-06 19:05:19 +00001061 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer56afa6e2015-12-21 22:09:27 +00001062 Header.TimeDateStamp = 0;
1063 }
Michael J. Spencerab3de492010-08-03 04:43:33 +00001064
Michael J. Spencer801a3592010-07-26 02:17:32 +00001065 // Write it all to disk...
1066 WriteFileHeader(Header);
Rui Ueyamaa24db232017-02-17 17:32:54 +00001067 writeSectionHeaders();
Michael J. Spencer801a3592010-07-26 02:17:32 +00001068
Rui Ueyama1e5304f2017-02-16 02:35:48 +00001069 // Write section contents.
1070 sections::iterator I = Sections.begin();
1071 sections::iterator IE = Sections.end();
1072 MCAssembler::iterator J = Asm.begin();
1073 MCAssembler::iterator JE = Asm.end();
1074 for (; I != IE && J != JE; ++I, ++J)
1075 writeSection(Asm, Layout, **I, *J);
Michael J. Spencer801a3592010-07-26 02:17:32 +00001076
Peter Collingbourne8e93a952018-05-21 18:17:42 +00001077 assert(W.OS.tell() == Header.PointerToSymbolTable &&
Michael J. Spencer801a3592010-07-26 02:17:32 +00001078 "Header::PointerToSymbolTable is insane!");
1079
Rui Ueyama1e5304f2017-02-16 02:35:48 +00001080 // Write a symbol table.
Yaron Kerenba8180b2014-12-04 08:30:39 +00001081 for (auto &Symbol : Symbols)
David Majnemere0d2a292015-05-30 04:56:02 +00001082 if (Symbol->getIndex() != -1)
Saleem Abdulrasool1c5e4e52014-04-28 03:34:48 +00001083 WriteSymbol(*Symbol);
Michael J. Spencer801a3592010-07-26 02:17:32 +00001084
Rui Ueyama1e5304f2017-02-16 02:35:48 +00001085 // Write a string table, which completes the entire COFF file.
Peter Collingbourne8e93a952018-05-21 18:17:42 +00001086 Strings.write(W.OS);
Peter Collingbourne0d4292f2018-05-21 18:23:50 +00001087
1088 return W.OS.tell() - StartOffset;
Chris Lattnerb1622902010-07-11 22:07:02 +00001089}
1090
Rafael Espindola1da0f912015-05-27 14:37:12 +00001091MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1092 : Machine(Machine_) {}
Rafael Espindoladf092702011-12-24 02:14:02 +00001093
Juergen Ributzka35436252013-11-19 00:57:56 +00001094// Pin the vtable to this file.
1095void MCWinCOFFObjectTargetWriter::anchor() {}
1096
Chris Lattnerb1622902010-07-11 22:07:02 +00001097//------------------------------------------------------------------------------
1098// WinCOFFObjectWriter factory function
1099
Lang Hamese4713462017-10-10 16:28:07 +00001100std::unique_ptr<MCObjectWriter> llvm::createWinCOFFObjectWriter(
Lang Hames1d8cdbb2017-10-10 00:50:29 +00001101 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) {
Lang Hamese4713462017-10-10 16:28:07 +00001102 return llvm::make_unique<WinCOFFObjectWriter>(std::move(MOTW), OS);
Michael J. Spencer933304e2010-07-26 03:01:28 +00001103}