blob: 9abd1a1590695bba4ac96e72f9697988212c5737 [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>
38
Chris Lattner2c52b792010-07-11 22:07:02 +000039using namespace llvm;
40
Chandler Carruthf58e3762014-04-22 03:04:17 +000041#define DEBUG_TYPE "WinCOFFObjectWriter"
42
Chris Lattner2c52b792010-07-11 22:07:02 +000043namespace {
Dmitri Gribenko226fea52013-01-13 16:01:15 +000044typedef SmallString<COFF::NameSize> name;
Chris Lattner2c52b792010-07-11 22:07:02 +000045
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000046enum AuxiliaryType {
47 ATFunctionDefinition,
48 ATbfAndefSymbol,
49 ATWeakExternal,
50 ATFile,
51 ATSectionDefinition
52};
Chris Lattner2c52b792010-07-11 22:07:02 +000053
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000054struct AuxSymbol {
Rafael Espindola11e9e212015-05-27 14:37:12 +000055 AuxiliaryType AuxType;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000056 COFF::Auxiliary Aux;
57};
Chris Lattner2c52b792010-07-11 22:07:02 +000058
Michael J. Spencerd6283772010-09-27 08:58:26 +000059class COFFSymbol;
60class COFFSection;
61
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000062class COFFSymbol {
63public:
64 COFF::symbol Data;
Chris Lattner2c52b792010-07-11 22:07:02 +000065
Dmitri Gribenko226fea52013-01-13 16:01:15 +000066 typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
Chris Lattner2c52b792010-07-11 22:07:02 +000067
Rafael Espindola11e9e212015-05-27 14:37:12 +000068 name Name;
69 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000070 AuxiliarySymbols Aux;
Rafael Espindola11e9e212015-05-27 14:37:12 +000071 COFFSymbol *Other;
72 COFFSection *Section;
73 int Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000074
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +000075 const MCSymbol *MC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000076
Dmitri Gribenko226fea52013-01-13 16:01:15 +000077 COFFSymbol(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000078 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000079
80 bool should_keep() const;
David Majnemer4eecd302015-05-30 04:56:02 +000081
82 int64_t getIndex() const { return Index; }
83 void setIndex(int Value) {
84 Index = Value;
85 if (MC)
86 MC->setIndex(static_cast<uint32_t>(Value));
87 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000088};
89
90// This class contains staging data for a COFF relocation entry.
91struct COFFRelocation {
92 COFF::relocation Data;
Rafael Espindola11e9e212015-05-27 14:37:12 +000093 COFFSymbol *Symb;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000094
Craig Topperbb694de2014-04-13 04:57:38 +000095 COFFRelocation() : Symb(nullptr) {}
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000096 static size_t size() { return COFF::RelocationSize; }
97};
98
99typedef std::vector<COFFRelocation> relocations;
100
101class COFFSection {
102public:
103 COFF::section Header;
104
Rafael Espindola11e9e212015-05-27 14:37:12 +0000105 std::string Name;
106 int Number;
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000107 MCSectionCOFF const *MCSection;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000108 COFFSymbol *Symbol;
109 relocations Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000110
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000111 COFFSection(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000112 static size_t size();
113};
114
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000115class WinCOFFObjectWriter : public MCObjectWriter {
116public:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000117 typedef std::vector<std::unique_ptr<COFFSymbol>> symbols;
David Blaikief564ab62014-04-15 05:25:03 +0000118 typedef std::vector<std::unique_ptr<COFFSection>> sections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000119
Rafael Espindola11e9e212015-05-27 14:37:12 +0000120 typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000121 typedef DenseMap<MCSection const *, COFFSection *> section_map;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000122
Ahmed Charles56440fd2014-03-06 05:51:42 +0000123 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000124
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000125 // Root level file contents.
126 COFF::header Header;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000127 sections Sections;
128 symbols Symbols;
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000129 StringTableBuilder Strings;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000130
131 // Maps used during object file creation.
132 section_map SectionMap;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000133 symbol_map SymbolMap;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000134
David Majnemer4d571592014-09-15 19:42:42 +0000135 bool UseBigObj;
136
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000137 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000138
Yaron Kerencca43c12014-09-16 21:31:04 +0000139 void reset() override {
140 memset(&Header, 0, sizeof(Header));
141 Header.Machine = TargetObjectWriter->getMachine();
142 Sections.clear();
143 Symbols.clear();
144 Strings.clear();
145 SectionMap.clear();
146 SymbolMap.clear();
147 MCObjectWriter::reset();
148 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000149
Michael J. Spencer17990d52010-10-16 08:25:57 +0000150 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola11e9e212015-05-27 14:37:12 +0000151 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000152 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000153
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000154 template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000155 object_t *createCOFFEntity(StringRef Name, list_t &List);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000156
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000157 void defineSection(MCSectionCOFF const &Sec);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000158 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000159 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000160
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000161 void SetSymbolName(COFFSymbol &S);
162 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000163
David Majnemer299674e2014-07-13 04:31:19 +0000164 bool ExportSymbol(const MCSymbol &Symbol, MCAssembler &Asm);
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000165
Michael J. Spencerd6283772010-09-27 08:58:26 +0000166 bool IsPhysicalSection(COFFSection *S);
167
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000168 // Entity writing methods.
169
170 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000171 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000172 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000173 void writeSectionHeader(const COFF::section &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000174 void WriteRelocation(const COFF::relocation &R);
175
176 // MCObjectWriter interface implementation.
177
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000178 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper34a61bc2014-03-08 07:02:02 +0000179 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000180
Jim Grosbach36e60e92015-06-04 22:24:41 +0000181 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000182 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000183 const MCFragment &FB, bool InSet,
184 bool IsPCRel) const override;
185
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000186 bool isWeak(const MCSymbol &Sym) const override;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000187
Jim Grosbach36e60e92015-06-04 22:24:41 +0000188 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000189 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000190 MCValue Target, bool &IsPCRel,
191 uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000192
Jim Grosbach36e60e92015-06-04 22:24:41 +0000193 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000194};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000195}
Chris Lattner2c52b792010-07-11 22:07:02 +0000196
Hans Wennborgba80b5d2014-09-28 00:22:27 +0000197static inline void write_uint32_le(void *Data, uint32_t Value) {
198 support::endian::write<uint32_t, support::little, support::unaligned>(Data,
199 Value);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000200}
201
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000202//------------------------------------------------------------------------------
203// Symbol class implementation
204
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000205COFFSymbol::COFFSymbol(StringRef name)
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000206 : Name(name.begin(), name.end()), Other(nullptr), Section(nullptr),
207 Relocations(0), MC(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000208 memset(&Data, 0, sizeof(Data));
209}
210
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000211// In the case that the name does not fit within 8 bytes, the offset
212// into the string table is stored in the last 4 bytes instead, leaving
213// the first 4 bytes as 0.
214void COFFSymbol::set_name_offset(uint32_t Offset) {
215 write_uint32_le(Data.Name + 0, 0);
216 write_uint32_le(Data.Name + 4, Offset);
217}
218
Michael J. Spencerd6283772010-09-27 08:58:26 +0000219/// logic to decide if the symbol should be reported in the symbol table
220bool COFFSymbol::should_keep() const {
221 // no section means its external, keep it
Craig Topperbb694de2014-04-13 04:57:38 +0000222 if (!Section)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000223 return true;
224
225 // if it has relocations pointing at it, keep it
Rafael Espindola11e9e212015-05-27 14:37:12 +0000226 if (Relocations > 0) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000227 assert(Section->Number != -1 && "Sections with relocations must be real!");
228 return true;
229 }
230
David Majnemer4eecd302015-05-30 04:56:02 +0000231 // if this is a safeseh handler, keep it
Pete Cooper6bf1f302015-06-08 17:17:19 +0000232 if (MC && (cast<MCSymbolCOFF>(MC)->isSafeSEH()))
David Majnemer4eecd302015-05-30 04:56:02 +0000233 return true;
234
Michael J. Spencerd6283772010-09-27 08:58:26 +0000235 // if the section its in is being droped, drop it
236 if (Section->Number == -1)
Rafael Espindola11e9e212015-05-27 14:37:12 +0000237 return false;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000238
239 // if it is the section symbol, keep it
240 if (Section->Symbol == this)
241 return true;
242
243 // if its temporary, drop it
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000244 if (MC && MC->isTemporary())
245 return false;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000246
247 // otherwise, keep it
248 return true;
249}
250
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000251//------------------------------------------------------------------------------
252// Section class implementation
253
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000254COFFSection::COFFSection(StringRef name)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000255 : Name(name), MCSection(nullptr), Symbol(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000256 memset(&Header, 0, sizeof(Header));
257}
258
Rafael Espindola11e9e212015-05-27 14:37:12 +0000259size_t COFFSection::size() { return COFF::SectionSize; }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000260
261//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000262// WinCOFFObjectWriter class implementation
263
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000264WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000265 raw_pwrite_stream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000266 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000267 memset(&Header, 0, sizeof(Header));
Michael J. Spencer377aa202010-08-21 05:58:13 +0000268
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000269 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000270}
271
Michael J. Spencer17990d52010-10-16 08:25:57 +0000272COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000273 return createCOFFEntity<COFFSymbol>(Name, Symbols);
274}
275
Yaron Keren56919ef2014-12-04 08:30:39 +0000276COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000277 symbol_map::iterator i = SymbolMap.find(Symbol);
278 if (i != SymbolMap.end())
279 return i->second;
Yaron Keren56919ef2014-12-04 08:30:39 +0000280 COFFSymbol *RetSymbol =
281 createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000282 SymbolMap[Symbol] = RetSymbol;
283 return RetSymbol;
284}
285
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000286COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000287 return createCOFFEntity<COFFSection>(Name, Sections);
288}
289
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000290/// A template used to lookup or create a symbol/section, and initialize it if
291/// needed.
292template <typename object_t, typename list_t>
Rafael Espindola11e9e212015-05-27 14:37:12 +0000293object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name, list_t &List) {
David Blaikief564ab62014-04-15 05:25:03 +0000294 List.push_back(make_unique<object_t>(Name));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000295
David Blaikief564ab62014-04-15 05:25:03 +0000296 return List.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000297}
298
299/// This function takes a section data object from the assembler
300/// and creates the associated COFF section staging object.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000301void WinCOFFObjectWriter::defineSection(MCSectionCOFF const &Sec) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000302 COFFSection *coff_section = createSection(Sec.getSectionName());
Rafael Espindola11e9e212015-05-27 14:37:12 +0000303 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
Rafael Espindola0766ae02014-06-06 19:26:12 +0000304 if (Sec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
305 if (const MCSymbol *S = Sec.getCOMDATSymbol()) {
306 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
307 if (COMDATSymbol->Section)
308 report_fatal_error("two sections have the same comdat");
309 COMDATSymbol->Section = coff_section;
310 }
311 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000312
Michael J. Spencerd6283772010-09-27 08:58:26 +0000313 coff_section->Symbol = coff_symbol;
314 coff_symbol->Section = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000315 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000316
317 // In this case the auxiliary symbol is a Section Definition.
318 coff_symbol->Aux.resize(1);
319 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
320 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000321 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
322
323 coff_section->Header.Characteristics = Sec.getCharacteristics();
324
325 uint32_t &Characteristics = coff_section->Header.Characteristics;
Rafael Espindola967d6a62015-05-21 21:02:35 +0000326 switch (Sec.getAlignment()) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000327 case 1:
328 Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES;
329 break;
330 case 2:
331 Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES;
332 break;
333 case 4:
334 Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES;
335 break;
336 case 8:
337 Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES;
338 break;
339 case 16:
340 Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES;
341 break;
342 case 32:
343 Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES;
344 break;
345 case 64:
346 Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES;
347 break;
348 case 128:
349 Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES;
350 break;
351 case 256:
352 Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES;
353 break;
354 case 512:
355 Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES;
356 break;
357 case 1024:
358 Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES;
359 break;
360 case 2048:
361 Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES;
362 break;
363 case 4096:
364 Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES;
365 break;
366 case 8192:
367 Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES;
368 break;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000369 default:
370 llvm_unreachable("unsupported section alignment");
371 }
372
373 // Bind internal COFF section to MC section.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000374 coff_section->MCSection = &Sec;
375 SectionMap[&Sec] = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000376}
377
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000378static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000379 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000380 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000381 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000382
383 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000384 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000385 return 0;
386
387 return Res;
388}
389
David Majnemera9bdb322014-04-08 22:33:40 +0000390/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000391/// and creates the associated COFF symbol staging object.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000392void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000393 MCAssembler &Assembler,
394 const MCAsmLayout &Layout) {
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000395 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Peter Collingbourne89886872013-04-22 18:48:56 +0000396 SymbolMap[&Symbol] = coff_symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000397
Pete Cooper6bf1f302015-06-08 17:17:19 +0000398 if (cast<MCSymbolCOFF>(Symbol).isWeakExternal()) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000399 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
400
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000401 if (Symbol.isVariable()) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000402 const MCSymbolRefExpr *SymRef =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000403 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000404
Peter Collingbourne89886872013-04-22 18:48:56 +0000405 if (!SymRef)
406 report_fatal_error("Weak externals may only alias symbols");
Michael J. Spencer17990d52010-10-16 08:25:57 +0000407
Peter Collingbourne89886872013-04-22 18:48:56 +0000408 coff_symbol->Other = GetOrCreateCOFFSymbol(&SymRef->getSymbol());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000409 } else {
Yaron Keren075759a2015-03-30 15:42:36 +0000410 std::string WeakName = (".weak." + Symbol.getName() + ".default").str();
Michael J. Spencer17990d52010-10-16 08:25:57 +0000411 COFFSymbol *WeakDefault = createSymbol(WeakName);
412 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000413 WeakDefault->Data.StorageClass = COFF::IMAGE_SYM_CLASS_EXTERNAL;
414 WeakDefault->Data.Type = 0;
415 WeakDefault->Data.Value = 0;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000416 coff_symbol->Other = WeakDefault;
417 }
418
419 // Setup the Weak External auxiliary symbol.
420 coff_symbol->Aux.resize(1);
421 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
422 coff_symbol->Aux[0].AuxType = ATWeakExternal;
423 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
424 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000425 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000426
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000427 coff_symbol->MC = &Symbol;
Peter Collingbourne89886872013-04-22 18:48:56 +0000428 } else {
Rafael Espindola575f79a2014-05-01 13:37:57 +0000429 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000430 coff_symbol->Data.Value = getSymbolValue(Symbol, Layout);
Reid Klecknerc1e76212013-09-17 23:18:05 +0000431
Pete Cooperad9f9c32015-06-08 17:17:12 +0000432 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(Symbol);
433 coff_symbol->Data.Type = SymbolCOFF.getType();
Pete Cooper6bf1f302015-06-08 17:17:19 +0000434 coff_symbol->Data.StorageClass = SymbolCOFF.getClass();
Peter Collingbourne89886872013-04-22 18:48:56 +0000435
436 // If no storage class was specified in the streamer, define it here.
David Majnemer4eecd302015-05-30 04:56:02 +0000437 if (coff_symbol->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000438 bool IsExternal = Symbol.isExternal() ||
439 (!Symbol.getFragment() && !Symbol.isVariable());
Peter Collingbourne89886872013-04-22 18:48:56 +0000440
David Majnemer299674e2014-07-13 04:31:19 +0000441 coff_symbol->Data.StorageClass = IsExternal
442 ? COFF::IMAGE_SYM_CLASS_EXTERNAL
443 : COFF::IMAGE_SYM_CLASS_STATIC;
Peter Collingbourne89886872013-04-22 18:48:56 +0000444 }
445
Rafael Espindola575f79a2014-05-01 13:37:57 +0000446 if (!Base) {
Reid Klecknerc1e76212013-09-17 23:18:05 +0000447 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola575f79a2014-05-01 13:37:57 +0000448 } else {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000449 if (Base->getFragment()) {
450 COFFSection *Sec = SectionMap[Base->getFragment()->getParent()];
Rafael Espindola0766ae02014-06-06 19:26:12 +0000451
452 if (coff_symbol->Section && coff_symbol->Section != Sec)
453 report_fatal_error("conflicting sections for symbol");
454
455 coff_symbol->Section = Sec;
456 }
Rafael Espindola575f79a2014-05-01 13:37:57 +0000457 }
Peter Collingbourne89886872013-04-22 18:48:56 +0000458
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000459 coff_symbol->MC = &Symbol;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000460 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000461}
462
Nico Rieck01143f92014-02-25 09:50:40 +0000463// Maximum offsets for different string table entry encodings.
464static const unsigned Max6DecimalOffset = 999999;
465static const unsigned Max7DecimalOffset = 9999999;
466static const uint64_t MaxBase64Offset = 0xFFFFFFFFFULL; // 64^6, including 0
467
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000468// Encode a string table entry offset in base 64, padded to 6 chars, and
469// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
470// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000471static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000472 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000473 "Illegal section name encoding for value");
474
475 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
476 "abcdefghijklmnopqrstuvwxyz"
477 "0123456789+/";
478
479 Buffer[0] = '/';
480 Buffer[1] = '/';
481
Rafael Espindola11e9e212015-05-27 14:37:12 +0000482 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000483 for (unsigned i = 0; i < 6; ++i) {
484 unsigned Rem = Value % 64;
485 Value /= 64;
486 *(Ptr--) = Alphabet[Rem];
487 }
488}
489
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000490void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000491 if (S.Name.size() > COFF::NameSize) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000492 uint64_t StringTableEntry = Strings.getOffset(S.Name);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000493
Nico Rieck01143f92014-02-25 09:50:40 +0000494 if (StringTableEntry <= Max6DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000495 std::sprintf(S.Header.Name, "/%d", unsigned(StringTableEntry));
Nico Rieck01143f92014-02-25 09:50:40 +0000496 } else if (StringTableEntry <= Max7DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000497 // With seven digits, we have to skip the terminating null. Because
498 // sprintf always appends it, we use a larger temporary buffer.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000499 char buffer[9] = {};
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000500 std::sprintf(buffer, "/%d", unsigned(StringTableEntry));
501 std::memcpy(S.Header.Name, buffer, 8);
Nico Rieck01143f92014-02-25 09:50:40 +0000502 } else if (StringTableEntry <= MaxBase64Offset) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000503 // Starting with 10,000,000, offsets are encoded as base64.
504 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000505 } else {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000506 report_fatal_error("COFF string table is greater than 64 GB.");
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000507 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000508 } else
509 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000510}
511
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000512void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
513 if (S.Name.size() > COFF::NameSize)
514 S.set_name_offset(Strings.getOffset(S.Name));
515 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000516 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000517}
518
David Majnemer299674e2014-07-13 04:31:19 +0000519bool WinCOFFObjectWriter::ExportSymbol(const MCSymbol &Symbol,
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000520 MCAssembler &Asm) {
521 // This doesn't seem to be right. Strings referred to from the .data section
522 // need symbols so they can be linked to code in the .text section right?
523
David Majnemer299674e2014-07-13 04:31:19 +0000524 // return Asm.isSymbolLinkerVisible(Symbol);
525
526 // Non-temporary labels should always be visible to the linker.
527 if (!Symbol.isTemporary())
528 return true;
529
Reid Klecknera9d62532015-06-11 22:32:23 +0000530 // Temporary variable symbols are invisible.
531 if (Symbol.isVariable())
David Majnemer299674e2014-07-13 04:31:19 +0000532 return false;
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000533
Reid Klecknera9d62532015-06-11 22:32:23 +0000534 // Absolute temporary labels are never visible.
535 return !Symbol.isAbsolute();
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000536}
537
Michael J. Spencerd6283772010-09-27 08:58:26 +0000538bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000539 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
540 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000541}
542
543//------------------------------------------------------------------------------
544// entity writing methods
545
546void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000547 if (UseBigObj) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000548 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
549 writeLE16(0xFFFF);
550 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
551 writeLE16(Header.Machine);
552 writeLE32(Header.TimeDateStamp);
553 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
554 writeLE32(0);
555 writeLE32(0);
556 writeLE32(0);
557 writeLE32(0);
558 writeLE32(Header.NumberOfSections);
559 writeLE32(Header.PointerToSymbolTable);
560 writeLE32(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000561 } else {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000562 writeLE16(Header.Machine);
563 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
564 writeLE32(Header.TimeDateStamp);
565 writeLE32(Header.PointerToSymbolTable);
566 writeLE32(Header.NumberOfSymbols);
567 writeLE16(Header.SizeOfOptionalHeader);
568 writeLE16(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000569 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000570}
571
David Blaikief564ab62014-04-15 05:25:03 +0000572void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000573 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
574 writeLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000575 if (UseBigObj)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000576 writeLE32(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000577 else
Jim Grosbach36e60e92015-06-04 22:24:41 +0000578 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
579 writeLE16(S.Data.Type);
580 write8(S.Data.StorageClass);
581 write8(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000582 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000583}
584
585void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000586 const COFFSymbol::AuxiliarySymbols &S) {
587 for (COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
588 i != e; ++i) {
589 switch (i->AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000590 case ATFunctionDefinition:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000591 writeLE32(i->Aux.FunctionDefinition.TagIndex);
592 writeLE32(i->Aux.FunctionDefinition.TotalSize);
593 writeLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
594 writeLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000595 WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000596 if (UseBigObj)
597 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000598 break;
599 case ATbfAndefSymbol:
600 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
Jim Grosbach36e60e92015-06-04 22:24:41 +0000601 writeLE16(i->Aux.bfAndefSymbol.Linenumber);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000602 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
Jim Grosbach36e60e92015-06-04 22:24:41 +0000603 writeLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000604 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000605 if (UseBigObj)
606 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000607 break;
608 case ATWeakExternal:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000609 writeLE32(i->Aux.WeakExternal.TagIndex);
610 writeLE32(i->Aux.WeakExternal.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000611 WriteZeros(sizeof(i->Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000612 if (UseBigObj)
613 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000614 break;
615 case ATFile:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000616 writeBytes(
David Majnemer4d571592014-09-15 19:42:42 +0000617 StringRef(reinterpret_cast<const char *>(&i->Aux),
618 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000619 break;
620 case ATSectionDefinition:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000621 writeLE32(i->Aux.SectionDefinition.Length);
622 writeLE16(i->Aux.SectionDefinition.NumberOfRelocations);
623 writeLE16(i->Aux.SectionDefinition.NumberOfLinenumbers);
624 writeLE32(i->Aux.SectionDefinition.CheckSum);
625 writeLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number));
626 write8(i->Aux.SectionDefinition.Selection);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000627 WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
Jim Grosbach36e60e92015-06-04 22:24:41 +0000628 writeLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000629 if (UseBigObj)
630 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000631 break;
632 }
633 }
634}
635
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000636void WinCOFFObjectWriter::writeSectionHeader(const COFF::section &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000637 writeBytes(StringRef(S.Name, COFF::NameSize));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000638
Jim Grosbach36e60e92015-06-04 22:24:41 +0000639 writeLE32(S.VirtualSize);
640 writeLE32(S.VirtualAddress);
641 writeLE32(S.SizeOfRawData);
642 writeLE32(S.PointerToRawData);
643 writeLE32(S.PointerToRelocations);
644 writeLE32(S.PointerToLineNumbers);
645 writeLE16(S.NumberOfRelocations);
646 writeLE16(S.NumberOfLineNumbers);
647 writeLE32(S.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000648}
649
650void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000651 writeLE32(R.VirtualAddress);
652 writeLE32(R.SymbolTableIndex);
653 writeLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000654}
655
656////////////////////////////////////////////////////////////////////////////////
657// MCObjectWriter interface implementations
658
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000659void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000660 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000661 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000662 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000663 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000664 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000665
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000666 for (const MCSymbol &Symbol : Asm.symbols())
667 if (ExportSymbol(Symbol, Asm))
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000668 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000669}
670
Jim Grosbach36e60e92015-06-04 22:24:41 +0000671bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000672 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000673 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000674 // MS LINK expects to be able to replace all references to a function with a
675 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
676 // away any relocations to functions.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000677 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
678 if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000679 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000680 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000681 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000682}
683
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000684bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000685 if (!Sym.isExternal())
Rafael Espindola88af4112015-04-17 11:27:13 +0000686 return false;
687
Rafael Espindola88af4112015-04-17 11:27:13 +0000688 if (!Sym.isInSection())
689 return false;
690
691 const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
692 if (!Sec.getCOMDATSymbol())
693 return false;
694
695 // It looks like for COFF it is invalid to replace a reference to a global
696 // in a comdat with a reference to a local.
697 // FIXME: Add a specification reference if available.
698 return true;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000699}
700
Jim Grosbach36e60e92015-06-04 22:24:41 +0000701void WinCOFFObjectWriter::recordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000702 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
703 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000704 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000705
Reid Klecknerdae7b4e2013-07-15 19:41:21 +0000706 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola88af4112015-04-17 11:27:13 +0000707 const MCSymbol &A = Symbol;
Rafael Espindola26410142015-06-01 01:52:18 +0000708 if (!A.isRegistered())
Rafael Espindola11e9e212015-05-27 14:37:12 +0000709 Asm.getContext().reportFatalError(Fixup.getLoc(),
710 Twine("symbol '") + A.getName() +
711 "' can not be undefined");
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000712
Rafael Espindola7549f872015-05-26 00:36:57 +0000713 MCSection *Section = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000714
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000715 // Mark this symbol as requiring an entry in the symbol table.
Rafael Espindola7549f872015-05-26 00:36:57 +0000716 assert(SectionMap.find(Section) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000717 "Section must already have been defined in executePostLayoutBinding!");
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000718 assert(SymbolMap.find(&A) != SymbolMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000719 "Symbol must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000720
Rafael Espindola7549f872015-05-26 00:36:57 +0000721 COFFSection *coff_section = SectionMap[Section];
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000722 COFFSymbol *coff_symbol = SymbolMap[&A];
Rafael Espindolaed164772011-04-20 14:01:45 +0000723 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000724 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000725
David Majnemera45a1762014-01-06 07:39:46 +0000726 if (SymB) {
727 const MCSymbol *B = &SymB->getSymbol();
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000728 if (!B->getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000729 Asm.getContext().reportFatalError(
David Majnemera45a1762014-01-06 07:39:46 +0000730 Fixup.getLoc(),
731 Twine("symbol '") + B->getName() +
732 "' can not be undefined in a subtraction expression");
733
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000734 if (!A.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 '") + Symbol.getName() +
738 "' can not be undefined in a subtraction expression");
739
740 CrossSection = &Symbol.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000741
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000742 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000743 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000744
745 // In the case where we have SymbA and SymB, we just need to store the delta
746 // between the two symbols. Update FixedValue to account for the delta, and
747 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000748 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000749 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000750 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000751 return;
David Majnemer1de30942015-02-09 06:31:31 +0000752 }
753
754 // Offset of the relocation in the section
755 int64_t OffsetOfRelocation =
756 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
757
Andy Ayers9e5c8512015-05-14 01:10:41 +0000758 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000759 } else {
760 FixedValue = Target.getConstant();
761 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000762
763 COFFRelocation Reloc;
764
Daniel Dunbar727be432010-07-31 21:08:54 +0000765 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000766 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000767
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000768 // Turn relocations for temporary symbols into section relocations.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000769 if (coff_symbol->MC->isTemporary() || CrossSection) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000770 Reloc.Symb = coff_symbol->Section->Symbol;
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000771 FixedValue += Layout.getFragmentOffset(coff_symbol->MC->getFragment()) +
772 coff_symbol->MC->getOffset();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000773 } else
774 Reloc.Symb = coff_symbol;
775
776 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000777
778 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000779 Reloc.Data.Type = TargetObjectWriter->getRelocType(
780 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000781
782 // FIXME: Can anyone explain what this does other than adjust for the size
783 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000784 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
785 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
786 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
787 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000788 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000789
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000790 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
791 switch (Reloc.Data.Type) {
792 case COFF::IMAGE_REL_ARM_ABSOLUTE:
793 case COFF::IMAGE_REL_ARM_ADDR32:
794 case COFF::IMAGE_REL_ARM_ADDR32NB:
795 case COFF::IMAGE_REL_ARM_TOKEN:
796 case COFF::IMAGE_REL_ARM_SECTION:
797 case COFF::IMAGE_REL_ARM_SECREL:
798 break;
799 case COFF::IMAGE_REL_ARM_BRANCH11:
800 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000801 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
802 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
803 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000804 case COFF::IMAGE_REL_ARM_BRANCH24:
805 case COFF::IMAGE_REL_ARM_BLX24:
806 case COFF::IMAGE_REL_ARM_MOV32A:
807 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
808 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000809 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000810 // generate the relocations however the rest of the MSVC toolchain is
811 // unable to handle it.
812 llvm_unreachable("unsupported relocation");
813 break;
814 case COFF::IMAGE_REL_ARM_MOV32T:
815 break;
816 case COFF::IMAGE_REL_ARM_BRANCH20T:
817 case COFF::IMAGE_REL_ARM_BRANCH24T:
818 case COFF::IMAGE_REL_ARM_BLX23T:
819 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
820 // perform a 4 byte adjustment to the relocation. Relative branches are
821 // offset by 4 on ARM, however, because there is no RELA relocations, all
822 // branches are offset by 4.
823 FixedValue = FixedValue + 4;
824 break;
825 }
826 }
827
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000828 if (TargetObjectWriter->recordRelocation(Fixup))
829 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000830}
831
Jim Grosbach36e60e92015-06-04 22:24:41 +0000832void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000833 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000834 size_t SectionsSize = Sections.size();
835 if (SectionsSize > static_cast<size_t>(INT32_MAX))
836 report_fatal_error(
837 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000838
David Majnemer4d571592014-09-15 19:42:42 +0000839 // Assign symbol and section indexes and offsets.
840 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
841
842 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
843
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000844 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000845 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000846 for (const auto &Section : Sections) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000847 Section->Number = Number;
848 Section->Symbol->Data.SectionNumber = Number;
849 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000850 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000851 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000852
David Majnemer4d571592014-09-15 19:42:42 +0000853 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000854 Header.NumberOfSymbols = 0;
855
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000856 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000857 // round up to calculate the number of auxiliary symbols required
858 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000859 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000860
861 COFFSymbol *file = createSymbol(".file");
862 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
863 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
864 file->Aux.resize(Count);
865
866 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000867 unsigned Length = Name.size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000868 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000869 Aux.AuxType = ATFile;
870
871 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000872 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000873 Length = Length - SymbolSize;
874 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000875 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000876 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
877 break;
878 }
879
880 Offset += SymbolSize;
881 }
882 }
883
David Majnemer9ab5ff12014-08-28 04:02:50 +0000884 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000885 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000886 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000887 Symbol->Data.SectionNumber = Symbol->Section->Number;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000888 if (Symbol->should_keep()) {
David Majnemer4eecd302015-05-30 04:56:02 +0000889 Symbol->setIndex(Header.NumberOfSymbols++);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000890 // Update auxiliary symbol info.
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000891 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
892 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
David Majnemer4eecd302015-05-30 04:56:02 +0000893 } else {
894 Symbol->setIndex(-1);
895 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000896 }
897
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000898 // Build string table.
899 for (const auto &S : Sections)
900 if (S->Name.size() > COFF::NameSize)
901 Strings.add(S->Name);
902 for (const auto &S : Symbols)
903 if (S->should_keep() && S->Name.size() > COFF::NameSize)
904 Strings.add(S->Name);
905 Strings.finalize(StringTableBuilder::WinCOFF);
906
907 // Set names.
908 for (const auto &S : Sections)
909 SetSectionName(*S);
910 for (auto &S : Symbols)
911 if (S->should_keep())
912 SetSymbolName(*S);
913
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000914 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000915 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000916 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000917 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000918 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
919 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000920 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000921 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000922 }
923 }
924
Nico Riecka37acf72013-07-06 12:13:10 +0000925 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000926 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000927 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000928 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
929 continue;
930
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000931 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +0000932
Rafael Espindola0766ae02014-06-06 19:26:12 +0000933 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
934 assert(COMDAT);
935 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
936 assert(COMDATSymbol);
937 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000938 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000939 report_fatal_error(
940 Twine("Missing associated COMDAT section for section ") +
941 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000942
943 // Skip this section if the associated section is unused.
944 if (Assoc->Number == -1)
945 continue;
946
David Majnemer4eecd302015-05-30 04:56:02 +0000947 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +0000948 }
949
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000950 // Assign file offsets to COFF object file structures.
951
952 unsigned offset = 0;
953
David Majnemer4d571592014-09-15 19:42:42 +0000954 if (UseBigObj)
955 offset += COFF::Header32Size;
956 else
957 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000958 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000959
Yaron Keren56919ef2014-12-04 08:30:39 +0000960 for (const auto &Section : Asm) {
Rafael Espindolaa554c052015-05-25 23:14:17 +0000961 COFFSection *Sec = SectionMap[&Section];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000962
Michael J. Spencerd6283772010-09-27 08:58:26 +0000963 if (Sec->Number == -1)
964 continue;
965
Rafael Espindola5a1e80b2015-05-26 02:00:36 +0000966 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000967
Michael J. Spencerd6283772010-09-27 08:58:26 +0000968 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000969 // Align the section data to a four byte boundary.
David Majnemerab2b25b2015-02-11 22:51:55 +0000970 offset = RoundUpToAlignment(offset, 4);
971 Sec->Header.PointerToRawData = offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000972
973 offset += Sec->Header.SizeOfRawData;
974 }
975
976 if (Sec->Relocations.size() > 0) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000977 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
978
979 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000980 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000981 // size is found in reloc #0. Microsoft tools understand this.
982 Sec->Header.NumberOfRelocations = 0xffff;
983 } else {
984 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
985 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000986 Sec->Header.PointerToRelocations = offset;
987
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000988 if (RelocationsOverflow) {
989 // Reloc #0 will contain actual count, so make room for it.
990 offset += COFF::RelocationSize;
991 }
992
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000993 offset += COFF::RelocationSize * Sec->Relocations.size();
994
Yaron Keren56919ef2014-12-04 08:30:39 +0000995 for (auto &Relocation : Sec->Relocations) {
David Majnemer4eecd302015-05-30 04:56:02 +0000996 assert(Relocation.Symb->getIndex() != -1);
997 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000998 }
999 }
1000
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001001 assert(Sec->Symbol->Aux.size() == 1 &&
1002 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +00001003 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001004 assert(Aux.AuxType == ATSectionDefinition &&
1005 "Section's symbol's aux symbol must be a Section Definition!");
1006 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
1007 Aux.Aux.SectionDefinition.NumberOfRelocations =
Rafael Espindola11e9e212015-05-27 14:37:12 +00001008 Sec->Header.NumberOfRelocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001009 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
Rafael Espindola11e9e212015-05-27 14:37:12 +00001010 Sec->Header.NumberOfLineNumbers;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001011 }
1012
1013 Header.PointerToSymbolTable = offset;
1014
Rafael Espindola5ac4ad22013-12-04 02:26:54 +00001015 // We want a deterministic output. It looks like GNU as also writes 0 in here.
Rafael Espindola9201e6b2013-12-04 02:02:55 +00001016 Header.TimeDateStamp = 0;
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +00001017
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001018 // Write it all to disk...
1019 WriteFileHeader(Header);
1020
1021 {
1022 sections::iterator i, ie;
Rafael Espindola63702e22015-06-01 01:30:01 +00001023 MCAssembler::iterator j, je;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001024
Yaron Keren56919ef2014-12-04 08:30:39 +00001025 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001026 if (Section->Number != -1) {
1027 if (Section->Relocations.size() >= 0xffff)
1028 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Jim Grosbach56ed0bb2015-06-04 23:25:54 +00001029 writeSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001030 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001031 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001032
David Majnemer6ddc6362015-09-01 21:23:58 +00001033 SmallVector<char, 128> SectionContents;
Rafael Espindola11e9e212015-05-27 14:37:12 +00001034 for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
1035 je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +00001036 (i != ie) && (j != je); ++i, ++j) {
1037
1038 if ((*i)->Number == -1)
1039 continue;
1040
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001041 if ((*i)->Header.PointerToRawData != 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001042 assert(getStream().tell() <= (*i)->Header.PointerToRawData &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001043 "Section::PointerToRawData is insane!");
1044
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001045 unsigned SectionDataPadding =
1046 (*i)->Header.PointerToRawData - getStream().tell();
David Majnemer3df3c612015-02-11 22:22:30 +00001047 assert(SectionDataPadding < 4 &&
1048 "Should only need at most three bytes of padding!");
1049
1050 WriteZeros(SectionDataPadding);
1051
David Majnemer6ddc6362015-09-01 21:23:58 +00001052 // Save the contents of the section to a temporary buffer, we need this
1053 // to CRC the data before we dump it into the object file.
1054 SectionContents.clear();
1055 raw_svector_ostream VecOS(SectionContents);
1056 raw_pwrite_stream &OldStream = getStream();
1057 // Redirect the output stream to our buffer.
1058 setStream(VecOS);
1059 // Fill our buffer with the section data.
Rafael Espindola64acc7f2015-05-26 02:17:21 +00001060 Asm.writeSectionData(&*j, Layout);
David Majnemer6ddc6362015-09-01 21:23:58 +00001061 // Reset the stream back to what it was before.
1062 setStream(OldStream);
1063
1064 // Calculate our CRC with an initial value of '0', this is not how
1065 // JamCRC is specified but it aligns with the expected output.
1066 JamCRC JC(/*Init=*/0x00000000U);
1067 JC.update(SectionContents);
1068
1069 // Write the section contents to the object file.
1070 getStream() << SectionContents;
1071
1072 // Update the section definition auxiliary symbol to record the CRC.
1073 COFFSection *Sec = SectionMap[&*j];
1074 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
1075 assert(AuxSyms.size() == 1 &&
1076 AuxSyms[0].AuxType == ATSectionDefinition);
1077 AuxSymbol &SecDef = AuxSyms[0];
1078 SecDef.Aux.SectionDefinition.CheckSum = JC.getCRC();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001079 }
1080
1081 if ((*i)->Relocations.size() > 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001082 assert(getStream().tell() == (*i)->Header.PointerToRelocations &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001083 "Section::PointerToRelocations is insane!");
1084
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001085 if ((*i)->Relocations.size() >= 0xffff) {
1086 // In case of overflow, write actual relocation count as first
1087 // relocation. Including the synthetic reloc itself (+ 1).
1088 COFF::relocation r;
1089 r.VirtualAddress = (*i)->Relocations.size() + 1;
1090 r.SymbolTableIndex = 0;
1091 r.Type = 0;
1092 WriteRelocation(r);
1093 }
1094
Yaron Keren56919ef2014-12-04 08:30:39 +00001095 for (const auto &Relocation : (*i)->Relocations)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001096 WriteRelocation(Relocation.Data);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001097 } else
1098 assert((*i)->Header.PointerToRelocations == 0 &&
1099 "Section::PointerToRelocations is insane!");
1100 }
1101 }
1102
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001103 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001104 "Header::PointerToSymbolTable is insane!");
1105
Yaron Keren56919ef2014-12-04 08:30:39 +00001106 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001107 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001108 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001109
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001110 getStream().write(Strings.data().data(), Strings.data().size());
Chris Lattner2c52b792010-07-11 22:07:02 +00001111}
1112
Rafael Espindola11e9e212015-05-27 14:37:12 +00001113MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1114 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001115
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001116// Pin the vtable to this file.
1117void MCWinCOFFObjectTargetWriter::anchor() {}
1118
Chris Lattner2c52b792010-07-11 22:07:02 +00001119//------------------------------------------------------------------------------
1120// WinCOFFObjectWriter factory function
1121
Rafael Espindola37099d92015-04-09 18:08:15 +00001122MCObjectWriter *
1123llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001124 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001125 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001126}