blob: 3ec1c09260053ec664b7670cf04ccde9e72c25e1 [file] [log] [blame]
Eugene Zelenko1d435522017-02-07 23:02:00 +00001//===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
Chris Lattner2c52b792010-07-11 22:07:02 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains an implementation of a Win32 COFF object file writer.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000014#include "llvm/ADT/DenseMap.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/ADT/STLExtras.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/SmallVector.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000018#include "llvm/ADT/StringRef.h"
Nico Riecka37acf72013-07-06 12:13:10 +000019#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000020#include "llvm/BinaryFormat/COFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCAsmLayout.h"
22#include "llvm/MC/MCAssembler.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCExpr.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000025#include "llvm/MC/MCFixup.h"
26#include "llvm/MC/MCFragment.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/MC/MCObjectWriter.h"
28#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCSectionCOFF.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000030#include "llvm/MC/MCSymbol.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000031#include "llvm/MC/MCSymbolCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/MC/MCValue.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000033#include "llvm/MC/MCWinCOFFObjectWriter.h"
Hans Wennborgf26bfc12014-09-29 22:43:20 +000034#include "llvm/MC/StringTableBuilder.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000035#include "llvm/Support/Casting.h"
Hans Wennborgba80b5d2014-09-28 00:22:27 +000036#include "llvm/Support/Endian.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000037#include "llvm/Support/ErrorHandling.h"
David Majnemer6ddc6362015-09-01 21:23:58 +000038#include "llvm/Support/JamCRC.h"
Peter Collingbournebc3089f2018-08-22 23:58:16 +000039#include "llvm/Support/LEB128.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000040#include "llvm/Support/MathExtras.h"
41#include "llvm/Support/raw_ostream.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000042#include <algorithm>
Eugene Zelenko1d435522017-02-07 23:02:00 +000043#include <cassert>
44#include <cstddef>
45#include <cstdint>
46#include <cstring>
David Majnemer088ba022015-09-01 23:46:11 +000047#include <ctime>
Eugene Zelenko1d435522017-02-07 23:02:00 +000048#include <memory>
49#include <string>
50#include <vector>
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000051
Chris Lattner2c52b792010-07-11 22:07:02 +000052using namespace llvm;
Rui Ueyama86e3ef92017-02-14 23:28:19 +000053using llvm::support::endian::write32le;
Chris Lattner2c52b792010-07-11 22:07:02 +000054
Chandler Carruthf58e3762014-04-22 03:04:17 +000055#define DEBUG_TYPE "WinCOFFObjectWriter"
56
Chris Lattner2c52b792010-07-11 22:07:02 +000057namespace {
Eugene Zelenko1d435522017-02-07 23:02:00 +000058
Eugene Zelenko7975b992017-04-26 22:31:39 +000059using name = SmallString<COFF::NameSize>;
Chris Lattner2c52b792010-07-11 22:07:02 +000060
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000061enum AuxiliaryType {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000062 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
Eugene Zelenko7975b992017-04-26 22:31:39 +000078 using AuxiliarySymbols = SmallVector<AuxSymbol, 1>;
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
Eugene Zelenko7975b992017-04-26 22:31:39 +0000110using relocations = std::vector<COFFRelocation>;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000111
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:
Peter Collingbournef17b1492018-05-21 18:17:42 +0000127 support::endian::Writer W;
128
Eugene Zelenko7975b992017-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. Spencerb5fc1382010-07-26 02:17:32 +0000131
Eugene Zelenko7975b992017-04-26 22:31:39 +0000132 using symbol_map = DenseMap<MCSymbol const *, COFFSymbol *>;
133 using section_map = DenseMap<MCSection const *, COFFSection *>;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000134
Ahmed Charles56440fd2014-03-06 05:51:42 +0000135 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000136
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000137 // Root level file contents.
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000138 COFF::header Header = {};
Rafael Espindola11e9e212015-05-27 14:37:12 +0000139 sections Sections;
140 symbols Symbols;
Rafael Espindola21956e42015-10-23 21:48:05 +0000141 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000142
143 // Maps used during object file creation.
144 section_map SectionMap;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000145 symbol_map SymbolMap;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000146
David Majnemer4d571592014-09-15 19:42:42 +0000147 bool UseBigObj;
148
Peter Collingbournebc3089f2018-08-22 23:58:16 +0000149 bool EmitAddrsigSection = false;
150 MCSectionCOFF *AddrsigSection;
151 std::vector<const MCSymbol *> AddrsigSyms;
152
Lang Hames77dff392017-10-10 00:50:29 +0000153 WinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
154 raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000155
Yaron Kerencca43c12014-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. Spencerb5fc1382010-07-26 02:17:32 +0000166
Michael J. Spencer17990d52010-10-16 08:25:57 +0000167 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola11e9e212015-05-27 14:37:12 +0000168 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000169 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000170
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000171 void defineSection(MCSectionCOFF const &Sec);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000172
173 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000174 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000175 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000176
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000177 void SetSymbolName(COFFSymbol &S);
178 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000179
Michael J. Spencerd6283772010-09-27 08:58:26 +0000180 bool IsPhysicalSection(COFFSection *S);
181
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000182 // Entity writing methods.
183
184 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000185 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000186 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
Rui Ueyamaac20c172017-02-17 17:32:54 +0000187 void writeSectionHeaders();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000188 void WriteRelocation(const COFF::relocation &R);
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000189 uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout,
190 const MCSection &MCSec);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000191 void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
192 const COFFSection &Sec, const MCSection &MCSec);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000193
194 // MCObjectWriter interface implementation.
195
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000196 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper34a61bc2014-03-08 07:02:02 +0000197 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000198
Jim Grosbach36e60e92015-06-04 22:24:41 +0000199 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000200 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000201 const MCFragment &FB, bool InSet,
202 bool IsPCRel) const override;
203
Jim Grosbach36e60e92015-06-04 22:24:41 +0000204 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000205 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindolaceecfe5b2017-07-11 23:56:10 +0000206 MCValue Target, uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000207
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000208 void createFileSymbols(MCAssembler &Asm);
209 void assignSectionNumbers();
210 void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout);
211
Peter Collingbournebc3089f2018-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 Collingbourne438390f2018-05-21 18:23:50 +0000217 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000218};
Eugene Zelenko1d435522017-02-07 23:02:00 +0000219
220} // end anonymous namespace
Chris Lattner2c52b792010-07-11 22:07:02 +0000221
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000222//------------------------------------------------------------------------------
223// Symbol class implementation
224
Michael J. Spencerb5fc1382010-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 Ueyama86e3ef92017-02-14 23:28:19 +0000229 write32le(Data.Name + 0, 0);
230 write32le(Data.Name + 4, Offset);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000231}
232
233//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000234// WinCOFFObjectWriter class implementation
235
Lang Hames77dff392017-10-10 00:50:29 +0000236WinCOFFObjectWriter::WinCOFFObjectWriter(
237 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS)
Peter Collingbourne59a6fc42018-05-21 18:28:57 +0000238 : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000239 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000240}
241
Michael J. Spencer17990d52010-10-16 08:25:57 +0000242COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000243 Symbols.push_back(make_unique<COFFSymbol>(Name));
244 return Symbols.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000245}
246
Yaron Keren56919ef2014-12-04 08:30:39 +0000247COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Rui Ueyama0fcdb482017-02-14 23:47:34 +0000248 COFFSymbol *&Ret = SymbolMap[Symbol];
249 if (!Ret)
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000250 Ret = createSymbol(Symbol->getName());
Rui Ueyama0fcdb482017-02-14 23:47:34 +0000251 return Ret;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000252}
253
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000254COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000255 Sections.emplace_back(make_unique<COFFSection>(Name));
256 return Sections.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000257}
258
Rui Ueyama24e27b42017-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. Spencerb5fc1382010-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 Ueyama09786c42017-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 Ueyama24e27b42017-02-15 00:15:54 +0000301
302 // Create a COMDAT symbol if needed.
Rui Ueyama09786c42017-02-15 00:28:48 +0000303 if (MCSec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
304 if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
Rafael Espindola0766ae02014-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 Ueyama09786c42017-02-15 00:28:48 +0000308 COMDATSymbol->Section = Section;
Rafael Espindola0766ae02014-06-06 19:26:12 +0000309 }
310 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000311
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000312 // In this case the auxiliary symbol is a Section Definition.
Rui Ueyama09786c42017-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. Spencerb5fc1382010-07-26 02:17:32 +0000317
Rui Ueyama09786c42017-02-15 00:28:48 +0000318 // Set section alignment.
319 Section->Header.Characteristics = MCSec.getCharacteristics();
320 Section->Header.Characteristics |= getAlignment(MCSec);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000321
322 // Bind internal COFF section to MC section.
Rui Ueyama09786c42017-02-15 00:28:48 +0000323 Section->MCSection = &MCSec;
324 SectionMap[&MCSec] = Section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000325}
326
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000327static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000328 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000329 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000330 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000331
332 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000333 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000334 return 0;
335
336 return Res;
337}
338
Rafael Espindola732eeaf22016-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 Majnemera9bdb322014-04-08 22:33:40 +0000354/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000355/// and creates the associated COFF symbol staging object.
Rui Ueyama789c4222017-02-15 01:09:01 +0000356void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000357 MCAssembler &Assembler,
358 const MCAsmLayout &Layout) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000359 COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
360 const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000361 COFFSection *Sec = nullptr;
362 if (Base && Base->getFragment()) {
363 Sec = SectionMap[Base->getFragment()->getParent()];
Rui Ueyama789c4222017-02-15 01:09:01 +0000364 if (Sym->Section && Sym->Section != Sec)
Rafael Espindola30c080a2016-05-26 18:48:23 +0000365 report_fatal_error("conflicting sections for symbol");
366 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000367
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000368 COFFSymbol *Local = nullptr;
Rui Ueyama789c4222017-02-15 01:09:01 +0000369 if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
370 Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000371
Rui Ueyama789c4222017-02-15 01:09:01 +0000372 COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000373 if (!WeakDefault) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000374 std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000375 WeakDefault = createSymbol(WeakName);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000376 if (!Sec)
377 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
378 else
379 WeakDefault->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000380 Local = WeakDefault;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000381 }
382
Rui Ueyama789c4222017-02-15 01:09:01 +0000383 Sym->Other = WeakDefault;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000384
Michael J. Spencer17990d52010-10-16 08:25:57 +0000385 // Setup the Weak External auxiliary symbol.
Rui Ueyama789c4222017-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 Espindola11e9e212015-05-27 14:37:12 +0000391 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000392 } else {
Rafael Espindola30c080a2016-05-26 18:48:23 +0000393 if (!Base)
Rui Ueyama789c4222017-02-15 01:09:01 +0000394 Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola30c080a2016-05-26 18:48:23 +0000395 else
Rui Ueyama789c4222017-02-15 01:09:01 +0000396 Sym->Section = Sec;
397 Local = Sym;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000398 }
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000399
400 if (Local) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000401 Local->Data.Value = getSymbolValue(MCSym, Layout);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000402
Rui Ueyama789c4222017-02-15 01:09:01 +0000403 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
Rafael Espindola732eeaf22016-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 Ueyama789c4222017-02-15 01:09:01 +0000409 bool IsExternal = MCSym.isExternal() ||
410 (!MCSym.getFragment() && !MCSym.isVariable());
Rafael Espindola732eeaf22016-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 Ueyama789c4222017-02-15 01:09:01 +0000417 Sym->MC = &MCSym;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000418}
419
Nico Rieck01143f92014-02-25 09:50:40 +0000420// Maximum offsets for different string table entry encodings.
David Majnemerf088f522016-01-24 20:46:11 +0000421enum : unsigned { Max7DecimalOffset = 9999999U };
422enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
Nico Rieck01143f92014-02-25 09:50:40 +0000423
Nico Rieck9d2c15e2014-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 Espindola11e9e212015-05-27 14:37:12 +0000427static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000428 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-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 Espindola11e9e212015-05-27 14:37:12 +0000438 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-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 Wennborgf26bfc12014-09-29 22:43:20 +0000446void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000447 if (S.Name.size() <= COFF::NameSize) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000448 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000449 return;
David Majnemerf088f522016-01-24 20:46:11 +0000450 }
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000451
452 uint64_t StringTableEntry = Strings.getOffset(S.Name);
453 if (StringTableEntry <= Max7DecimalOffset) {
Rui Ueyama4b58f5772017-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 Ueyamaa39d1482017-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. Spencerd6283772010-09-27 08:58:26 +0000466}
467
Hans Wennborgf26bfc12014-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. Spencerd6283772010-09-27 08:58:26 +0000472 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000473}
474
Michael J. Spencerd6283772010-09-27 08:58:26 +0000475bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000476 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
477 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000478}
479
480//------------------------------------------------------------------------------
481// entity writing methods
482
483void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000484 if (UseBigObj) {
Peter Collingbournef17b1492018-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 Majnemer4d571592014-09-15 19:42:42 +0000498 } else {
Peter Collingbournef17b1492018-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 Majnemer4d571592014-09-15 19:42:42 +0000506 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000507}
508
David Blaikief564ab62014-04-15 05:25:03 +0000509void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000510 W.OS.write(S.Data.Name, COFF::NameSize);
511 W.write<uint32_t>(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000512 if (UseBigObj)
Peter Collingbournef17b1492018-05-21 18:17:42 +0000513 W.write<uint32_t>(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000514 else
Peter Collingbournef17b1492018-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 Blaikief564ab62014-04-15 05:25:03 +0000519 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000520}
521
522void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000523 const COFFSymbol::AuxiliarySymbols &S) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000524 for (const AuxSymbol &i : S) {
525 switch (i.AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000526 case ATWeakExternal:
Peter Collingbournef17b1492018-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 Majnemer4d571592014-09-15 19:42:42 +0000530 if (UseBigObj)
Peter Collingbournef17b1492018-05-21 18:17:42 +0000531 W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000532 break;
533 case ATFile:
Peter Collingbournef17b1492018-05-21 18:17:42 +0000534 W.OS.write(reinterpret_cast<const char *>(&i.Aux),
535 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000536 break;
537 case ATSectionDefinition:
Peter Collingbournef17b1492018-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 Majnemer4d571592014-09-15 19:42:42 +0000546 if (UseBigObj)
Peter Collingbournef17b1492018-05-21 18:17:42 +0000547 W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000548 break;
549 }
550 }
551}
552
Rui Ueyamaac20c172017-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());
Mandeep Singh Grangdb456ef2018-04-13 19:47:01 +0000561 llvm::sort(Arr.begin(), Arr.end(),
562 [](const COFFSection *A, const COFFSection *B) {
563 return A->Number < B->Number;
564 });
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000565
Rui Ueyamaac20c172017-02-17 17:32:54 +0000566 for (auto &Section : Arr) {
567 if (Section->Number == -1)
568 continue;
569
570 COFF::section &S = Section->Header;
571 if (Section->Relocations.size() >= 0xffff)
572 S.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Peter Collingbournef17b1492018-05-21 18:17:42 +0000573 W.OS.write(S.Name, COFF::NameSize);
574 W.write<uint32_t>(S.VirtualSize);
575 W.write<uint32_t>(S.VirtualAddress);
576 W.write<uint32_t>(S.SizeOfRawData);
577 W.write<uint32_t>(S.PointerToRawData);
578 W.write<uint32_t>(S.PointerToRelocations);
579 W.write<uint32_t>(S.PointerToLineNumbers);
580 W.write<uint16_t>(S.NumberOfRelocations);
581 W.write<uint16_t>(S.NumberOfLineNumbers);
582 W.write<uint32_t>(S.Characteristics);
Rui Ueyamaac20c172017-02-17 17:32:54 +0000583 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000584}
585
586void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000587 W.write<uint32_t>(R.VirtualAddress);
588 W.write<uint32_t>(R.SymbolTableIndex);
589 W.write<uint16_t>(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000590}
591
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000592// Write MCSec's contents. What this function does is essentially
593// "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
594// because it needs to compute a CRC.
595uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler &Asm,
596 const MCAsmLayout &Layout,
597 const MCSection &MCSec) {
598 // Save the contents of the section to a temporary buffer, we need this
599 // to CRC the data before we dump it into the object file.
600 SmallVector<char, 128> Buf;
601 raw_svector_ostream VecOS(Buf);
Peter Collingbourne147db3e2018-05-21 18:11:35 +0000602 Asm.writeSectionData(VecOS, &MCSec, Layout);
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000603
604 // Write the section contents to the object file.
Peter Collingbournef17b1492018-05-21 18:17:42 +0000605 W.OS << Buf;
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000606
607 // Calculate our CRC with an initial value of '0', this is not how
608 // JamCRC is specified but it aligns with the expected output.
609 JamCRC JC(/*Init=*/0);
610 JC.update(Buf);
611 return JC.getCRC();
612}
613
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000614void WinCOFFObjectWriter::writeSection(MCAssembler &Asm,
615 const MCAsmLayout &Layout,
616 const COFFSection &Sec,
617 const MCSection &MCSec) {
618 if (Sec.Number == -1)
619 return;
620
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000621 // Write the section contents.
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000622 if (Sec.Header.PointerToRawData != 0) {
Peter Collingbournea67161f2018-08-23 05:39:36 +0000623 assert(W.OS.tell() == Sec.Header.PointerToRawData &&
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000624 "Section::PointerToRawData is insane!");
625
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000626 uint32_t CRC = writeSectionContents(Asm, Layout, MCSec);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000627
628 // Update the section definition auxiliary symbol to record the CRC.
629 COFFSection *Sec = SectionMap[&MCSec];
630 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
631 assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
632 AuxSymbol &SecDef = AuxSyms[0];
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000633 SecDef.Aux.SectionDefinition.CheckSum = CRC;
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000634 }
635
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000636 // Write relocations for this section.
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000637 if (Sec.Relocations.empty()) {
638 assert(Sec.Header.PointerToRelocations == 0 &&
639 "Section::PointerToRelocations is insane!");
640 return;
641 }
642
Peter Collingbournef17b1492018-05-21 18:17:42 +0000643 assert(W.OS.tell() == Sec.Header.PointerToRelocations &&
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000644 "Section::PointerToRelocations is insane!");
645
646 if (Sec.Relocations.size() >= 0xffff) {
647 // In case of overflow, write actual relocation count as first
648 // relocation. Including the synthetic reloc itself (+ 1).
649 COFF::relocation R;
650 R.VirtualAddress = Sec.Relocations.size() + 1;
651 R.SymbolTableIndex = 0;
652 R.Type = 0;
653 WriteRelocation(R);
654 }
655
656 for (const auto &Relocation : Sec.Relocations)
657 WriteRelocation(Relocation.Data);
658}
659
Chris Lattner2c52b792010-07-11 22:07:02 +0000660////////////////////////////////////////////////////////////////////////////////
661// MCObjectWriter interface implementations
662
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000663void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000664 const MCAsmLayout &Layout) {
Peter Collingbournebc3089f2018-08-22 23:58:16 +0000665 if (EmitAddrsigSection) {
666 AddrsigSection = Asm.getContext().getCOFFSection(
667 ".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE,
668 SectionKind::getMetadata());
669 Asm.registerSection(*AddrsigSection);
670 }
671
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000672 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000673 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000674 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000675 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000676
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000677 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000678 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000679 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000680}
681
Jim Grosbach36e60e92015-06-04 22:24:41 +0000682bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000683 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000684 bool InSet, bool IsPCRel) const {
Reid Kleckner5a5ac652018-03-14 19:24:32 +0000685 // Don't drop relocations between functions, even if they are in the same text
686 // section. Multiple Visual C++ linker features depend on having the
687 // relocations present. The /INCREMENTAL flag will cause these relocations to
688 // point to thunks, and the /GUARD:CF flag assumes that it can use relocations
689 // to approximate the set of all address taken functions. LLD's implementation
690 // of /GUARD:CF also relies on the existance of these relocations.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000691 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
Reid Kleckner5a5ac652018-03-14 19:24:32 +0000692 if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000693 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000694 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000695 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000696}
697
Rafael Espindolaceecfe5b2017-07-11 23:56:10 +0000698void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
699 const MCAsmLayout &Layout,
700 const MCFragment *Fragment,
701 const MCFixup &Fixup, MCValue Target,
702 uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000703 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000704
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000705 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000706 if (!A.isRegistered()) {
707 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000708 Twine("symbol '") + A.getName() +
709 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000710 return;
711 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000712 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000713 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000714 Twine("assembler label '") + A.getName() +
715 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000716 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000717 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000718
Rui Ueyama62376782017-02-16 01:06:45 +0000719 MCSection *MCSec = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000720
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000721 // Mark this symbol as requiring an entry in the symbol table.
Rui Ueyama62376782017-02-16 01:06:45 +0000722 assert(SectionMap.find(MCSec) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000723 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000724
Rui Ueyama62376782017-02-16 01:06:45 +0000725 COFFSection *Sec = SectionMap[MCSec];
Rafael Espindolaed164772011-04-20 14:01:45 +0000726 const MCSymbolRefExpr *SymB = Target.getSymB();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000727
David Majnemera45a1762014-01-06 07:39:46 +0000728 if (SymB) {
729 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000730 if (!B->getFragment()) {
731 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000732 Fixup.getLoc(),
733 Twine("symbol '") + B->getName() +
734 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000735 return;
736 }
David Majnemera45a1762014-01-06 07:39:46 +0000737
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000738 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000739 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000740
David Majnemer1de30942015-02-09 06:31:31 +0000741 // Offset of the relocation in the section
742 int64_t OffsetOfRelocation =
743 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
744
Andy Ayers9e5c8512015-05-14 01:10:41 +0000745 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000746 } else {
747 FixedValue = Target.getConstant();
748 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000749
750 COFFRelocation Reloc;
751
Daniel Dunbar727be432010-07-31 21:08:54 +0000752 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000753 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000754
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000755 // Turn relocations for temporary symbols into section relocations.
Rafael Espindolad2edd132017-06-22 21:57:04 +0000756 if (A.isTemporary()) {
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000757 MCSection *TargetSection = &A.getSection();
758 assert(
759 SectionMap.find(TargetSection) != SectionMap.end() &&
760 "Section must already have been defined in executePostLayoutBinding!");
761 Reloc.Symb = SectionMap[TargetSection]->Symbol;
762 FixedValue += Layout.getSymbolOffset(A);
763 } else {
764 assert(
765 SymbolMap.find(&A) != SymbolMap.end() &&
766 "Symbol must already have been defined in executePostLayoutBinding!");
767 Reloc.Symb = SymbolMap[&A];
768 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000769
770 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000771
772 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola58173b92017-06-23 04:07:44 +0000773 Reloc.Data.Type = TargetObjectWriter->getRelocType(
774 Asm.getContext(), Target, Fixup, SymB, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000775
776 // FIXME: Can anyone explain what this does other than adjust for the size
777 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000778 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
779 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
780 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
781 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000782 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000783
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000784 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
785 switch (Reloc.Data.Type) {
786 case COFF::IMAGE_REL_ARM_ABSOLUTE:
787 case COFF::IMAGE_REL_ARM_ADDR32:
788 case COFF::IMAGE_REL_ARM_ADDR32NB:
789 case COFF::IMAGE_REL_ARM_TOKEN:
790 case COFF::IMAGE_REL_ARM_SECTION:
791 case COFF::IMAGE_REL_ARM_SECREL:
792 break;
793 case COFF::IMAGE_REL_ARM_BRANCH11:
794 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000795 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
796 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
797 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000798 case COFF::IMAGE_REL_ARM_BRANCH24:
799 case COFF::IMAGE_REL_ARM_BLX24:
800 case COFF::IMAGE_REL_ARM_MOV32A:
801 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
802 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000803 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000804 // generate the relocations however the rest of the MSVC toolchain is
805 // unable to handle it.
806 llvm_unreachable("unsupported relocation");
807 break;
808 case COFF::IMAGE_REL_ARM_MOV32T:
809 break;
810 case COFF::IMAGE_REL_ARM_BRANCH20T:
811 case COFF::IMAGE_REL_ARM_BRANCH24T:
812 case COFF::IMAGE_REL_ARM_BLX23T:
813 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
814 // perform a 4 byte adjustment to the relocation. Relative branches are
815 // offset by 4 on ARM, however, because there is no RELA relocations, all
816 // branches are offset by 4.
817 FixedValue = FixedValue + 4;
818 break;
819 }
820 }
821
Simon Pilgrimf2fbf432016-11-20 13:47:59 +0000822 // The fixed value never makes sense for section indices, ignore it.
David Majnemer408b5e62016-02-05 01:55:49 +0000823 if (Fixup.getKind() == FK_SecRel_2)
824 FixedValue = 0;
825
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000826 if (TargetObjectWriter->recordRelocation(Fixup))
Rui Ueyama62376782017-02-16 01:06:45 +0000827 Sec->Relocations.push_back(Reloc);
828}
829
830static std::time_t getTime() {
831 std::time_t Now = time(nullptr);
832 if (Now < 0 || !isUInt<32>(Now))
833 return UINT32_MAX;
834 return Now;
Chris Lattner2c52b792010-07-11 22:07:02 +0000835}
836
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000837// Create .file symbols.
838void WinCOFFObjectWriter::createFileSymbols(MCAssembler &Asm) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000839 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000840 // round up to calculate the number of auxiliary symbols required
841 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000842 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000843
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000844 COFFSymbol *File = createSymbol(".file");
845 File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
846 File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
847 File->Aux.resize(Count);
David Majnemer4d571592014-09-15 19:42:42 +0000848
849 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000850 unsigned Length = Name.size();
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000851 for (auto &Aux : File->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000852 Aux.AuxType = ATFile;
853
854 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000855 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000856 Length = Length - SymbolSize;
857 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000858 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000859 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
860 break;
861 }
862
863 Offset += SymbolSize;
864 }
865 }
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000866}
867
Rui Ueyamaac20c172017-02-17 17:32:54 +0000868static bool isAssociative(const COFFSection &Section) {
869 return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
870 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
871}
872
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000873void WinCOFFObjectWriter::assignSectionNumbers() {
874 size_t I = 1;
Rui Ueyamaac20c172017-02-17 17:32:54 +0000875 auto Assign = [&](COFFSection &Section) {
876 Section.Number = I;
877 Section.Symbol->Data.SectionNumber = I;
878 Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I;
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000879 ++I;
Rui Ueyamaac20c172017-02-17 17:32:54 +0000880 };
881
882 // Although it is not explicitly requested by the Microsoft COFF spec,
883 // we should avoid emitting forward associative section references,
884 // because MSVC link.exe as of 2017 cannot handle that.
885 for (const std::unique_ptr<COFFSection> &Section : Sections)
886 if (!isAssociative(*Section))
887 Assign(*Section);
888 for (const std::unique_ptr<COFFSection> &Section : Sections)
889 if (isAssociative(*Section))
890 Assign(*Section);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000891}
892
893// Assign file offsets to COFF object file structures.
894void WinCOFFObjectWriter::assignFileOffsets(MCAssembler &Asm,
895 const MCAsmLayout &Layout) {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000896 unsigned Offset = W.OS.tell();
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000897
898 Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
899 Offset += COFF::SectionSize * Header.NumberOfSections;
900
901 for (const auto &Section : Asm) {
902 COFFSection *Sec = SectionMap[&Section];
903
904 if (Sec->Number == -1)
905 continue;
906
907 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
908
909 if (IsPhysicalSection(Sec)) {
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000910 Sec->Header.PointerToRawData = Offset;
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000911 Offset += Sec->Header.SizeOfRawData;
912 }
913
914 if (!Sec->Relocations.empty()) {
915 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
916
917 if (RelocationsOverflow) {
918 // Signal overflow by setting NumberOfRelocations to max value. Actual
919 // size is found in reloc #0. Microsoft tools understand this.
920 Sec->Header.NumberOfRelocations = 0xffff;
921 } else {
922 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
923 }
924 Sec->Header.PointerToRelocations = Offset;
925
926 if (RelocationsOverflow) {
927 // Reloc #0 will contain actual count, so make room for it.
928 Offset += COFF::RelocationSize;
929 }
930
931 Offset += COFF::RelocationSize * Sec->Relocations.size();
932
933 for (auto &Relocation : Sec->Relocations) {
934 assert(Relocation.Symb->getIndex() != -1);
935 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
936 }
937 }
938
939 assert(Sec->Symbol->Aux.size() == 1 &&
940 "Section's symbol must have one aux!");
941 AuxSymbol &Aux = Sec->Symbol->Aux[0];
942 assert(Aux.AuxType == ATSectionDefinition &&
943 "Section's symbol's aux symbol must be a Section Definition!");
944 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
945 Aux.Aux.SectionDefinition.NumberOfRelocations =
946 Sec->Header.NumberOfRelocations;
947 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
948 Sec->Header.NumberOfLineNumbers;
949 }
950
951 Header.PointerToSymbolTable = Offset;
952}
953
Peter Collingbourne438390f2018-05-21 18:23:50 +0000954uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
955 const MCAsmLayout &Layout) {
956 uint64_t StartOffset = W.OS.tell();
957
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000958 if (Sections.size() > INT32_MAX)
959 report_fatal_error(
960 "PE COFF object files can't have more than 2147483647 sections");
961
962 UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
963 Header.NumberOfSections = Sections.size();
964 Header.NumberOfSymbols = 0;
965
966 assignSectionNumbers();
967 createFileSymbols(Asm);
David Majnemer4d571592014-09-15 19:42:42 +0000968
David Majnemer9ab5ff12014-08-28 04:02:50 +0000969 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000970 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000971 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000972 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000973 Symbol->setIndex(Header.NumberOfSymbols++);
974 // Update auxiliary symbol info.
975 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
976 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000977 }
978
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000979 // Build string table.
980 for (const auto &S : Sections)
981 if (S->Name.size() > COFF::NameSize)
982 Strings.add(S->Name);
983 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000984 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000985 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000986 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000987
988 // Set names.
989 for (const auto &S : Sections)
990 SetSectionName(*S);
991 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000992 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000993
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000994 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000995 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000996 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000997 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000998 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
999 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001000 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +00001001 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001002 }
1003 }
1004
Nico Riecka37acf72013-07-06 12:13:10 +00001005 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +00001006 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001007 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +00001008 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1009 continue;
1010
Rafael Espindolaf59264f2015-05-27 14:45:54 +00001011 const MCSectionCOFF &MCSec = *Section->MCSection;
Reid Kleckner602c0da2018-08-16 21:34:41 +00001012 const MCSymbol *AssocMCSym = MCSec.getCOMDATSymbol();
1013 assert(AssocMCSym);
Nico Riecka37acf72013-07-06 12:13:10 +00001014
Reid Kleckner602c0da2018-08-16 21:34:41 +00001015 // It's an error to try to associate with an undefined symbol or a symbol
1016 // without a section.
1017 if (!AssocMCSym->isInSection()) {
1018 Asm.getContext().reportError(
1019 SMLoc(), Twine("cannot make section ") + MCSec.getSectionName() +
1020 Twine(" associative with sectionless symbol ") +
1021 AssocMCSym->getName());
1022 continue;
1023 }
1024
1025 const auto *AssocMCSec = cast<MCSectionCOFF>(&AssocMCSym->getSection());
1026 assert(SectionMap.count(AssocMCSec));
1027 COFFSection *AssocSec = SectionMap[AssocMCSec];
Nico Riecka37acf72013-07-06 12:13:10 +00001028
1029 // Skip this section if the associated section is unused.
Reid Kleckner602c0da2018-08-16 21:34:41 +00001030 if (AssocSec->Number == -1)
Nico Riecka37acf72013-07-06 12:13:10 +00001031 continue;
1032
Reid Kleckner602c0da2018-08-16 21:34:41 +00001033 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = AssocSec->Number;
Nico Riecka37acf72013-07-06 12:13:10 +00001034 }
1035
Peter Collingbournebc3089f2018-08-22 23:58:16 +00001036 // Create the contents of the .llvm_addrsig section.
1037 if (EmitAddrsigSection) {
1038 auto Frag = new MCDataFragment(AddrsigSection);
Peter Collingbourne6579c812018-08-23 06:57:49 +00001039 Frag->setLayoutOrder(0);
Peter Collingbournebc3089f2018-08-22 23:58:16 +00001040 raw_svector_ostream OS(Frag->getContents());
1041 for (const MCSymbol *S : AddrsigSyms) {
1042 if (!S->isTemporary()) {
1043 encodeULEB128(S->getIndex(), OS);
1044 continue;
1045 }
1046
1047 MCSection *TargetSection = &S->getSection();
1048 assert(SectionMap.find(TargetSection) != SectionMap.end() &&
1049 "Section must already have been defined in "
1050 "executePostLayoutBinding!");
1051 encodeULEB128(SectionMap[TargetSection]->Symbol->getIndex(), OS);
1052 }
1053 }
1054
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001055 assignFileOffsets(Asm, Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001056
David Majnemer088ba022015-09-01 23:46:11 +00001057 // MS LINK expects to be able to use this timestamp to implement their
1058 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +00001059 if (Asm.isIncrementalLinkerCompatible()) {
Rui Ueyama62376782017-02-16 01:06:45 +00001060 Header.TimeDateStamp = getTime();
David Majnemer03e2cc32015-12-21 22:09:27 +00001061 } else {
Nico Weber891419a2016-01-06 19:05:19 +00001062 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +00001063 Header.TimeDateStamp = 0;
1064 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +00001065
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001066 // Write it all to disk...
1067 WriteFileHeader(Header);
Rui Ueyamaac20c172017-02-17 17:32:54 +00001068 writeSectionHeaders();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001069
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001070 // Write section contents.
1071 sections::iterator I = Sections.begin();
1072 sections::iterator IE = Sections.end();
1073 MCAssembler::iterator J = Asm.begin();
1074 MCAssembler::iterator JE = Asm.end();
1075 for (; I != IE && J != JE; ++I, ++J)
1076 writeSection(Asm, Layout, **I, *J);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001077
Peter Collingbournef17b1492018-05-21 18:17:42 +00001078 assert(W.OS.tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001079 "Header::PointerToSymbolTable is insane!");
1080
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001081 // Write a symbol table.
Yaron Keren56919ef2014-12-04 08:30:39 +00001082 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001083 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001084 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001085
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001086 // Write a string table, which completes the entire COFF file.
Peter Collingbournef17b1492018-05-21 18:17:42 +00001087 Strings.write(W.OS);
Peter Collingbourne438390f2018-05-21 18:23:50 +00001088
1089 return W.OS.tell() - StartOffset;
Chris Lattner2c52b792010-07-11 22:07:02 +00001090}
1091
Rafael Espindola11e9e212015-05-27 14:37:12 +00001092MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1093 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001094
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001095// Pin the vtable to this file.
1096void MCWinCOFFObjectTargetWriter::anchor() {}
1097
Chris Lattner2c52b792010-07-11 22:07:02 +00001098//------------------------------------------------------------------------------
1099// WinCOFFObjectWriter factory function
1100
Lang Hames60fbc7c2017-10-10 16:28:07 +00001101std::unique_ptr<MCObjectWriter> llvm::createWinCOFFObjectWriter(
Lang Hames77dff392017-10-10 00:50:29 +00001102 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) {
Lang Hames60fbc7c2017-10-10 16:28:07 +00001103 return llvm::make_unique<WinCOFFObjectWriter>(std::move(MOTW), OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001104}