blob: 875a61425f13733f5cdc88dd1db0465819d93f19 [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;
Oliver Stannard9be59af2015-11-17 10:00:43 +0000709 if (!A.isRegistered()) {
710 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000711 Twine("symbol '") + A.getName() +
712 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000713 return;
714 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000715 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000716 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000717 Twine("assembler label '") + A.getName() +
718 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000719 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000720 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000721
Rafael Espindola7549f872015-05-26 00:36:57 +0000722 MCSection *Section = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000723
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000724 // Mark this symbol as requiring an entry in the symbol table.
Rafael Espindola7549f872015-05-26 00:36:57 +0000725 assert(SectionMap.find(Section) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000726 "Section must already have been defined in executePostLayoutBinding!");
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000727 assert(SymbolMap.find(&A) != SymbolMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000728 "Symbol must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000729
Rafael Espindola7549f872015-05-26 00:36:57 +0000730 COFFSection *coff_section = SectionMap[Section];
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000731 COFFSymbol *coff_symbol = SymbolMap[&A];
Rafael Espindolaed164772011-04-20 14:01:45 +0000732 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000733 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000734
David Majnemera45a1762014-01-06 07:39:46 +0000735 if (SymB) {
736 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000737 if (!B->getFragment()) {
738 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000739 Fixup.getLoc(),
740 Twine("symbol '") + B->getName() +
741 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000742 return;
743 }
David Majnemera45a1762014-01-06 07:39:46 +0000744
Oliver Stannard9be59af2015-11-17 10:00:43 +0000745 if (!A.getFragment()) {
746 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000747 Fixup.getLoc(),
748 Twine("symbol '") + Symbol.getName() +
749 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000750 return;
751 }
David Majnemera45a1762014-01-06 07:39:46 +0000752
753 CrossSection = &Symbol.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000754
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000755 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000756 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000757
758 // In the case where we have SymbA and SymB, we just need to store the delta
759 // between the two symbols. Update FixedValue to account for the delta, and
760 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000761 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000762 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000763 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000764 return;
David Majnemer1de30942015-02-09 06:31:31 +0000765 }
766
767 // Offset of the relocation in the section
768 int64_t OffsetOfRelocation =
769 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
770
Andy Ayers9e5c8512015-05-14 01:10:41 +0000771 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000772 } else {
773 FixedValue = Target.getConstant();
774 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000775
776 COFFRelocation Reloc;
777
Daniel Dunbar727be432010-07-31 21:08:54 +0000778 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000779 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000780
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000781 // Turn relocations for temporary symbols into section relocations.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000782 if (coff_symbol->MC->isTemporary() || CrossSection) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000783 Reloc.Symb = coff_symbol->Section->Symbol;
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000784 FixedValue += Layout.getFragmentOffset(coff_symbol->MC->getFragment()) +
785 coff_symbol->MC->getOffset();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000786 } else
787 Reloc.Symb = coff_symbol;
788
789 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000790
791 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000792 Reloc.Data.Type = TargetObjectWriter->getRelocType(
793 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000794
795 // FIXME: Can anyone explain what this does other than adjust for the size
796 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000797 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
798 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
799 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
800 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000801 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000802
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000803 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
804 switch (Reloc.Data.Type) {
805 case COFF::IMAGE_REL_ARM_ABSOLUTE:
806 case COFF::IMAGE_REL_ARM_ADDR32:
807 case COFF::IMAGE_REL_ARM_ADDR32NB:
808 case COFF::IMAGE_REL_ARM_TOKEN:
809 case COFF::IMAGE_REL_ARM_SECTION:
810 case COFF::IMAGE_REL_ARM_SECREL:
811 break;
812 case COFF::IMAGE_REL_ARM_BRANCH11:
813 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000814 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
815 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
816 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000817 case COFF::IMAGE_REL_ARM_BRANCH24:
818 case COFF::IMAGE_REL_ARM_BLX24:
819 case COFF::IMAGE_REL_ARM_MOV32A:
820 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
821 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000822 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000823 // generate the relocations however the rest of the MSVC toolchain is
824 // unable to handle it.
825 llvm_unreachable("unsupported relocation");
826 break;
827 case COFF::IMAGE_REL_ARM_MOV32T:
828 break;
829 case COFF::IMAGE_REL_ARM_BRANCH20T:
830 case COFF::IMAGE_REL_ARM_BRANCH24T:
831 case COFF::IMAGE_REL_ARM_BLX23T:
832 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
833 // perform a 4 byte adjustment to the relocation. Relative branches are
834 // offset by 4 on ARM, however, because there is no RELA relocations, all
835 // branches are offset by 4.
836 FixedValue = FixedValue + 4;
837 break;
838 }
839 }
840
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000841 if (TargetObjectWriter->recordRelocation(Fixup))
842 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000843}
844
Jim Grosbach36e60e92015-06-04 22:24:41 +0000845void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000846 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000847 size_t SectionsSize = Sections.size();
848 if (SectionsSize > static_cast<size_t>(INT32_MAX))
849 report_fatal_error(
850 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000851
David Majnemer4d571592014-09-15 19:42:42 +0000852 // Assign symbol and section indexes and offsets.
853 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
854
855 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
856
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000857 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000858 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000859 for (const auto &Section : Sections) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000860 Section->Number = Number;
861 Section->Symbol->Data.SectionNumber = Number;
862 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000863 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000864 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000865
David Majnemer4d571592014-09-15 19:42:42 +0000866 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000867 Header.NumberOfSymbols = 0;
868
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000869 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000870 // round up to calculate the number of auxiliary symbols required
871 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000872 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000873
874 COFFSymbol *file = createSymbol(".file");
875 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
876 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
877 file->Aux.resize(Count);
878
879 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000880 unsigned Length = Name.size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000881 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000882 Aux.AuxType = ATFile;
883
884 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000885 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000886 Length = Length - SymbolSize;
887 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000888 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000889 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
890 break;
891 }
892
893 Offset += SymbolSize;
894 }
895 }
896
David Majnemer9ab5ff12014-08-28 04:02:50 +0000897 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000898 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000899 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000900 Symbol->Data.SectionNumber = Symbol->Section->Number;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000901 if (Symbol->should_keep()) {
David Majnemer4eecd302015-05-30 04:56:02 +0000902 Symbol->setIndex(Header.NumberOfSymbols++);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000903 // Update auxiliary symbol info.
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000904 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
905 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
David Majnemer4eecd302015-05-30 04:56:02 +0000906 } else {
907 Symbol->setIndex(-1);
908 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000909 }
910
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000911 // Build string table.
912 for (const auto &S : Sections)
913 if (S->Name.size() > COFF::NameSize)
914 Strings.add(S->Name);
915 for (const auto &S : Symbols)
916 if (S->should_keep() && S->Name.size() > COFF::NameSize)
917 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000918 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000919
920 // Set names.
921 for (const auto &S : Sections)
922 SetSectionName(*S);
923 for (auto &S : Symbols)
924 if (S->should_keep())
925 SetSymbolName(*S);
926
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000927 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000928 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000929 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000930 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000931 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
932 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000933 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000934 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000935 }
936 }
937
Nico Riecka37acf72013-07-06 12:13:10 +0000938 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000939 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000940 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000941 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
942 continue;
943
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000944 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +0000945
Rafael Espindola0766ae02014-06-06 19:26:12 +0000946 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
947 assert(COMDAT);
948 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
949 assert(COMDATSymbol);
950 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000951 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000952 report_fatal_error(
953 Twine("Missing associated COMDAT section for section ") +
954 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000955
956 // Skip this section if the associated section is unused.
957 if (Assoc->Number == -1)
958 continue;
959
David Majnemer4eecd302015-05-30 04:56:02 +0000960 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +0000961 }
962
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000963 // Assign file offsets to COFF object file structures.
964
Manuel Klimek272d3f12015-11-18 15:24:17 +0000965 unsigned offset = getInitialOffset();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000966
David Majnemer4d571592014-09-15 19:42:42 +0000967 if (UseBigObj)
968 offset += COFF::Header32Size;
969 else
970 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000971 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000972
Yaron Keren56919ef2014-12-04 08:30:39 +0000973 for (const auto &Section : Asm) {
Rafael Espindolaa554c052015-05-25 23:14:17 +0000974 COFFSection *Sec = SectionMap[&Section];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000975
Michael J. Spencerd6283772010-09-27 08:58:26 +0000976 if (Sec->Number == -1)
977 continue;
978
Rafael Espindola5a1e80b2015-05-26 02:00:36 +0000979 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000980
Michael J. Spencerd6283772010-09-27 08:58:26 +0000981 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000982 // Align the section data to a four byte boundary.
David Majnemerab2b25b2015-02-11 22:51:55 +0000983 offset = RoundUpToAlignment(offset, 4);
984 Sec->Header.PointerToRawData = offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000985
986 offset += Sec->Header.SizeOfRawData;
987 }
988
989 if (Sec->Relocations.size() > 0) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000990 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
991
992 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000993 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000994 // size is found in reloc #0. Microsoft tools understand this.
995 Sec->Header.NumberOfRelocations = 0xffff;
996 } else {
997 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
998 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000999 Sec->Header.PointerToRelocations = offset;
1000
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001001 if (RelocationsOverflow) {
1002 // Reloc #0 will contain actual count, so make room for it.
1003 offset += COFF::RelocationSize;
1004 }
1005
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001006 offset += COFF::RelocationSize * Sec->Relocations.size();
1007
Yaron Keren56919ef2014-12-04 08:30:39 +00001008 for (auto &Relocation : Sec->Relocations) {
David Majnemer4eecd302015-05-30 04:56:02 +00001009 assert(Relocation.Symb->getIndex() != -1);
1010 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001011 }
1012 }
1013
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001014 assert(Sec->Symbol->Aux.size() == 1 &&
1015 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +00001016 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001017 assert(Aux.AuxType == ATSectionDefinition &&
1018 "Section's symbol's aux symbol must be a Section Definition!");
1019 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
1020 Aux.Aux.SectionDefinition.NumberOfRelocations =
Rafael Espindola11e9e212015-05-27 14:37:12 +00001021 Sec->Header.NumberOfRelocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001022 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
Rafael Espindola11e9e212015-05-27 14:37:12 +00001023 Sec->Header.NumberOfLineNumbers;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001024 }
1025
1026 Header.PointerToSymbolTable = offset;
1027
NAKAMURA Takumi2f9e8c02015-09-05 01:17:49 +00001028#if (ENABLE_TIMESTAMPS == 1)
David Majnemer088ba022015-09-01 23:46:11 +00001029 // MS LINK expects to be able to use this timestamp to implement their
1030 // /INCREMENTAL feature.
1031 std::time_t Now = time(nullptr);
David Majnemer5ca46f02015-09-04 07:22:36 +00001032 if (Now < 0 || !isUInt<32>(Now))
David Majnemer088ba022015-09-01 23:46:11 +00001033 Now = UINT32_MAX;
1034 Header.TimeDateStamp = Now;
NAKAMURA Takumi2f9e8c02015-09-05 01:17:49 +00001035#else
1036 // We want a deterministic output. It looks like GNU as also writes 0 in here.
1037 Header.TimeDateStamp = 0;
1038#endif
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +00001039
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001040 // Write it all to disk...
1041 WriteFileHeader(Header);
1042
1043 {
1044 sections::iterator i, ie;
Rafael Espindola63702e22015-06-01 01:30:01 +00001045 MCAssembler::iterator j, je;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001046
Yaron Keren56919ef2014-12-04 08:30:39 +00001047 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001048 if (Section->Number != -1) {
1049 if (Section->Relocations.size() >= 0xffff)
1050 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Jim Grosbach56ed0bb2015-06-04 23:25:54 +00001051 writeSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001052 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001053 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001054
David Majnemer6ddc6362015-09-01 21:23:58 +00001055 SmallVector<char, 128> SectionContents;
Rafael Espindola11e9e212015-05-27 14:37:12 +00001056 for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
1057 je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +00001058 (i != ie) && (j != je); ++i, ++j) {
1059
1060 if ((*i)->Number == -1)
1061 continue;
1062
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001063 if ((*i)->Header.PointerToRawData != 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001064 assert(getStream().tell() <= (*i)->Header.PointerToRawData &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001065 "Section::PointerToRawData is insane!");
1066
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001067 unsigned SectionDataPadding =
1068 (*i)->Header.PointerToRawData - getStream().tell();
David Majnemer3df3c612015-02-11 22:22:30 +00001069 assert(SectionDataPadding < 4 &&
1070 "Should only need at most three bytes of padding!");
1071
1072 WriteZeros(SectionDataPadding);
1073
David Majnemer6ddc6362015-09-01 21:23:58 +00001074 // Save the contents of the section to a temporary buffer, we need this
1075 // to CRC the data before we dump it into the object file.
1076 SectionContents.clear();
1077 raw_svector_ostream VecOS(SectionContents);
1078 raw_pwrite_stream &OldStream = getStream();
1079 // Redirect the output stream to our buffer.
1080 setStream(VecOS);
1081 // Fill our buffer with the section data.
Rafael Espindola64acc7f2015-05-26 02:17:21 +00001082 Asm.writeSectionData(&*j, Layout);
David Majnemer6ddc6362015-09-01 21:23:58 +00001083 // Reset the stream back to what it was before.
1084 setStream(OldStream);
1085
1086 // Calculate our CRC with an initial value of '0', this is not how
1087 // JamCRC is specified but it aligns with the expected output.
1088 JamCRC JC(/*Init=*/0x00000000U);
1089 JC.update(SectionContents);
1090
1091 // Write the section contents to the object file.
1092 getStream() << SectionContents;
1093
1094 // Update the section definition auxiliary symbol to record the CRC.
1095 COFFSection *Sec = SectionMap[&*j];
1096 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
1097 assert(AuxSyms.size() == 1 &&
1098 AuxSyms[0].AuxType == ATSectionDefinition);
1099 AuxSymbol &SecDef = AuxSyms[0];
1100 SecDef.Aux.SectionDefinition.CheckSum = JC.getCRC();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001101 }
1102
1103 if ((*i)->Relocations.size() > 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001104 assert(getStream().tell() == (*i)->Header.PointerToRelocations &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001105 "Section::PointerToRelocations is insane!");
1106
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001107 if ((*i)->Relocations.size() >= 0xffff) {
1108 // In case of overflow, write actual relocation count as first
1109 // relocation. Including the synthetic reloc itself (+ 1).
1110 COFF::relocation r;
1111 r.VirtualAddress = (*i)->Relocations.size() + 1;
1112 r.SymbolTableIndex = 0;
1113 r.Type = 0;
1114 WriteRelocation(r);
1115 }
1116
Yaron Keren56919ef2014-12-04 08:30:39 +00001117 for (const auto &Relocation : (*i)->Relocations)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001118 WriteRelocation(Relocation.Data);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001119 } else
1120 assert((*i)->Header.PointerToRelocations == 0 &&
1121 "Section::PointerToRelocations is insane!");
1122 }
1123 }
1124
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001125 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001126 "Header::PointerToSymbolTable is insane!");
1127
Yaron Keren56919ef2014-12-04 08:30:39 +00001128 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001129 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001130 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001131
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001132 getStream().write(Strings.data().data(), Strings.data().size());
Chris Lattner2c52b792010-07-11 22:07:02 +00001133}
1134
Rafael Espindola11e9e212015-05-27 14:37:12 +00001135MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1136 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001137
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001138// Pin the vtable to this file.
1139void MCWinCOFFObjectTargetWriter::anchor() {}
1140
Chris Lattner2c52b792010-07-11 22:07:02 +00001141//------------------------------------------------------------------------------
1142// WinCOFFObjectWriter factory function
1143
Rafael Espindola37099d92015-04-09 18:08:15 +00001144MCObjectWriter *
1145llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001146 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001147 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001148}