blob: 8d4d3df21ab48b959b2b4e0562a9a502046851ed [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"
Pete Cooperad9f9c32015-06-08 17:17:12 +000028#include "llvm/MC/MCSymbolCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#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"
David Majnemer6ddc6362015-09-01 21:23:58 +000035#include "llvm/Support/JamCRC.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000036#include "llvm/Support/TimeValue.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000037#include <cstdio>
David Majnemer088ba022015-09-01 23:46:11 +000038#include <ctime>
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000039
Chris Lattner2c52b792010-07-11 22:07:02 +000040using namespace llvm;
41
Chandler Carruthf58e3762014-04-22 03:04:17 +000042#define DEBUG_TYPE "WinCOFFObjectWriter"
43
Chris Lattner2c52b792010-07-11 22:07:02 +000044namespace {
Dmitri Gribenko226fea52013-01-13 16:01:15 +000045typedef SmallString<COFF::NameSize> name;
Chris Lattner2c52b792010-07-11 22:07:02 +000046
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000047enum AuxiliaryType {
48 ATFunctionDefinition,
49 ATbfAndefSymbol,
50 ATWeakExternal,
51 ATFile,
52 ATSectionDefinition
53};
Chris Lattner2c52b792010-07-11 22:07:02 +000054
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000055struct AuxSymbol {
Rafael Espindola11e9e212015-05-27 14:37:12 +000056 AuxiliaryType AuxType;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000057 COFF::Auxiliary Aux;
58};
Chris Lattner2c52b792010-07-11 22:07:02 +000059
Michael J. Spencerd6283772010-09-27 08:58:26 +000060class COFFSymbol;
61class COFFSection;
62
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000063class COFFSymbol {
64public:
65 COFF::symbol Data;
Chris Lattner2c52b792010-07-11 22:07:02 +000066
Dmitri Gribenko226fea52013-01-13 16:01:15 +000067 typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
Chris Lattner2c52b792010-07-11 22:07:02 +000068
Rafael Espindola11e9e212015-05-27 14:37:12 +000069 name Name;
70 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000071 AuxiliarySymbols Aux;
Rafael Espindola11e9e212015-05-27 14:37:12 +000072 COFFSymbol *Other;
73 COFFSection *Section;
74 int Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000075
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +000076 const MCSymbol *MC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000077
Dmitri Gribenko226fea52013-01-13 16:01:15 +000078 COFFSymbol(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000079 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000080
81 bool should_keep() const;
David Majnemer4eecd302015-05-30 04:56:02 +000082
83 int64_t getIndex() const { return Index; }
84 void setIndex(int Value) {
85 Index = Value;
86 if (MC)
87 MC->setIndex(static_cast<uint32_t>(Value));
88 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000089};
90
91// This class contains staging data for a COFF relocation entry.
92struct COFFRelocation {
93 COFF::relocation Data;
Rafael Espindola11e9e212015-05-27 14:37:12 +000094 COFFSymbol *Symb;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000095
Craig Topperbb694de2014-04-13 04:57:38 +000096 COFFRelocation() : Symb(nullptr) {}
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000097 static size_t size() { return COFF::RelocationSize; }
98};
99
100typedef std::vector<COFFRelocation> relocations;
101
102class COFFSection {
103public:
104 COFF::section Header;
105
Rafael Espindola11e9e212015-05-27 14:37:12 +0000106 std::string Name;
107 int Number;
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000108 MCSectionCOFF const *MCSection;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000109 COFFSymbol *Symbol;
110 relocations Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000111
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000112 COFFSection(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000113 static size_t size();
114};
115
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000116class WinCOFFObjectWriter : public MCObjectWriter {
117public:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000118 typedef std::vector<std::unique_ptr<COFFSymbol>> symbols;
David Blaikief564ab62014-04-15 05:25:03 +0000119 typedef std::vector<std::unique_ptr<COFFSection>> sections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000120
Rafael Espindola11e9e212015-05-27 14:37:12 +0000121 typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000122 typedef DenseMap<MCSection const *, COFFSection *> section_map;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000123
Ahmed Charles56440fd2014-03-06 05:51:42 +0000124 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000125
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000126 // Root level file contents.
127 COFF::header Header;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000128 sections Sections;
129 symbols Symbols;
Rafael Espindola21956e42015-10-23 21:48:05 +0000130 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000131
132 // Maps used during object file creation.
133 section_map SectionMap;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000134 symbol_map SymbolMap;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000135
David Majnemer4d571592014-09-15 19:42:42 +0000136 bool UseBigObj;
137
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000138 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000139
Yaron Kerencca43c12014-09-16 21:31:04 +0000140 void reset() override {
141 memset(&Header, 0, sizeof(Header));
142 Header.Machine = TargetObjectWriter->getMachine();
143 Sections.clear();
144 Symbols.clear();
145 Strings.clear();
146 SectionMap.clear();
147 SymbolMap.clear();
148 MCObjectWriter::reset();
149 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000150
Michael J. Spencer17990d52010-10-16 08:25:57 +0000151 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola11e9e212015-05-27 14:37:12 +0000152 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000153 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000154
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000155 template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000156 object_t *createCOFFEntity(StringRef Name, list_t &List);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000157
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000158 void defineSection(MCSectionCOFF const &Sec);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000159 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000160 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000161
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000162 void SetSymbolName(COFFSymbol &S);
163 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000164
David Majnemer299674e2014-07-13 04:31:19 +0000165 bool ExportSymbol(const MCSymbol &Symbol, MCAssembler &Asm);
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000166
Michael J. Spencerd6283772010-09-27 08:58:26 +0000167 bool IsPhysicalSection(COFFSection *S);
168
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000169 // Entity writing methods.
170
171 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000172 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000173 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000174 void writeSectionHeader(const COFF::section &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000175 void WriteRelocation(const COFF::relocation &R);
176
177 // MCObjectWriter interface implementation.
178
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000179 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper34a61bc2014-03-08 07:02:02 +0000180 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000181
Jim Grosbach36e60e92015-06-04 22:24:41 +0000182 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000183 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000184 const MCFragment &FB, bool InSet,
185 bool IsPCRel) const override;
186
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000187 bool isWeak(const MCSymbol &Sym) const override;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000188
Jim Grosbach36e60e92015-06-04 22:24:41 +0000189 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000190 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000191 MCValue Target, bool &IsPCRel,
192 uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000193
Jim Grosbach36e60e92015-06-04 22:24:41 +0000194 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000195};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000196}
Chris Lattner2c52b792010-07-11 22:07:02 +0000197
Hans Wennborgba80b5d2014-09-28 00:22:27 +0000198static inline void write_uint32_le(void *Data, uint32_t Value) {
199 support::endian::write<uint32_t, support::little, support::unaligned>(Data,
200 Value);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000201}
202
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000203//------------------------------------------------------------------------------
204// Symbol class implementation
205
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000206COFFSymbol::COFFSymbol(StringRef name)
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000207 : Name(name.begin(), name.end()), Other(nullptr), Section(nullptr),
208 Relocations(0), MC(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000209 memset(&Data, 0, sizeof(Data));
210}
211
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000212// In the case that the name does not fit within 8 bytes, the offset
213// into the string table is stored in the last 4 bytes instead, leaving
214// the first 4 bytes as 0.
215void COFFSymbol::set_name_offset(uint32_t Offset) {
216 write_uint32_le(Data.Name + 0, 0);
217 write_uint32_le(Data.Name + 4, Offset);
218}
219
Michael J. Spencerd6283772010-09-27 08:58:26 +0000220/// logic to decide if the symbol should be reported in the symbol table
221bool COFFSymbol::should_keep() const {
222 // no section means its external, keep it
Craig Topperbb694de2014-04-13 04:57:38 +0000223 if (!Section)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000224 return true;
225
226 // if it has relocations pointing at it, keep it
Rafael Espindola11e9e212015-05-27 14:37:12 +0000227 if (Relocations > 0) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000228 assert(Section->Number != -1 && "Sections with relocations must be real!");
229 return true;
230 }
231
David Majnemer4eecd302015-05-30 04:56:02 +0000232 // if this is a safeseh handler, keep it
Pete Cooper6bf1f302015-06-08 17:17:19 +0000233 if (MC && (cast<MCSymbolCOFF>(MC)->isSafeSEH()))
David Majnemer4eecd302015-05-30 04:56:02 +0000234 return true;
235
Michael J. Spencerd6283772010-09-27 08:58:26 +0000236 // if the section its in is being droped, drop it
237 if (Section->Number == -1)
Rafael Espindola11e9e212015-05-27 14:37:12 +0000238 return false;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000239
240 // if it is the section symbol, keep it
241 if (Section->Symbol == this)
242 return true;
243
244 // if its temporary, drop it
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000245 if (MC && MC->isTemporary())
246 return false;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000247
248 // otherwise, keep it
249 return true;
250}
251
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000252//------------------------------------------------------------------------------
253// Section class implementation
254
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000255COFFSection::COFFSection(StringRef name)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000256 : Name(name), MCSection(nullptr), Symbol(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000257 memset(&Header, 0, sizeof(Header));
258}
259
Rafael Espindola11e9e212015-05-27 14:37:12 +0000260size_t COFFSection::size() { return COFF::SectionSize; }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000261
262//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000263// WinCOFFObjectWriter class implementation
264
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000265WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000266 raw_pwrite_stream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000267 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000268 memset(&Header, 0, sizeof(Header));
Michael J. Spencer377aa202010-08-21 05:58:13 +0000269
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000270 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000271}
272
Michael J. Spencer17990d52010-10-16 08:25:57 +0000273COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000274 return createCOFFEntity<COFFSymbol>(Name, Symbols);
275}
276
Yaron Keren56919ef2014-12-04 08:30:39 +0000277COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000278 symbol_map::iterator i = SymbolMap.find(Symbol);
279 if (i != SymbolMap.end())
280 return i->second;
Yaron Keren56919ef2014-12-04 08:30:39 +0000281 COFFSymbol *RetSymbol =
282 createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000283 SymbolMap[Symbol] = RetSymbol;
284 return RetSymbol;
285}
286
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000287COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000288 return createCOFFEntity<COFFSection>(Name, Sections);
289}
290
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000291/// A template used to lookup or create a symbol/section, and initialize it if
292/// needed.
293template <typename object_t, typename list_t>
Rafael Espindola11e9e212015-05-27 14:37:12 +0000294object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name, list_t &List) {
David Blaikief564ab62014-04-15 05:25:03 +0000295 List.push_back(make_unique<object_t>(Name));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000296
David Blaikief564ab62014-04-15 05:25:03 +0000297 return List.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000298}
299
300/// This function takes a section data object from the assembler
301/// and creates the associated COFF section staging object.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000302void WinCOFFObjectWriter::defineSection(MCSectionCOFF const &Sec) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000303 COFFSection *coff_section = createSection(Sec.getSectionName());
Rafael Espindola11e9e212015-05-27 14:37:12 +0000304 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
Rafael Espindola0766ae02014-06-06 19:26:12 +0000305 if (Sec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
306 if (const MCSymbol *S = Sec.getCOMDATSymbol()) {
307 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
308 if (COMDATSymbol->Section)
309 report_fatal_error("two sections have the same comdat");
310 COMDATSymbol->Section = coff_section;
311 }
312 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000313
Michael J. Spencerd6283772010-09-27 08:58:26 +0000314 coff_section->Symbol = coff_symbol;
315 coff_symbol->Section = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000316 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000317
318 // In this case the auxiliary symbol is a Section Definition.
319 coff_symbol->Aux.resize(1);
320 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
321 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000322 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
323
324 coff_section->Header.Characteristics = Sec.getCharacteristics();
325
326 uint32_t &Characteristics = coff_section->Header.Characteristics;
Rafael Espindola967d6a62015-05-21 21:02:35 +0000327 switch (Sec.getAlignment()) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000328 case 1:
329 Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES;
330 break;
331 case 2:
332 Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES;
333 break;
334 case 4:
335 Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES;
336 break;
337 case 8:
338 Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES;
339 break;
340 case 16:
341 Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES;
342 break;
343 case 32:
344 Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES;
345 break;
346 case 64:
347 Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES;
348 break;
349 case 128:
350 Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES;
351 break;
352 case 256:
353 Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES;
354 break;
355 case 512:
356 Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES;
357 break;
358 case 1024:
359 Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES;
360 break;
361 case 2048:
362 Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES;
363 break;
364 case 4096:
365 Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES;
366 break;
367 case 8192:
368 Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES;
369 break;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000370 default:
371 llvm_unreachable("unsupported section alignment");
372 }
373
374 // Bind internal COFF section to MC section.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000375 coff_section->MCSection = &Sec;
376 SectionMap[&Sec] = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000377}
378
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000379static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000380 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000381 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000382 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000383
384 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000385 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000386 return 0;
387
388 return Res;
389}
390
David Majnemera9bdb322014-04-08 22:33:40 +0000391/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000392/// and creates the associated COFF symbol staging object.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000393void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000394 MCAssembler &Assembler,
395 const MCAsmLayout &Layout) {
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000396 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Peter Collingbourne89886872013-04-22 18:48:56 +0000397 SymbolMap[&Symbol] = coff_symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000398
Pete Cooper6bf1f302015-06-08 17:17:19 +0000399 if (cast<MCSymbolCOFF>(Symbol).isWeakExternal()) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000400 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
401
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000402 if (Symbol.isVariable()) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000403 const MCSymbolRefExpr *SymRef =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000404 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000405
Peter Collingbourne89886872013-04-22 18:48:56 +0000406 if (!SymRef)
407 report_fatal_error("Weak externals may only alias symbols");
Michael J. Spencer17990d52010-10-16 08:25:57 +0000408
Peter Collingbourne89886872013-04-22 18:48:56 +0000409 coff_symbol->Other = GetOrCreateCOFFSymbol(&SymRef->getSymbol());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000410 } else {
Yaron Keren075759a2015-03-30 15:42:36 +0000411 std::string WeakName = (".weak." + Symbol.getName() + ".default").str();
Michael J. Spencer17990d52010-10-16 08:25:57 +0000412 COFFSymbol *WeakDefault = createSymbol(WeakName);
413 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000414 WeakDefault->Data.StorageClass = COFF::IMAGE_SYM_CLASS_EXTERNAL;
415 WeakDefault->Data.Type = 0;
416 WeakDefault->Data.Value = 0;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000417 coff_symbol->Other = WeakDefault;
418 }
419
420 // Setup the Weak External auxiliary symbol.
421 coff_symbol->Aux.resize(1);
422 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
423 coff_symbol->Aux[0].AuxType = ATWeakExternal;
424 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
425 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000426 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000427
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000428 coff_symbol->MC = &Symbol;
Peter Collingbourne89886872013-04-22 18:48:56 +0000429 } else {
Rafael Espindola575f79a2014-05-01 13:37:57 +0000430 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000431 coff_symbol->Data.Value = getSymbolValue(Symbol, Layout);
Reid Klecknerc1e76212013-09-17 23:18:05 +0000432
Pete Cooperad9f9c32015-06-08 17:17:12 +0000433 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(Symbol);
434 coff_symbol->Data.Type = SymbolCOFF.getType();
Pete Cooper6bf1f302015-06-08 17:17:19 +0000435 coff_symbol->Data.StorageClass = SymbolCOFF.getClass();
Peter Collingbourne89886872013-04-22 18:48:56 +0000436
437 // If no storage class was specified in the streamer, define it here.
David Majnemer4eecd302015-05-30 04:56:02 +0000438 if (coff_symbol->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000439 bool IsExternal = Symbol.isExternal() ||
440 (!Symbol.getFragment() && !Symbol.isVariable());
Peter Collingbourne89886872013-04-22 18:48:56 +0000441
David Majnemer299674e2014-07-13 04:31:19 +0000442 coff_symbol->Data.StorageClass = IsExternal
443 ? COFF::IMAGE_SYM_CLASS_EXTERNAL
444 : COFF::IMAGE_SYM_CLASS_STATIC;
Peter Collingbourne89886872013-04-22 18:48:56 +0000445 }
446
Rafael Espindola575f79a2014-05-01 13:37:57 +0000447 if (!Base) {
Reid Klecknerc1e76212013-09-17 23:18:05 +0000448 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola575f79a2014-05-01 13:37:57 +0000449 } else {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000450 if (Base->getFragment()) {
451 COFFSection *Sec = SectionMap[Base->getFragment()->getParent()];
Rafael Espindola0766ae02014-06-06 19:26:12 +0000452
453 if (coff_symbol->Section && coff_symbol->Section != Sec)
454 report_fatal_error("conflicting sections for symbol");
455
456 coff_symbol->Section = Sec;
457 }
Rafael Espindola575f79a2014-05-01 13:37:57 +0000458 }
Peter Collingbourne89886872013-04-22 18:48:56 +0000459
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000460 coff_symbol->MC = &Symbol;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000461 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000462}
463
Nico Rieck01143f92014-02-25 09:50:40 +0000464// Maximum offsets for different string table entry encodings.
465static const unsigned Max6DecimalOffset = 999999;
466static const unsigned Max7DecimalOffset = 9999999;
467static const uint64_t MaxBase64Offset = 0xFFFFFFFFFULL; // 64^6, including 0
468
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000469// Encode a string table entry offset in base 64, padded to 6 chars, and
470// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
471// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000472static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000473 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000474 "Illegal section name encoding for value");
475
476 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
477 "abcdefghijklmnopqrstuvwxyz"
478 "0123456789+/";
479
480 Buffer[0] = '/';
481 Buffer[1] = '/';
482
Rafael Espindola11e9e212015-05-27 14:37:12 +0000483 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000484 for (unsigned i = 0; i < 6; ++i) {
485 unsigned Rem = Value % 64;
486 Value /= 64;
487 *(Ptr--) = Alphabet[Rem];
488 }
489}
490
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000491void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000492 if (S.Name.size() > COFF::NameSize) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000493 uint64_t StringTableEntry = Strings.getOffset(S.Name);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000494
Nico Rieck01143f92014-02-25 09:50:40 +0000495 if (StringTableEntry <= Max6DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000496 std::sprintf(S.Header.Name, "/%d", unsigned(StringTableEntry));
Nico Rieck01143f92014-02-25 09:50:40 +0000497 } else if (StringTableEntry <= Max7DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000498 // With seven digits, we have to skip the terminating null. Because
499 // sprintf always appends it, we use a larger temporary buffer.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000500 char buffer[9] = {};
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000501 std::sprintf(buffer, "/%d", unsigned(StringTableEntry));
502 std::memcpy(S.Header.Name, buffer, 8);
Nico Rieck01143f92014-02-25 09:50:40 +0000503 } else if (StringTableEntry <= MaxBase64Offset) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000504 // Starting with 10,000,000, offsets are encoded as base64.
505 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000506 } else {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000507 report_fatal_error("COFF string table is greater than 64 GB.");
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000508 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000509 } else
510 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000511}
512
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000513void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
514 if (S.Name.size() > COFF::NameSize)
515 S.set_name_offset(Strings.getOffset(S.Name));
516 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000517 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000518}
519
David Majnemer299674e2014-07-13 04:31:19 +0000520bool WinCOFFObjectWriter::ExportSymbol(const MCSymbol &Symbol,
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000521 MCAssembler &Asm) {
522 // This doesn't seem to be right. Strings referred to from the .data section
523 // need symbols so they can be linked to code in the .text section right?
524
David Majnemer299674e2014-07-13 04:31:19 +0000525 // return Asm.isSymbolLinkerVisible(Symbol);
526
527 // Non-temporary labels should always be visible to the linker.
528 if (!Symbol.isTemporary())
529 return true;
530
Reid Klecknera9d62532015-06-11 22:32:23 +0000531 // Temporary variable symbols are invisible.
532 if (Symbol.isVariable())
David Majnemer299674e2014-07-13 04:31:19 +0000533 return false;
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000534
Reid Klecknera9d62532015-06-11 22:32:23 +0000535 // Absolute temporary labels are never visible.
536 return !Symbol.isAbsolute();
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
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000637void 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
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000660void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000661 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.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000678 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
679 if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000680 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");
Reid Kleckner85dfb682015-09-16 16:26:29 +0000713 if (A.isTemporary() && A.isUndefined()) {
714 Asm.getContext().reportFatalError(Fixup.getLoc(),
715 Twine("assembler label '") + A.getName() +
716 "' can not be undefined");
717 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000718
Rafael Espindola7549f872015-05-26 00:36:57 +0000719 MCSection *Section = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000720
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000721 // Mark this symbol as requiring an entry in the symbol table.
Rafael Espindola7549f872015-05-26 00:36:57 +0000722 assert(SectionMap.find(Section) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000723 "Section must already have been defined in executePostLayoutBinding!");
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000724 assert(SymbolMap.find(&A) != SymbolMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000725 "Symbol must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000726
Rafael Espindola7549f872015-05-26 00:36:57 +0000727 COFFSection *coff_section = SectionMap[Section];
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000728 COFFSymbol *coff_symbol = SymbolMap[&A];
Rafael Espindolaed164772011-04-20 14:01:45 +0000729 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000730 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000731
David Majnemera45a1762014-01-06 07:39:46 +0000732 if (SymB) {
733 const MCSymbol *B = &SymB->getSymbol();
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000734 if (!B->getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000735 Asm.getContext().reportFatalError(
David Majnemera45a1762014-01-06 07:39:46 +0000736 Fixup.getLoc(),
737 Twine("symbol '") + B->getName() +
738 "' can not be undefined in a subtraction expression");
739
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000740 if (!A.getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000741 Asm.getContext().reportFatalError(
David Majnemera45a1762014-01-06 07:39:46 +0000742 Fixup.getLoc(),
743 Twine("symbol '") + Symbol.getName() +
744 "' can not be undefined in a subtraction expression");
745
746 CrossSection = &Symbol.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000747
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000748 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000749 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000750
751 // In the case where we have SymbA and SymB, we just need to store the delta
752 // between the two symbols. Update FixedValue to account for the delta, and
753 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000754 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000755 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000756 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000757 return;
David Majnemer1de30942015-02-09 06:31:31 +0000758 }
759
760 // Offset of the relocation in the section
761 int64_t OffsetOfRelocation =
762 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
763
Andy Ayers9e5c8512015-05-14 01:10:41 +0000764 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000765 } else {
766 FixedValue = Target.getConstant();
767 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000768
769 COFFRelocation Reloc;
770
Daniel Dunbar727be432010-07-31 21:08:54 +0000771 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000772 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000773
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000774 // Turn relocations for temporary symbols into section relocations.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000775 if (coff_symbol->MC->isTemporary() || CrossSection) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000776 Reloc.Symb = coff_symbol->Section->Symbol;
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000777 FixedValue += Layout.getFragmentOffset(coff_symbol->MC->getFragment()) +
778 coff_symbol->MC->getOffset();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000779 } else
780 Reloc.Symb = coff_symbol;
781
782 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000783
784 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000785 Reloc.Data.Type = TargetObjectWriter->getRelocType(
786 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000787
788 // FIXME: Can anyone explain what this does other than adjust for the size
789 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000790 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
791 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
792 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
793 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000794 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000795
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000796 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
797 switch (Reloc.Data.Type) {
798 case COFF::IMAGE_REL_ARM_ABSOLUTE:
799 case COFF::IMAGE_REL_ARM_ADDR32:
800 case COFF::IMAGE_REL_ARM_ADDR32NB:
801 case COFF::IMAGE_REL_ARM_TOKEN:
802 case COFF::IMAGE_REL_ARM_SECTION:
803 case COFF::IMAGE_REL_ARM_SECREL:
804 break;
805 case COFF::IMAGE_REL_ARM_BRANCH11:
806 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000807 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
808 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
809 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000810 case COFF::IMAGE_REL_ARM_BRANCH24:
811 case COFF::IMAGE_REL_ARM_BLX24:
812 case COFF::IMAGE_REL_ARM_MOV32A:
813 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
814 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000815 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000816 // generate the relocations however the rest of the MSVC toolchain is
817 // unable to handle it.
818 llvm_unreachable("unsupported relocation");
819 break;
820 case COFF::IMAGE_REL_ARM_MOV32T:
821 break;
822 case COFF::IMAGE_REL_ARM_BRANCH20T:
823 case COFF::IMAGE_REL_ARM_BRANCH24T:
824 case COFF::IMAGE_REL_ARM_BLX23T:
825 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
826 // perform a 4 byte adjustment to the relocation. Relative branches are
827 // offset by 4 on ARM, however, because there is no RELA relocations, all
828 // branches are offset by 4.
829 FixedValue = FixedValue + 4;
830 break;
831 }
832 }
833
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000834 if (TargetObjectWriter->recordRelocation(Fixup))
835 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000836}
837
Jim Grosbach36e60e92015-06-04 22:24:41 +0000838void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000839 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000840 size_t SectionsSize = Sections.size();
841 if (SectionsSize > static_cast<size_t>(INT32_MAX))
842 report_fatal_error(
843 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000844
David Majnemer4d571592014-09-15 19:42:42 +0000845 // Assign symbol and section indexes and offsets.
846 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
847
848 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
849
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000850 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000851 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000852 for (const auto &Section : Sections) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000853 Section->Number = Number;
854 Section->Symbol->Data.SectionNumber = Number;
855 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000856 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000857 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000858
David Majnemer4d571592014-09-15 19:42:42 +0000859 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000860 Header.NumberOfSymbols = 0;
861
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000862 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000863 // round up to calculate the number of auxiliary symbols required
864 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000865 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000866
867 COFFSymbol *file = createSymbol(".file");
868 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
869 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
870 file->Aux.resize(Count);
871
872 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000873 unsigned Length = Name.size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000874 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000875 Aux.AuxType = ATFile;
876
877 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000878 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000879 Length = Length - SymbolSize;
880 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000881 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000882 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
883 break;
884 }
885
886 Offset += SymbolSize;
887 }
888 }
889
David Majnemer9ab5ff12014-08-28 04:02:50 +0000890 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000891 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000892 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000893 Symbol->Data.SectionNumber = Symbol->Section->Number;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000894 if (Symbol->should_keep()) {
David Majnemer4eecd302015-05-30 04:56:02 +0000895 Symbol->setIndex(Header.NumberOfSymbols++);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000896 // Update auxiliary symbol info.
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000897 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
898 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
David Majnemer4eecd302015-05-30 04:56:02 +0000899 } else {
900 Symbol->setIndex(-1);
901 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000902 }
903
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000904 // Build string table.
905 for (const auto &S : Sections)
906 if (S->Name.size() > COFF::NameSize)
907 Strings.add(S->Name);
908 for (const auto &S : Symbols)
909 if (S->should_keep() && S->Name.size() > COFF::NameSize)
910 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000911 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000912
913 // Set names.
914 for (const auto &S : Sections)
915 SetSectionName(*S);
916 for (auto &S : Symbols)
917 if (S->should_keep())
918 SetSymbolName(*S);
919
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000920 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000921 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000922 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000923 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000924 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
925 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000926 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000927 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000928 }
929 }
930
Nico Riecka37acf72013-07-06 12:13:10 +0000931 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000932 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000933 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000934 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
935 continue;
936
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000937 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +0000938
Rafael Espindola0766ae02014-06-06 19:26:12 +0000939 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
940 assert(COMDAT);
941 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
942 assert(COMDATSymbol);
943 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000944 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000945 report_fatal_error(
946 Twine("Missing associated COMDAT section for section ") +
947 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000948
949 // Skip this section if the associated section is unused.
950 if (Assoc->Number == -1)
951 continue;
952
David Majnemer4eecd302015-05-30 04:56:02 +0000953 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +0000954 }
955
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000956 // Assign file offsets to COFF object file structures.
957
958 unsigned offset = 0;
959
David Majnemer4d571592014-09-15 19:42:42 +0000960 if (UseBigObj)
961 offset += COFF::Header32Size;
962 else
963 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000964 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000965
Yaron Keren56919ef2014-12-04 08:30:39 +0000966 for (const auto &Section : Asm) {
Rafael Espindolaa554c052015-05-25 23:14:17 +0000967 COFFSection *Sec = SectionMap[&Section];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000968
Michael J. Spencerd6283772010-09-27 08:58:26 +0000969 if (Sec->Number == -1)
970 continue;
971
Rafael Espindola5a1e80b2015-05-26 02:00:36 +0000972 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000973
Michael J. Spencerd6283772010-09-27 08:58:26 +0000974 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000975 // Align the section data to a four byte boundary.
David Majnemerab2b25b2015-02-11 22:51:55 +0000976 offset = RoundUpToAlignment(offset, 4);
977 Sec->Header.PointerToRawData = offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000978
979 offset += Sec->Header.SizeOfRawData;
980 }
981
982 if (Sec->Relocations.size() > 0) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000983 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
984
985 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000986 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000987 // size is found in reloc #0. Microsoft tools understand this.
988 Sec->Header.NumberOfRelocations = 0xffff;
989 } else {
990 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
991 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000992 Sec->Header.PointerToRelocations = offset;
993
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000994 if (RelocationsOverflow) {
995 // Reloc #0 will contain actual count, so make room for it.
996 offset += COFF::RelocationSize;
997 }
998
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000999 offset += COFF::RelocationSize * Sec->Relocations.size();
1000
Yaron Keren56919ef2014-12-04 08:30:39 +00001001 for (auto &Relocation : Sec->Relocations) {
David Majnemer4eecd302015-05-30 04:56:02 +00001002 assert(Relocation.Symb->getIndex() != -1);
1003 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001004 }
1005 }
1006
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001007 assert(Sec->Symbol->Aux.size() == 1 &&
1008 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +00001009 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001010 assert(Aux.AuxType == ATSectionDefinition &&
1011 "Section's symbol's aux symbol must be a Section Definition!");
1012 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
1013 Aux.Aux.SectionDefinition.NumberOfRelocations =
Rafael Espindola11e9e212015-05-27 14:37:12 +00001014 Sec->Header.NumberOfRelocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001015 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
Rafael Espindola11e9e212015-05-27 14:37:12 +00001016 Sec->Header.NumberOfLineNumbers;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001017 }
1018
1019 Header.PointerToSymbolTable = offset;
1020
NAKAMURA Takumi2f9e8c02015-09-05 01:17:49 +00001021#if (ENABLE_TIMESTAMPS == 1)
David Majnemer088ba022015-09-01 23:46:11 +00001022 // MS LINK expects to be able to use this timestamp to implement their
1023 // /INCREMENTAL feature.
1024 std::time_t Now = time(nullptr);
David Majnemer5ca46f02015-09-04 07:22:36 +00001025 if (Now < 0 || !isUInt<32>(Now))
David Majnemer088ba022015-09-01 23:46:11 +00001026 Now = UINT32_MAX;
1027 Header.TimeDateStamp = Now;
NAKAMURA Takumi2f9e8c02015-09-05 01:17:49 +00001028#else
1029 // We want a deterministic output. It looks like GNU as also writes 0 in here.
1030 Header.TimeDateStamp = 0;
1031#endif
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +00001032
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001033 // Write it all to disk...
1034 WriteFileHeader(Header);
1035
1036 {
1037 sections::iterator i, ie;
Rafael Espindola63702e22015-06-01 01:30:01 +00001038 MCAssembler::iterator j, je;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001039
Yaron Keren56919ef2014-12-04 08:30:39 +00001040 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001041 if (Section->Number != -1) {
1042 if (Section->Relocations.size() >= 0xffff)
1043 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Jim Grosbach56ed0bb2015-06-04 23:25:54 +00001044 writeSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001045 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001046 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001047
David Majnemer6ddc6362015-09-01 21:23:58 +00001048 SmallVector<char, 128> SectionContents;
Rafael Espindola11e9e212015-05-27 14:37:12 +00001049 for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
1050 je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +00001051 (i != ie) && (j != je); ++i, ++j) {
1052
1053 if ((*i)->Number == -1)
1054 continue;
1055
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001056 if ((*i)->Header.PointerToRawData != 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001057 assert(getStream().tell() <= (*i)->Header.PointerToRawData &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001058 "Section::PointerToRawData is insane!");
1059
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001060 unsigned SectionDataPadding =
1061 (*i)->Header.PointerToRawData - getStream().tell();
David Majnemer3df3c612015-02-11 22:22:30 +00001062 assert(SectionDataPadding < 4 &&
1063 "Should only need at most three bytes of padding!");
1064
1065 WriteZeros(SectionDataPadding);
1066
David Majnemer6ddc6362015-09-01 21:23:58 +00001067 // Save the contents of the section to a temporary buffer, we need this
1068 // to CRC the data before we dump it into the object file.
1069 SectionContents.clear();
1070 raw_svector_ostream VecOS(SectionContents);
1071 raw_pwrite_stream &OldStream = getStream();
1072 // Redirect the output stream to our buffer.
1073 setStream(VecOS);
1074 // Fill our buffer with the section data.
Rafael Espindola64acc7f2015-05-26 02:17:21 +00001075 Asm.writeSectionData(&*j, Layout);
David Majnemer6ddc6362015-09-01 21:23:58 +00001076 // Reset the stream back to what it was before.
1077 setStream(OldStream);
1078
1079 // Calculate our CRC with an initial value of '0', this is not how
1080 // JamCRC is specified but it aligns with the expected output.
1081 JamCRC JC(/*Init=*/0x00000000U);
1082 JC.update(SectionContents);
1083
1084 // Write the section contents to the object file.
1085 getStream() << SectionContents;
1086
1087 // Update the section definition auxiliary symbol to record the CRC.
1088 COFFSection *Sec = SectionMap[&*j];
1089 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
1090 assert(AuxSyms.size() == 1 &&
1091 AuxSyms[0].AuxType == ATSectionDefinition);
1092 AuxSymbol &SecDef = AuxSyms[0];
1093 SecDef.Aux.SectionDefinition.CheckSum = JC.getCRC();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001094 }
1095
1096 if ((*i)->Relocations.size() > 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001097 assert(getStream().tell() == (*i)->Header.PointerToRelocations &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001098 "Section::PointerToRelocations is insane!");
1099
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001100 if ((*i)->Relocations.size() >= 0xffff) {
1101 // In case of overflow, write actual relocation count as first
1102 // relocation. Including the synthetic reloc itself (+ 1).
1103 COFF::relocation r;
1104 r.VirtualAddress = (*i)->Relocations.size() + 1;
1105 r.SymbolTableIndex = 0;
1106 r.Type = 0;
1107 WriteRelocation(r);
1108 }
1109
Yaron Keren56919ef2014-12-04 08:30:39 +00001110 for (const auto &Relocation : (*i)->Relocations)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001111 WriteRelocation(Relocation.Data);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001112 } else
1113 assert((*i)->Header.PointerToRelocations == 0 &&
1114 "Section::PointerToRelocations is insane!");
1115 }
1116 }
1117
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001118 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001119 "Header::PointerToSymbolTable is insane!");
1120
Yaron Keren56919ef2014-12-04 08:30:39 +00001121 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001122 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001123 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001124
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001125 getStream().write(Strings.data().data(), Strings.data().size());
Chris Lattner2c52b792010-07-11 22:07:02 +00001126}
1127
Rafael Espindola11e9e212015-05-27 14:37:12 +00001128MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1129 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001130
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001131// Pin the vtable to this file.
1132void MCWinCOFFObjectTargetWriter::anchor() {}
1133
Chris Lattner2c52b792010-07-11 22:07:02 +00001134//------------------------------------------------------------------------------
1135// WinCOFFObjectWriter factory function
1136
Rafael Espindola37099d92015-04-09 18:08:15 +00001137MCObjectWriter *
1138llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001139 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001140 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001141}