blob: e5af4ab8321ee8cfc2267a5bf5ca9dd5b37fd2c5 [file] [log] [blame]
Chris Lattner2c52b792010-07-11 22:07:02 +00001//===-- llvm/MC/WinCOFFObjectWriter.cpp -------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains an implementation of a Win32 COFF object file writer.
11//
12//===----------------------------------------------------------------------===//
13
Rafael Espindola908d2ed2011-12-24 02:14:02 +000014#include "llvm/MC/MCWinCOFFObjectWriter.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000015#include "llvm/ADT/DenseMap.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000016#include "llvm/ADT/STLExtras.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000017#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
Nico Riecka37acf72013-07-06 12:13:10 +000019#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCAsmLayout.h"
21#include "llvm/MC/MCAssembler.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCObjectWriter.h"
25#include "llvm/MC/MCSection.h"
26#include "llvm/MC/MCSectionCOFF.h"
27#include "llvm/MC/MCSymbol.h"
28#include "llvm/MC/MCValue.h"
Hans Wennborgf26bfc12014-09-29 22:43:20 +000029#include "llvm/MC/StringTableBuilder.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000030#include "llvm/Support/COFF.h"
31#include "llvm/Support/Debug.h"
Hans Wennborgba80b5d2014-09-28 00:22:27 +000032#include "llvm/Support/Endian.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000033#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000034#include "llvm/Support/TimeValue.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000035#include <cstdio>
36
Chris Lattner2c52b792010-07-11 22:07:02 +000037using namespace llvm;
38
Chandler Carruthf58e3762014-04-22 03:04:17 +000039#define DEBUG_TYPE "WinCOFFObjectWriter"
40
Chris Lattner2c52b792010-07-11 22:07:02 +000041namespace {
Dmitri Gribenko226fea52013-01-13 16:01:15 +000042typedef SmallString<COFF::NameSize> name;
Chris Lattner2c52b792010-07-11 22:07:02 +000043
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000044enum AuxiliaryType {
45 ATFunctionDefinition,
46 ATbfAndefSymbol,
47 ATWeakExternal,
48 ATFile,
49 ATSectionDefinition
50};
Chris Lattner2c52b792010-07-11 22:07:02 +000051
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000052struct AuxSymbol {
Rafael Espindola11e9e212015-05-27 14:37:12 +000053 AuxiliaryType AuxType;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000054 COFF::Auxiliary Aux;
55};
Chris Lattner2c52b792010-07-11 22:07:02 +000056
Michael J. Spencerd6283772010-09-27 08:58:26 +000057class COFFSymbol;
58class COFFSection;
59
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000060class COFFSymbol {
61public:
62 COFF::symbol Data;
Chris Lattner2c52b792010-07-11 22:07:02 +000063
Dmitri Gribenko226fea52013-01-13 16:01:15 +000064 typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
Chris Lattner2c52b792010-07-11 22:07:02 +000065
Rafael Espindola11e9e212015-05-27 14:37:12 +000066 name Name;
67 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000068 AuxiliarySymbols Aux;
Rafael Espindola11e9e212015-05-27 14:37:12 +000069 COFFSymbol *Other;
70 COFFSection *Section;
71 int Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000072
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +000073 const MCSymbol *MC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000074
Dmitri Gribenko226fea52013-01-13 16:01:15 +000075 COFFSymbol(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000076 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000077
78 bool should_keep() const;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000079};
80
81// This class contains staging data for a COFF relocation entry.
82struct COFFRelocation {
83 COFF::relocation Data;
Rafael Espindola11e9e212015-05-27 14:37:12 +000084 COFFSymbol *Symb;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000085
Craig Topperbb694de2014-04-13 04:57:38 +000086 COFFRelocation() : Symb(nullptr) {}
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000087 static size_t size() { return COFF::RelocationSize; }
88};
89
90typedef std::vector<COFFRelocation> relocations;
91
92class COFFSection {
93public:
94 COFF::section Header;
95
Rafael Espindola11e9e212015-05-27 14:37:12 +000096 std::string Name;
97 int Number;
Rafael Espindolaf59264f2015-05-27 14:45:54 +000098 MCSectionCOFF const *MCSection;
Rafael Espindola11e9e212015-05-27 14:37:12 +000099 COFFSymbol *Symbol;
100 relocations Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000101
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000102 COFFSection(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000103 static size_t size();
104};
105
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000106class WinCOFFObjectWriter : public MCObjectWriter {
107public:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000108 typedef std::vector<std::unique_ptr<COFFSymbol>> symbols;
David Blaikief564ab62014-04-15 05:25:03 +0000109 typedef std::vector<std::unique_ptr<COFFSection>> sections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000110
Rafael Espindola11e9e212015-05-27 14:37:12 +0000111 typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000112 typedef DenseMap<MCSection const *, COFFSection *> section_map;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000113
Ahmed Charles56440fd2014-03-06 05:51:42 +0000114 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000115
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000116 // Root level file contents.
117 COFF::header Header;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000118 sections Sections;
119 symbols Symbols;
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000120 StringTableBuilder Strings;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000121
122 // Maps used during object file creation.
123 section_map SectionMap;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000124 symbol_map SymbolMap;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000125
David Majnemer4d571592014-09-15 19:42:42 +0000126 bool UseBigObj;
127
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000128 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000129
Yaron Kerencca43c12014-09-16 21:31:04 +0000130 void reset() override {
131 memset(&Header, 0, sizeof(Header));
132 Header.Machine = TargetObjectWriter->getMachine();
133 Sections.clear();
134 Symbols.clear();
135 Strings.clear();
136 SectionMap.clear();
137 SymbolMap.clear();
138 MCObjectWriter::reset();
139 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000140
Michael J. Spencer17990d52010-10-16 08:25:57 +0000141 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola11e9e212015-05-27 14:37:12 +0000142 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000143 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000144
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000145 template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000146 object_t *createCOFFEntity(StringRef Name, list_t &List);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000147
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000148 void defineSection(MCSectionCOFF const &Sec);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000149 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000150 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000151
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000152 void SetSymbolName(COFFSymbol &S);
153 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000154
David Majnemer299674e2014-07-13 04:31:19 +0000155 bool ExportSymbol(const MCSymbol &Symbol, MCAssembler &Asm);
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000156
Michael J. Spencerd6283772010-09-27 08:58:26 +0000157 bool IsPhysicalSection(COFFSection *S);
158
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000159 // Entity writing methods.
160
161 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000162 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000163 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
164 void WriteSectionHeader(const COFF::section &S);
165 void WriteRelocation(const COFF::relocation &R);
166
167 // MCObjectWriter interface implementation.
168
Craig Topper34a61bc2014-03-08 07:02:02 +0000169 void ExecutePostLayoutBinding(MCAssembler &Asm,
170 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000171
David Majnemer2cc4bc772014-11-11 08:43:57 +0000172 bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000173 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000174 const MCFragment &FB, bool InSet,
175 bool IsPCRel) const override;
176
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000177 bool isWeak(const MCSymbol &Sym) const override;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000178
Rafael Espindola26585542015-01-19 21:11:14 +0000179 void RecordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000180 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000181 MCValue Target, bool &IsPCRel,
182 uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000183
Craig Topper34a61bc2014-03-08 07:02:02 +0000184 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000185};
Chris Lattner2c52b792010-07-11 22:07:02 +0000186}
187
Hans Wennborgba80b5d2014-09-28 00:22:27 +0000188static inline void write_uint32_le(void *Data, uint32_t Value) {
189 support::endian::write<uint32_t, support::little, support::unaligned>(Data,
190 Value);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000191}
192
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000193//------------------------------------------------------------------------------
194// Symbol class implementation
195
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000196COFFSymbol::COFFSymbol(StringRef name)
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000197 : Name(name.begin(), name.end()), Other(nullptr), Section(nullptr),
198 Relocations(0), MC(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000199 memset(&Data, 0, sizeof(Data));
200}
201
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000202// In the case that the name does not fit within 8 bytes, the offset
203// into the string table is stored in the last 4 bytes instead, leaving
204// the first 4 bytes as 0.
205void COFFSymbol::set_name_offset(uint32_t Offset) {
206 write_uint32_le(Data.Name + 0, 0);
207 write_uint32_le(Data.Name + 4, Offset);
208}
209
Michael J. Spencerd6283772010-09-27 08:58:26 +0000210/// logic to decide if the symbol should be reported in the symbol table
211bool COFFSymbol::should_keep() const {
212 // no section means its external, keep it
Craig Topperbb694de2014-04-13 04:57:38 +0000213 if (!Section)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000214 return true;
215
216 // if it has relocations pointing at it, keep it
Rafael Espindola11e9e212015-05-27 14:37:12 +0000217 if (Relocations > 0) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000218 assert(Section->Number != -1 && "Sections with relocations must be real!");
219 return true;
220 }
221
222 // if the section its in is being droped, drop it
223 if (Section->Number == -1)
Rafael Espindola11e9e212015-05-27 14:37:12 +0000224 return false;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000225
226 // if it is the section symbol, keep it
227 if (Section->Symbol == this)
228 return true;
229
230 // if its temporary, drop it
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000231 if (MC && MC->isTemporary())
232 return false;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000233
234 // otherwise, keep it
235 return true;
236}
237
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000238//------------------------------------------------------------------------------
239// Section class implementation
240
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000241COFFSection::COFFSection(StringRef name)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000242 : Name(name), MCSection(nullptr), Symbol(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000243 memset(&Header, 0, sizeof(Header));
244}
245
Rafael Espindola11e9e212015-05-27 14:37:12 +0000246size_t COFFSection::size() { return COFF::SectionSize; }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000247
248//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000249// WinCOFFObjectWriter class implementation
250
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000251WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000252 raw_pwrite_stream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000253 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000254 memset(&Header, 0, sizeof(Header));
Michael J. Spencer377aa202010-08-21 05:58:13 +0000255
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000256 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000257}
258
Michael J. Spencer17990d52010-10-16 08:25:57 +0000259COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000260 return createCOFFEntity<COFFSymbol>(Name, Symbols);
261}
262
Yaron Keren56919ef2014-12-04 08:30:39 +0000263COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000264 symbol_map::iterator i = SymbolMap.find(Symbol);
265 if (i != SymbolMap.end())
266 return i->second;
Yaron Keren56919ef2014-12-04 08:30:39 +0000267 COFFSymbol *RetSymbol =
268 createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000269 SymbolMap[Symbol] = RetSymbol;
270 return RetSymbol;
271}
272
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000273COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000274 return createCOFFEntity<COFFSection>(Name, Sections);
275}
276
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000277/// A template used to lookup or create a symbol/section, and initialize it if
278/// needed.
279template <typename object_t, typename list_t>
Rafael Espindola11e9e212015-05-27 14:37:12 +0000280object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name, list_t &List) {
David Blaikief564ab62014-04-15 05:25:03 +0000281 List.push_back(make_unique<object_t>(Name));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000282
David Blaikief564ab62014-04-15 05:25:03 +0000283 return List.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000284}
285
286/// This function takes a section data object from the assembler
287/// and creates the associated COFF section staging object.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000288void WinCOFFObjectWriter::defineSection(MCSectionCOFF const &Sec) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000289 COFFSection *coff_section = createSection(Sec.getSectionName());
Rafael Espindola11e9e212015-05-27 14:37:12 +0000290 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
Rafael Espindola0766ae02014-06-06 19:26:12 +0000291 if (Sec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
292 if (const MCSymbol *S = Sec.getCOMDATSymbol()) {
293 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
294 if (COMDATSymbol->Section)
295 report_fatal_error("two sections have the same comdat");
296 COMDATSymbol->Section = coff_section;
297 }
298 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000299
Michael J. Spencerd6283772010-09-27 08:58:26 +0000300 coff_section->Symbol = coff_symbol;
301 coff_symbol->Section = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000302 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000303
304 // In this case the auxiliary symbol is a Section Definition.
305 coff_symbol->Aux.resize(1);
306 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
307 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000308 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
309
310 coff_section->Header.Characteristics = Sec.getCharacteristics();
311
312 uint32_t &Characteristics = coff_section->Header.Characteristics;
Rafael Espindola967d6a62015-05-21 21:02:35 +0000313 switch (Sec.getAlignment()) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000314 case 1:
315 Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES;
316 break;
317 case 2:
318 Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES;
319 break;
320 case 4:
321 Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES;
322 break;
323 case 8:
324 Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES;
325 break;
326 case 16:
327 Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES;
328 break;
329 case 32:
330 Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES;
331 break;
332 case 64:
333 Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES;
334 break;
335 case 128:
336 Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES;
337 break;
338 case 256:
339 Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES;
340 break;
341 case 512:
342 Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES;
343 break;
344 case 1024:
345 Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES;
346 break;
347 case 2048:
348 Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES;
349 break;
350 case 4096:
351 Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES;
352 break;
353 case 8192:
354 Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES;
355 break;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000356 default:
357 llvm_unreachable("unsupported section alignment");
358 }
359
360 // Bind internal COFF section to MC section.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000361 coff_section->MCSection = &Sec;
362 SectionMap[&Sec] = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000363}
364
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000365static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000366 const MCAsmLayout &Layout) {
Rafael Espindolabeb60602015-05-29 20:41:47 +0000367 const MCSymbol &Data = Symbol.getData();
Rafael Espindola14672502015-05-29 17:48:04 +0000368 if (Symbol.isCommon() && Data.isExternal())
369 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000370
371 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000372 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000373 return 0;
374
375 return Res;
376}
377
David Majnemera9bdb322014-04-08 22:33:40 +0000378/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000379/// and creates the associated COFF symbol staging object.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000380void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000381 MCAssembler &Assembler,
382 const MCAsmLayout &Layout) {
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000383 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Peter Collingbourne89886872013-04-22 18:48:56 +0000384 SymbolMap[&Symbol] = coff_symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000385
Rafael Espindola2229d332015-05-29 19:07:51 +0000386 if (Symbol.getFlags() & COFF::SF_WeakExternal) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000387 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
388
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000389 if (Symbol.isVariable()) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000390 const MCSymbolRefExpr *SymRef =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000391 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000392
Peter Collingbourne89886872013-04-22 18:48:56 +0000393 if (!SymRef)
394 report_fatal_error("Weak externals may only alias symbols");
Michael J. Spencer17990d52010-10-16 08:25:57 +0000395
Peter Collingbourne89886872013-04-22 18:48:56 +0000396 coff_symbol->Other = GetOrCreateCOFFSymbol(&SymRef->getSymbol());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000397 } else {
Yaron Keren075759a2015-03-30 15:42:36 +0000398 std::string WeakName = (".weak." + Symbol.getName() + ".default").str();
Michael J. Spencer17990d52010-10-16 08:25:57 +0000399 COFFSymbol *WeakDefault = createSymbol(WeakName);
400 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000401 WeakDefault->Data.StorageClass = COFF::IMAGE_SYM_CLASS_EXTERNAL;
402 WeakDefault->Data.Type = 0;
403 WeakDefault->Data.Value = 0;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000404 coff_symbol->Other = WeakDefault;
405 }
406
407 // Setup the Weak External auxiliary symbol.
408 coff_symbol->Aux.resize(1);
409 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
410 coff_symbol->Aux[0].AuxType = ATWeakExternal;
411 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
412 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000413 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000414
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000415 coff_symbol->MC = &Symbol;
Peter Collingbourne89886872013-04-22 18:48:56 +0000416 } else {
Rafael Espindolabeb60602015-05-29 20:41:47 +0000417 const MCSymbol &ResSymData = Symbol.getData();
Rafael Espindola575f79a2014-05-01 13:37:57 +0000418 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000419 coff_symbol->Data.Value = getSymbolValue(Symbol, Layout);
Reid Klecknerc1e76212013-09-17 23:18:05 +0000420
Rafael Espindola2229d332015-05-29 19:07:51 +0000421 coff_symbol->Data.Type = (Symbol.getFlags() & 0x0000FFFF) >> 0;
422 coff_symbol->Data.StorageClass = (Symbol.getFlags() & 0x00FF0000) >> 16;
Peter Collingbourne89886872013-04-22 18:48:56 +0000423
424 // If no storage class was specified in the streamer, define it here.
425 if (coff_symbol->Data.StorageClass == 0) {
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000426 bool IsExternal = ResSymData.isExternal() ||
427 (!ResSymData.getFragment() && !Symbol.isVariable());
Peter Collingbourne89886872013-04-22 18:48:56 +0000428
David Majnemer299674e2014-07-13 04:31:19 +0000429 coff_symbol->Data.StorageClass = IsExternal
430 ? COFF::IMAGE_SYM_CLASS_EXTERNAL
431 : COFF::IMAGE_SYM_CLASS_STATIC;
Peter Collingbourne89886872013-04-22 18:48:56 +0000432 }
433
Rafael Espindola575f79a2014-05-01 13:37:57 +0000434 if (!Base) {
Reid Klecknerc1e76212013-09-17 23:18:05 +0000435 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola575f79a2014-05-01 13:37:57 +0000436 } else {
Rafael Espindolabeb60602015-05-29 20:41:47 +0000437 const MCSymbol &BaseData = Base->getData();
Benjamin Kramer3e67db92014-10-11 15:07:21 +0000438 if (BaseData.getFragment()) {
Rafael Espindola7549f872015-05-26 00:36:57 +0000439 COFFSection *Sec = SectionMap[BaseData.getFragment()->getParent()];
Rafael Espindola0766ae02014-06-06 19:26:12 +0000440
441 if (coff_symbol->Section && coff_symbol->Section != Sec)
442 report_fatal_error("conflicting sections for symbol");
443
444 coff_symbol->Section = Sec;
445 }
Rafael Espindola575f79a2014-05-01 13:37:57 +0000446 }
Peter Collingbourne89886872013-04-22 18:48:56 +0000447
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000448 coff_symbol->MC = &Symbol;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000449 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000450}
451
Nico Rieck01143f92014-02-25 09:50:40 +0000452// Maximum offsets for different string table entry encodings.
453static const unsigned Max6DecimalOffset = 999999;
454static const unsigned Max7DecimalOffset = 9999999;
455static const uint64_t MaxBase64Offset = 0xFFFFFFFFFULL; // 64^6, including 0
456
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000457// Encode a string table entry offset in base 64, padded to 6 chars, and
458// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
459// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000460static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000461 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000462 "Illegal section name encoding for value");
463
464 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
465 "abcdefghijklmnopqrstuvwxyz"
466 "0123456789+/";
467
468 Buffer[0] = '/';
469 Buffer[1] = '/';
470
Rafael Espindola11e9e212015-05-27 14:37:12 +0000471 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000472 for (unsigned i = 0; i < 6; ++i) {
473 unsigned Rem = Value % 64;
474 Value /= 64;
475 *(Ptr--) = Alphabet[Rem];
476 }
477}
478
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000479void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000480 if (S.Name.size() > COFF::NameSize) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000481 uint64_t StringTableEntry = Strings.getOffset(S.Name);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000482
Nico Rieck01143f92014-02-25 09:50:40 +0000483 if (StringTableEntry <= Max6DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000484 std::sprintf(S.Header.Name, "/%d", unsigned(StringTableEntry));
Nico Rieck01143f92014-02-25 09:50:40 +0000485 } else if (StringTableEntry <= Max7DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000486 // With seven digits, we have to skip the terminating null. Because
487 // sprintf always appends it, we use a larger temporary buffer.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000488 char buffer[9] = {};
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000489 std::sprintf(buffer, "/%d", unsigned(StringTableEntry));
490 std::memcpy(S.Header.Name, buffer, 8);
Nico Rieck01143f92014-02-25 09:50:40 +0000491 } else if (StringTableEntry <= MaxBase64Offset) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000492 // Starting with 10,000,000, offsets are encoded as base64.
493 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000494 } else {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000495 report_fatal_error("COFF string table is greater than 64 GB.");
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000496 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000497 } else
498 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000499}
500
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000501void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
502 if (S.Name.size() > COFF::NameSize)
503 S.set_name_offset(Strings.getOffset(S.Name));
504 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000505 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000506}
507
David Majnemer299674e2014-07-13 04:31:19 +0000508bool WinCOFFObjectWriter::ExportSymbol(const MCSymbol &Symbol,
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000509 MCAssembler &Asm) {
510 // This doesn't seem to be right. Strings referred to from the .data section
511 // need symbols so they can be linked to code in the .text section right?
512
David Majnemer299674e2014-07-13 04:31:19 +0000513 // return Asm.isSymbolLinkerVisible(Symbol);
514
515 // Non-temporary labels should always be visible to the linker.
516 if (!Symbol.isTemporary())
517 return true;
518
519 // Absolute temporary labels are never visible.
520 if (!Symbol.isInSection())
521 return false;
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000522
523 // For now, all non-variable symbols are exported,
524 // the linker will sort the rest out for us.
David Majnemer299674e2014-07-13 04:31:19 +0000525 return !Symbol.isVariable();
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000526}
527
Michael J. Spencerd6283772010-09-27 08:58:26 +0000528bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000529 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
530 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000531}
532
533//------------------------------------------------------------------------------
534// entity writing methods
535
536void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000537 if (UseBigObj) {
538 WriteLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
539 WriteLE16(0xFFFF);
540 WriteLE16(COFF::BigObjHeader::MinBigObjectVersion);
541 WriteLE16(Header.Machine);
542 WriteLE32(Header.TimeDateStamp);
Benjamin Kramer97fbdd52015-04-17 11:12:43 +0000543 WriteBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
David Majnemer15f7ed92014-09-15 20:28:38 +0000544 WriteLE32(0);
545 WriteLE32(0);
546 WriteLE32(0);
547 WriteLE32(0);
David Majnemer4d571592014-09-15 19:42:42 +0000548 WriteLE32(Header.NumberOfSections);
549 WriteLE32(Header.PointerToSymbolTable);
550 WriteLE32(Header.NumberOfSymbols);
551 } else {
552 WriteLE16(Header.Machine);
553 WriteLE16(static_cast<int16_t>(Header.NumberOfSections));
554 WriteLE32(Header.TimeDateStamp);
555 WriteLE32(Header.PointerToSymbolTable);
556 WriteLE32(Header.NumberOfSymbols);
557 WriteLE16(Header.SizeOfOptionalHeader);
558 WriteLE16(Header.Characteristics);
559 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000560}
561
David Blaikief564ab62014-04-15 05:25:03 +0000562void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
563 WriteBytes(StringRef(S.Data.Name, COFF::NameSize));
564 WriteLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000565 if (UseBigObj)
566 WriteLE32(S.Data.SectionNumber);
567 else
568 WriteLE16(static_cast<int16_t>(S.Data.SectionNumber));
David Blaikief564ab62014-04-15 05:25:03 +0000569 WriteLE16(S.Data.Type);
570 Write8(S.Data.StorageClass);
571 Write8(S.Data.NumberOfAuxSymbols);
572 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000573}
574
575void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000576 const COFFSymbol::AuxiliarySymbols &S) {
577 for (COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
578 i != e; ++i) {
579 switch (i->AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000580 case ATFunctionDefinition:
581 WriteLE32(i->Aux.FunctionDefinition.TagIndex);
582 WriteLE32(i->Aux.FunctionDefinition.TotalSize);
583 WriteLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
584 WriteLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
585 WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000586 if (UseBigObj)
587 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000588 break;
589 case ATbfAndefSymbol:
590 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
591 WriteLE16(i->Aux.bfAndefSymbol.Linenumber);
592 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
593 WriteLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
594 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000595 if (UseBigObj)
596 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000597 break;
598 case ATWeakExternal:
599 WriteLE32(i->Aux.WeakExternal.TagIndex);
600 WriteLE32(i->Aux.WeakExternal.Characteristics);
601 WriteZeros(sizeof(i->Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000602 if (UseBigObj)
603 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000604 break;
605 case ATFile:
David Majnemer4d571592014-09-15 19:42:42 +0000606 WriteBytes(
607 StringRef(reinterpret_cast<const char *>(&i->Aux),
608 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000609 break;
610 case ATSectionDefinition:
611 WriteLE32(i->Aux.SectionDefinition.Length);
612 WriteLE16(i->Aux.SectionDefinition.NumberOfRelocations);
613 WriteLE16(i->Aux.SectionDefinition.NumberOfLinenumbers);
614 WriteLE32(i->Aux.SectionDefinition.CheckSum);
David Majnemer4d571592014-09-15 19:42:42 +0000615 WriteLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000616 Write8(i->Aux.SectionDefinition.Selection);
617 WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000618 WriteLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number >> 16));
619 if (UseBigObj)
620 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000621 break;
622 }
623 }
624}
625
626void WinCOFFObjectWriter::WriteSectionHeader(const COFF::section &S) {
627 WriteBytes(StringRef(S.Name, COFF::NameSize));
628
629 WriteLE32(S.VirtualSize);
630 WriteLE32(S.VirtualAddress);
631 WriteLE32(S.SizeOfRawData);
632 WriteLE32(S.PointerToRawData);
633 WriteLE32(S.PointerToRelocations);
634 WriteLE32(S.PointerToLineNumbers);
635 WriteLE16(S.NumberOfRelocations);
636 WriteLE16(S.NumberOfLineNumbers);
637 WriteLE32(S.Characteristics);
638}
639
640void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
641 WriteLE32(R.VirtualAddress);
642 WriteLE32(R.SymbolTableIndex);
643 WriteLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000644}
645
646////////////////////////////////////////////////////////////////////////////////
647// MCObjectWriter interface implementations
648
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000649void WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
650 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000651 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000652 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000653 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000654 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000655
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000656 for (const MCSymbol &Symbol : Asm.symbols())
657 if (ExportSymbol(Symbol, Asm))
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000658 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000659}
660
David Majnemer2cc4bc772014-11-11 08:43:57 +0000661bool WinCOFFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000662 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000663 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000664 // MS LINK expects to be able to replace all references to a function with a
665 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
666 // away any relocations to functions.
Rafael Espindola2229d332015-05-29 19:07:51 +0000667 if ((((SymA.getFlags() & COFF::SF_TypeMask) >> COFF::SF_TypeShift) >>
David Majnemer2cc4bc772014-11-11 08:43:57 +0000668 COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
669 return false;
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000670 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000671 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000672}
673
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000674bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
Rafael Espindolabeb60602015-05-29 20:41:47 +0000675 const MCSymbol &SD = Sym.getData();
Rafael Espindola88af4112015-04-17 11:27:13 +0000676 if (!SD.isExternal())
677 return false;
678
Rafael Espindola88af4112015-04-17 11:27:13 +0000679 if (!Sym.isInSection())
680 return false;
681
682 const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
683 if (!Sec.getCOMDATSymbol())
684 return false;
685
686 // It looks like for COFF it is invalid to replace a reference to a global
687 // in a comdat with a reference to a local.
688 // FIXME: Add a specification reference if available.
689 return true;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000690}
691
Rafael Espindola26585542015-01-19 21:11:14 +0000692void WinCOFFObjectWriter::RecordRelocation(
693 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
694 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000695 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000696
Reid Klecknerdae7b4e2013-07-15 19:41:21 +0000697 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola88af4112015-04-17 11:27:13 +0000698 const MCSymbol &A = Symbol;
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000699 if (!Asm.hasSymbolData(A))
Rafael Espindola11e9e212015-05-27 14:37:12 +0000700 Asm.getContext().reportFatalError(Fixup.getLoc(),
701 Twine("symbol '") + A.getName() +
702 "' can not be undefined");
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000703
Rafael Espindolabeb60602015-05-29 20:41:47 +0000704 const MCSymbol &A_SD = A.getData();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000705
Rafael Espindola7549f872015-05-26 00:36:57 +0000706 MCSection *Section = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000707
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000708 // Mark this symbol as requiring an entry in the symbol table.
Rafael Espindola7549f872015-05-26 00:36:57 +0000709 assert(SectionMap.find(Section) != SectionMap.end() &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000710 "Section must already have been defined in ExecutePostLayoutBinding!");
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000711 assert(SymbolMap.find(&A) != SymbolMap.end() &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000712 "Symbol must already have been defined in ExecutePostLayoutBinding!");
713
Rafael Espindola7549f872015-05-26 00:36:57 +0000714 COFFSection *coff_section = SectionMap[Section];
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000715 COFFSymbol *coff_symbol = SymbolMap[&A];
Rafael Espindolaed164772011-04-20 14:01:45 +0000716 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000717 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000718
David Majnemera45a1762014-01-06 07:39:46 +0000719 if (SymB) {
720 const MCSymbol *B = &SymB->getSymbol();
Rafael Espindolabeb60602015-05-29 20:41:47 +0000721 const MCSymbol &B_SD = B->getData();
David Majnemera45a1762014-01-06 07:39:46 +0000722 if (!B_SD.getFragment())
Jim Grosbach6f482002015-05-18 18:43:14 +0000723 Asm.getContext().reportFatalError(
David Majnemera45a1762014-01-06 07:39:46 +0000724 Fixup.getLoc(),
725 Twine("symbol '") + B->getName() +
726 "' can not be undefined in a subtraction expression");
727
728 if (!A_SD.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 '") + Symbol.getName() +
732 "' can not be undefined in a subtraction expression");
733
734 CrossSection = &Symbol.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000735
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000736 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000737 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000738
739 // In the case where we have SymbA and SymB, we just need to store the delta
740 // between the two symbols. Update FixedValue to account for the delta, and
741 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000742 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000743 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000744 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000745 return;
David Majnemer1de30942015-02-09 06:31:31 +0000746 }
747
748 // Offset of the relocation in the section
749 int64_t OffsetOfRelocation =
750 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
751
Andy Ayers9e5c8512015-05-14 01:10:41 +0000752 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000753 } else {
754 FixedValue = Target.getConstant();
755 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000756
757 COFFRelocation Reloc;
758
Daniel Dunbar727be432010-07-31 21:08:54 +0000759 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000760 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000761
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000762 // Turn relocations for temporary symbols into section relocations.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000763 if (coff_symbol->MC->isTemporary() || CrossSection) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000764 Reloc.Symb = coff_symbol->Section->Symbol;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000765 FixedValue +=
766 Layout.getFragmentOffset(coff_symbol->MC->getData().getFragment()) +
Rafael Espindola14672502015-05-29 17:48:04 +0000767 coff_symbol->MC->getOffset();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000768 } else
769 Reloc.Symb = coff_symbol;
770
771 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000772
773 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000774 Reloc.Data.Type = TargetObjectWriter->getRelocType(
775 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000776
777 // FIXME: Can anyone explain what this does other than adjust for the size
778 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000779 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
780 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
781 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
782 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000783 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000784
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000785 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
786 switch (Reloc.Data.Type) {
787 case COFF::IMAGE_REL_ARM_ABSOLUTE:
788 case COFF::IMAGE_REL_ARM_ADDR32:
789 case COFF::IMAGE_REL_ARM_ADDR32NB:
790 case COFF::IMAGE_REL_ARM_TOKEN:
791 case COFF::IMAGE_REL_ARM_SECTION:
792 case COFF::IMAGE_REL_ARM_SECREL:
793 break;
794 case COFF::IMAGE_REL_ARM_BRANCH11:
795 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000796 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
797 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
798 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000799 case COFF::IMAGE_REL_ARM_BRANCH24:
800 case COFF::IMAGE_REL_ARM_BLX24:
801 case COFF::IMAGE_REL_ARM_MOV32A:
802 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
803 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000804 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000805 // generate the relocations however the rest of the MSVC toolchain is
806 // unable to handle it.
807 llvm_unreachable("unsupported relocation");
808 break;
809 case COFF::IMAGE_REL_ARM_MOV32T:
810 break;
811 case COFF::IMAGE_REL_ARM_BRANCH20T:
812 case COFF::IMAGE_REL_ARM_BRANCH24T:
813 case COFF::IMAGE_REL_ARM_BLX23T:
814 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
815 // perform a 4 byte adjustment to the relocation. Relative branches are
816 // offset by 4 on ARM, however, because there is no RELA relocations, all
817 // branches are offset by 4.
818 FixedValue = FixedValue + 4;
819 break;
820 }
821 }
822
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000823 if (TargetObjectWriter->recordRelocation(Fixup))
824 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000825}
826
Rafael Espindolabce26a12010-10-05 15:11:03 +0000827void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000828 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000829 size_t SectionsSize = Sections.size();
830 if (SectionsSize > static_cast<size_t>(INT32_MAX))
831 report_fatal_error(
832 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000833
David Majnemer4d571592014-09-15 19:42:42 +0000834 // Assign symbol and section indexes and offsets.
835 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
836
837 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
838
839 DenseMap<COFFSection *, int32_t> SectionIndices(
840 NextPowerOf2(NumberOfSections));
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000841
842 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000843 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000844 for (const auto &Section : Sections) {
David Majnemer4d571592014-09-15 19:42:42 +0000845 SectionIndices[Section.get()] = Number;
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000846 Section->Number = Number;
847 Section->Symbol->Data.SectionNumber = Number;
848 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000849 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000850 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000851
David Majnemer4d571592014-09-15 19:42:42 +0000852 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000853 Header.NumberOfSymbols = 0;
854
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000855 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000856 // round up to calculate the number of auxiliary symbols required
857 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000858 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000859
860 COFFSymbol *file = createSymbol(".file");
861 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
862 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
863 file->Aux.resize(Count);
864
865 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000866 unsigned Length = Name.size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000867 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000868 Aux.AuxType = ATFile;
869
870 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000871 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000872 Length = Length - SymbolSize;
873 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000874 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000875 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
876 break;
877 }
878
879 Offset += SymbolSize;
880 }
881 }
882
David Majnemer9ab5ff12014-08-28 04:02:50 +0000883 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000884 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000885 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000886 Symbol->Data.SectionNumber = Symbol->Section->Number;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000887 if (Symbol->should_keep()) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000888 Symbol->Index = Header.NumberOfSymbols++;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000889 // Update auxiliary symbol info.
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000890 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
891 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000892 } else
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000893 Symbol->Index = -1;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000894 }
895
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000896 // Build string table.
897 for (const auto &S : Sections)
898 if (S->Name.size() > COFF::NameSize)
899 Strings.add(S->Name);
900 for (const auto &S : Symbols)
901 if (S->should_keep() && S->Name.size() > COFF::NameSize)
902 Strings.add(S->Name);
903 Strings.finalize(StringTableBuilder::WinCOFF);
904
905 // Set names.
906 for (const auto &S : Sections)
907 SetSectionName(*S);
908 for (auto &S : Symbols)
909 if (S->should_keep())
910 SetSymbolName(*S);
911
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000912 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000913 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000914 if (Symbol->Other) {
915 assert(Symbol->Index != -1);
916 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
917 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000918 "Symbol's aux symbol must be a Weak External!");
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000919 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000920 }
921 }
922
Nico Riecka37acf72013-07-06 12:13:10 +0000923 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000924 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000925 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000926 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
927 continue;
928
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000929 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +0000930
Rafael Espindola0766ae02014-06-06 19:26:12 +0000931 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
932 assert(COMDAT);
933 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
934 assert(COMDATSymbol);
935 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000936 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000937 report_fatal_error(
938 Twine("Missing associated COMDAT section for section ") +
939 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000940
941 // Skip this section if the associated section is unused.
942 if (Assoc->Number == -1)
943 continue;
944
Rafael Espindola11e9e212015-05-27 14:37:12 +0000945 Section->Symbol->Aux[0].Aux.SectionDefinition.Number =
946 SectionIndices[Assoc];
Nico Riecka37acf72013-07-06 12:13:10 +0000947 }
948
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000949 // Assign file offsets to COFF object file structures.
950
951 unsigned offset = 0;
952
David Majnemer4d571592014-09-15 19:42:42 +0000953 if (UseBigObj)
954 offset += COFF::Header32Size;
955 else
956 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000957 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000958
Yaron Keren56919ef2014-12-04 08:30:39 +0000959 for (const auto &Section : Asm) {
Rafael Espindolaa554c052015-05-25 23:14:17 +0000960 COFFSection *Sec = SectionMap[&Section];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000961
Michael J. Spencerd6283772010-09-27 08:58:26 +0000962 if (Sec->Number == -1)
963 continue;
964
Rafael Espindola5a1e80b2015-05-26 02:00:36 +0000965 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000966
Michael J. Spencerd6283772010-09-27 08:58:26 +0000967 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000968 // Align the section data to a four byte boundary.
David Majnemerab2b25b2015-02-11 22:51:55 +0000969 offset = RoundUpToAlignment(offset, 4);
970 Sec->Header.PointerToRawData = offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000971
972 offset += Sec->Header.SizeOfRawData;
973 }
974
975 if (Sec->Relocations.size() > 0) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000976 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
977
978 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000979 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000980 // size is found in reloc #0. Microsoft tools understand this.
981 Sec->Header.NumberOfRelocations = 0xffff;
982 } else {
983 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
984 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000985 Sec->Header.PointerToRelocations = offset;
986
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000987 if (RelocationsOverflow) {
988 // Reloc #0 will contain actual count, so make room for it.
989 offset += COFF::RelocationSize;
990 }
991
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000992 offset += COFF::RelocationSize * Sec->Relocations.size();
993
Yaron Keren56919ef2014-12-04 08:30:39 +0000994 for (auto &Relocation : Sec->Relocations) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000995 assert(Relocation.Symb->Index != -1);
996 Relocation.Data.SymbolTableIndex = Relocation.Symb->Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000997 }
998 }
999
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001000 assert(Sec->Symbol->Aux.size() == 1 &&
1001 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +00001002 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001003 assert(Aux.AuxType == ATSectionDefinition &&
1004 "Section's symbol's aux symbol must be a Section Definition!");
1005 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
1006 Aux.Aux.SectionDefinition.NumberOfRelocations =
Rafael Espindola11e9e212015-05-27 14:37:12 +00001007 Sec->Header.NumberOfRelocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001008 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
Rafael Espindola11e9e212015-05-27 14:37:12 +00001009 Sec->Header.NumberOfLineNumbers;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001010 }
1011
1012 Header.PointerToSymbolTable = offset;
1013
Rafael Espindola5ac4ad22013-12-04 02:26:54 +00001014 // We want a deterministic output. It looks like GNU as also writes 0 in here.
Rafael Espindola9201e6b2013-12-04 02:02:55 +00001015 Header.TimeDateStamp = 0;
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +00001016
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001017 // Write it all to disk...
1018 WriteFileHeader(Header);
1019
1020 {
1021 sections::iterator i, ie;
1022 MCAssembler::const_iterator j, je;
1023
Yaron Keren56919ef2014-12-04 08:30:39 +00001024 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001025 if (Section->Number != -1) {
1026 if (Section->Relocations.size() >= 0xffff)
1027 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
1028 WriteSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001029 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001030 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001031
Rafael Espindola11e9e212015-05-27 14:37:12 +00001032 for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
1033 je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +00001034 (i != ie) && (j != je); ++i, ++j) {
1035
1036 if ((*i)->Number == -1)
1037 continue;
1038
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001039 if ((*i)->Header.PointerToRawData != 0) {
David Majnemer3df3c612015-02-11 22:22:30 +00001040 assert(OS.tell() <= (*i)->Header.PointerToRawData &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001041 "Section::PointerToRawData is insane!");
1042
David Majnemer3df3c612015-02-11 22:22:30 +00001043 unsigned SectionDataPadding = (*i)->Header.PointerToRawData - OS.tell();
1044 assert(SectionDataPadding < 4 &&
1045 "Should only need at most three bytes of padding!");
1046
1047 WriteZeros(SectionDataPadding);
1048
Rafael Espindola64acc7f2015-05-26 02:17:21 +00001049 Asm.writeSectionData(&*j, Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001050 }
1051
1052 if ((*i)->Relocations.size() > 0) {
1053 assert(OS.tell() == (*i)->Header.PointerToRelocations &&
1054 "Section::PointerToRelocations is insane!");
1055
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001056 if ((*i)->Relocations.size() >= 0xffff) {
1057 // In case of overflow, write actual relocation count as first
1058 // relocation. Including the synthetic reloc itself (+ 1).
1059 COFF::relocation r;
1060 r.VirtualAddress = (*i)->Relocations.size() + 1;
1061 r.SymbolTableIndex = 0;
1062 r.Type = 0;
1063 WriteRelocation(r);
1064 }
1065
Yaron Keren56919ef2014-12-04 08:30:39 +00001066 for (const auto &Relocation : (*i)->Relocations)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001067 WriteRelocation(Relocation.Data);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001068 } else
1069 assert((*i)->Header.PointerToRelocations == 0 &&
1070 "Section::PointerToRelocations is insane!");
1071 }
1072 }
1073
1074 assert(OS.tell() == Header.PointerToSymbolTable &&
1075 "Header::PointerToSymbolTable is insane!");
1076
Yaron Keren56919ef2014-12-04 08:30:39 +00001077 for (auto &Symbol : Symbols)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001078 if (Symbol->Index != -1)
1079 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001080
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001081 OS.write(Strings.data().data(), Strings.data().size());
Chris Lattner2c52b792010-07-11 22:07:02 +00001082}
1083
Rafael Espindola11e9e212015-05-27 14:37:12 +00001084MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1085 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001086
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001087// Pin the vtable to this file.
1088void MCWinCOFFObjectTargetWriter::anchor() {}
1089
Chris Lattner2c52b792010-07-11 22:07:02 +00001090//------------------------------------------------------------------------------
1091// WinCOFFObjectWriter factory function
1092
Rafael Espindola37099d92015-04-09 18:08:15 +00001093MCObjectWriter *
1094llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001095 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001096 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001097}