blob: 4e811bb2aa052920474d65becf822bbd3b83b6a9 [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"
David Majnemer4eecd302015-05-30 04:56:02 +000024#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/MC/MCObjectWriter.h"
26#include "llvm/MC/MCSection.h"
27#include "llvm/MC/MCSectionCOFF.h"
28#include "llvm/MC/MCSymbol.h"
29#include "llvm/MC/MCValue.h"
Hans Wennborgf26bfc12014-09-29 22:43:20 +000030#include "llvm/MC/StringTableBuilder.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000031#include "llvm/Support/COFF.h"
32#include "llvm/Support/Debug.h"
Hans Wennborgba80b5d2014-09-28 00:22:27 +000033#include "llvm/Support/Endian.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000034#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000035#include "llvm/Support/TimeValue.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000036#include <cstdio>
37
Chris Lattner2c52b792010-07-11 22:07:02 +000038using namespace llvm;
39
Chandler Carruthf58e3762014-04-22 03:04:17 +000040#define DEBUG_TYPE "WinCOFFObjectWriter"
41
Chris Lattner2c52b792010-07-11 22:07:02 +000042namespace {
Dmitri Gribenko226fea52013-01-13 16:01:15 +000043typedef SmallString<COFF::NameSize> name;
Chris Lattner2c52b792010-07-11 22:07:02 +000044
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000045enum AuxiliaryType {
46 ATFunctionDefinition,
47 ATbfAndefSymbol,
48 ATWeakExternal,
49 ATFile,
50 ATSectionDefinition
51};
Chris Lattner2c52b792010-07-11 22:07:02 +000052
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000053struct AuxSymbol {
Rafael Espindola11e9e212015-05-27 14:37:12 +000054 AuxiliaryType AuxType;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000055 COFF::Auxiliary Aux;
56};
Chris Lattner2c52b792010-07-11 22:07:02 +000057
Michael J. Spencerd6283772010-09-27 08:58:26 +000058class COFFSymbol;
59class COFFSection;
60
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000061class COFFSymbol {
62public:
63 COFF::symbol Data;
Chris Lattner2c52b792010-07-11 22:07:02 +000064
Dmitri Gribenko226fea52013-01-13 16:01:15 +000065 typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
Chris Lattner2c52b792010-07-11 22:07:02 +000066
Rafael Espindola11e9e212015-05-27 14:37:12 +000067 name Name;
68 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000069 AuxiliarySymbols Aux;
Rafael Espindola11e9e212015-05-27 14:37:12 +000070 COFFSymbol *Other;
71 COFFSection *Section;
72 int Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000073
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +000074 const MCSymbol *MC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000075
Dmitri Gribenko226fea52013-01-13 16:01:15 +000076 COFFSymbol(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000077 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000078
79 bool should_keep() const;
David Majnemer4eecd302015-05-30 04:56:02 +000080
81 int64_t getIndex() const { return Index; }
82 void setIndex(int Value) {
83 Index = Value;
84 if (MC)
85 MC->setIndex(static_cast<uint32_t>(Value));
86 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000087};
88
89// This class contains staging data for a COFF relocation entry.
90struct COFFRelocation {
91 COFF::relocation Data;
Rafael Espindola11e9e212015-05-27 14:37:12 +000092 COFFSymbol *Symb;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000093
Craig Topperbb694de2014-04-13 04:57:38 +000094 COFFRelocation() : Symb(nullptr) {}
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000095 static size_t size() { return COFF::RelocationSize; }
96};
97
98typedef std::vector<COFFRelocation> relocations;
99
100class COFFSection {
101public:
102 COFF::section Header;
103
Rafael Espindola11e9e212015-05-27 14:37:12 +0000104 std::string Name;
105 int Number;
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000106 MCSectionCOFF const *MCSection;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000107 COFFSymbol *Symbol;
108 relocations Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000109
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000110 COFFSection(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000111 static size_t size();
112};
113
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000114class WinCOFFObjectWriter : public MCObjectWriter {
115public:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000116 typedef std::vector<std::unique_ptr<COFFSymbol>> symbols;
David Blaikief564ab62014-04-15 05:25:03 +0000117 typedef std::vector<std::unique_ptr<COFFSection>> sections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000118
Rafael Espindola11e9e212015-05-27 14:37:12 +0000119 typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000120 typedef DenseMap<MCSection const *, COFFSection *> section_map;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000121
Ahmed Charles56440fd2014-03-06 05:51:42 +0000122 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000123
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000124 // Root level file contents.
125 COFF::header Header;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000126 sections Sections;
127 symbols Symbols;
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000128 StringTableBuilder Strings;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000129
130 // Maps used during object file creation.
131 section_map SectionMap;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000132 symbol_map SymbolMap;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000133
David Majnemer4d571592014-09-15 19:42:42 +0000134 bool UseBigObj;
135
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000136 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000137
Yaron Kerencca43c12014-09-16 21:31:04 +0000138 void reset() override {
139 memset(&Header, 0, sizeof(Header));
140 Header.Machine = TargetObjectWriter->getMachine();
141 Sections.clear();
142 Symbols.clear();
143 Strings.clear();
144 SectionMap.clear();
145 SymbolMap.clear();
146 MCObjectWriter::reset();
147 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000148
Michael J. Spencer17990d52010-10-16 08:25:57 +0000149 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola11e9e212015-05-27 14:37:12 +0000150 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000151 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000152
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000153 template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000154 object_t *createCOFFEntity(StringRef Name, list_t &List);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000155
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000156 void defineSection(MCSectionCOFF const &Sec);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000157 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000158 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000159
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000160 void SetSymbolName(COFFSymbol &S);
161 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000162
David Majnemer299674e2014-07-13 04:31:19 +0000163 bool ExportSymbol(const MCSymbol &Symbol, MCAssembler &Asm);
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000164
Michael J. Spencerd6283772010-09-27 08:58:26 +0000165 bool IsPhysicalSection(COFFSection *S);
166
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000167 // Entity writing methods.
168
169 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000170 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000171 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
172 void WriteSectionHeader(const COFF::section &S);
173 void WriteRelocation(const COFF::relocation &R);
174
175 // MCObjectWriter interface implementation.
176
Craig Topper34a61bc2014-03-08 07:02:02 +0000177 void ExecutePostLayoutBinding(MCAssembler &Asm,
178 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000179
Jim Grosbach36e60e92015-06-04 22:24:41 +0000180 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000181 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000182 const MCFragment &FB, bool InSet,
183 bool IsPCRel) const override;
184
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000185 bool isWeak(const MCSymbol &Sym) const override;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000186
Jim Grosbach36e60e92015-06-04 22:24:41 +0000187 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000188 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000189 MCValue Target, bool &IsPCRel,
190 uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000191
Jim Grosbach36e60e92015-06-04 22:24:41 +0000192 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000193};
Chris Lattner2c52b792010-07-11 22:07:02 +0000194}
195
Hans Wennborgba80b5d2014-09-28 00:22:27 +0000196static inline void write_uint32_le(void *Data, uint32_t Value) {
197 support::endian::write<uint32_t, support::little, support::unaligned>(Data,
198 Value);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000199}
200
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000201//------------------------------------------------------------------------------
202// Symbol class implementation
203
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000204COFFSymbol::COFFSymbol(StringRef name)
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000205 : Name(name.begin(), name.end()), Other(nullptr), Section(nullptr),
206 Relocations(0), MC(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000207 memset(&Data, 0, sizeof(Data));
208}
209
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000210// In the case that the name does not fit within 8 bytes, the offset
211// into the string table is stored in the last 4 bytes instead, leaving
212// the first 4 bytes as 0.
213void COFFSymbol::set_name_offset(uint32_t Offset) {
214 write_uint32_le(Data.Name + 0, 0);
215 write_uint32_le(Data.Name + 4, Offset);
216}
217
Michael J. Spencerd6283772010-09-27 08:58:26 +0000218/// logic to decide if the symbol should be reported in the symbol table
219bool COFFSymbol::should_keep() const {
220 // no section means its external, keep it
Craig Topperbb694de2014-04-13 04:57:38 +0000221 if (!Section)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000222 return true;
223
224 // if it has relocations pointing at it, keep it
Rafael Espindola11e9e212015-05-27 14:37:12 +0000225 if (Relocations > 0) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000226 assert(Section->Number != -1 && "Sections with relocations must be real!");
227 return true;
228 }
229
David Majnemer4eecd302015-05-30 04:56:02 +0000230 // if this is a safeseh handler, keep it
231 if (MC && (MC->getFlags() & COFF::SF_SafeSEH))
232 return true;
233
Michael J. Spencerd6283772010-09-27 08:58:26 +0000234 // if the section its in is being droped, drop it
235 if (Section->Number == -1)
Rafael Espindola11e9e212015-05-27 14:37:12 +0000236 return false;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000237
238 // if it is the section symbol, keep it
239 if (Section->Symbol == this)
240 return true;
241
242 // if its temporary, drop it
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000243 if (MC && MC->isTemporary())
244 return false;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000245
246 // otherwise, keep it
247 return true;
248}
249
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000250//------------------------------------------------------------------------------
251// Section class implementation
252
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000253COFFSection::COFFSection(StringRef name)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000254 : Name(name), MCSection(nullptr), Symbol(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000255 memset(&Header, 0, sizeof(Header));
256}
257
Rafael Espindola11e9e212015-05-27 14:37:12 +0000258size_t COFFSection::size() { return COFF::SectionSize; }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000259
260//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000261// WinCOFFObjectWriter class implementation
262
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000263WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000264 raw_pwrite_stream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000265 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000266 memset(&Header, 0, sizeof(Header));
Michael J. Spencer377aa202010-08-21 05:58:13 +0000267
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000268 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000269}
270
Michael J. Spencer17990d52010-10-16 08:25:57 +0000271COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000272 return createCOFFEntity<COFFSymbol>(Name, Symbols);
273}
274
Yaron Keren56919ef2014-12-04 08:30:39 +0000275COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000276 symbol_map::iterator i = SymbolMap.find(Symbol);
277 if (i != SymbolMap.end())
278 return i->second;
Yaron Keren56919ef2014-12-04 08:30:39 +0000279 COFFSymbol *RetSymbol =
280 createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000281 SymbolMap[Symbol] = RetSymbol;
282 return RetSymbol;
283}
284
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000285COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000286 return createCOFFEntity<COFFSection>(Name, Sections);
287}
288
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000289/// A template used to lookup or create a symbol/section, and initialize it if
290/// needed.
291template <typename object_t, typename list_t>
Rafael Espindola11e9e212015-05-27 14:37:12 +0000292object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name, list_t &List) {
David Blaikief564ab62014-04-15 05:25:03 +0000293 List.push_back(make_unique<object_t>(Name));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000294
David Blaikief564ab62014-04-15 05:25:03 +0000295 return List.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000296}
297
298/// This function takes a section data object from the assembler
299/// and creates the associated COFF section staging object.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000300void WinCOFFObjectWriter::defineSection(MCSectionCOFF const &Sec) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000301 COFFSection *coff_section = createSection(Sec.getSectionName());
Rafael Espindola11e9e212015-05-27 14:37:12 +0000302 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;
Rafael Espindola967d6a62015-05-21 21:02:35 +0000325 switch (Sec.getAlignment()) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000326 case 1:
327 Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES;
328 break;
329 case 2:
330 Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES;
331 break;
332 case 4:
333 Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES;
334 break;
335 case 8:
336 Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES;
337 break;
338 case 16:
339 Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES;
340 break;
341 case 32:
342 Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES;
343 break;
344 case 64:
345 Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES;
346 break;
347 case 128:
348 Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES;
349 break;
350 case 256:
351 Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES;
352 break;
353 case 512:
354 Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES;
355 break;
356 case 1024:
357 Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES;
358 break;
359 case 2048:
360 Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES;
361 break;
362 case 4096:
363 Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES;
364 break;
365 case 8192:
366 Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES;
367 break;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000368 default:
369 llvm_unreachable("unsupported section alignment");
370 }
371
372 // Bind internal COFF section to MC section.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000373 coff_section->MCSection = &Sec;
374 SectionMap[&Sec] = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000375}
376
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000377static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000378 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000379 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000380 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000381
382 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000383 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000384 return 0;
385
386 return Res;
387}
388
David Majnemera9bdb322014-04-08 22:33:40 +0000389/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000390/// and creates the associated COFF symbol staging object.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000391void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000392 MCAssembler &Assembler,
393 const MCAsmLayout &Layout) {
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000394 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Peter Collingbourne89886872013-04-22 18:48:56 +0000395 SymbolMap[&Symbol] = coff_symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000396
Rafael Espindola2229d332015-05-29 19:07:51 +0000397 if (Symbol.getFlags() & COFF::SF_WeakExternal) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000398 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
399
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000400 if (Symbol.isVariable()) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000401 const MCSymbolRefExpr *SymRef =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000402 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000403
Peter Collingbourne89886872013-04-22 18:48:56 +0000404 if (!SymRef)
405 report_fatal_error("Weak externals may only alias symbols");
Michael J. Spencer17990d52010-10-16 08:25:57 +0000406
Peter Collingbourne89886872013-04-22 18:48:56 +0000407 coff_symbol->Other = GetOrCreateCOFFSymbol(&SymRef->getSymbol());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000408 } else {
Yaron Keren075759a2015-03-30 15:42:36 +0000409 std::string WeakName = (".weak." + Symbol.getName() + ".default").str();
Michael J. Spencer17990d52010-10-16 08:25:57 +0000410 COFFSymbol *WeakDefault = createSymbol(WeakName);
411 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000412 WeakDefault->Data.StorageClass = COFF::IMAGE_SYM_CLASS_EXTERNAL;
413 WeakDefault->Data.Type = 0;
414 WeakDefault->Data.Value = 0;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000415 coff_symbol->Other = WeakDefault;
416 }
417
418 // Setup the Weak External auxiliary symbol.
419 coff_symbol->Aux.resize(1);
420 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
421 coff_symbol->Aux[0].AuxType = ATWeakExternal;
422 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
423 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000424 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000425
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000426 coff_symbol->MC = &Symbol;
Peter Collingbourne89886872013-04-22 18:48:56 +0000427 } else {
Rafael Espindola575f79a2014-05-01 13:37:57 +0000428 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000429 coff_symbol->Data.Value = getSymbolValue(Symbol, Layout);
Reid Klecknerc1e76212013-09-17 23:18:05 +0000430
David Majnemer4eecd302015-05-30 04:56:02 +0000431 coff_symbol->Data.Type =
432 (Symbol.getFlags() & COFF::SF_TypeMask) >> COFF::SF_TypeShift;
433 coff_symbol->Data.StorageClass =
434 (Symbol.getFlags() & COFF::SF_ClassMask) >> COFF::SF_ClassShift;
Peter Collingbourne89886872013-04-22 18:48:56 +0000435
436 // If no storage class was specified in the streamer, define it here.
David Majnemer4eecd302015-05-30 04:56:02 +0000437 if (coff_symbol->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000438 bool IsExternal = Symbol.isExternal() ||
439 (!Symbol.getFragment() && !Symbol.isVariable());
Peter Collingbourne89886872013-04-22 18:48:56 +0000440
David Majnemer299674e2014-07-13 04:31:19 +0000441 coff_symbol->Data.StorageClass = IsExternal
442 ? COFF::IMAGE_SYM_CLASS_EXTERNAL
443 : COFF::IMAGE_SYM_CLASS_STATIC;
Peter Collingbourne89886872013-04-22 18:48:56 +0000444 }
445
Rafael Espindola575f79a2014-05-01 13:37:57 +0000446 if (!Base) {
Reid Klecknerc1e76212013-09-17 23:18:05 +0000447 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola575f79a2014-05-01 13:37:57 +0000448 } else {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000449 if (Base->getFragment()) {
450 COFFSection *Sec = SectionMap[Base->getFragment()->getParent()];
Rafael Espindola0766ae02014-06-06 19:26:12 +0000451
452 if (coff_symbol->Section && coff_symbol->Section != Sec)
453 report_fatal_error("conflicting sections for symbol");
454
455 coff_symbol->Section = Sec;
456 }
Rafael Espindola575f79a2014-05-01 13:37:57 +0000457 }
Peter Collingbourne89886872013-04-22 18:48:56 +0000458
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000459 coff_symbol->MC = &Symbol;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000460 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000461}
462
Nico Rieck01143f92014-02-25 09:50:40 +0000463// Maximum offsets for different string table entry encodings.
464static const unsigned Max6DecimalOffset = 999999;
465static const unsigned Max7DecimalOffset = 9999999;
466static const uint64_t MaxBase64Offset = 0xFFFFFFFFFULL; // 64^6, including 0
467
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000468// Encode a string table entry offset in base 64, padded to 6 chars, and
469// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
470// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000471static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000472 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000473 "Illegal section name encoding for value");
474
475 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
476 "abcdefghijklmnopqrstuvwxyz"
477 "0123456789+/";
478
479 Buffer[0] = '/';
480 Buffer[1] = '/';
481
Rafael Espindola11e9e212015-05-27 14:37:12 +0000482 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000483 for (unsigned i = 0; i < 6; ++i) {
484 unsigned Rem = Value % 64;
485 Value /= 64;
486 *(Ptr--) = Alphabet[Rem];
487 }
488}
489
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000490void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000491 if (S.Name.size() > COFF::NameSize) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000492 uint64_t StringTableEntry = Strings.getOffset(S.Name);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000493
Nico Rieck01143f92014-02-25 09:50:40 +0000494 if (StringTableEntry <= Max6DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000495 std::sprintf(S.Header.Name, "/%d", unsigned(StringTableEntry));
Nico Rieck01143f92014-02-25 09:50:40 +0000496 } else if (StringTableEntry <= Max7DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000497 // With seven digits, we have to skip the terminating null. Because
498 // sprintf always appends it, we use a larger temporary buffer.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000499 char buffer[9] = {};
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000500 std::sprintf(buffer, "/%d", unsigned(StringTableEntry));
501 std::memcpy(S.Header.Name, buffer, 8);
Nico Rieck01143f92014-02-25 09:50:40 +0000502 } else if (StringTableEntry <= MaxBase64Offset) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000503 // Starting with 10,000,000, offsets are encoded as base64.
504 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000505 } else {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000506 report_fatal_error("COFF string table is greater than 64 GB.");
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000507 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000508 } else
509 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000510}
511
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000512void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
513 if (S.Name.size() > COFF::NameSize)
514 S.set_name_offset(Strings.getOffset(S.Name));
515 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000516 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000517}
518
David Majnemer299674e2014-07-13 04:31:19 +0000519bool WinCOFFObjectWriter::ExportSymbol(const MCSymbol &Symbol,
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000520 MCAssembler &Asm) {
521 // This doesn't seem to be right. Strings referred to from the .data section
522 // need symbols so they can be linked to code in the .text section right?
523
David Majnemer299674e2014-07-13 04:31:19 +0000524 // return Asm.isSymbolLinkerVisible(Symbol);
525
526 // Non-temporary labels should always be visible to the linker.
527 if (!Symbol.isTemporary())
528 return true;
529
530 // Absolute temporary labels are never visible.
531 if (!Symbol.isInSection())
532 return false;
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000533
534 // For now, all non-variable symbols are exported,
535 // the linker will sort the rest out for us.
David Majnemer299674e2014-07-13 04:31:19 +0000536 return !Symbol.isVariable();
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000537}
538
Michael J. Spencerd6283772010-09-27 08:58:26 +0000539bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000540 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
541 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000542}
543
544//------------------------------------------------------------------------------
545// entity writing methods
546
547void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000548 if (UseBigObj) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000549 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
550 writeLE16(0xFFFF);
551 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
552 writeLE16(Header.Machine);
553 writeLE32(Header.TimeDateStamp);
554 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
555 writeLE32(0);
556 writeLE32(0);
557 writeLE32(0);
558 writeLE32(0);
559 writeLE32(Header.NumberOfSections);
560 writeLE32(Header.PointerToSymbolTable);
561 writeLE32(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000562 } else {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000563 writeLE16(Header.Machine);
564 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
565 writeLE32(Header.TimeDateStamp);
566 writeLE32(Header.PointerToSymbolTable);
567 writeLE32(Header.NumberOfSymbols);
568 writeLE16(Header.SizeOfOptionalHeader);
569 writeLE16(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000570 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000571}
572
David Blaikief564ab62014-04-15 05:25:03 +0000573void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000574 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
575 writeLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000576 if (UseBigObj)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000577 writeLE32(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000578 else
Jim Grosbach36e60e92015-06-04 22:24:41 +0000579 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
580 writeLE16(S.Data.Type);
581 write8(S.Data.StorageClass);
582 write8(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000583 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000584}
585
586void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000587 const COFFSymbol::AuxiliarySymbols &S) {
588 for (COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
589 i != e; ++i) {
590 switch (i->AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000591 case ATFunctionDefinition:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000592 writeLE32(i->Aux.FunctionDefinition.TagIndex);
593 writeLE32(i->Aux.FunctionDefinition.TotalSize);
594 writeLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
595 writeLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000596 WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000597 if (UseBigObj)
598 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000599 break;
600 case ATbfAndefSymbol:
601 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
Jim Grosbach36e60e92015-06-04 22:24:41 +0000602 writeLE16(i->Aux.bfAndefSymbol.Linenumber);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000603 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
Jim Grosbach36e60e92015-06-04 22:24:41 +0000604 writeLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000605 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000606 if (UseBigObj)
607 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000608 break;
609 case ATWeakExternal:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000610 writeLE32(i->Aux.WeakExternal.TagIndex);
611 writeLE32(i->Aux.WeakExternal.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000612 WriteZeros(sizeof(i->Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000613 if (UseBigObj)
614 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000615 break;
616 case ATFile:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000617 writeBytes(
David Majnemer4d571592014-09-15 19:42:42 +0000618 StringRef(reinterpret_cast<const char *>(&i->Aux),
619 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000620 break;
621 case ATSectionDefinition:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000622 writeLE32(i->Aux.SectionDefinition.Length);
623 writeLE16(i->Aux.SectionDefinition.NumberOfRelocations);
624 writeLE16(i->Aux.SectionDefinition.NumberOfLinenumbers);
625 writeLE32(i->Aux.SectionDefinition.CheckSum);
626 writeLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number));
627 write8(i->Aux.SectionDefinition.Selection);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000628 WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
Jim Grosbach36e60e92015-06-04 22:24:41 +0000629 writeLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000630 if (UseBigObj)
631 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000632 break;
633 }
634 }
635}
636
637void WinCOFFObjectWriter::WriteSectionHeader(const COFF::section &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000638 writeBytes(StringRef(S.Name, COFF::NameSize));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000639
Jim Grosbach36e60e92015-06-04 22:24:41 +0000640 writeLE32(S.VirtualSize);
641 writeLE32(S.VirtualAddress);
642 writeLE32(S.SizeOfRawData);
643 writeLE32(S.PointerToRawData);
644 writeLE32(S.PointerToRelocations);
645 writeLE32(S.PointerToLineNumbers);
646 writeLE16(S.NumberOfRelocations);
647 writeLE16(S.NumberOfLineNumbers);
648 writeLE32(S.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000649}
650
651void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000652 writeLE32(R.VirtualAddress);
653 writeLE32(R.SymbolTableIndex);
654 writeLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000655}
656
657////////////////////////////////////////////////////////////////////////////////
658// MCObjectWriter interface implementations
659
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000660void WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
661 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000662 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000663 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000664 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000665 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000666
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000667 for (const MCSymbol &Symbol : Asm.symbols())
668 if (ExportSymbol(Symbol, Asm))
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000669 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000670}
671
Jim Grosbach36e60e92015-06-04 22:24:41 +0000672bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000673 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000674 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000675 // MS LINK expects to be able to replace all references to a function with a
676 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
677 // away any relocations to functions.
Rafael Espindola2229d332015-05-29 19:07:51 +0000678 if ((((SymA.getFlags() & COFF::SF_TypeMask) >> COFF::SF_TypeShift) >>
David Majnemer2cc4bc772014-11-11 08:43:57 +0000679 COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
680 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000681 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000682 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000683}
684
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000685bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000686 if (!Sym.isExternal())
Rafael Espindola88af4112015-04-17 11:27:13 +0000687 return false;
688
Rafael Espindola88af4112015-04-17 11:27:13 +0000689 if (!Sym.isInSection())
690 return false;
691
692 const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
693 if (!Sec.getCOMDATSymbol())
694 return false;
695
696 // It looks like for COFF it is invalid to replace a reference to a global
697 // in a comdat with a reference to a local.
698 // FIXME: Add a specification reference if available.
699 return true;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000700}
701
Jim Grosbach36e60e92015-06-04 22:24:41 +0000702void WinCOFFObjectWriter::recordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000703 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
704 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000705 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000706
Reid Klecknerdae7b4e2013-07-15 19:41:21 +0000707 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola88af4112015-04-17 11:27:13 +0000708 const MCSymbol &A = Symbol;
Rafael Espindola26410142015-06-01 01:52:18 +0000709 if (!A.isRegistered())
Rafael Espindola11e9e212015-05-27 14:37:12 +0000710 Asm.getContext().reportFatalError(Fixup.getLoc(),
711 Twine("symbol '") + A.getName() +
712 "' can not be undefined");
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000713
Rafael Espindola7549f872015-05-26 00:36:57 +0000714 MCSection *Section = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000715
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000716 // Mark this symbol as requiring an entry in the symbol table.
Rafael Espindola7549f872015-05-26 00:36:57 +0000717 assert(SectionMap.find(Section) != SectionMap.end() &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000718 "Section must already have been defined in ExecutePostLayoutBinding!");
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000719 assert(SymbolMap.find(&A) != SymbolMap.end() &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000720 "Symbol must already have been defined in ExecutePostLayoutBinding!");
721
Rafael Espindola7549f872015-05-26 00:36:57 +0000722 COFFSection *coff_section = SectionMap[Section];
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000723 COFFSymbol *coff_symbol = SymbolMap[&A];
Rafael Espindolaed164772011-04-20 14:01:45 +0000724 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000725 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000726
David Majnemera45a1762014-01-06 07:39:46 +0000727 if (SymB) {
728 const MCSymbol *B = &SymB->getSymbol();
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000729 if (!B->getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000730 Asm.getContext().reportFatalError(
David Majnemera45a1762014-01-06 07:39:46 +0000731 Fixup.getLoc(),
732 Twine("symbol '") + B->getName() +
733 "' can not be undefined in a subtraction expression");
734
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000735 if (!A.getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000736 Asm.getContext().reportFatalError(
David Majnemera45a1762014-01-06 07:39:46 +0000737 Fixup.getLoc(),
738 Twine("symbol '") + Symbol.getName() +
739 "' can not be undefined in a subtraction expression");
740
741 CrossSection = &Symbol.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000742
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000743 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000744 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000745
746 // In the case where we have SymbA and SymB, we just need to store the delta
747 // between the two symbols. Update FixedValue to account for the delta, and
748 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000749 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000750 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000751 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000752 return;
David Majnemer1de30942015-02-09 06:31:31 +0000753 }
754
755 // Offset of the relocation in the section
756 int64_t OffsetOfRelocation =
757 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
758
Andy Ayers9e5c8512015-05-14 01:10:41 +0000759 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000760 } else {
761 FixedValue = Target.getConstant();
762 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000763
764 COFFRelocation Reloc;
765
Daniel Dunbar727be432010-07-31 21:08:54 +0000766 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000767 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000768
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000769 // Turn relocations for temporary symbols into section relocations.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000770 if (coff_symbol->MC->isTemporary() || CrossSection) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000771 Reloc.Symb = coff_symbol->Section->Symbol;
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000772 FixedValue += Layout.getFragmentOffset(coff_symbol->MC->getFragment()) +
773 coff_symbol->MC->getOffset();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000774 } else
775 Reloc.Symb = coff_symbol;
776
777 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000778
779 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000780 Reloc.Data.Type = TargetObjectWriter->getRelocType(
781 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000782
783 // FIXME: Can anyone explain what this does other than adjust for the size
784 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000785 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
786 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
787 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
788 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000789 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000790
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000791 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
792 switch (Reloc.Data.Type) {
793 case COFF::IMAGE_REL_ARM_ABSOLUTE:
794 case COFF::IMAGE_REL_ARM_ADDR32:
795 case COFF::IMAGE_REL_ARM_ADDR32NB:
796 case COFF::IMAGE_REL_ARM_TOKEN:
797 case COFF::IMAGE_REL_ARM_SECTION:
798 case COFF::IMAGE_REL_ARM_SECREL:
799 break;
800 case COFF::IMAGE_REL_ARM_BRANCH11:
801 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000802 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
803 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
804 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000805 case COFF::IMAGE_REL_ARM_BRANCH24:
806 case COFF::IMAGE_REL_ARM_BLX24:
807 case COFF::IMAGE_REL_ARM_MOV32A:
808 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
809 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000810 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000811 // generate the relocations however the rest of the MSVC toolchain is
812 // unable to handle it.
813 llvm_unreachable("unsupported relocation");
814 break;
815 case COFF::IMAGE_REL_ARM_MOV32T:
816 break;
817 case COFF::IMAGE_REL_ARM_BRANCH20T:
818 case COFF::IMAGE_REL_ARM_BRANCH24T:
819 case COFF::IMAGE_REL_ARM_BLX23T:
820 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
821 // perform a 4 byte adjustment to the relocation. Relative branches are
822 // offset by 4 on ARM, however, because there is no RELA relocations, all
823 // branches are offset by 4.
824 FixedValue = FixedValue + 4;
825 break;
826 }
827 }
828
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000829 if (TargetObjectWriter->recordRelocation(Fixup))
830 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000831}
832
Jim Grosbach36e60e92015-06-04 22:24:41 +0000833void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000834 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000835 size_t SectionsSize = Sections.size();
836 if (SectionsSize > static_cast<size_t>(INT32_MAX))
837 report_fatal_error(
838 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000839
David Majnemer4d571592014-09-15 19:42:42 +0000840 // Assign symbol and section indexes and offsets.
841 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
842
843 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
844
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000845 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000846 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000847 for (const auto &Section : Sections) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000848 Section->Number = Number;
849 Section->Symbol->Data.SectionNumber = Number;
850 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000851 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000852 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000853
David Majnemer4d571592014-09-15 19:42:42 +0000854 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000855 Header.NumberOfSymbols = 0;
856
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000857 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000858 // round up to calculate the number of auxiliary symbols required
859 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000860 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000861
862 COFFSymbol *file = createSymbol(".file");
863 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
864 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
865 file->Aux.resize(Count);
866
867 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000868 unsigned Length = Name.size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000869 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000870 Aux.AuxType = ATFile;
871
872 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000873 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000874 Length = Length - SymbolSize;
875 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000876 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000877 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
878 break;
879 }
880
881 Offset += SymbolSize;
882 }
883 }
884
David Majnemer9ab5ff12014-08-28 04:02:50 +0000885 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000886 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000887 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000888 Symbol->Data.SectionNumber = Symbol->Section->Number;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000889 if (Symbol->should_keep()) {
David Majnemer4eecd302015-05-30 04:56:02 +0000890 Symbol->setIndex(Header.NumberOfSymbols++);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000891 // Update auxiliary symbol info.
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000892 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
893 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
David Majnemer4eecd302015-05-30 04:56:02 +0000894 } else {
895 Symbol->setIndex(-1);
896 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000897 }
898
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000899 // Build string table.
900 for (const auto &S : Sections)
901 if (S->Name.size() > COFF::NameSize)
902 Strings.add(S->Name);
903 for (const auto &S : Symbols)
904 if (S->should_keep() && S->Name.size() > COFF::NameSize)
905 Strings.add(S->Name);
906 Strings.finalize(StringTableBuilder::WinCOFF);
907
908 // Set names.
909 for (const auto &S : Sections)
910 SetSectionName(*S);
911 for (auto &S : Symbols)
912 if (S->should_keep())
913 SetSymbolName(*S);
914
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000915 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000916 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000917 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000918 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000919 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
920 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000921 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000922 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000923 }
924 }
925
Nico Riecka37acf72013-07-06 12:13:10 +0000926 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000927 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000928 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000929 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
930 continue;
931
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000932 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +0000933
Rafael Espindola0766ae02014-06-06 19:26:12 +0000934 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
935 assert(COMDAT);
936 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
937 assert(COMDATSymbol);
938 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000939 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000940 report_fatal_error(
941 Twine("Missing associated COMDAT section for section ") +
942 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000943
944 // Skip this section if the associated section is unused.
945 if (Assoc->Number == -1)
946 continue;
947
David Majnemer4eecd302015-05-30 04:56:02 +0000948 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +0000949 }
950
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000951 // Assign file offsets to COFF object file structures.
952
953 unsigned offset = 0;
954
David Majnemer4d571592014-09-15 19:42:42 +0000955 if (UseBigObj)
956 offset += COFF::Header32Size;
957 else
958 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000959 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000960
Yaron Keren56919ef2014-12-04 08:30:39 +0000961 for (const auto &Section : Asm) {
Rafael Espindolaa554c052015-05-25 23:14:17 +0000962 COFFSection *Sec = SectionMap[&Section];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000963
Michael J. Spencerd6283772010-09-27 08:58:26 +0000964 if (Sec->Number == -1)
965 continue;
966
Rafael Espindola5a1e80b2015-05-26 02:00:36 +0000967 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000968
Michael J. Spencerd6283772010-09-27 08:58:26 +0000969 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000970 // Align the section data to a four byte boundary.
David Majnemerab2b25b2015-02-11 22:51:55 +0000971 offset = RoundUpToAlignment(offset, 4);
972 Sec->Header.PointerToRawData = offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000973
974 offset += Sec->Header.SizeOfRawData;
975 }
976
977 if (Sec->Relocations.size() > 0) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000978 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
979
980 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000981 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000982 // size is found in reloc #0. Microsoft tools understand this.
983 Sec->Header.NumberOfRelocations = 0xffff;
984 } else {
985 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
986 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000987 Sec->Header.PointerToRelocations = offset;
988
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000989 if (RelocationsOverflow) {
990 // Reloc #0 will contain actual count, so make room for it.
991 offset += COFF::RelocationSize;
992 }
993
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000994 offset += COFF::RelocationSize * Sec->Relocations.size();
995
Yaron Keren56919ef2014-12-04 08:30:39 +0000996 for (auto &Relocation : Sec->Relocations) {
David Majnemer4eecd302015-05-30 04:56:02 +0000997 assert(Relocation.Symb->getIndex() != -1);
998 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000999 }
1000 }
1001
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001002 assert(Sec->Symbol->Aux.size() == 1 &&
1003 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +00001004 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001005 assert(Aux.AuxType == ATSectionDefinition &&
1006 "Section's symbol's aux symbol must be a Section Definition!");
1007 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
1008 Aux.Aux.SectionDefinition.NumberOfRelocations =
Rafael Espindola11e9e212015-05-27 14:37:12 +00001009 Sec->Header.NumberOfRelocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001010 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
Rafael Espindola11e9e212015-05-27 14:37:12 +00001011 Sec->Header.NumberOfLineNumbers;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001012 }
1013
1014 Header.PointerToSymbolTable = offset;
1015
Rafael Espindola5ac4ad22013-12-04 02:26:54 +00001016 // We want a deterministic output. It looks like GNU as also writes 0 in here.
Rafael Espindola9201e6b2013-12-04 02:02:55 +00001017 Header.TimeDateStamp = 0;
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +00001018
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001019 // Write it all to disk...
1020 WriteFileHeader(Header);
1021
1022 {
1023 sections::iterator i, ie;
Rafael Espindola63702e22015-06-01 01:30:01 +00001024 MCAssembler::iterator j, je;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001025
Yaron Keren56919ef2014-12-04 08:30:39 +00001026 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001027 if (Section->Number != -1) {
1028 if (Section->Relocations.size() >= 0xffff)
1029 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
1030 WriteSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001031 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001032 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001033
Rafael Espindola11e9e212015-05-27 14:37:12 +00001034 for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
1035 je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +00001036 (i != ie) && (j != je); ++i, ++j) {
1037
1038 if ((*i)->Number == -1)
1039 continue;
1040
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001041 if ((*i)->Header.PointerToRawData != 0) {
David Majnemer3df3c612015-02-11 22:22:30 +00001042 assert(OS.tell() <= (*i)->Header.PointerToRawData &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001043 "Section::PointerToRawData is insane!");
1044
David Majnemer3df3c612015-02-11 22:22:30 +00001045 unsigned SectionDataPadding = (*i)->Header.PointerToRawData - OS.tell();
1046 assert(SectionDataPadding < 4 &&
1047 "Should only need at most three bytes of padding!");
1048
1049 WriteZeros(SectionDataPadding);
1050
Rafael Espindola64acc7f2015-05-26 02:17:21 +00001051 Asm.writeSectionData(&*j, Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001052 }
1053
1054 if ((*i)->Relocations.size() > 0) {
1055 assert(OS.tell() == (*i)->Header.PointerToRelocations &&
1056 "Section::PointerToRelocations is insane!");
1057
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001058 if ((*i)->Relocations.size() >= 0xffff) {
1059 // In case of overflow, write actual relocation count as first
1060 // relocation. Including the synthetic reloc itself (+ 1).
1061 COFF::relocation r;
1062 r.VirtualAddress = (*i)->Relocations.size() + 1;
1063 r.SymbolTableIndex = 0;
1064 r.Type = 0;
1065 WriteRelocation(r);
1066 }
1067
Yaron Keren56919ef2014-12-04 08:30:39 +00001068 for (const auto &Relocation : (*i)->Relocations)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001069 WriteRelocation(Relocation.Data);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001070 } else
1071 assert((*i)->Header.PointerToRelocations == 0 &&
1072 "Section::PointerToRelocations is insane!");
1073 }
1074 }
1075
1076 assert(OS.tell() == Header.PointerToSymbolTable &&
1077 "Header::PointerToSymbolTable is insane!");
1078
Yaron Keren56919ef2014-12-04 08:30:39 +00001079 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001080 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001081 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001082
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001083 OS.write(Strings.data().data(), Strings.data().size());
Chris Lattner2c52b792010-07-11 22:07:02 +00001084}
1085
Rafael Espindola11e9e212015-05-27 14:37:12 +00001086MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1087 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001088
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001089// Pin the vtable to this file.
1090void MCWinCOFFObjectTargetWriter::anchor() {}
1091
Chris Lattner2c52b792010-07-11 22:07:02 +00001092//------------------------------------------------------------------------------
1093// WinCOFFObjectWriter factory function
1094
Rafael Espindola37099d92015-04-09 18:08:15 +00001095MCObjectWriter *
1096llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001097 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001098 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001099}