blob: 36b317eebb921907ebe03dbfcd45d482ef9b0167 [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"
David Majnemer18663f82015-12-21 08:03:07 +000020#include "llvm/Config/config.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCAsmLayout.h"
22#include "llvm/MC/MCAssembler.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCExpr.h"
David Majnemer4eecd302015-05-30 04:56:02 +000025#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/MC/MCObjectWriter.h"
27#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCSectionCOFF.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000029#include "llvm/MC/MCSymbolCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/MC/MCValue.h"
Hans Wennborgf26bfc12014-09-29 22:43:20 +000031#include "llvm/MC/StringTableBuilder.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000032#include "llvm/Support/COFF.h"
33#include "llvm/Support/Debug.h"
Hans Wennborgba80b5d2014-09-28 00:22:27 +000034#include "llvm/Support/Endian.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000035#include "llvm/Support/ErrorHandling.h"
David Majnemer6ddc6362015-09-01 21:23:58 +000036#include "llvm/Support/JamCRC.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000037#include "llvm/Support/TimeValue.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000038#include <cstdio>
David Majnemer088ba022015-09-01 23:46:11 +000039#include <ctime>
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000040
Chris Lattner2c52b792010-07-11 22:07:02 +000041using namespace llvm;
42
Chandler Carruthf58e3762014-04-22 03:04:17 +000043#define DEBUG_TYPE "WinCOFFObjectWriter"
44
Chris Lattner2c52b792010-07-11 22:07:02 +000045namespace {
Dmitri Gribenko226fea52013-01-13 16:01:15 +000046typedef SmallString<COFF::NameSize> name;
Chris Lattner2c52b792010-07-11 22:07:02 +000047
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000048enum AuxiliaryType {
49 ATFunctionDefinition,
50 ATbfAndefSymbol,
51 ATWeakExternal,
52 ATFile,
53 ATSectionDefinition
54};
Chris Lattner2c52b792010-07-11 22:07:02 +000055
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000056struct AuxSymbol {
Rafael Espindola11e9e212015-05-27 14:37:12 +000057 AuxiliaryType AuxType;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000058 COFF::Auxiliary Aux;
59};
Chris Lattner2c52b792010-07-11 22:07:02 +000060
Michael J. Spencerd6283772010-09-27 08:58:26 +000061class COFFSymbol;
62class COFFSection;
63
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000064class COFFSymbol {
65public:
66 COFF::symbol Data;
Chris Lattner2c52b792010-07-11 22:07:02 +000067
Dmitri Gribenko226fea52013-01-13 16:01:15 +000068 typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
Chris Lattner2c52b792010-07-11 22:07:02 +000069
Rafael Espindola11e9e212015-05-27 14:37:12 +000070 name Name;
71 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000072 AuxiliarySymbols Aux;
Rafael Espindola11e9e212015-05-27 14:37:12 +000073 COFFSymbol *Other;
74 COFFSection *Section;
75 int Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000076
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +000077 const MCSymbol *MC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000078
Dmitri Gribenko226fea52013-01-13 16:01:15 +000079 COFFSymbol(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000080 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000081
David Majnemer4eecd302015-05-30 04:56:02 +000082 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;
Rafael Espindola21956e42015-10-23 21:48:05 +0000129 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
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
Michael J. Spencerd6283772010-09-27 08:58:26 +0000164 bool IsPhysicalSection(COFFSection *S);
165
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000166 // Entity writing methods.
167
168 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000169 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000170 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000171 void writeSectionHeader(const COFF::section &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000172 void WriteRelocation(const COFF::relocation &R);
173
174 // MCObjectWriter interface implementation.
175
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000176 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper34a61bc2014-03-08 07:02:02 +0000177 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000178
Jim Grosbach36e60e92015-06-04 22:24:41 +0000179 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000180 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000181 const MCFragment &FB, bool InSet,
182 bool IsPCRel) const override;
183
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000184 bool isWeak(const MCSymbol &Sym) const override;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000185
Jim Grosbach36e60e92015-06-04 22:24:41 +0000186 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000187 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000188 MCValue Target, bool &IsPCRel,
189 uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000190
Jim Grosbach36e60e92015-06-04 22:24:41 +0000191 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000192};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000193}
Chris Lattner2c52b792010-07-11 22:07:02 +0000194
Hans Wennborgba80b5d2014-09-28 00:22:27 +0000195static inline void write_uint32_le(void *Data, uint32_t Value) {
196 support::endian::write<uint32_t, support::little, support::unaligned>(Data,
197 Value);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000198}
199
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000200//------------------------------------------------------------------------------
201// Symbol class implementation
202
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000203COFFSymbol::COFFSymbol(StringRef name)
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000204 : Name(name.begin(), name.end()), Other(nullptr), Section(nullptr),
205 Relocations(0), MC(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000206 memset(&Data, 0, sizeof(Data));
207}
208
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000209// In the case that the name does not fit within 8 bytes, the offset
210// into the string table is stored in the last 4 bytes instead, leaving
211// the first 4 bytes as 0.
212void COFFSymbol::set_name_offset(uint32_t Offset) {
213 write_uint32_le(Data.Name + 0, 0);
214 write_uint32_le(Data.Name + 4, Offset);
215}
216
217//------------------------------------------------------------------------------
218// Section class implementation
219
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000220COFFSection::COFFSection(StringRef name)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000221 : Name(name), MCSection(nullptr), Symbol(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000222 memset(&Header, 0, sizeof(Header));
223}
224
Rafael Espindola11e9e212015-05-27 14:37:12 +0000225size_t COFFSection::size() { return COFF::SectionSize; }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000226
227//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000228// WinCOFFObjectWriter class implementation
229
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000230WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000231 raw_pwrite_stream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000232 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000233 memset(&Header, 0, sizeof(Header));
Michael J. Spencer377aa202010-08-21 05:58:13 +0000234
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000235 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000236}
237
Michael J. Spencer17990d52010-10-16 08:25:57 +0000238COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000239 return createCOFFEntity<COFFSymbol>(Name, Symbols);
240}
241
Yaron Keren56919ef2014-12-04 08:30:39 +0000242COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000243 symbol_map::iterator i = SymbolMap.find(Symbol);
244 if (i != SymbolMap.end())
245 return i->second;
Yaron Keren56919ef2014-12-04 08:30:39 +0000246 COFFSymbol *RetSymbol =
247 createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000248 SymbolMap[Symbol] = RetSymbol;
249 return RetSymbol;
250}
251
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000252COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000253 return createCOFFEntity<COFFSection>(Name, Sections);
254}
255
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000256/// A template used to lookup or create a symbol/section, and initialize it if
257/// needed.
258template <typename object_t, typename list_t>
Rafael Espindola11e9e212015-05-27 14:37:12 +0000259object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name, list_t &List) {
David Blaikief564ab62014-04-15 05:25:03 +0000260 List.push_back(make_unique<object_t>(Name));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000261
David Blaikief564ab62014-04-15 05:25:03 +0000262 return List.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000263}
264
265/// This function takes a section data object from the assembler
266/// and creates the associated COFF section staging object.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000267void WinCOFFObjectWriter::defineSection(MCSectionCOFF const &Sec) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000268 COFFSection *coff_section = createSection(Sec.getSectionName());
Rafael Espindola11e9e212015-05-27 14:37:12 +0000269 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
Rafael Espindola0766ae02014-06-06 19:26:12 +0000270 if (Sec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
271 if (const MCSymbol *S = Sec.getCOMDATSymbol()) {
272 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
273 if (COMDATSymbol->Section)
274 report_fatal_error("two sections have the same comdat");
275 COMDATSymbol->Section = coff_section;
276 }
277 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000278
Michael J. Spencerd6283772010-09-27 08:58:26 +0000279 coff_section->Symbol = coff_symbol;
280 coff_symbol->Section = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000281 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000282
283 // In this case the auxiliary symbol is a Section Definition.
284 coff_symbol->Aux.resize(1);
285 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
286 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000287 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
288
289 coff_section->Header.Characteristics = Sec.getCharacteristics();
290
291 uint32_t &Characteristics = coff_section->Header.Characteristics;
Rafael Espindola967d6a62015-05-21 21:02:35 +0000292 switch (Sec.getAlignment()) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000293 case 1:
294 Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES;
295 break;
296 case 2:
297 Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES;
298 break;
299 case 4:
300 Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES;
301 break;
302 case 8:
303 Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES;
304 break;
305 case 16:
306 Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES;
307 break;
308 case 32:
309 Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES;
310 break;
311 case 64:
312 Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES;
313 break;
314 case 128:
315 Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES;
316 break;
317 case 256:
318 Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES;
319 break;
320 case 512:
321 Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES;
322 break;
323 case 1024:
324 Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES;
325 break;
326 case 2048:
327 Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES;
328 break;
329 case 4096:
330 Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES;
331 break;
332 case 8192:
333 Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES;
334 break;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000335 default:
336 llvm_unreachable("unsupported section alignment");
337 }
338
339 // Bind internal COFF section to MC section.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000340 coff_section->MCSection = &Sec;
341 SectionMap[&Sec] = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000342}
343
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000344static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000345 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000346 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000347 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000348
349 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000350 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000351 return 0;
352
353 return Res;
354}
355
David Majnemera9bdb322014-04-08 22:33:40 +0000356/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000357/// and creates the associated COFF symbol staging object.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000358void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000359 MCAssembler &Assembler,
360 const MCAsmLayout &Layout) {
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000361 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000362
Pete Cooper6bf1f302015-06-08 17:17:19 +0000363 if (cast<MCSymbolCOFF>(Symbol).isWeakExternal()) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000364 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
365
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000366 if (Symbol.isVariable()) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000367 const MCSymbolRefExpr *SymRef =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000368 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000369
Peter Collingbourne89886872013-04-22 18:48:56 +0000370 if (!SymRef)
371 report_fatal_error("Weak externals may only alias symbols");
Michael J. Spencer17990d52010-10-16 08:25:57 +0000372
Peter Collingbourne89886872013-04-22 18:48:56 +0000373 coff_symbol->Other = GetOrCreateCOFFSymbol(&SymRef->getSymbol());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000374 } else {
Yaron Keren075759a2015-03-30 15:42:36 +0000375 std::string WeakName = (".weak." + Symbol.getName() + ".default").str();
Michael J. Spencer17990d52010-10-16 08:25:57 +0000376 COFFSymbol *WeakDefault = createSymbol(WeakName);
377 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000378 WeakDefault->Data.StorageClass = COFF::IMAGE_SYM_CLASS_EXTERNAL;
379 WeakDefault->Data.Type = 0;
380 WeakDefault->Data.Value = 0;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000381 coff_symbol->Other = WeakDefault;
382 }
383
384 // Setup the Weak External auxiliary symbol.
385 coff_symbol->Aux.resize(1);
386 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
387 coff_symbol->Aux[0].AuxType = ATWeakExternal;
388 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
389 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000390 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000391
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000392 coff_symbol->MC = &Symbol;
Peter Collingbourne89886872013-04-22 18:48:56 +0000393 } else {
Rafael Espindola575f79a2014-05-01 13:37:57 +0000394 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000395 coff_symbol->Data.Value = getSymbolValue(Symbol, Layout);
Reid Klecknerc1e76212013-09-17 23:18:05 +0000396
Pete Cooperad9f9c32015-06-08 17:17:12 +0000397 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(Symbol);
398 coff_symbol->Data.Type = SymbolCOFF.getType();
Pete Cooper6bf1f302015-06-08 17:17:19 +0000399 coff_symbol->Data.StorageClass = SymbolCOFF.getClass();
Peter Collingbourne89886872013-04-22 18:48:56 +0000400
401 // If no storage class was specified in the streamer, define it here.
David Majnemer4eecd302015-05-30 04:56:02 +0000402 if (coff_symbol->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000403 bool IsExternal = Symbol.isExternal() ||
404 (!Symbol.getFragment() && !Symbol.isVariable());
Peter Collingbourne89886872013-04-22 18:48:56 +0000405
David Majnemer299674e2014-07-13 04:31:19 +0000406 coff_symbol->Data.StorageClass = IsExternal
407 ? COFF::IMAGE_SYM_CLASS_EXTERNAL
408 : COFF::IMAGE_SYM_CLASS_STATIC;
Peter Collingbourne89886872013-04-22 18:48:56 +0000409 }
410
Rafael Espindola575f79a2014-05-01 13:37:57 +0000411 if (!Base) {
Reid Klecknerc1e76212013-09-17 23:18:05 +0000412 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola575f79a2014-05-01 13:37:57 +0000413 } else {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000414 if (Base->getFragment()) {
415 COFFSection *Sec = SectionMap[Base->getFragment()->getParent()];
Rafael Espindola0766ae02014-06-06 19:26:12 +0000416
417 if (coff_symbol->Section && coff_symbol->Section != Sec)
418 report_fatal_error("conflicting sections for symbol");
419
420 coff_symbol->Section = Sec;
421 }
Rafael Espindola575f79a2014-05-01 13:37:57 +0000422 }
Peter Collingbourne89886872013-04-22 18:48:56 +0000423
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000424 coff_symbol->MC = &Symbol;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000425 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000426}
427
Nico Rieck01143f92014-02-25 09:50:40 +0000428// Maximum offsets for different string table entry encodings.
429static const unsigned Max6DecimalOffset = 999999;
430static const unsigned Max7DecimalOffset = 9999999;
431static const uint64_t MaxBase64Offset = 0xFFFFFFFFFULL; // 64^6, including 0
432
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000433// Encode a string table entry offset in base 64, padded to 6 chars, and
434// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
435// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000436static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000437 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000438 "Illegal section name encoding for value");
439
440 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
441 "abcdefghijklmnopqrstuvwxyz"
442 "0123456789+/";
443
444 Buffer[0] = '/';
445 Buffer[1] = '/';
446
Rafael Espindola11e9e212015-05-27 14:37:12 +0000447 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000448 for (unsigned i = 0; i < 6; ++i) {
449 unsigned Rem = Value % 64;
450 Value /= 64;
451 *(Ptr--) = Alphabet[Rem];
452 }
453}
454
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000455void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000456 if (S.Name.size() > COFF::NameSize) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000457 uint64_t StringTableEntry = Strings.getOffset(S.Name);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000458
Nico Rieck01143f92014-02-25 09:50:40 +0000459 if (StringTableEntry <= Max6DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000460 std::sprintf(S.Header.Name, "/%d", unsigned(StringTableEntry));
Nico Rieck01143f92014-02-25 09:50:40 +0000461 } else if (StringTableEntry <= Max7DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000462 // With seven digits, we have to skip the terminating null. Because
463 // sprintf always appends it, we use a larger temporary buffer.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000464 char buffer[9] = {};
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000465 std::sprintf(buffer, "/%d", unsigned(StringTableEntry));
466 std::memcpy(S.Header.Name, buffer, 8);
Nico Rieck01143f92014-02-25 09:50:40 +0000467 } else if (StringTableEntry <= MaxBase64Offset) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000468 // Starting with 10,000,000, offsets are encoded as base64.
469 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000470 } else {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000471 report_fatal_error("COFF string table is greater than 64 GB.");
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000472 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000473 } else
474 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000475}
476
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000477void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
478 if (S.Name.size() > COFF::NameSize)
479 S.set_name_offset(Strings.getOffset(S.Name));
480 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000481 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000482}
483
Michael J. Spencerd6283772010-09-27 08:58:26 +0000484bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000485 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
486 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000487}
488
489//------------------------------------------------------------------------------
490// entity writing methods
491
492void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000493 if (UseBigObj) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000494 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
495 writeLE16(0xFFFF);
496 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
497 writeLE16(Header.Machine);
498 writeLE32(Header.TimeDateStamp);
499 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
500 writeLE32(0);
501 writeLE32(0);
502 writeLE32(0);
503 writeLE32(0);
504 writeLE32(Header.NumberOfSections);
505 writeLE32(Header.PointerToSymbolTable);
506 writeLE32(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000507 } else {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000508 writeLE16(Header.Machine);
509 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
510 writeLE32(Header.TimeDateStamp);
511 writeLE32(Header.PointerToSymbolTable);
512 writeLE32(Header.NumberOfSymbols);
513 writeLE16(Header.SizeOfOptionalHeader);
514 writeLE16(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000515 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000516}
517
David Blaikief564ab62014-04-15 05:25:03 +0000518void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000519 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
520 writeLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000521 if (UseBigObj)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000522 writeLE32(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000523 else
Jim Grosbach36e60e92015-06-04 22:24:41 +0000524 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
525 writeLE16(S.Data.Type);
526 write8(S.Data.StorageClass);
527 write8(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000528 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000529}
530
531void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000532 const COFFSymbol::AuxiliarySymbols &S) {
533 for (COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
534 i != e; ++i) {
535 switch (i->AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000536 case ATFunctionDefinition:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000537 writeLE32(i->Aux.FunctionDefinition.TagIndex);
538 writeLE32(i->Aux.FunctionDefinition.TotalSize);
539 writeLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
540 writeLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000541 WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000542 if (UseBigObj)
543 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000544 break;
545 case ATbfAndefSymbol:
546 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
Jim Grosbach36e60e92015-06-04 22:24:41 +0000547 writeLE16(i->Aux.bfAndefSymbol.Linenumber);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000548 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
Jim Grosbach36e60e92015-06-04 22:24:41 +0000549 writeLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000550 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000551 if (UseBigObj)
552 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000553 break;
554 case ATWeakExternal:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000555 writeLE32(i->Aux.WeakExternal.TagIndex);
556 writeLE32(i->Aux.WeakExternal.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000557 WriteZeros(sizeof(i->Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000558 if (UseBigObj)
559 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000560 break;
561 case ATFile:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000562 writeBytes(
David Majnemer4d571592014-09-15 19:42:42 +0000563 StringRef(reinterpret_cast<const char *>(&i->Aux),
564 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000565 break;
566 case ATSectionDefinition:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000567 writeLE32(i->Aux.SectionDefinition.Length);
568 writeLE16(i->Aux.SectionDefinition.NumberOfRelocations);
569 writeLE16(i->Aux.SectionDefinition.NumberOfLinenumbers);
570 writeLE32(i->Aux.SectionDefinition.CheckSum);
571 writeLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number));
572 write8(i->Aux.SectionDefinition.Selection);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000573 WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
Jim Grosbach36e60e92015-06-04 22:24:41 +0000574 writeLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000575 if (UseBigObj)
576 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000577 break;
578 }
579 }
580}
581
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000582void WinCOFFObjectWriter::writeSectionHeader(const COFF::section &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000583 writeBytes(StringRef(S.Name, COFF::NameSize));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000584
Jim Grosbach36e60e92015-06-04 22:24:41 +0000585 writeLE32(S.VirtualSize);
586 writeLE32(S.VirtualAddress);
587 writeLE32(S.SizeOfRawData);
588 writeLE32(S.PointerToRawData);
589 writeLE32(S.PointerToRelocations);
590 writeLE32(S.PointerToLineNumbers);
591 writeLE16(S.NumberOfRelocations);
592 writeLE16(S.NumberOfLineNumbers);
593 writeLE32(S.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000594}
595
596void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000597 writeLE32(R.VirtualAddress);
598 writeLE32(R.SymbolTableIndex);
599 writeLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000600}
601
602////////////////////////////////////////////////////////////////////////////////
603// MCObjectWriter interface implementations
604
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000605void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000606 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000607 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000608 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000609 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000610 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000611
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000612 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000613 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000614 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000615}
616
Jim Grosbach36e60e92015-06-04 22:24:41 +0000617bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000618 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000619 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000620 // MS LINK expects to be able to replace all references to a function with a
621 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
622 // away any relocations to functions.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000623 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
David Majnemer03e2cc32015-12-21 22:09:27 +0000624 if (Asm.isIncrementalLinkerCompatible() &&
625 (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000626 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000627 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000628 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000629}
630
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000631bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000632 if (!Sym.isExternal())
Rafael Espindola88af4112015-04-17 11:27:13 +0000633 return false;
634
Rafael Espindola88af4112015-04-17 11:27:13 +0000635 if (!Sym.isInSection())
636 return false;
637
638 const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
639 if (!Sec.getCOMDATSymbol())
640 return false;
641
642 // It looks like for COFF it is invalid to replace a reference to a global
643 // in a comdat with a reference to a local.
644 // FIXME: Add a specification reference if available.
645 return true;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000646}
647
Jim Grosbach36e60e92015-06-04 22:24:41 +0000648void WinCOFFObjectWriter::recordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000649 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
650 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000651 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000652
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000653 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000654 if (!A.isRegistered()) {
655 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000656 Twine("symbol '") + A.getName() +
657 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000658 return;
659 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000660 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000661 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000662 Twine("assembler label '") + A.getName() +
663 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000664 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000665 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000666
Rafael Espindola7549f872015-05-26 00:36:57 +0000667 MCSection *Section = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000668
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000669 // Mark this symbol as requiring an entry in the symbol table.
Rafael Espindola7549f872015-05-26 00:36:57 +0000670 assert(SectionMap.find(Section) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000671 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000672
Rafael Espindola7549f872015-05-26 00:36:57 +0000673 COFFSection *coff_section = SectionMap[Section];
Rafael Espindolaed164772011-04-20 14:01:45 +0000674 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000675 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000676
David Majnemera45a1762014-01-06 07:39:46 +0000677 if (SymB) {
678 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000679 if (!B->getFragment()) {
680 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000681 Fixup.getLoc(),
682 Twine("symbol '") + B->getName() +
683 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000684 return;
685 }
David Majnemera45a1762014-01-06 07:39:46 +0000686
Oliver Stannard9be59af2015-11-17 10:00:43 +0000687 if (!A.getFragment()) {
688 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000689 Fixup.getLoc(),
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000690 Twine("symbol '") + A.getName() +
David Majnemera45a1762014-01-06 07:39:46 +0000691 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000692 return;
693 }
David Majnemera45a1762014-01-06 07:39:46 +0000694
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000695 CrossSection = &A.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000696
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000697 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000698 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000699
700 // In the case where we have SymbA and SymB, we just need to store the delta
701 // between the two symbols. Update FixedValue to account for the delta, and
702 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000703 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000704 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000705 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000706 return;
David Majnemer1de30942015-02-09 06:31:31 +0000707 }
708
709 // Offset of the relocation in the section
710 int64_t OffsetOfRelocation =
711 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
712
Andy Ayers9e5c8512015-05-14 01:10:41 +0000713 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000714 } else {
715 FixedValue = Target.getConstant();
716 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000717
718 COFFRelocation Reloc;
719
Daniel Dunbar727be432010-07-31 21:08:54 +0000720 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000721 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000722
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000723 // Turn relocations for temporary symbols into section relocations.
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000724 if (A.isTemporary() || CrossSection) {
725 MCSection *TargetSection = &A.getSection();
726 assert(
727 SectionMap.find(TargetSection) != SectionMap.end() &&
728 "Section must already have been defined in executePostLayoutBinding!");
729 Reloc.Symb = SectionMap[TargetSection]->Symbol;
730 FixedValue += Layout.getSymbolOffset(A);
731 } else {
732 assert(
733 SymbolMap.find(&A) != SymbolMap.end() &&
734 "Symbol must already have been defined in executePostLayoutBinding!");
735 Reloc.Symb = SymbolMap[&A];
736 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000737
738 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000739
740 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000741 Reloc.Data.Type = TargetObjectWriter->getRelocType(
742 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000743
744 // FIXME: Can anyone explain what this does other than adjust for the size
745 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000746 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
747 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
748 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
749 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000750 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000751
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000752 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
753 switch (Reloc.Data.Type) {
754 case COFF::IMAGE_REL_ARM_ABSOLUTE:
755 case COFF::IMAGE_REL_ARM_ADDR32:
756 case COFF::IMAGE_REL_ARM_ADDR32NB:
757 case COFF::IMAGE_REL_ARM_TOKEN:
758 case COFF::IMAGE_REL_ARM_SECTION:
759 case COFF::IMAGE_REL_ARM_SECREL:
760 break;
761 case COFF::IMAGE_REL_ARM_BRANCH11:
762 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000763 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
764 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
765 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000766 case COFF::IMAGE_REL_ARM_BRANCH24:
767 case COFF::IMAGE_REL_ARM_BLX24:
768 case COFF::IMAGE_REL_ARM_MOV32A:
769 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
770 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000771 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000772 // generate the relocations however the rest of the MSVC toolchain is
773 // unable to handle it.
774 llvm_unreachable("unsupported relocation");
775 break;
776 case COFF::IMAGE_REL_ARM_MOV32T:
777 break;
778 case COFF::IMAGE_REL_ARM_BRANCH20T:
779 case COFF::IMAGE_REL_ARM_BRANCH24T:
780 case COFF::IMAGE_REL_ARM_BLX23T:
781 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
782 // perform a 4 byte adjustment to the relocation. Relative branches are
783 // offset by 4 on ARM, however, because there is no RELA relocations, all
784 // branches are offset by 4.
785 FixedValue = FixedValue + 4;
786 break;
787 }
788 }
789
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000790 if (TargetObjectWriter->recordRelocation(Fixup))
791 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000792}
793
Jim Grosbach36e60e92015-06-04 22:24:41 +0000794void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000795 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000796 size_t SectionsSize = Sections.size();
797 if (SectionsSize > static_cast<size_t>(INT32_MAX))
798 report_fatal_error(
799 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000800
David Majnemer4d571592014-09-15 19:42:42 +0000801 // Assign symbol and section indexes and offsets.
802 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
803
804 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
805
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000806 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000807 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000808 for (const auto &Section : Sections) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000809 Section->Number = Number;
810 Section->Symbol->Data.SectionNumber = Number;
811 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000812 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000813 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000814
David Majnemer4d571592014-09-15 19:42:42 +0000815 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000816 Header.NumberOfSymbols = 0;
817
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000818 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000819 // round up to calculate the number of auxiliary symbols required
820 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000821 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000822
823 COFFSymbol *file = createSymbol(".file");
824 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
825 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
826 file->Aux.resize(Count);
827
828 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000829 unsigned Length = Name.size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000830 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000831 Aux.AuxType = ATFile;
832
833 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000834 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000835 Length = Length - SymbolSize;
836 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000837 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000838 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
839 break;
840 }
841
842 Offset += SymbolSize;
843 }
844 }
845
David Majnemer9ab5ff12014-08-28 04:02:50 +0000846 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000847 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000848 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000849 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000850 Symbol->setIndex(Header.NumberOfSymbols++);
851 // Update auxiliary symbol info.
852 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
853 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000854 }
855
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000856 // Build string table.
857 for (const auto &S : Sections)
858 if (S->Name.size() > COFF::NameSize)
859 Strings.add(S->Name);
860 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000861 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000862 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000863 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000864
865 // Set names.
866 for (const auto &S : Sections)
867 SetSectionName(*S);
868 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000869 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000870
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000871 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000872 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000873 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000874 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000875 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
876 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000877 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000878 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000879 }
880 }
881
Nico Riecka37acf72013-07-06 12:13:10 +0000882 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000883 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000884 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000885 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
886 continue;
887
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000888 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +0000889
Rafael Espindola0766ae02014-06-06 19:26:12 +0000890 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
891 assert(COMDAT);
892 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
893 assert(COMDATSymbol);
894 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000895 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000896 report_fatal_error(
897 Twine("Missing associated COMDAT section for section ") +
898 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000899
900 // Skip this section if the associated section is unused.
901 if (Assoc->Number == -1)
902 continue;
903
David Majnemer4eecd302015-05-30 04:56:02 +0000904 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +0000905 }
906
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000907 // Assign file offsets to COFF object file structures.
908
Manuel Klimek272d3f12015-11-18 15:24:17 +0000909 unsigned offset = getInitialOffset();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000910
David Majnemer4d571592014-09-15 19:42:42 +0000911 if (UseBigObj)
912 offset += COFF::Header32Size;
913 else
914 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000915 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000916
Yaron Keren56919ef2014-12-04 08:30:39 +0000917 for (const auto &Section : Asm) {
Rafael Espindolaa554c052015-05-25 23:14:17 +0000918 COFFSection *Sec = SectionMap[&Section];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000919
Michael J. Spencerd6283772010-09-27 08:58:26 +0000920 if (Sec->Number == -1)
921 continue;
922
Rafael Espindola5a1e80b2015-05-26 02:00:36 +0000923 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000924
Michael J. Spencerd6283772010-09-27 08:58:26 +0000925 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000926 // Align the section data to a four byte boundary.
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000927 offset = alignTo(offset, 4);
David Majnemerab2b25b2015-02-11 22:51:55 +0000928 Sec->Header.PointerToRawData = offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000929
930 offset += Sec->Header.SizeOfRawData;
931 }
932
933 if (Sec->Relocations.size() > 0) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000934 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
935
936 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000937 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000938 // size is found in reloc #0. Microsoft tools understand this.
939 Sec->Header.NumberOfRelocations = 0xffff;
940 } else {
941 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
942 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000943 Sec->Header.PointerToRelocations = offset;
944
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000945 if (RelocationsOverflow) {
946 // Reloc #0 will contain actual count, so make room for it.
947 offset += COFF::RelocationSize;
948 }
949
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000950 offset += COFF::RelocationSize * Sec->Relocations.size();
951
Yaron Keren56919ef2014-12-04 08:30:39 +0000952 for (auto &Relocation : Sec->Relocations) {
David Majnemer4eecd302015-05-30 04:56:02 +0000953 assert(Relocation.Symb->getIndex() != -1);
954 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000955 }
956 }
957
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000958 assert(Sec->Symbol->Aux.size() == 1 &&
959 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000960 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000961 assert(Aux.AuxType == ATSectionDefinition &&
962 "Section's symbol's aux symbol must be a Section Definition!");
963 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
964 Aux.Aux.SectionDefinition.NumberOfRelocations =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000965 Sec->Header.NumberOfRelocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000966 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000967 Sec->Header.NumberOfLineNumbers;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000968 }
969
970 Header.PointerToSymbolTable = offset;
971
David Majnemer088ba022015-09-01 23:46:11 +0000972 // MS LINK expects to be able to use this timestamp to implement their
973 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +0000974 if (Asm.isIncrementalLinkerCompatible()) {
975 std::time_t Now = time(nullptr);
976 if (Now < 0 || !isUInt<32>(Now))
977 Now = UINT32_MAX;
978 Header.TimeDateStamp = Now;
979 } else {
Nico Weber891419a2016-01-06 19:05:19 +0000980 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +0000981 Header.TimeDateStamp = 0;
982 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +0000983
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000984 // Write it all to disk...
985 WriteFileHeader(Header);
986
987 {
988 sections::iterator i, ie;
Rafael Espindola63702e22015-06-01 01:30:01 +0000989 MCAssembler::iterator j, je;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000990
Yaron Keren56919ef2014-12-04 08:30:39 +0000991 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000992 if (Section->Number != -1) {
993 if (Section->Relocations.size() >= 0xffff)
994 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000995 writeSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000996 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000997 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000998
David Majnemer6ddc6362015-09-01 21:23:58 +0000999 SmallVector<char, 128> SectionContents;
Rafael Espindola11e9e212015-05-27 14:37:12 +00001000 for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
1001 je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +00001002 (i != ie) && (j != je); ++i, ++j) {
1003
1004 if ((*i)->Number == -1)
1005 continue;
1006
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001007 if ((*i)->Header.PointerToRawData != 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001008 assert(getStream().tell() <= (*i)->Header.PointerToRawData &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001009 "Section::PointerToRawData is insane!");
1010
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001011 unsigned SectionDataPadding =
1012 (*i)->Header.PointerToRawData - getStream().tell();
David Majnemer3df3c612015-02-11 22:22:30 +00001013 assert(SectionDataPadding < 4 &&
1014 "Should only need at most three bytes of padding!");
1015
1016 WriteZeros(SectionDataPadding);
1017
David Majnemer6ddc6362015-09-01 21:23:58 +00001018 // Save the contents of the section to a temporary buffer, we need this
1019 // to CRC the data before we dump it into the object file.
1020 SectionContents.clear();
1021 raw_svector_ostream VecOS(SectionContents);
1022 raw_pwrite_stream &OldStream = getStream();
1023 // Redirect the output stream to our buffer.
1024 setStream(VecOS);
1025 // Fill our buffer with the section data.
Rafael Espindola64acc7f2015-05-26 02:17:21 +00001026 Asm.writeSectionData(&*j, Layout);
David Majnemer6ddc6362015-09-01 21:23:58 +00001027 // Reset the stream back to what it was before.
1028 setStream(OldStream);
1029
1030 // Calculate our CRC with an initial value of '0', this is not how
1031 // JamCRC is specified but it aligns with the expected output.
1032 JamCRC JC(/*Init=*/0x00000000U);
1033 JC.update(SectionContents);
1034
1035 // Write the section contents to the object file.
1036 getStream() << SectionContents;
1037
1038 // Update the section definition auxiliary symbol to record the CRC.
1039 COFFSection *Sec = SectionMap[&*j];
1040 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
1041 assert(AuxSyms.size() == 1 &&
1042 AuxSyms[0].AuxType == ATSectionDefinition);
1043 AuxSymbol &SecDef = AuxSyms[0];
1044 SecDef.Aux.SectionDefinition.CheckSum = JC.getCRC();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001045 }
1046
1047 if ((*i)->Relocations.size() > 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001048 assert(getStream().tell() == (*i)->Header.PointerToRelocations &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001049 "Section::PointerToRelocations is insane!");
1050
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001051 if ((*i)->Relocations.size() >= 0xffff) {
1052 // In case of overflow, write actual relocation count as first
1053 // relocation. Including the synthetic reloc itself (+ 1).
1054 COFF::relocation r;
1055 r.VirtualAddress = (*i)->Relocations.size() + 1;
1056 r.SymbolTableIndex = 0;
1057 r.Type = 0;
1058 WriteRelocation(r);
1059 }
1060
Yaron Keren56919ef2014-12-04 08:30:39 +00001061 for (const auto &Relocation : (*i)->Relocations)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001062 WriteRelocation(Relocation.Data);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001063 } else
1064 assert((*i)->Header.PointerToRelocations == 0 &&
1065 "Section::PointerToRelocations is insane!");
1066 }
1067 }
1068
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001069 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001070 "Header::PointerToSymbolTable is insane!");
1071
Yaron Keren56919ef2014-12-04 08:30:39 +00001072 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001073 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001074 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001075
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001076 getStream().write(Strings.data().data(), Strings.data().size());
Chris Lattner2c52b792010-07-11 22:07:02 +00001077}
1078
Rafael Espindola11e9e212015-05-27 14:37:12 +00001079MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1080 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001081
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001082// Pin the vtable to this file.
1083void MCWinCOFFObjectTargetWriter::anchor() {}
1084
Chris Lattner2c52b792010-07-11 22:07:02 +00001085//------------------------------------------------------------------------------
1086// WinCOFFObjectWriter factory function
1087
Rafael Espindola37099d92015-04-09 18:08:15 +00001088MCObjectWriter *
1089llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001090 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001091 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001092}