blob: d5bffe93936cbc2668e3d6c9215717b64acaea8c [file] [log] [blame]
Chris Lattner2c52b792010-07-11 22:07:02 +00001//===-- llvm/MC/WinCOFFObjectWriter.cpp -------------------------*- C++ -*-===//
2//
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
Rafael Espindola908d2ed2011-12-24 02:14:02 +000014#include "llvm/MC/MCWinCOFFObjectWriter.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000015#include "llvm/ADT/DenseMap.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000016#include "llvm/ADT/STLExtras.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000017#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
Nico Riecka37acf72013-07-06 12:13:10 +000019#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCAsmLayout.h"
21#include "llvm/MC/MCAssembler.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCObjectWriter.h"
25#include "llvm/MC/MCSection.h"
26#include "llvm/MC/MCSectionCOFF.h"
27#include "llvm/MC/MCSymbol.h"
28#include "llvm/MC/MCValue.h"
Hans Wennborgf26bfc12014-09-29 22:43:20 +000029#include "llvm/MC/StringTableBuilder.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000030#include "llvm/Support/COFF.h"
31#include "llvm/Support/Debug.h"
Hans Wennborgba80b5d2014-09-28 00:22:27 +000032#include "llvm/Support/Endian.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000033#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000034#include "llvm/Support/TimeValue.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000035#include <cstdio>
36
Chris Lattner2c52b792010-07-11 22:07:02 +000037using namespace llvm;
38
Chandler Carruthf58e3762014-04-22 03:04:17 +000039#define DEBUG_TYPE "WinCOFFObjectWriter"
40
Chris Lattner2c52b792010-07-11 22:07:02 +000041namespace {
Dmitri Gribenko226fea52013-01-13 16:01:15 +000042typedef SmallString<COFF::NameSize> name;
Chris Lattner2c52b792010-07-11 22:07:02 +000043
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000044enum AuxiliaryType {
45 ATFunctionDefinition,
46 ATbfAndefSymbol,
47 ATWeakExternal,
48 ATFile,
49 ATSectionDefinition
50};
Chris Lattner2c52b792010-07-11 22:07:02 +000051
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000052struct AuxSymbol {
53 AuxiliaryType AuxType;
54 COFF::Auxiliary Aux;
55};
Chris Lattner2c52b792010-07-11 22:07:02 +000056
Michael J. Spencerd6283772010-09-27 08:58:26 +000057class COFFSymbol;
58class COFFSection;
59
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000060class COFFSymbol {
61public:
62 COFF::symbol Data;
Chris Lattner2c52b792010-07-11 22:07:02 +000063
Dmitri Gribenko226fea52013-01-13 16:01:15 +000064 typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
Chris Lattner2c52b792010-07-11 22:07:02 +000065
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000066 name Name;
Michael J. Spencerce2e5352010-09-27 21:17:39 +000067 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000068 AuxiliarySymbols Aux;
69 COFFSymbol *Other;
Michael J. Spencerd6283772010-09-27 08:58:26 +000070 COFFSection *Section;
71 int Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000072
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +000073 const MCSymbol *MC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000074
Dmitri Gribenko226fea52013-01-13 16:01:15 +000075 COFFSymbol(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000076 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000077
78 bool should_keep() const;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000079};
80
81// This class contains staging data for a COFF relocation entry.
82struct COFFRelocation {
83 COFF::relocation Data;
84 COFFSymbol *Symb;
85
Craig Topperbb694de2014-04-13 04:57:38 +000086 COFFRelocation() : Symb(nullptr) {}
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000087 static size_t size() { return COFF::RelocationSize; }
88};
89
90typedef std::vector<COFFRelocation> relocations;
91
92class COFFSection {
93public:
94 COFF::section Header;
95
96 std::string Name;
Michael J. Spencerce2e5352010-09-27 21:17:39 +000097 int Number;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000098 MCSectionData const *MCData;
Michael J. Spencerd6283772010-09-27 08:58:26 +000099 COFFSymbol *Symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000100 relocations Relocations;
101
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000102 COFFSection(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000103 static size_t size();
104};
105
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000106class WinCOFFObjectWriter : public MCObjectWriter {
107public:
108
David Blaikief564ab62014-04-15 05:25:03 +0000109 typedef std::vector<std::unique_ptr<COFFSymbol>> symbols;
110 typedef std::vector<std::unique_ptr<COFFSection>> sections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000111
Michael J. Spencer17990d52010-10-16 08:25:57 +0000112 typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
113 typedef DenseMap<MCSection const *, COFFSection *> section_map;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000114
Ahmed Charles56440fd2014-03-06 05:51:42 +0000115 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000116
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000117 // Root level file contents.
118 COFF::header Header;
119 sections Sections;
120 symbols Symbols;
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000121 StringTableBuilder Strings;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000122
123 // Maps used during object file creation.
124 section_map SectionMap;
125 symbol_map SymbolMap;
126
David Majnemer4d571592014-09-15 19:42:42 +0000127 bool UseBigObj;
128
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000129 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000130
Yaron Kerencca43c12014-09-16 21:31:04 +0000131 void reset() override {
132 memset(&Header, 0, sizeof(Header));
133 Header.Machine = TargetObjectWriter->getMachine();
134 Sections.clear();
135 Symbols.clear();
136 Strings.clear();
137 SectionMap.clear();
138 SymbolMap.clear();
139 MCObjectWriter::reset();
140 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000141
Michael J. Spencer17990d52010-10-16 08:25:57 +0000142 COFFSymbol *createSymbol(StringRef Name);
143 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol * Symbol);
144 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000145
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000146 template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000147 object_t *createCOFFEntity(StringRef Name, list_t &List);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000148
149 void DefineSection(MCSectionData const &SectionData);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000150 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000151 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000152
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000153 void SetSymbolName(COFFSymbol &S);
154 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000155
David Majnemer299674e2014-07-13 04:31:19 +0000156 bool ExportSymbol(const MCSymbol &Symbol, MCAssembler &Asm);
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000157
Michael J. Spencerd6283772010-09-27 08:58:26 +0000158 bool IsPhysicalSection(COFFSection *S);
159
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000160 // Entity writing methods.
161
162 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000163 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000164 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
165 void WriteSectionHeader(const COFF::section &S);
166 void WriteRelocation(const COFF::relocation &R);
167
168 // MCObjectWriter interface implementation.
169
Craig Topper34a61bc2014-03-08 07:02:02 +0000170 void ExecutePostLayoutBinding(MCAssembler &Asm,
171 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000172
David Majnemer2cc4bc772014-11-11 08:43:57 +0000173 bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000174 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000175 const MCFragment &FB, bool InSet,
176 bool IsPCRel) const override;
177
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000178 bool isWeak(const MCSymbol &Sym) const override;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000179
Rafael Espindola26585542015-01-19 21:11:14 +0000180 void RecordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000181 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000182 MCValue Target, bool &IsPCRel,
183 uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000184
Craig Topper34a61bc2014-03-08 07:02:02 +0000185 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000186};
Chris Lattner2c52b792010-07-11 22:07:02 +0000187}
188
Hans Wennborgba80b5d2014-09-28 00:22:27 +0000189static inline void write_uint32_le(void *Data, uint32_t Value) {
190 support::endian::write<uint32_t, support::little, support::unaligned>(Data,
191 Value);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000192}
193
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000194//------------------------------------------------------------------------------
195// Symbol class implementation
196
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000197COFFSymbol::COFFSymbol(StringRef name)
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000198 : Name(name.begin(), name.end()), Other(nullptr), Section(nullptr),
199 Relocations(0), MC(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000200 memset(&Data, 0, sizeof(Data));
201}
202
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000203// In the case that the name does not fit within 8 bytes, the offset
204// into the string table is stored in the last 4 bytes instead, leaving
205// the first 4 bytes as 0.
206void COFFSymbol::set_name_offset(uint32_t Offset) {
207 write_uint32_le(Data.Name + 0, 0);
208 write_uint32_le(Data.Name + 4, Offset);
209}
210
Michael J. Spencerd6283772010-09-27 08:58:26 +0000211/// logic to decide if the symbol should be reported in the symbol table
212bool COFFSymbol::should_keep() const {
213 // no section means its external, keep it
Craig Topperbb694de2014-04-13 04:57:38 +0000214 if (!Section)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000215 return true;
216
217 // if it has relocations pointing at it, keep it
218 if (Relocations > 0) {
219 assert(Section->Number != -1 && "Sections with relocations must be real!");
220 return true;
221 }
222
223 // if the section its in is being droped, drop it
224 if (Section->Number == -1)
225 return false;
226
227 // if it is the section symbol, keep it
228 if (Section->Symbol == this)
229 return true;
230
231 // if its temporary, drop it
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000232 if (MC && MC->isTemporary())
233 return false;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000234
235 // otherwise, keep it
236 return true;
237}
238
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000239//------------------------------------------------------------------------------
240// Section class implementation
241
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000242COFFSection::COFFSection(StringRef name)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000243 : Name(name)
Craig Topperbb694de2014-04-13 04:57:38 +0000244 , MCData(nullptr)
245 , Symbol(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000246 memset(&Header, 0, sizeof(Header));
247}
248
249size_t COFFSection::size() {
250 return COFF::SectionSize;
251}
252
253//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000254// WinCOFFObjectWriter class implementation
255
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000256WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000257 raw_pwrite_stream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000258 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000259 memset(&Header, 0, sizeof(Header));
Michael J. Spencer377aa202010-08-21 05:58:13 +0000260
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000261 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000262}
263
Michael J. Spencer17990d52010-10-16 08:25:57 +0000264COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000265 return createCOFFEntity<COFFSymbol>(Name, Symbols);
266}
267
Yaron Keren56919ef2014-12-04 08:30:39 +0000268COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000269 symbol_map::iterator i = SymbolMap.find(Symbol);
270 if (i != SymbolMap.end())
271 return i->second;
Yaron Keren56919ef2014-12-04 08:30:39 +0000272 COFFSymbol *RetSymbol =
273 createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000274 SymbolMap[Symbol] = RetSymbol;
275 return RetSymbol;
276}
277
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000278COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000279 return createCOFFEntity<COFFSection>(Name, Sections);
280}
281
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000282/// A template used to lookup or create a symbol/section, and initialize it if
283/// needed.
284template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000285object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name,
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000286 list_t &List) {
David Blaikief564ab62014-04-15 05:25:03 +0000287 List.push_back(make_unique<object_t>(Name));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000288
David Blaikief564ab62014-04-15 05:25:03 +0000289 return List.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000290}
291
292/// This function takes a section data object from the assembler
293/// and creates the associated COFF section staging object.
294void WinCOFFObjectWriter::DefineSection(MCSectionData const &SectionData) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000295 assert(SectionData.getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000296 && "Got non-COFF section in the COFF backend!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000297 // FIXME: Not sure how to verify this (at least in a debug build).
298 MCSectionCOFF const &Sec =
299 static_cast<MCSectionCOFF const &>(SectionData.getSection());
300
301 COFFSection *coff_section = createSection(Sec.getSectionName());
302 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
Rafael Espindola0766ae02014-06-06 19:26:12 +0000303 if (Sec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
304 if (const MCSymbol *S = Sec.getCOMDATSymbol()) {
305 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
306 if (COMDATSymbol->Section)
307 report_fatal_error("two sections have the same comdat");
308 COMDATSymbol->Section = coff_section;
309 }
310 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000311
Michael J. Spencerd6283772010-09-27 08:58:26 +0000312 coff_section->Symbol = coff_symbol;
313 coff_symbol->Section = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000314 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000315
316 // In this case the auxiliary symbol is a Section Definition.
317 coff_symbol->Aux.resize(1);
318 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
319 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000320 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
321
322 coff_section->Header.Characteristics = Sec.getCharacteristics();
323
324 uint32_t &Characteristics = coff_section->Header.Characteristics;
325 switch (SectionData.getAlignment()) {
326 case 1: Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES; break;
327 case 2: Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES; break;
328 case 4: Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES; break;
329 case 8: Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES; break;
330 case 16: Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES; break;
331 case 32: Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES; break;
332 case 64: Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES; break;
333 case 128: Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES; break;
334 case 256: Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES; break;
335 case 512: Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES; break;
336 case 1024: Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES; break;
337 case 2048: Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES; break;
338 case 4096: Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES; break;
339 case 8192: Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES; break;
340 default:
341 llvm_unreachable("unsupported section alignment");
342 }
343
344 // Bind internal COFF section to MC section.
345 coff_section->MCData = &SectionData;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000346 SectionMap[&SectionData.getSection()] = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000347}
348
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000349static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000350 const MCAsmLayout &Layout) {
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000351 const MCSymbolData &Data = Symbol.getData();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000352 if (Data.isCommon() && Data.isExternal())
353 return Data.getCommonSize();
354
355 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000356 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000357 return 0;
358
359 return Res;
360}
361
David Majnemera9bdb322014-04-08 22:33:40 +0000362/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000363/// and creates the associated COFF symbol staging object.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000364void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000365 MCAssembler &Assembler,
366 const MCAsmLayout &Layout) {
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000367 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Peter Collingbourne89886872013-04-22 18:48:56 +0000368 SymbolMap[&Symbol] = coff_symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000369
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000370 if (Symbol.getData().getFlags() & COFF::SF_WeakExternal) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000371 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
372
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000373 if (Symbol.isVariable()) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000374 const MCSymbolRefExpr *SymRef =
375 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000376
Peter Collingbourne89886872013-04-22 18:48:56 +0000377 if (!SymRef)
378 report_fatal_error("Weak externals may only alias symbols");
Michael J. Spencer17990d52010-10-16 08:25:57 +0000379
Peter Collingbourne89886872013-04-22 18:48:56 +0000380 coff_symbol->Other = GetOrCreateCOFFSymbol(&SymRef->getSymbol());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000381 } else {
Yaron Keren075759a2015-03-30 15:42:36 +0000382 std::string WeakName = (".weak." + Symbol.getName() + ".default").str();
Michael J. Spencer17990d52010-10-16 08:25:57 +0000383 COFFSymbol *WeakDefault = createSymbol(WeakName);
384 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
385 WeakDefault->Data.StorageClass = COFF::IMAGE_SYM_CLASS_EXTERNAL;
386 WeakDefault->Data.Type = 0;
387 WeakDefault->Data.Value = 0;
388 coff_symbol->Other = WeakDefault;
389 }
390
391 // Setup the Weak External auxiliary symbol.
392 coff_symbol->Aux.resize(1);
393 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
394 coff_symbol->Aux[0].AuxType = ATWeakExternal;
395 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
396 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
397 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000398
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000399 coff_symbol->MC = &Symbol;
Peter Collingbourne89886872013-04-22 18:48:56 +0000400 } else {
Rafael Espindolaea9f9d42014-05-01 19:02:03 +0000401 const MCSymbolData &ResSymData = Assembler.getSymbolData(Symbol);
Rafael Espindola575f79a2014-05-01 13:37:57 +0000402 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000403 coff_symbol->Data.Value = getSymbolValue(Symbol, Layout);
Reid Klecknerc1e76212013-09-17 23:18:05 +0000404
Peter Collingbourne89886872013-04-22 18:48:56 +0000405 coff_symbol->Data.Type = (ResSymData.getFlags() & 0x0000FFFF) >> 0;
406 coff_symbol->Data.StorageClass = (ResSymData.getFlags() & 0x00FF0000) >> 16;
407
408 // If no storage class was specified in the streamer, define it here.
409 if (coff_symbol->Data.StorageClass == 0) {
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000410 bool IsExternal = ResSymData.isExternal() ||
411 (!ResSymData.getFragment() && !Symbol.isVariable());
Peter Collingbourne89886872013-04-22 18:48:56 +0000412
David Majnemer299674e2014-07-13 04:31:19 +0000413 coff_symbol->Data.StorageClass = IsExternal
414 ? COFF::IMAGE_SYM_CLASS_EXTERNAL
415 : COFF::IMAGE_SYM_CLASS_STATIC;
Peter Collingbourne89886872013-04-22 18:48:56 +0000416 }
417
Rafael Espindola575f79a2014-05-01 13:37:57 +0000418 if (!Base) {
Reid Klecknerc1e76212013-09-17 23:18:05 +0000419 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola575f79a2014-05-01 13:37:57 +0000420 } else {
421 const MCSymbolData &BaseData = Assembler.getSymbolData(*Base);
Benjamin Kramer3e67db92014-10-11 15:07:21 +0000422 if (BaseData.getFragment()) {
Rafael Espindola0766ae02014-06-06 19:26:12 +0000423 COFFSection *Sec =
Benjamin Kramer3e67db92014-10-11 15:07:21 +0000424 SectionMap[&BaseData.getFragment()->getParent()->getSection()];
Rafael Espindola0766ae02014-06-06 19:26:12 +0000425
426 if (coff_symbol->Section && coff_symbol->Section != Sec)
427 report_fatal_error("conflicting sections for symbol");
428
429 coff_symbol->Section = Sec;
430 }
Rafael Espindola575f79a2014-05-01 13:37:57 +0000431 }
Peter Collingbourne89886872013-04-22 18:48:56 +0000432
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000433 coff_symbol->MC = &Symbol;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000434 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000435}
436
Nico Rieck01143f92014-02-25 09:50:40 +0000437// Maximum offsets for different string table entry encodings.
438static const unsigned Max6DecimalOffset = 999999;
439static const unsigned Max7DecimalOffset = 9999999;
440static const uint64_t MaxBase64Offset = 0xFFFFFFFFFULL; // 64^6, including 0
441
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000442// Encode a string table entry offset in base 64, padded to 6 chars, and
443// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
444// Buffer must be at least 8 bytes large. No terminating null appended.
445static void encodeBase64StringEntry(char* Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000446 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000447 "Illegal section name encoding for value");
448
449 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
450 "abcdefghijklmnopqrstuvwxyz"
451 "0123456789+/";
452
453 Buffer[0] = '/';
454 Buffer[1] = '/';
455
456 char* Ptr = Buffer + 7;
457 for (unsigned i = 0; i < 6; ++i) {
458 unsigned Rem = Value % 64;
459 Value /= 64;
460 *(Ptr--) = Alphabet[Rem];
461 }
462}
463
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000464void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000465 if (S.Name.size() > COFF::NameSize) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000466 uint64_t StringTableEntry = Strings.getOffset(S.Name);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000467
Nico Rieck01143f92014-02-25 09:50:40 +0000468 if (StringTableEntry <= Max6DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000469 std::sprintf(S.Header.Name, "/%d", unsigned(StringTableEntry));
Nico Rieck01143f92014-02-25 09:50:40 +0000470 } else if (StringTableEntry <= Max7DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000471 // With seven digits, we have to skip the terminating null. Because
472 // sprintf always appends it, we use a larger temporary buffer.
473 char buffer[9] = { };
474 std::sprintf(buffer, "/%d", unsigned(StringTableEntry));
475 std::memcpy(S.Header.Name, buffer, 8);
Nico Rieck01143f92014-02-25 09:50:40 +0000476 } else if (StringTableEntry <= MaxBase64Offset) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000477 // Starting with 10,000,000, offsets are encoded as base64.
478 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000479 } else {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000480 report_fatal_error("COFF string table is greater than 64 GB.");
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000481 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000482 } else
483 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000484}
485
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000486void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
487 if (S.Name.size() > COFF::NameSize)
488 S.set_name_offset(Strings.getOffset(S.Name));
489 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000490 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000491}
492
David Majnemer299674e2014-07-13 04:31:19 +0000493bool WinCOFFObjectWriter::ExportSymbol(const MCSymbol &Symbol,
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000494 MCAssembler &Asm) {
495 // This doesn't seem to be right. Strings referred to from the .data section
496 // need symbols so they can be linked to code in the .text section right?
497
David Majnemer299674e2014-07-13 04:31:19 +0000498 // return Asm.isSymbolLinkerVisible(Symbol);
499
500 // Non-temporary labels should always be visible to the linker.
501 if (!Symbol.isTemporary())
502 return true;
503
504 // Absolute temporary labels are never visible.
505 if (!Symbol.isInSection())
506 return false;
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000507
508 // For now, all non-variable symbols are exported,
509 // the linker will sort the rest out for us.
David Majnemer299674e2014-07-13 04:31:19 +0000510 return !Symbol.isVariable();
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000511}
512
Michael J. Spencerd6283772010-09-27 08:58:26 +0000513bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
514 return (S->Header.Characteristics
515 & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000516}
517
518//------------------------------------------------------------------------------
519// entity writing methods
520
521void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000522 if (UseBigObj) {
523 WriteLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
524 WriteLE16(0xFFFF);
525 WriteLE16(COFF::BigObjHeader::MinBigObjectVersion);
526 WriteLE16(Header.Machine);
527 WriteLE32(Header.TimeDateStamp);
Benjamin Kramer97fbdd52015-04-17 11:12:43 +0000528 WriteBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
David Majnemer15f7ed92014-09-15 20:28:38 +0000529 WriteLE32(0);
530 WriteLE32(0);
531 WriteLE32(0);
532 WriteLE32(0);
David Majnemer4d571592014-09-15 19:42:42 +0000533 WriteLE32(Header.NumberOfSections);
534 WriteLE32(Header.PointerToSymbolTable);
535 WriteLE32(Header.NumberOfSymbols);
536 } else {
537 WriteLE16(Header.Machine);
538 WriteLE16(static_cast<int16_t>(Header.NumberOfSections));
539 WriteLE32(Header.TimeDateStamp);
540 WriteLE32(Header.PointerToSymbolTable);
541 WriteLE32(Header.NumberOfSymbols);
542 WriteLE16(Header.SizeOfOptionalHeader);
543 WriteLE16(Header.Characteristics);
544 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000545}
546
David Blaikief564ab62014-04-15 05:25:03 +0000547void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
548 WriteBytes(StringRef(S.Data.Name, COFF::NameSize));
549 WriteLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000550 if (UseBigObj)
551 WriteLE32(S.Data.SectionNumber);
552 else
553 WriteLE16(static_cast<int16_t>(S.Data.SectionNumber));
David Blaikief564ab62014-04-15 05:25:03 +0000554 WriteLE16(S.Data.Type);
555 Write8(S.Data.StorageClass);
556 Write8(S.Data.NumberOfAuxSymbols);
557 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000558}
559
560void WinCOFFObjectWriter::WriteAuxiliarySymbols(
561 const COFFSymbol::AuxiliarySymbols &S) {
562 for(COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
563 i != e; ++i) {
564 switch(i->AuxType) {
565 case ATFunctionDefinition:
566 WriteLE32(i->Aux.FunctionDefinition.TagIndex);
567 WriteLE32(i->Aux.FunctionDefinition.TotalSize);
568 WriteLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
569 WriteLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
570 WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000571 if (UseBigObj)
572 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000573 break;
574 case ATbfAndefSymbol:
575 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
576 WriteLE16(i->Aux.bfAndefSymbol.Linenumber);
577 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
578 WriteLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
579 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000580 if (UseBigObj)
581 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000582 break;
583 case ATWeakExternal:
584 WriteLE32(i->Aux.WeakExternal.TagIndex);
585 WriteLE32(i->Aux.WeakExternal.Characteristics);
586 WriteZeros(sizeof(i->Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000587 if (UseBigObj)
588 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000589 break;
590 case ATFile:
David Majnemer4d571592014-09-15 19:42:42 +0000591 WriteBytes(
592 StringRef(reinterpret_cast<const char *>(&i->Aux),
593 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000594 break;
595 case ATSectionDefinition:
596 WriteLE32(i->Aux.SectionDefinition.Length);
597 WriteLE16(i->Aux.SectionDefinition.NumberOfRelocations);
598 WriteLE16(i->Aux.SectionDefinition.NumberOfLinenumbers);
599 WriteLE32(i->Aux.SectionDefinition.CheckSum);
David Majnemer4d571592014-09-15 19:42:42 +0000600 WriteLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000601 Write8(i->Aux.SectionDefinition.Selection);
602 WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000603 WriteLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number >> 16));
604 if (UseBigObj)
605 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000606 break;
607 }
608 }
609}
610
611void WinCOFFObjectWriter::WriteSectionHeader(const COFF::section &S) {
612 WriteBytes(StringRef(S.Name, COFF::NameSize));
613
614 WriteLE32(S.VirtualSize);
615 WriteLE32(S.VirtualAddress);
616 WriteLE32(S.SizeOfRawData);
617 WriteLE32(S.PointerToRawData);
618 WriteLE32(S.PointerToRelocations);
619 WriteLE32(S.PointerToLineNumbers);
620 WriteLE16(S.NumberOfRelocations);
621 WriteLE16(S.NumberOfLineNumbers);
622 WriteLE32(S.Characteristics);
623}
624
625void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
626 WriteLE32(R.VirtualAddress);
627 WriteLE32(R.SymbolTableIndex);
628 WriteLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000629}
630
631////////////////////////////////////////////////////////////////////////////////
632// MCObjectWriter interface implementations
633
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000634void WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
635 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000636 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000637 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000638 for (const auto &Section : Asm)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000639 DefineSection(Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000640
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000641 for (const MCSymbol &Symbol : Asm.symbols())
642 if (ExportSymbol(Symbol, Asm))
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000643 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000644}
645
David Majnemer2cc4bc772014-11-11 08:43:57 +0000646bool WinCOFFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000647 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000648 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000649 // MS LINK expects to be able to replace all references to a function with a
650 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
651 // away any relocations to functions.
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000652 if ((((SymA.getData().getFlags() & COFF::SF_TypeMask) >>
653 COFF::SF_TypeShift) >>
David Majnemer2cc4bc772014-11-11 08:43:57 +0000654 COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
655 return false;
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000656 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000657 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000658}
659
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000660bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
661 const MCSymbolData &SD = Sym.getData();
Rafael Espindola88af4112015-04-17 11:27:13 +0000662 if (!SD.isExternal())
663 return false;
664
Rafael Espindola88af4112015-04-17 11:27:13 +0000665 if (!Sym.isInSection())
666 return false;
667
668 const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
669 if (!Sec.getCOMDATSymbol())
670 return false;
671
672 // It looks like for COFF it is invalid to replace a reference to a global
673 // in a comdat with a reference to a local.
674 // FIXME: Add a specification reference if available.
675 return true;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000676}
677
Rafael Espindola26585542015-01-19 21:11:14 +0000678void WinCOFFObjectWriter::RecordRelocation(
679 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
680 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000681 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000682
Reid Klecknerdae7b4e2013-07-15 19:41:21 +0000683 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola88af4112015-04-17 11:27:13 +0000684 const MCSymbol &A = Symbol;
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000685 if (!Asm.hasSymbolData(A))
Jim Grosbach6f482002015-05-18 18:43:14 +0000686 Asm.getContext().reportFatalError(
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000687 Fixup.getLoc(),
688 Twine("symbol '") + A.getName() + "' can not be undefined");
689
David Blaikie908f4d42014-04-24 16:59:40 +0000690 const MCSymbolData &A_SD = Asm.getSymbolData(A);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000691
692 MCSectionData const *SectionData = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000693
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000694 // Mark this symbol as requiring an entry in the symbol table.
Michael J. Spencer17990d52010-10-16 08:25:57 +0000695 assert(SectionMap.find(&SectionData->getSection()) != SectionMap.end() &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000696 "Section must already have been defined in ExecutePostLayoutBinding!");
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000697 assert(SymbolMap.find(&A) != SymbolMap.end() &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000698 "Symbol must already have been defined in ExecutePostLayoutBinding!");
699
Michael J. Spencer17990d52010-10-16 08:25:57 +0000700 COFFSection *coff_section = SectionMap[&SectionData->getSection()];
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000701 COFFSymbol *coff_symbol = SymbolMap[&A];
Rafael Espindolaed164772011-04-20 14:01:45 +0000702 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000703 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000704
David Majnemera45a1762014-01-06 07:39:46 +0000705 if (SymB) {
706 const MCSymbol *B = &SymB->getSymbol();
David Blaikie908f4d42014-04-24 16:59:40 +0000707 const MCSymbolData &B_SD = Asm.getSymbolData(*B);
David Majnemera45a1762014-01-06 07:39:46 +0000708 if (!B_SD.getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000709 Asm.getContext().reportFatalError(
David Majnemera45a1762014-01-06 07:39:46 +0000710 Fixup.getLoc(),
711 Twine("symbol '") + B->getName() +
712 "' can not be undefined in a subtraction expression");
713
714 if (!A_SD.getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000715 Asm.getContext().reportFatalError(
David Majnemera45a1762014-01-06 07:39:46 +0000716 Fixup.getLoc(),
717 Twine("symbol '") + Symbol.getName() +
718 "' can not be undefined in a subtraction expression");
719
720 CrossSection = &Symbol.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000721
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000722 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000723 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000724
725 // In the case where we have SymbA and SymB, we just need to store the delta
726 // between the two symbols. Update FixedValue to account for the delta, and
727 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000728 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000729 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000730 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000731 return;
David Majnemer1de30942015-02-09 06:31:31 +0000732 }
733
734 // Offset of the relocation in the section
735 int64_t OffsetOfRelocation =
736 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
737
Andy Ayers9e5c8512015-05-14 01:10:41 +0000738 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000739 } else {
740 FixedValue = Target.getConstant();
741 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000742
743 COFFRelocation Reloc;
744
Daniel Dunbar727be432010-07-31 21:08:54 +0000745 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000746 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000747
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000748 // Turn relocations for temporary symbols into section relocations.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000749 if (coff_symbol->MC->isTemporary() || CrossSection) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000750 Reloc.Symb = coff_symbol->Section->Symbol;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000751 FixedValue +=
752 Layout.getFragmentOffset(coff_symbol->MC->getData().getFragment()) +
753 coff_symbol->MC->getData().getOffset();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000754 } else
755 Reloc.Symb = coff_symbol;
756
757 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000758
759 Reloc.Data.VirtualAddress += Fixup.getOffset();
Saleem Abdulrasool10ed0ba2015-01-22 04:03:32 +0000760 Reloc.Data.Type =
761 TargetObjectWriter->getRelocType(Target, Fixup, CrossSection,
762 Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000763
764 // FIXME: Can anyone explain what this does other than adjust for the size
765 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000766 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
767 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
768 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
769 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000770 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000771
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000772 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
773 switch (Reloc.Data.Type) {
774 case COFF::IMAGE_REL_ARM_ABSOLUTE:
775 case COFF::IMAGE_REL_ARM_ADDR32:
776 case COFF::IMAGE_REL_ARM_ADDR32NB:
777 case COFF::IMAGE_REL_ARM_TOKEN:
778 case COFF::IMAGE_REL_ARM_SECTION:
779 case COFF::IMAGE_REL_ARM_SECREL:
780 break;
781 case COFF::IMAGE_REL_ARM_BRANCH11:
782 case COFF::IMAGE_REL_ARM_BLX11:
783 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
784 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
785 // for Windows CE).
786 case COFF::IMAGE_REL_ARM_BRANCH24:
787 case COFF::IMAGE_REL_ARM_BLX24:
788 case COFF::IMAGE_REL_ARM_MOV32A:
789 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
790 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000791 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000792 // generate the relocations however the rest of the MSVC toolchain is
793 // unable to handle it.
794 llvm_unreachable("unsupported relocation");
795 break;
796 case COFF::IMAGE_REL_ARM_MOV32T:
797 break;
798 case COFF::IMAGE_REL_ARM_BRANCH20T:
799 case COFF::IMAGE_REL_ARM_BRANCH24T:
800 case COFF::IMAGE_REL_ARM_BLX23T:
801 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
802 // perform a 4 byte adjustment to the relocation. Relative branches are
803 // offset by 4 on ARM, however, because there is no RELA relocations, all
804 // branches are offset by 4.
805 FixedValue = FixedValue + 4;
806 break;
807 }
808 }
809
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000810 if (TargetObjectWriter->recordRelocation(Fixup))
811 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000812}
813
Rafael Espindolabce26a12010-10-05 15:11:03 +0000814void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000815 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000816 size_t SectionsSize = Sections.size();
817 if (SectionsSize > static_cast<size_t>(INT32_MAX))
818 report_fatal_error(
819 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000820
David Majnemer4d571592014-09-15 19:42:42 +0000821 // Assign symbol and section indexes and offsets.
822 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
823
824 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
825
826 DenseMap<COFFSection *, int32_t> SectionIndices(
827 NextPowerOf2(NumberOfSections));
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000828
829 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000830 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000831 for (const auto &Section : Sections) {
David Majnemer4d571592014-09-15 19:42:42 +0000832 SectionIndices[Section.get()] = Number;
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000833 Section->Number = Number;
834 Section->Symbol->Data.SectionNumber = Number;
835 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000836 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000837 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000838
David Majnemer4d571592014-09-15 19:42:42 +0000839 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000840 Header.NumberOfSymbols = 0;
841
David Majnemer4d571592014-09-15 19:42:42 +0000842 for (auto FI = Asm.file_names_begin(), FE = Asm.file_names_end();
843 FI != FE; ++FI) {
844 // round up to calculate the number of auxiliary symbols required
845 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
846 unsigned Count = (FI->size() + SymbolSize - 1) / SymbolSize;
847
848 COFFSymbol *file = createSymbol(".file");
849 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
850 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
851 file->Aux.resize(Count);
852
853 unsigned Offset = 0;
854 unsigned Length = FI->size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000855 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000856 Aux.AuxType = ATFile;
857
858 if (Length > SymbolSize) {
859 memcpy(&Aux.Aux, FI->c_str() + Offset, SymbolSize);
860 Length = Length - SymbolSize;
861 } else {
862 memcpy(&Aux.Aux, FI->c_str() + Offset, Length);
863 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
864 break;
865 }
866
867 Offset += SymbolSize;
868 }
869 }
870
David Majnemer9ab5ff12014-08-28 04:02:50 +0000871 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000872 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000873 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000874 Symbol->Data.SectionNumber = Symbol->Section->Number;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000875 if (Symbol->should_keep()) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000876 Symbol->Index = Header.NumberOfSymbols++;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000877 // Update auxiliary symbol info.
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000878 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
879 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000880 } else
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000881 Symbol->Index = -1;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000882 }
883
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000884 // Build string table.
885 for (const auto &S : Sections)
886 if (S->Name.size() > COFF::NameSize)
887 Strings.add(S->Name);
888 for (const auto &S : Symbols)
889 if (S->should_keep() && S->Name.size() > COFF::NameSize)
890 Strings.add(S->Name);
891 Strings.finalize(StringTableBuilder::WinCOFF);
892
893 // Set names.
894 for (const auto &S : Sections)
895 SetSectionName(*S);
896 for (auto &S : Symbols)
897 if (S->should_keep())
898 SetSymbolName(*S);
899
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000900 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000901 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000902 if (Symbol->Other) {
903 assert(Symbol->Index != -1);
904 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
905 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000906 "Symbol's aux symbol must be a Weak External!");
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000907 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000908 }
909 }
910
Nico Riecka37acf72013-07-06 12:13:10 +0000911 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000912 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000913 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000914 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
915 continue;
916
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000917 const MCSectionCOFF &MCSec =
918 static_cast<const MCSectionCOFF &>(Section->MCData->getSection());
Nico Riecka37acf72013-07-06 12:13:10 +0000919
Rafael Espindola0766ae02014-06-06 19:26:12 +0000920 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
921 assert(COMDAT);
922 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
923 assert(COMDATSymbol);
924 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000925 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000926 report_fatal_error(
927 Twine("Missing associated COMDAT section for section ") +
928 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000929
930 // Skip this section if the associated section is unused.
931 if (Assoc->Number == -1)
932 continue;
933
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000934 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = SectionIndices[Assoc];
Nico Riecka37acf72013-07-06 12:13:10 +0000935 }
936
937
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000938 // Assign file offsets to COFF object file structures.
939
940 unsigned offset = 0;
941
David Majnemer4d571592014-09-15 19:42:42 +0000942 if (UseBigObj)
943 offset += COFF::Header32Size;
944 else
945 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000946 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000947
Yaron Keren56919ef2014-12-04 08:30:39 +0000948 for (const auto &Section : Asm) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000949 COFFSection *Sec = SectionMap[&Section.getSection()];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000950
Michael J. Spencerd6283772010-09-27 08:58:26 +0000951 if (Sec->Number == -1)
952 continue;
953
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000954 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000955
Michael J. Spencerd6283772010-09-27 08:58:26 +0000956 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000957 // Align the section data to a four byte boundary.
David Majnemerab2b25b2015-02-11 22:51:55 +0000958 offset = RoundUpToAlignment(offset, 4);
959 Sec->Header.PointerToRawData = offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000960
961 offset += Sec->Header.SizeOfRawData;
962 }
963
964 if (Sec->Relocations.size() > 0) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000965 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
966
967 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000968 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000969 // size is found in reloc #0. Microsoft tools understand this.
970 Sec->Header.NumberOfRelocations = 0xffff;
971 } else {
972 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
973 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000974 Sec->Header.PointerToRelocations = offset;
975
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000976 if (RelocationsOverflow) {
977 // Reloc #0 will contain actual count, so make room for it.
978 offset += COFF::RelocationSize;
979 }
980
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000981 offset += COFF::RelocationSize * Sec->Relocations.size();
982
Yaron Keren56919ef2014-12-04 08:30:39 +0000983 for (auto &Relocation : Sec->Relocations) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000984 assert(Relocation.Symb->Index != -1);
985 Relocation.Data.SymbolTableIndex = Relocation.Symb->Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000986 }
987 }
988
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000989 assert(Sec->Symbol->Aux.size() == 1 &&
990 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000991 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000992 assert(Aux.AuxType == ATSectionDefinition &&
993 "Section's symbol's aux symbol must be a Section Definition!");
994 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
995 Aux.Aux.SectionDefinition.NumberOfRelocations =
996 Sec->Header.NumberOfRelocations;
997 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
998 Sec->Header.NumberOfLineNumbers;
999 }
1000
1001 Header.PointerToSymbolTable = offset;
1002
Rafael Espindola5ac4ad22013-12-04 02:26:54 +00001003 // We want a deterministic output. It looks like GNU as also writes 0 in here.
Rafael Espindola9201e6b2013-12-04 02:02:55 +00001004 Header.TimeDateStamp = 0;
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +00001005
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001006 // Write it all to disk...
1007 WriteFileHeader(Header);
1008
1009 {
1010 sections::iterator i, ie;
1011 MCAssembler::const_iterator j, je;
1012
Yaron Keren56919ef2014-12-04 08:30:39 +00001013 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001014 if (Section->Number != -1) {
1015 if (Section->Relocations.size() >= 0xffff)
1016 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
1017 WriteSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001018 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001019 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001020
1021 for (i = Sections.begin(), ie = Sections.end(),
1022 j = Asm.begin(), je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +00001023 (i != ie) && (j != je); ++i, ++j) {
1024
1025 if ((*i)->Number == -1)
1026 continue;
1027
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001028 if ((*i)->Header.PointerToRawData != 0) {
David Majnemer3df3c612015-02-11 22:22:30 +00001029 assert(OS.tell() <= (*i)->Header.PointerToRawData &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001030 "Section::PointerToRawData is insane!");
1031
David Majnemer3df3c612015-02-11 22:22:30 +00001032 unsigned SectionDataPadding = (*i)->Header.PointerToRawData - OS.tell();
1033 assert(SectionDataPadding < 4 &&
1034 "Should only need at most three bytes of padding!");
1035
1036 WriteZeros(SectionDataPadding);
1037
Jim Grosbach18e2fe42011-12-06 00:03:48 +00001038 Asm.writeSectionData(j, Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001039 }
1040
1041 if ((*i)->Relocations.size() > 0) {
1042 assert(OS.tell() == (*i)->Header.PointerToRelocations &&
1043 "Section::PointerToRelocations is insane!");
1044
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001045 if ((*i)->Relocations.size() >= 0xffff) {
1046 // In case of overflow, write actual relocation count as first
1047 // relocation. Including the synthetic reloc itself (+ 1).
1048 COFF::relocation r;
1049 r.VirtualAddress = (*i)->Relocations.size() + 1;
1050 r.SymbolTableIndex = 0;
1051 r.Type = 0;
1052 WriteRelocation(r);
1053 }
1054
Yaron Keren56919ef2014-12-04 08:30:39 +00001055 for (const auto &Relocation : (*i)->Relocations)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001056 WriteRelocation(Relocation.Data);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001057 } else
1058 assert((*i)->Header.PointerToRelocations == 0 &&
1059 "Section::PointerToRelocations is insane!");
1060 }
1061 }
1062
1063 assert(OS.tell() == Header.PointerToSymbolTable &&
1064 "Header::PointerToSymbolTable is insane!");
1065
Yaron Keren56919ef2014-12-04 08:30:39 +00001066 for (auto &Symbol : Symbols)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001067 if (Symbol->Index != -1)
1068 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001069
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001070 OS.write(Strings.data().data(), Strings.data().size());
Chris Lattner2c52b792010-07-11 22:07:02 +00001071}
1072
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001073MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_) :
1074 Machine(Machine_) {
1075}
1076
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001077// Pin the vtable to this file.
1078void MCWinCOFFObjectTargetWriter::anchor() {}
1079
Chris Lattner2c52b792010-07-11 22:07:02 +00001080//------------------------------------------------------------------------------
1081// WinCOFFObjectWriter factory function
1082
Rafael Espindola37099d92015-04-09 18:08:15 +00001083MCObjectWriter *
1084llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001085 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001086 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001087}