blob: cb5998437b0a378e01428f504dd803b3658f625a [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);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000362 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
363 COFFSection *Sec = nullptr;
364 if (Base && Base->getFragment()) {
365 Sec = SectionMap[Base->getFragment()->getParent()];
366 if (coff_symbol->Section && coff_symbol->Section != Sec)
367 report_fatal_error("conflicting sections for symbol");
368 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000369
Pete Cooper6bf1f302015-06-08 17:17:19 +0000370 if (cast<MCSymbolCOFF>(Symbol).isWeakExternal()) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000371 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
372
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000373 if (Symbol.isVariable()) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000374 const MCSymbolRefExpr *SymRef =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000375 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000376
Peter Collingbourne89886872013-04-22 18:48:56 +0000377 if (!SymRef)
378 report_fatal_error("Weak externals may only alias symbols");
Michael J. Spencer17990d52010-10-16 08:25:57 +0000379
Peter Collingbourne89886872013-04-22 18:48:56 +0000380 coff_symbol->Other = GetOrCreateCOFFSymbol(&SymRef->getSymbol());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000381 } else {
Yaron Keren075759a2015-03-30 15:42:36 +0000382 std::string WeakName = (".weak." + Symbol.getName() + ".default").str();
Michael J. Spencer17990d52010-10-16 08:25:57 +0000383 COFFSymbol *WeakDefault = createSymbol(WeakName);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000384
385 if (!Sec)
386 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
387 else
388 WeakDefault->Section = Sec;
389
Rafael Espindola11e9e212015-05-27 14:37:12 +0000390 WeakDefault->Data.StorageClass = COFF::IMAGE_SYM_CLASS_EXTERNAL;
391 WeakDefault->Data.Type = 0;
Rafael Espindola6ddf5f42016-05-26 18:04:53 +0000392 WeakDefault->Data.Value = getSymbolValue(Symbol, Layout);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000393 coff_symbol->Other = WeakDefault;
394 }
395
396 // Setup the Weak External auxiliary symbol.
397 coff_symbol->Aux.resize(1);
398 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
399 coff_symbol->Aux[0].AuxType = ATWeakExternal;
400 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
401 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000402 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000403
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000404 coff_symbol->MC = &Symbol;
Peter Collingbourne89886872013-04-22 18:48:56 +0000405 } else {
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000406 coff_symbol->Data.Value = getSymbolValue(Symbol, Layout);
Reid Klecknerc1e76212013-09-17 23:18:05 +0000407
Pete Cooperad9f9c32015-06-08 17:17:12 +0000408 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(Symbol);
409 coff_symbol->Data.Type = SymbolCOFF.getType();
Pete Cooper6bf1f302015-06-08 17:17:19 +0000410 coff_symbol->Data.StorageClass = SymbolCOFF.getClass();
Peter Collingbourne89886872013-04-22 18:48:56 +0000411
412 // If no storage class was specified in the streamer, define it here.
David Majnemer4eecd302015-05-30 04:56:02 +0000413 if (coff_symbol->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000414 bool IsExternal = Symbol.isExternal() ||
415 (!Symbol.getFragment() && !Symbol.isVariable());
Peter Collingbourne89886872013-04-22 18:48:56 +0000416
David Majnemer299674e2014-07-13 04:31:19 +0000417 coff_symbol->Data.StorageClass = IsExternal
418 ? COFF::IMAGE_SYM_CLASS_EXTERNAL
419 : COFF::IMAGE_SYM_CLASS_STATIC;
Peter Collingbourne89886872013-04-22 18:48:56 +0000420 }
421
Rafael Espindola30c080a2016-05-26 18:48:23 +0000422 if (!Base)
Reid Klecknerc1e76212013-09-17 23:18:05 +0000423 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola30c080a2016-05-26 18:48:23 +0000424 else
425 coff_symbol->Section = Sec;
Peter Collingbourne89886872013-04-22 18:48:56 +0000426
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000427 coff_symbol->MC = &Symbol;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000428 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000429}
430
Nico Rieck01143f92014-02-25 09:50:40 +0000431// Maximum offsets for different string table entry encodings.
David Majnemerf088f522016-01-24 20:46:11 +0000432enum : unsigned { Max7DecimalOffset = 9999999U };
433enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
Nico Rieck01143f92014-02-25 09:50:40 +0000434
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000435// Encode a string table entry offset in base 64, padded to 6 chars, and
436// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
437// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000438static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000439 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000440 "Illegal section name encoding for value");
441
442 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
443 "abcdefghijklmnopqrstuvwxyz"
444 "0123456789+/";
445
446 Buffer[0] = '/';
447 Buffer[1] = '/';
448
Rafael Espindola11e9e212015-05-27 14:37:12 +0000449 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000450 for (unsigned i = 0; i < 6; ++i) {
451 unsigned Rem = Value % 64;
452 Value /= 64;
453 *(Ptr--) = Alphabet[Rem];
454 }
455}
456
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000457void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000458 if (S.Name.size() > COFF::NameSize) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000459 uint64_t StringTableEntry = Strings.getOffset(S.Name);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000460
David Majnemerf088f522016-01-24 20:46:11 +0000461 if (StringTableEntry <= Max7DecimalOffset) {
462 SmallVector<char, COFF::NameSize> Buffer;
463 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
464 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
465
466 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
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 }
David Majnemerf088f522016-01-24 20:46:11 +0000473 } else {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000474 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
David Majnemerf088f522016-01-24 20:46:11 +0000475 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000476}
477
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000478void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
479 if (S.Name.size() > COFF::NameSize)
480 S.set_name_offset(Strings.getOffset(S.Name));
481 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000482 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000483}
484
Michael J. Spencerd6283772010-09-27 08:58:26 +0000485bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000486 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
487 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000488}
489
490//------------------------------------------------------------------------------
491// entity writing methods
492
493void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000494 if (UseBigObj) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000495 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
496 writeLE16(0xFFFF);
497 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
498 writeLE16(Header.Machine);
499 writeLE32(Header.TimeDateStamp);
500 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
501 writeLE32(0);
502 writeLE32(0);
503 writeLE32(0);
504 writeLE32(0);
505 writeLE32(Header.NumberOfSections);
506 writeLE32(Header.PointerToSymbolTable);
507 writeLE32(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000508 } else {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000509 writeLE16(Header.Machine);
510 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
511 writeLE32(Header.TimeDateStamp);
512 writeLE32(Header.PointerToSymbolTable);
513 writeLE32(Header.NumberOfSymbols);
514 writeLE16(Header.SizeOfOptionalHeader);
515 writeLE16(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000516 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000517}
518
David Blaikief564ab62014-04-15 05:25:03 +0000519void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000520 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
521 writeLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000522 if (UseBigObj)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000523 writeLE32(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000524 else
Jim Grosbach36e60e92015-06-04 22:24:41 +0000525 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
526 writeLE16(S.Data.Type);
527 write8(S.Data.StorageClass);
528 write8(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000529 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000530}
531
532void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000533 const COFFSymbol::AuxiliarySymbols &S) {
534 for (COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
535 i != e; ++i) {
536 switch (i->AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000537 case ATFunctionDefinition:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000538 writeLE32(i->Aux.FunctionDefinition.TagIndex);
539 writeLE32(i->Aux.FunctionDefinition.TotalSize);
540 writeLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
541 writeLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000542 WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000543 if (UseBigObj)
544 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000545 break;
546 case ATbfAndefSymbol:
547 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
Jim Grosbach36e60e92015-06-04 22:24:41 +0000548 writeLE16(i->Aux.bfAndefSymbol.Linenumber);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000549 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
Jim Grosbach36e60e92015-06-04 22:24:41 +0000550 writeLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000551 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000552 if (UseBigObj)
553 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000554 break;
555 case ATWeakExternal:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000556 writeLE32(i->Aux.WeakExternal.TagIndex);
557 writeLE32(i->Aux.WeakExternal.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000558 WriteZeros(sizeof(i->Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000559 if (UseBigObj)
560 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000561 break;
562 case ATFile:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000563 writeBytes(
David Majnemer4d571592014-09-15 19:42:42 +0000564 StringRef(reinterpret_cast<const char *>(&i->Aux),
565 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000566 break;
567 case ATSectionDefinition:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000568 writeLE32(i->Aux.SectionDefinition.Length);
569 writeLE16(i->Aux.SectionDefinition.NumberOfRelocations);
570 writeLE16(i->Aux.SectionDefinition.NumberOfLinenumbers);
571 writeLE32(i->Aux.SectionDefinition.CheckSum);
572 writeLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number));
573 write8(i->Aux.SectionDefinition.Selection);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000574 WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
Jim Grosbach36e60e92015-06-04 22:24:41 +0000575 writeLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000576 if (UseBigObj)
577 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000578 break;
579 }
580 }
581}
582
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000583void WinCOFFObjectWriter::writeSectionHeader(const COFF::section &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000584 writeBytes(StringRef(S.Name, COFF::NameSize));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000585
Jim Grosbach36e60e92015-06-04 22:24:41 +0000586 writeLE32(S.VirtualSize);
587 writeLE32(S.VirtualAddress);
588 writeLE32(S.SizeOfRawData);
589 writeLE32(S.PointerToRawData);
590 writeLE32(S.PointerToRelocations);
591 writeLE32(S.PointerToLineNumbers);
592 writeLE16(S.NumberOfRelocations);
593 writeLE16(S.NumberOfLineNumbers);
594 writeLE32(S.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000595}
596
597void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000598 writeLE32(R.VirtualAddress);
599 writeLE32(R.SymbolTableIndex);
600 writeLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000601}
602
603////////////////////////////////////////////////////////////////////////////////
604// MCObjectWriter interface implementations
605
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000606void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000607 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000608 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000609 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000610 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000611 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000612
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000613 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000614 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000615 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000616}
617
Jim Grosbach36e60e92015-06-04 22:24:41 +0000618bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000619 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000620 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000621 // MS LINK expects to be able to replace all references to a function with a
622 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
623 // away any relocations to functions.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000624 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
David Majnemer03e2cc32015-12-21 22:09:27 +0000625 if (Asm.isIncrementalLinkerCompatible() &&
626 (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000627 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000628 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000629 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000630}
631
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000632bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000633 if (!Sym.isExternal())
Rafael Espindola88af4112015-04-17 11:27:13 +0000634 return false;
635
Rafael Espindola88af4112015-04-17 11:27:13 +0000636 if (!Sym.isInSection())
637 return false;
638
639 const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
640 if (!Sec.getCOMDATSymbol())
641 return false;
642
643 // It looks like for COFF it is invalid to replace a reference to a global
644 // in a comdat with a reference to a local.
645 // FIXME: Add a specification reference if available.
646 return true;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000647}
648
Jim Grosbach36e60e92015-06-04 22:24:41 +0000649void WinCOFFObjectWriter::recordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000650 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
651 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000652 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000653
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000654 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000655 if (!A.isRegistered()) {
656 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000657 Twine("symbol '") + A.getName() +
658 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000659 return;
660 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000661 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000662 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000663 Twine("assembler label '") + A.getName() +
664 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000665 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000666 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000667
Rafael Espindola7549f872015-05-26 00:36:57 +0000668 MCSection *Section = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000669
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000670 // Mark this symbol as requiring an entry in the symbol table.
Rafael Espindola7549f872015-05-26 00:36:57 +0000671 assert(SectionMap.find(Section) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000672 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000673
Rafael Espindola7549f872015-05-26 00:36:57 +0000674 COFFSection *coff_section = SectionMap[Section];
Rafael Espindolaed164772011-04-20 14:01:45 +0000675 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000676 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000677
David Majnemera45a1762014-01-06 07:39:46 +0000678 if (SymB) {
679 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000680 if (!B->getFragment()) {
681 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000682 Fixup.getLoc(),
683 Twine("symbol '") + B->getName() +
684 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000685 return;
686 }
David Majnemera45a1762014-01-06 07:39:46 +0000687
Oliver Stannard9be59af2015-11-17 10:00:43 +0000688 if (!A.getFragment()) {
689 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000690 Fixup.getLoc(),
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000691 Twine("symbol '") + A.getName() +
David Majnemera45a1762014-01-06 07:39:46 +0000692 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000693 return;
694 }
David Majnemera45a1762014-01-06 07:39:46 +0000695
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000696 CrossSection = &A.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000697
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000698 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000699 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000700
701 // In the case where we have SymbA and SymB, we just need to store the delta
702 // between the two symbols. Update FixedValue to account for the delta, and
703 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000704 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000705 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000706 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000707 return;
David Majnemer1de30942015-02-09 06:31:31 +0000708 }
709
710 // Offset of the relocation in the section
711 int64_t OffsetOfRelocation =
712 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
713
Andy Ayers9e5c8512015-05-14 01:10:41 +0000714 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000715 } else {
716 FixedValue = Target.getConstant();
717 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000718
719 COFFRelocation Reloc;
720
Daniel Dunbar727be432010-07-31 21:08:54 +0000721 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000722 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000723
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000724 // Turn relocations for temporary symbols into section relocations.
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000725 if (A.isTemporary() || CrossSection) {
726 MCSection *TargetSection = &A.getSection();
727 assert(
728 SectionMap.find(TargetSection) != SectionMap.end() &&
729 "Section must already have been defined in executePostLayoutBinding!");
730 Reloc.Symb = SectionMap[TargetSection]->Symbol;
731 FixedValue += Layout.getSymbolOffset(A);
732 } else {
733 assert(
734 SymbolMap.find(&A) != SymbolMap.end() &&
735 "Symbol must already have been defined in executePostLayoutBinding!");
736 Reloc.Symb = SymbolMap[&A];
737 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000738
739 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000740
741 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000742 Reloc.Data.Type = TargetObjectWriter->getRelocType(
743 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000744
745 // FIXME: Can anyone explain what this does other than adjust for the size
746 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000747 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
748 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
749 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
750 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000751 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000752
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000753 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
754 switch (Reloc.Data.Type) {
755 case COFF::IMAGE_REL_ARM_ABSOLUTE:
756 case COFF::IMAGE_REL_ARM_ADDR32:
757 case COFF::IMAGE_REL_ARM_ADDR32NB:
758 case COFF::IMAGE_REL_ARM_TOKEN:
759 case COFF::IMAGE_REL_ARM_SECTION:
760 case COFF::IMAGE_REL_ARM_SECREL:
761 break;
762 case COFF::IMAGE_REL_ARM_BRANCH11:
763 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000764 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
765 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
766 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000767 case COFF::IMAGE_REL_ARM_BRANCH24:
768 case COFF::IMAGE_REL_ARM_BLX24:
769 case COFF::IMAGE_REL_ARM_MOV32A:
770 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
771 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000772 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000773 // generate the relocations however the rest of the MSVC toolchain is
774 // unable to handle it.
775 llvm_unreachable("unsupported relocation");
776 break;
777 case COFF::IMAGE_REL_ARM_MOV32T:
778 break;
779 case COFF::IMAGE_REL_ARM_BRANCH20T:
780 case COFF::IMAGE_REL_ARM_BRANCH24T:
781 case COFF::IMAGE_REL_ARM_BLX23T:
782 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
783 // perform a 4 byte adjustment to the relocation. Relative branches are
784 // offset by 4 on ARM, however, because there is no RELA relocations, all
785 // branches are offset by 4.
786 FixedValue = FixedValue + 4;
787 break;
788 }
789 }
790
David Majnemer408b5e62016-02-05 01:55:49 +0000791 // The fixed value never makes sense for section indicies, ignore it.
792 if (Fixup.getKind() == FK_SecRel_2)
793 FixedValue = 0;
794
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000795 if (TargetObjectWriter->recordRelocation(Fixup))
796 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000797}
798
Jim Grosbach36e60e92015-06-04 22:24:41 +0000799void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000800 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000801 size_t SectionsSize = Sections.size();
802 if (SectionsSize > static_cast<size_t>(INT32_MAX))
803 report_fatal_error(
804 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000805
David Majnemer4d571592014-09-15 19:42:42 +0000806 // Assign symbol and section indexes and offsets.
807 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
808
809 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
810
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000811 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000812 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000813 for (const auto &Section : Sections) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000814 Section->Number = Number;
815 Section->Symbol->Data.SectionNumber = Number;
816 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000817 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000818 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000819
David Majnemer4d571592014-09-15 19:42:42 +0000820 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000821 Header.NumberOfSymbols = 0;
822
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000823 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000824 // round up to calculate the number of auxiliary symbols required
825 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000826 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000827
828 COFFSymbol *file = createSymbol(".file");
829 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
830 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
831 file->Aux.resize(Count);
832
833 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000834 unsigned Length = Name.size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000835 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000836 Aux.AuxType = ATFile;
837
838 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000839 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000840 Length = Length - SymbolSize;
841 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000842 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000843 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
844 break;
845 }
846
847 Offset += SymbolSize;
848 }
849 }
850
David Majnemer9ab5ff12014-08-28 04:02:50 +0000851 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000852 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000853 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000854 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000855 Symbol->setIndex(Header.NumberOfSymbols++);
856 // Update auxiliary symbol info.
857 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
858 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000859 }
860
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000861 // Build string table.
862 for (const auto &S : Sections)
863 if (S->Name.size() > COFF::NameSize)
864 Strings.add(S->Name);
865 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000866 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000867 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000868 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000869
870 // Set names.
871 for (const auto &S : Sections)
872 SetSectionName(*S);
873 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000874 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000875
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000876 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000877 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000878 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000879 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000880 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
881 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000882 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000883 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000884 }
885 }
886
Nico Riecka37acf72013-07-06 12:13:10 +0000887 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000888 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000889 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000890 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
891 continue;
892
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000893 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +0000894
Rafael Espindola0766ae02014-06-06 19:26:12 +0000895 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
896 assert(COMDAT);
897 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
898 assert(COMDATSymbol);
899 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000900 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000901 report_fatal_error(
902 Twine("Missing associated COMDAT section for section ") +
903 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000904
905 // Skip this section if the associated section is unused.
906 if (Assoc->Number == -1)
907 continue;
908
David Majnemer4eecd302015-05-30 04:56:02 +0000909 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +0000910 }
911
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000912 // Assign file offsets to COFF object file structures.
913
Manuel Klimek272d3f12015-11-18 15:24:17 +0000914 unsigned offset = getInitialOffset();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000915
David Majnemer4d571592014-09-15 19:42:42 +0000916 if (UseBigObj)
917 offset += COFF::Header32Size;
918 else
919 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000920 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000921
Yaron Keren56919ef2014-12-04 08:30:39 +0000922 for (const auto &Section : Asm) {
Rafael Espindolaa554c052015-05-25 23:14:17 +0000923 COFFSection *Sec = SectionMap[&Section];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000924
Michael J. Spencerd6283772010-09-27 08:58:26 +0000925 if (Sec->Number == -1)
926 continue;
927
Rafael Espindola5a1e80b2015-05-26 02:00:36 +0000928 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000929
Michael J. Spencerd6283772010-09-27 08:58:26 +0000930 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000931 // Align the section data to a four byte boundary.
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000932 offset = alignTo(offset, 4);
David Majnemerab2b25b2015-02-11 22:51:55 +0000933 Sec->Header.PointerToRawData = offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000934
935 offset += Sec->Header.SizeOfRawData;
936 }
937
938 if (Sec->Relocations.size() > 0) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000939 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
940
941 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000942 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000943 // size is found in reloc #0. Microsoft tools understand this.
944 Sec->Header.NumberOfRelocations = 0xffff;
945 } else {
946 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
947 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000948 Sec->Header.PointerToRelocations = offset;
949
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000950 if (RelocationsOverflow) {
951 // Reloc #0 will contain actual count, so make room for it.
952 offset += COFF::RelocationSize;
953 }
954
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000955 offset += COFF::RelocationSize * Sec->Relocations.size();
956
Yaron Keren56919ef2014-12-04 08:30:39 +0000957 for (auto &Relocation : Sec->Relocations) {
David Majnemer4eecd302015-05-30 04:56:02 +0000958 assert(Relocation.Symb->getIndex() != -1);
959 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000960 }
961 }
962
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000963 assert(Sec->Symbol->Aux.size() == 1 &&
964 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000965 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000966 assert(Aux.AuxType == ATSectionDefinition &&
967 "Section's symbol's aux symbol must be a Section Definition!");
968 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
969 Aux.Aux.SectionDefinition.NumberOfRelocations =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000970 Sec->Header.NumberOfRelocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000971 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000972 Sec->Header.NumberOfLineNumbers;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000973 }
974
975 Header.PointerToSymbolTable = offset;
976
David Majnemer088ba022015-09-01 23:46:11 +0000977 // MS LINK expects to be able to use this timestamp to implement their
978 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +0000979 if (Asm.isIncrementalLinkerCompatible()) {
980 std::time_t Now = time(nullptr);
981 if (Now < 0 || !isUInt<32>(Now))
982 Now = UINT32_MAX;
983 Header.TimeDateStamp = Now;
984 } else {
Nico Weber891419a2016-01-06 19:05:19 +0000985 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +0000986 Header.TimeDateStamp = 0;
987 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +0000988
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000989 // Write it all to disk...
990 WriteFileHeader(Header);
991
992 {
993 sections::iterator i, ie;
Rafael Espindola63702e22015-06-01 01:30:01 +0000994 MCAssembler::iterator j, je;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000995
Yaron Keren56919ef2014-12-04 08:30:39 +0000996 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000997 if (Section->Number != -1) {
998 if (Section->Relocations.size() >= 0xffff)
999 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Jim Grosbach56ed0bb2015-06-04 23:25:54 +00001000 writeSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001001 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001002 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001003
David Majnemer6ddc6362015-09-01 21:23:58 +00001004 SmallVector<char, 128> SectionContents;
Rafael Espindola11e9e212015-05-27 14:37:12 +00001005 for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
1006 je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +00001007 (i != ie) && (j != je); ++i, ++j) {
1008
1009 if ((*i)->Number == -1)
1010 continue;
1011
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001012 if ((*i)->Header.PointerToRawData != 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001013 assert(getStream().tell() <= (*i)->Header.PointerToRawData &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001014 "Section::PointerToRawData is insane!");
1015
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001016 unsigned SectionDataPadding =
1017 (*i)->Header.PointerToRawData - getStream().tell();
David Majnemer3df3c612015-02-11 22:22:30 +00001018 assert(SectionDataPadding < 4 &&
1019 "Should only need at most three bytes of padding!");
1020
1021 WriteZeros(SectionDataPadding);
1022
David Majnemer6ddc6362015-09-01 21:23:58 +00001023 // Save the contents of the section to a temporary buffer, we need this
1024 // to CRC the data before we dump it into the object file.
1025 SectionContents.clear();
1026 raw_svector_ostream VecOS(SectionContents);
1027 raw_pwrite_stream &OldStream = getStream();
1028 // Redirect the output stream to our buffer.
1029 setStream(VecOS);
1030 // Fill our buffer with the section data.
Rafael Espindola64acc7f2015-05-26 02:17:21 +00001031 Asm.writeSectionData(&*j, Layout);
David Majnemer6ddc6362015-09-01 21:23:58 +00001032 // Reset the stream back to what it was before.
1033 setStream(OldStream);
1034
1035 // Calculate our CRC with an initial value of '0', this is not how
1036 // JamCRC is specified but it aligns with the expected output.
1037 JamCRC JC(/*Init=*/0x00000000U);
1038 JC.update(SectionContents);
1039
1040 // Write the section contents to the object file.
1041 getStream() << SectionContents;
1042
1043 // Update the section definition auxiliary symbol to record the CRC.
1044 COFFSection *Sec = SectionMap[&*j];
1045 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
1046 assert(AuxSyms.size() == 1 &&
1047 AuxSyms[0].AuxType == ATSectionDefinition);
1048 AuxSymbol &SecDef = AuxSyms[0];
1049 SecDef.Aux.SectionDefinition.CheckSum = JC.getCRC();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001050 }
1051
1052 if ((*i)->Relocations.size() > 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001053 assert(getStream().tell() == (*i)->Header.PointerToRelocations &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001054 "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
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001074 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001075 "Header::PointerToSymbolTable is insane!");
1076
Yaron Keren56919ef2014-12-04 08:30:39 +00001077 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001078 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001079 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001080
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001081 getStream().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}