blob: 6b8742d9e22e9ffc1d36586bb56e53a0b10da681 [file] [log] [blame]
Eugene Zelenko1d435522017-02-07 23:02:00 +00001//===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
Chris Lattner2c52b792010-07-11 22:07:02 +00002//
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
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000014#include "llvm/ADT/DenseMap.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/SmallVector.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/ADT/STLExtras.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000018#include "llvm/ADT/StringRef.h"
Nico Riecka37acf72013-07-06 12:13:10 +000019#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCAsmLayout.h"
21#include "llvm/MC/MCAssembler.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000024#include "llvm/MC/MCFixup.h"
25#include "llvm/MC/MCFragment.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"
Eugene Zelenko1d435522017-02-07 23:02:00 +000029#include "llvm/MC/MCSymbol.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000030#include "llvm/MC/MCSymbolCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/MC/MCValue.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000032#include "llvm/MC/MCWinCOFFObjectWriter.h"
Hans Wennborgf26bfc12014-09-29 22:43:20 +000033#include "llvm/MC/StringTableBuilder.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000034#include "llvm/Support/Casting.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000035#include "llvm/Support/COFF.h"
Hans Wennborgba80b5d2014-09-28 00:22:27 +000036#include "llvm/Support/Endian.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000037#include "llvm/Support/ErrorHandling.h"
David Majnemer6ddc6362015-09-01 21:23:58 +000038#include "llvm/Support/JamCRC.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000039#include "llvm/Support/MathExtras.h"
40#include "llvm/Support/raw_ostream.h"
41#include <cassert>
42#include <cstddef>
43#include <cstdint>
44#include <cstring>
David Majnemer088ba022015-09-01 23:46:11 +000045#include <ctime>
Eugene Zelenko1d435522017-02-07 23:02:00 +000046#include <memory>
47#include <string>
48#include <vector>
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000049
Chris Lattner2c52b792010-07-11 22:07:02 +000050using namespace llvm;
Rui Ueyama86e3ef92017-02-14 23:28:19 +000051using llvm::support::endian::write32le;
Chris Lattner2c52b792010-07-11 22:07:02 +000052
Chandler Carruthf58e3762014-04-22 03:04:17 +000053#define DEBUG_TYPE "WinCOFFObjectWriter"
54
Chris Lattner2c52b792010-07-11 22:07:02 +000055namespace {
Eugene Zelenko1d435522017-02-07 23:02:00 +000056
Dmitri Gribenko226fea52013-01-13 16:01:15 +000057typedef SmallString<COFF::NameSize> name;
Chris Lattner2c52b792010-07-11 22:07:02 +000058
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000059enum AuxiliaryType {
60 ATFunctionDefinition,
61 ATbfAndefSymbol,
62 ATWeakExternal,
63 ATFile,
64 ATSectionDefinition
65};
Chris Lattner2c52b792010-07-11 22:07:02 +000066
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000067struct AuxSymbol {
Rafael Espindola11e9e212015-05-27 14:37:12 +000068 AuxiliaryType AuxType;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000069 COFF::Auxiliary Aux;
70};
Chris Lattner2c52b792010-07-11 22:07:02 +000071
Michael J. Spencerd6283772010-09-27 08:58:26 +000072class COFFSection;
73
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000074class COFFSymbol {
75public:
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +000076 COFF::symbol Data = {};
Chris Lattner2c52b792010-07-11 22:07:02 +000077
Dmitri Gribenko226fea52013-01-13 16:01:15 +000078 typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
Chris Lattner2c52b792010-07-11 22:07:02 +000079
Rafael Espindola11e9e212015-05-27 14:37:12 +000080 name Name;
81 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000082 AuxiliarySymbols Aux;
Eugene Zelenko1d435522017-02-07 23:02:00 +000083 COFFSymbol *Other = nullptr;
84 COFFSection *Section = nullptr;
85 int Relocations = 0;
86 const MCSymbol *MC = nullptr;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000087
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +000088 COFFSymbol(StringRef Name) : Name(Name) {}
Eugene Zelenko1d435522017-02-07 23:02:00 +000089
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000090 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000091
David Majnemer4eecd302015-05-30 04:56:02 +000092 int64_t getIndex() const { return Index; }
93 void setIndex(int Value) {
94 Index = Value;
95 if (MC)
96 MC->setIndex(static_cast<uint32_t>(Value));
97 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000098};
99
100// This class contains staging data for a COFF relocation entry.
101struct COFFRelocation {
102 COFF::relocation Data;
Eugene Zelenko1d435522017-02-07 23:02:00 +0000103 COFFSymbol *Symb = nullptr;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000104
Eugene Zelenko1d435522017-02-07 23:02:00 +0000105 COFFRelocation() = default;
106
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000107 static size_t size() { return COFF::RelocationSize; }
108};
109
110typedef std::vector<COFFRelocation> relocations;
111
112class COFFSection {
113public:
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000114 COFF::section Header = {};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000115
Rafael Espindola11e9e212015-05-27 14:37:12 +0000116 std::string Name;
117 int Number;
Eugene Zelenko1d435522017-02-07 23:02:00 +0000118 MCSectionCOFF const *MCSection = nullptr;
119 COFFSymbol *Symbol = nullptr;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000120 relocations Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000121
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000122 COFFSection(StringRef Name) : Name(Name) {}
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000123};
124
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000125class WinCOFFObjectWriter : public MCObjectWriter {
126public:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000127 typedef std::vector<std::unique_ptr<COFFSymbol>> symbols;
David Blaikief564ab62014-04-15 05:25:03 +0000128 typedef std::vector<std::unique_ptr<COFFSection>> sections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000129
Rafael Espindola11e9e212015-05-27 14:37:12 +0000130 typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000131 typedef DenseMap<MCSection const *, COFFSection *> section_map;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000132
Ahmed Charles56440fd2014-03-06 05:51:42 +0000133 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000134
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000135 // Root level file contents.
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000136 COFF::header Header = {};
Rafael Espindola11e9e212015-05-27 14:37:12 +0000137 sections Sections;
138 symbols Symbols;
Rafael Espindola21956e42015-10-23 21:48:05 +0000139 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000140
141 // Maps used during object file creation.
142 section_map SectionMap;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000143 symbol_map SymbolMap;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000144
David Majnemer4d571592014-09-15 19:42:42 +0000145 bool UseBigObj;
146
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000147 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000148
Yaron Kerencca43c12014-09-16 21:31:04 +0000149 void reset() override {
150 memset(&Header, 0, sizeof(Header));
151 Header.Machine = TargetObjectWriter->getMachine();
152 Sections.clear();
153 Symbols.clear();
154 Strings.clear();
155 SectionMap.clear();
156 SymbolMap.clear();
157 MCObjectWriter::reset();
158 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000159
Michael J. Spencer17990d52010-10-16 08:25:57 +0000160 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola11e9e212015-05-27 14:37:12 +0000161 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000162 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000163
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000164 void defineSection(MCSectionCOFF const &Sec);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000165
166 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000167 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000168 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000169
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000170 void SetSymbolName(COFFSymbol &S);
171 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000172
Michael J. Spencerd6283772010-09-27 08:58:26 +0000173 bool IsPhysicalSection(COFFSection *S);
174
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000175 // Entity writing methods.
176
177 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000178 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000179 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000180 void writeSectionHeader(const COFF::section &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000181 void WriteRelocation(const COFF::relocation &R);
182
183 // MCObjectWriter interface implementation.
184
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000185 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper34a61bc2014-03-08 07:02:02 +0000186 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000187
Jim Grosbach36e60e92015-06-04 22:24:41 +0000188 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000189 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000190 const MCFragment &FB, bool InSet,
191 bool IsPCRel) const override;
192
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000193 bool isWeak(const MCSymbol &Sym) const override;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000194
Jim Grosbach36e60e92015-06-04 22:24:41 +0000195 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000196 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000197 MCValue Target, bool &IsPCRel,
198 uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000199
Jim Grosbach36e60e92015-06-04 22:24:41 +0000200 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000201};
Eugene Zelenko1d435522017-02-07 23:02:00 +0000202
203} // end anonymous namespace
Chris Lattner2c52b792010-07-11 22:07:02 +0000204
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000205//------------------------------------------------------------------------------
206// Symbol class implementation
207
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000208// In the case that the name does not fit within 8 bytes, the offset
209// into the string table is stored in the last 4 bytes instead, leaving
210// the first 4 bytes as 0.
211void COFFSymbol::set_name_offset(uint32_t Offset) {
Rui Ueyama86e3ef92017-02-14 23:28:19 +0000212 write32le(Data.Name + 0, 0);
213 write32le(Data.Name + 4, Offset);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000214}
215
216//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000217// WinCOFFObjectWriter class implementation
218
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000219WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000220 raw_pwrite_stream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000221 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000222 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000223}
224
Michael J. Spencer17990d52010-10-16 08:25:57 +0000225COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000226 Symbols.push_back(make_unique<COFFSymbol>(Name));
227 return Symbols.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000228}
229
Yaron Keren56919ef2014-12-04 08:30:39 +0000230COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Rui Ueyama0fcdb482017-02-14 23:47:34 +0000231 COFFSymbol *&Ret = SymbolMap[Symbol];
232 if (!Ret)
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000233 Ret = createSymbol(Symbol->getName());
Rui Ueyama0fcdb482017-02-14 23:47:34 +0000234 return Ret;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000235}
236
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000237COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000238 Sections.emplace_back(make_unique<COFFSection>(Name));
239 return Sections.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000240}
241
Rui Ueyama24e27b42017-02-15 00:15:54 +0000242static uint32_t getAlignment(const MCSectionCOFF &Sec) {
243 switch (Sec.getAlignment()) {
244 case 1:
245 return COFF::IMAGE_SCN_ALIGN_1BYTES;
246 case 2:
247 return COFF::IMAGE_SCN_ALIGN_2BYTES;
248 case 4:
249 return COFF::IMAGE_SCN_ALIGN_4BYTES;
250 case 8:
251 return COFF::IMAGE_SCN_ALIGN_8BYTES;
252 case 16:
253 return COFF::IMAGE_SCN_ALIGN_16BYTES;
254 case 32:
255 return COFF::IMAGE_SCN_ALIGN_32BYTES;
256 case 64:
257 return COFF::IMAGE_SCN_ALIGN_64BYTES;
258 case 128:
259 return COFF::IMAGE_SCN_ALIGN_128BYTES;
260 case 256:
261 return COFF::IMAGE_SCN_ALIGN_256BYTES;
262 case 512:
263 return COFF::IMAGE_SCN_ALIGN_512BYTES;
264 case 1024:
265 return COFF::IMAGE_SCN_ALIGN_1024BYTES;
266 case 2048:
267 return COFF::IMAGE_SCN_ALIGN_2048BYTES;
268 case 4096:
269 return COFF::IMAGE_SCN_ALIGN_4096BYTES;
270 case 8192:
271 return COFF::IMAGE_SCN_ALIGN_8192BYTES;
272 }
273 llvm_unreachable("unsupported section alignment");
274}
275
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000276/// This function takes a section data object from the assembler
277/// and creates the associated COFF section staging object.
Rui Ueyama09786c42017-02-15 00:28:48 +0000278void WinCOFFObjectWriter::defineSection(const MCSectionCOFF &MCSec) {
279 COFFSection *Section = createSection(MCSec.getSectionName());
280 COFFSymbol *Symbol = createSymbol(MCSec.getSectionName());
281 Section->Symbol = Symbol;
282 Symbol->Section = Section;
283 Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Rui Ueyama24e27b42017-02-15 00:15:54 +0000284
285 // Create a COMDAT symbol if needed.
Rui Ueyama09786c42017-02-15 00:28:48 +0000286 if (MCSec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
287 if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
Rafael Espindola0766ae02014-06-06 19:26:12 +0000288 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
289 if (COMDATSymbol->Section)
290 report_fatal_error("two sections have the same comdat");
Rui Ueyama09786c42017-02-15 00:28:48 +0000291 COMDATSymbol->Section = Section;
Rafael Espindola0766ae02014-06-06 19:26:12 +0000292 }
293 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000294
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000295 // In this case the auxiliary symbol is a Section Definition.
Rui Ueyama09786c42017-02-15 00:28:48 +0000296 Symbol->Aux.resize(1);
297 Symbol->Aux[0] = {};
298 Symbol->Aux[0].AuxType = ATSectionDefinition;
299 Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000300
Rui Ueyama09786c42017-02-15 00:28:48 +0000301 // Set section alignment.
302 Section->Header.Characteristics = MCSec.getCharacteristics();
303 Section->Header.Characteristics |= getAlignment(MCSec);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000304
305 // Bind internal COFF section to MC section.
Rui Ueyama09786c42017-02-15 00:28:48 +0000306 Section->MCSection = &MCSec;
307 SectionMap[&MCSec] = Section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000308}
309
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000310static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000311 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000312 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000313 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000314
315 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000316 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000317 return 0;
318
319 return Res;
320}
321
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000322COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
323 if (!Symbol.isVariable())
324 return nullptr;
325
326 const MCSymbolRefExpr *SymRef =
327 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
328 if (!SymRef)
329 return nullptr;
330
331 const MCSymbol &Aliasee = SymRef->getSymbol();
332 if (!Aliasee.isUndefined())
333 return nullptr;
334 return GetOrCreateCOFFSymbol(&Aliasee);
335}
336
David Majnemera9bdb322014-04-08 22:33:40 +0000337/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000338/// and creates the associated COFF symbol staging object.
Rui Ueyama789c4222017-02-15 01:09:01 +0000339void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000340 MCAssembler &Assembler,
341 const MCAsmLayout &Layout) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000342 COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
343 const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000344 COFFSection *Sec = nullptr;
345 if (Base && Base->getFragment()) {
346 Sec = SectionMap[Base->getFragment()->getParent()];
Rui Ueyama789c4222017-02-15 01:09:01 +0000347 if (Sym->Section && Sym->Section != Sec)
Rafael Espindola30c080a2016-05-26 18:48:23 +0000348 report_fatal_error("conflicting sections for symbol");
349 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000350
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000351 COFFSymbol *Local = nullptr;
Rui Ueyama789c4222017-02-15 01:09:01 +0000352 if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
353 Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000354
Rui Ueyama789c4222017-02-15 01:09:01 +0000355 COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000356 if (!WeakDefault) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000357 std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000358 WeakDefault = createSymbol(WeakName);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000359 if (!Sec)
360 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
361 else
362 WeakDefault->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000363 Local = WeakDefault;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000364 }
365
Rui Ueyama789c4222017-02-15 01:09:01 +0000366 Sym->Other = WeakDefault;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000367
Michael J. Spencer17990d52010-10-16 08:25:57 +0000368 // Setup the Weak External auxiliary symbol.
Rui Ueyama789c4222017-02-15 01:09:01 +0000369 Sym->Aux.resize(1);
370 memset(&Sym->Aux[0], 0, sizeof(Sym->Aux[0]));
371 Sym->Aux[0].AuxType = ATWeakExternal;
372 Sym->Aux[0].Aux.WeakExternal.TagIndex = 0;
373 Sym->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000374 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000375 } else {
Rafael Espindola30c080a2016-05-26 18:48:23 +0000376 if (!Base)
Rui Ueyama789c4222017-02-15 01:09:01 +0000377 Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola30c080a2016-05-26 18:48:23 +0000378 else
Rui Ueyama789c4222017-02-15 01:09:01 +0000379 Sym->Section = Sec;
380 Local = Sym;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000381 }
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000382
383 if (Local) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000384 Local->Data.Value = getSymbolValue(MCSym, Layout);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000385
Rui Ueyama789c4222017-02-15 01:09:01 +0000386 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000387 Local->Data.Type = SymbolCOFF.getType();
388 Local->Data.StorageClass = SymbolCOFF.getClass();
389
390 // If no storage class was specified in the streamer, define it here.
391 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000392 bool IsExternal = MCSym.isExternal() ||
393 (!MCSym.getFragment() && !MCSym.isVariable());
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000394
395 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
396 : COFF::IMAGE_SYM_CLASS_STATIC;
397 }
398 }
399
Rui Ueyama789c4222017-02-15 01:09:01 +0000400 Sym->MC = &MCSym;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000401}
402
Nico Rieck01143f92014-02-25 09:50:40 +0000403// Maximum offsets for different string table entry encodings.
David Majnemerf088f522016-01-24 20:46:11 +0000404enum : unsigned { Max7DecimalOffset = 9999999U };
405enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
Nico Rieck01143f92014-02-25 09:50:40 +0000406
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000407// Encode a string table entry offset in base 64, padded to 6 chars, and
408// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
409// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000410static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000411 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000412 "Illegal section name encoding for value");
413
414 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
415 "abcdefghijklmnopqrstuvwxyz"
416 "0123456789+/";
417
418 Buffer[0] = '/';
419 Buffer[1] = '/';
420
Rafael Espindola11e9e212015-05-27 14:37:12 +0000421 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000422 for (unsigned i = 0; i < 6; ++i) {
423 unsigned Rem = Value % 64;
424 Value /= 64;
425 *(Ptr--) = Alphabet[Rem];
426 }
427}
428
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000429void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000430 if (S.Name.size() <= COFF::NameSize) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000431 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000432 return;
David Majnemerf088f522016-01-24 20:46:11 +0000433 }
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000434
435 uint64_t StringTableEntry = Strings.getOffset(S.Name);
436 if (StringTableEntry <= Max7DecimalOffset) {
Rui Ueyama4b58f5772017-02-15 01:48:33 +0000437 SmallVector<char, COFF::NameSize> Buffer;
438 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
439 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
440 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000441 return;
442 }
443 if (StringTableEntry <= MaxBase64Offset) {
444 // Starting with 10,000,000, offsets are encoded as base64.
445 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
446 return;
447 }
448 report_fatal_error("COFF string table is greater than 64 GB.");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000449}
450
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000451void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
452 if (S.Name.size() > COFF::NameSize)
453 S.set_name_offset(Strings.getOffset(S.Name));
454 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000455 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000456}
457
Michael J. Spencerd6283772010-09-27 08:58:26 +0000458bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000459 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
460 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000461}
462
463//------------------------------------------------------------------------------
464// entity writing methods
465
466void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000467 if (UseBigObj) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000468 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
469 writeLE16(0xFFFF);
470 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
471 writeLE16(Header.Machine);
472 writeLE32(Header.TimeDateStamp);
473 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
474 writeLE32(0);
475 writeLE32(0);
476 writeLE32(0);
477 writeLE32(0);
478 writeLE32(Header.NumberOfSections);
479 writeLE32(Header.PointerToSymbolTable);
480 writeLE32(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000481 } else {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000482 writeLE16(Header.Machine);
483 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
484 writeLE32(Header.TimeDateStamp);
485 writeLE32(Header.PointerToSymbolTable);
486 writeLE32(Header.NumberOfSymbols);
487 writeLE16(Header.SizeOfOptionalHeader);
488 writeLE16(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000489 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000490}
491
David Blaikief564ab62014-04-15 05:25:03 +0000492void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000493 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
494 writeLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000495 if (UseBigObj)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000496 writeLE32(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000497 else
Jim Grosbach36e60e92015-06-04 22:24:41 +0000498 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
499 writeLE16(S.Data.Type);
500 write8(S.Data.StorageClass);
501 write8(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000502 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000503}
504
505void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000506 const COFFSymbol::AuxiliarySymbols &S) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000507 for (const AuxSymbol &i : S) {
508 switch (i.AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000509 case ATFunctionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000510 writeLE32(i.Aux.FunctionDefinition.TagIndex);
511 writeLE32(i.Aux.FunctionDefinition.TotalSize);
512 writeLE32(i.Aux.FunctionDefinition.PointerToLinenumber);
513 writeLE32(i.Aux.FunctionDefinition.PointerToNextFunction);
514 WriteZeros(sizeof(i.Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000515 if (UseBigObj)
516 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000517 break;
518 case ATbfAndefSymbol:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000519 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused1));
520 writeLE16(i.Aux.bfAndefSymbol.Linenumber);
521 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused2));
522 writeLE32(i.Aux.bfAndefSymbol.PointerToNextFunction);
523 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000524 if (UseBigObj)
525 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000526 break;
527 case ATWeakExternal:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000528 writeLE32(i.Aux.WeakExternal.TagIndex);
529 writeLE32(i.Aux.WeakExternal.Characteristics);
530 WriteZeros(sizeof(i.Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000531 if (UseBigObj)
532 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000533 break;
534 case ATFile:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000535 writeBytes(
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000536 StringRef(reinterpret_cast<const char *>(&i.Aux),
David Majnemer4d571592014-09-15 19:42:42 +0000537 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000538 break;
539 case ATSectionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000540 writeLE32(i.Aux.SectionDefinition.Length);
541 writeLE16(i.Aux.SectionDefinition.NumberOfRelocations);
542 writeLE16(i.Aux.SectionDefinition.NumberOfLinenumbers);
543 writeLE32(i.Aux.SectionDefinition.CheckSum);
544 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
545 write8(i.Aux.SectionDefinition.Selection);
546 WriteZeros(sizeof(i.Aux.SectionDefinition.unused));
547 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000548 if (UseBigObj)
549 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000550 break;
551 }
552 }
553}
554
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000555void WinCOFFObjectWriter::writeSectionHeader(const COFF::section &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000556 writeBytes(StringRef(S.Name, COFF::NameSize));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000557
Jim Grosbach36e60e92015-06-04 22:24:41 +0000558 writeLE32(S.VirtualSize);
559 writeLE32(S.VirtualAddress);
560 writeLE32(S.SizeOfRawData);
561 writeLE32(S.PointerToRawData);
562 writeLE32(S.PointerToRelocations);
563 writeLE32(S.PointerToLineNumbers);
564 writeLE16(S.NumberOfRelocations);
565 writeLE16(S.NumberOfLineNumbers);
566 writeLE32(S.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000567}
568
569void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000570 writeLE32(R.VirtualAddress);
571 writeLE32(R.SymbolTableIndex);
572 writeLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000573}
574
575////////////////////////////////////////////////////////////////////////////////
576// MCObjectWriter interface implementations
577
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000578void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000579 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000580 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000581 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000582 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000583 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000584
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000585 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000586 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000587 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000588}
589
Jim Grosbach36e60e92015-06-04 22:24:41 +0000590bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000591 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000592 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000593 // MS LINK expects to be able to replace all references to a function with a
594 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
595 // away any relocations to functions.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000596 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
David Majnemer03e2cc32015-12-21 22:09:27 +0000597 if (Asm.isIncrementalLinkerCompatible() &&
598 (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000599 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000600 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000601 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000602}
603
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000604bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000605 if (!Sym.isExternal())
Rafael Espindola88af4112015-04-17 11:27:13 +0000606 return false;
607
Rafael Espindola88af4112015-04-17 11:27:13 +0000608 if (!Sym.isInSection())
609 return false;
610
611 const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
612 if (!Sec.getCOMDATSymbol())
613 return false;
614
615 // It looks like for COFF it is invalid to replace a reference to a global
616 // in a comdat with a reference to a local.
617 // FIXME: Add a specification reference if available.
618 return true;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000619}
620
Jim Grosbach36e60e92015-06-04 22:24:41 +0000621void WinCOFFObjectWriter::recordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000622 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
623 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000624 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000625
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000626 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000627 if (!A.isRegistered()) {
628 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000629 Twine("symbol '") + A.getName() +
630 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000631 return;
632 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000633 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000634 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000635 Twine("assembler label '") + A.getName() +
636 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000637 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000638 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000639
Rui Ueyama62376782017-02-16 01:06:45 +0000640 MCSection *MCSec = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000641
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000642 // Mark this symbol as requiring an entry in the symbol table.
Rui Ueyama62376782017-02-16 01:06:45 +0000643 assert(SectionMap.find(MCSec) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000644 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000645
Rui Ueyama62376782017-02-16 01:06:45 +0000646 COFFSection *Sec = SectionMap[MCSec];
Rafael Espindolaed164772011-04-20 14:01:45 +0000647 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000648 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000649
David Majnemera45a1762014-01-06 07:39:46 +0000650 if (SymB) {
651 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000652 if (!B->getFragment()) {
653 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000654 Fixup.getLoc(),
655 Twine("symbol '") + B->getName() +
656 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000657 return;
658 }
David Majnemera45a1762014-01-06 07:39:46 +0000659
Oliver Stannard9be59af2015-11-17 10:00:43 +0000660 if (!A.getFragment()) {
661 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000662 Fixup.getLoc(),
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000663 Twine("symbol '") + A.getName() +
David Majnemera45a1762014-01-06 07:39:46 +0000664 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000665 return;
666 }
David Majnemera45a1762014-01-06 07:39:46 +0000667
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000668 CrossSection = &A.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000669
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000670 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000671 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000672
673 // In the case where we have SymbA and SymB, we just need to store the delta
674 // between the two symbols. Update FixedValue to account for the delta, and
675 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000676 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000677 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000678 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000679 return;
David Majnemer1de30942015-02-09 06:31:31 +0000680 }
681
682 // Offset of the relocation in the section
683 int64_t OffsetOfRelocation =
684 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
685
Andy Ayers9e5c8512015-05-14 01:10:41 +0000686 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000687 } else {
688 FixedValue = Target.getConstant();
689 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000690
691 COFFRelocation Reloc;
692
Daniel Dunbar727be432010-07-31 21:08:54 +0000693 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000694 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000695
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000696 // Turn relocations for temporary symbols into section relocations.
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000697 if (A.isTemporary() || CrossSection) {
698 MCSection *TargetSection = &A.getSection();
699 assert(
700 SectionMap.find(TargetSection) != SectionMap.end() &&
701 "Section must already have been defined in executePostLayoutBinding!");
702 Reloc.Symb = SectionMap[TargetSection]->Symbol;
703 FixedValue += Layout.getSymbolOffset(A);
704 } else {
705 assert(
706 SymbolMap.find(&A) != SymbolMap.end() &&
707 "Symbol must already have been defined in executePostLayoutBinding!");
708 Reloc.Symb = SymbolMap[&A];
709 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000710
711 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000712
713 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000714 Reloc.Data.Type = TargetObjectWriter->getRelocType(
715 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000716
717 // FIXME: Can anyone explain what this does other than adjust for the size
718 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000719 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
720 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
721 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
722 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000723 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000724
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000725 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
726 switch (Reloc.Data.Type) {
727 case COFF::IMAGE_REL_ARM_ABSOLUTE:
728 case COFF::IMAGE_REL_ARM_ADDR32:
729 case COFF::IMAGE_REL_ARM_ADDR32NB:
730 case COFF::IMAGE_REL_ARM_TOKEN:
731 case COFF::IMAGE_REL_ARM_SECTION:
732 case COFF::IMAGE_REL_ARM_SECREL:
733 break;
734 case COFF::IMAGE_REL_ARM_BRANCH11:
735 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000736 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
737 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
738 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000739 case COFF::IMAGE_REL_ARM_BRANCH24:
740 case COFF::IMAGE_REL_ARM_BLX24:
741 case COFF::IMAGE_REL_ARM_MOV32A:
742 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
743 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000744 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000745 // generate the relocations however the rest of the MSVC toolchain is
746 // unable to handle it.
747 llvm_unreachable("unsupported relocation");
748 break;
749 case COFF::IMAGE_REL_ARM_MOV32T:
750 break;
751 case COFF::IMAGE_REL_ARM_BRANCH20T:
752 case COFF::IMAGE_REL_ARM_BRANCH24T:
753 case COFF::IMAGE_REL_ARM_BLX23T:
754 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
755 // perform a 4 byte adjustment to the relocation. Relative branches are
756 // offset by 4 on ARM, however, because there is no RELA relocations, all
757 // branches are offset by 4.
758 FixedValue = FixedValue + 4;
759 break;
760 }
761 }
762
Simon Pilgrimf2fbf432016-11-20 13:47:59 +0000763 // The fixed value never makes sense for section indices, ignore it.
David Majnemer408b5e62016-02-05 01:55:49 +0000764 if (Fixup.getKind() == FK_SecRel_2)
765 FixedValue = 0;
766
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000767 if (TargetObjectWriter->recordRelocation(Fixup))
Rui Ueyama62376782017-02-16 01:06:45 +0000768 Sec->Relocations.push_back(Reloc);
769}
770
771static std::time_t getTime() {
772 std::time_t Now = time(nullptr);
773 if (Now < 0 || !isUInt<32>(Now))
774 return UINT32_MAX;
775 return Now;
Chris Lattner2c52b792010-07-11 22:07:02 +0000776}
777
Jim Grosbach36e60e92015-06-04 22:24:41 +0000778void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000779 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000780 size_t SectionsSize = Sections.size();
781 if (SectionsSize > static_cast<size_t>(INT32_MAX))
782 report_fatal_error(
783 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000784
David Majnemer4d571592014-09-15 19:42:42 +0000785 // Assign symbol and section indexes and offsets.
786 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
787
788 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
789
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000790 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000791 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000792 for (const auto &Section : Sections) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000793 Section->Number = Number;
794 Section->Symbol->Data.SectionNumber = Number;
795 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000796 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000797 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000798
David Majnemer4d571592014-09-15 19:42:42 +0000799 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000800 Header.NumberOfSymbols = 0;
801
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000802 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000803 // round up to calculate the number of auxiliary symbols required
804 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000805 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000806
807 COFFSymbol *file = createSymbol(".file");
808 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
809 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
810 file->Aux.resize(Count);
811
812 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000813 unsigned Length = Name.size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000814 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000815 Aux.AuxType = ATFile;
816
817 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000818 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000819 Length = Length - SymbolSize;
820 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000821 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000822 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
823 break;
824 }
825
826 Offset += SymbolSize;
827 }
828 }
829
David Majnemer9ab5ff12014-08-28 04:02:50 +0000830 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000831 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000832 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000833 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000834 Symbol->setIndex(Header.NumberOfSymbols++);
835 // Update auxiliary symbol info.
836 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
837 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000838 }
839
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000840 // Build string table.
841 for (const auto &S : Sections)
842 if (S->Name.size() > COFF::NameSize)
843 Strings.add(S->Name);
844 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000845 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000846 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000847 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000848
849 // Set names.
850 for (const auto &S : Sections)
851 SetSectionName(*S);
852 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000853 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000854
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000855 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000856 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000857 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000858 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000859 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
860 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000861 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000862 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000863 }
864 }
865
Nico Riecka37acf72013-07-06 12:13:10 +0000866 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000867 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000868 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000869 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
870 continue;
871
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000872 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +0000873
Rafael Espindola0766ae02014-06-06 19:26:12 +0000874 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
875 assert(COMDAT);
876 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
877 assert(COMDATSymbol);
878 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000879 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000880 report_fatal_error(
881 Twine("Missing associated COMDAT section for section ") +
882 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000883
884 // Skip this section if the associated section is unused.
885 if (Assoc->Number == -1)
886 continue;
887
David Majnemer4eecd302015-05-30 04:56:02 +0000888 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +0000889 }
890
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000891 // Assign file offsets to COFF object file structures.
892
Rui Ueyama62376782017-02-16 01:06:45 +0000893 unsigned Offset = getInitialOffset();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000894
David Majnemer4d571592014-09-15 19:42:42 +0000895 if (UseBigObj)
Rui Ueyama62376782017-02-16 01:06:45 +0000896 Offset += COFF::Header32Size;
David Majnemer4d571592014-09-15 19:42:42 +0000897 else
Rui Ueyama62376782017-02-16 01:06:45 +0000898 Offset += COFF::Header16Size;
899 Offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000900
Yaron Keren56919ef2014-12-04 08:30:39 +0000901 for (const auto &Section : Asm) {
Rafael Espindolaa554c052015-05-25 23:14:17 +0000902 COFFSection *Sec = SectionMap[&Section];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000903
Michael J. Spencerd6283772010-09-27 08:58:26 +0000904 if (Sec->Number == -1)
905 continue;
906
Rafael Espindola5a1e80b2015-05-26 02:00:36 +0000907 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000908
Michael J. Spencerd6283772010-09-27 08:58:26 +0000909 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000910 // Align the section data to a four byte boundary.
Rui Ueyama62376782017-02-16 01:06:45 +0000911 Offset = alignTo(Offset, 4);
912 Sec->Header.PointerToRawData = Offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000913
Rui Ueyama62376782017-02-16 01:06:45 +0000914 Offset += Sec->Header.SizeOfRawData;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000915 }
916
Eugene Zelenko1d435522017-02-07 23:02:00 +0000917 if (!Sec->Relocations.empty()) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000918 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
919
920 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000921 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000922 // size is found in reloc #0. Microsoft tools understand this.
923 Sec->Header.NumberOfRelocations = 0xffff;
924 } else {
925 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
926 }
Rui Ueyama62376782017-02-16 01:06:45 +0000927 Sec->Header.PointerToRelocations = Offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000928
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000929 if (RelocationsOverflow) {
930 // Reloc #0 will contain actual count, so make room for it.
Rui Ueyama62376782017-02-16 01:06:45 +0000931 Offset += COFF::RelocationSize;
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000932 }
933
Rui Ueyama62376782017-02-16 01:06:45 +0000934 Offset += COFF::RelocationSize * Sec->Relocations.size();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000935
Yaron Keren56919ef2014-12-04 08:30:39 +0000936 for (auto &Relocation : Sec->Relocations) {
David Majnemer4eecd302015-05-30 04:56:02 +0000937 assert(Relocation.Symb->getIndex() != -1);
938 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000939 }
940 }
941
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000942 assert(Sec->Symbol->Aux.size() == 1 &&
943 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000944 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000945 assert(Aux.AuxType == ATSectionDefinition &&
946 "Section's symbol's aux symbol must be a Section Definition!");
947 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
948 Aux.Aux.SectionDefinition.NumberOfRelocations =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000949 Sec->Header.NumberOfRelocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000950 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000951 Sec->Header.NumberOfLineNumbers;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000952 }
953
Rui Ueyama62376782017-02-16 01:06:45 +0000954 Header.PointerToSymbolTable = Offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000955
David Majnemer088ba022015-09-01 23:46:11 +0000956 // MS LINK expects to be able to use this timestamp to implement their
957 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +0000958 if (Asm.isIncrementalLinkerCompatible()) {
Rui Ueyama62376782017-02-16 01:06:45 +0000959 Header.TimeDateStamp = getTime();
David Majnemer03e2cc32015-12-21 22:09:27 +0000960 } else {
Nico Weber891419a2016-01-06 19:05:19 +0000961 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +0000962 Header.TimeDateStamp = 0;
963 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +0000964
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000965 // Write it all to disk...
966 WriteFileHeader(Header);
967
Rui Ueyama62376782017-02-16 01:06:45 +0000968 for (auto &Section : Sections) {
969 if (Section->Number != -1) {
970 if (Section->Relocations.size() >= 0xffff)
971 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
972 writeSectionHeader(Section->Header);
973 }
974 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000975
Rui Ueyama62376782017-02-16 01:06:45 +0000976 SmallVector<char, 128> Buf;
977
978 sections::iterator I, IE;
979 MCAssembler::iterator J, JE;
980 for (I = Sections.begin(), IE = Sections.end(), J = Asm.begin(),
981 JE = Asm.end();
982 I != IE && J != JE; ++I, ++J) {
983 COFFSection &Sec = *I->get();
984 MCSection &MCSec = *J;
985
986 if (Sec.Number == -1)
987 continue;
988
989 if (Sec.Header.PointerToRawData != 0) {
990 assert(getStream().tell() <= Sec.Header.PointerToRawData &&
991 "Section::PointerToRawData is insane!");
992
993 unsigned SectionDataPadding =
994 Sec.Header.PointerToRawData - getStream().tell();
995 assert(SectionDataPadding < 4 &&
996 "Should only need at most three bytes of padding!");
997
998 WriteZeros(SectionDataPadding);
999
1000 // Save the contents of the section to a temporary buffer, we need this
1001 // to CRC the data before we dump it into the object file.
1002 Buf.clear();
1003 raw_svector_ostream VecOS(Buf);
1004 raw_pwrite_stream &OldStream = getStream();
1005 // Redirect the output stream to our buffer.
1006 setStream(VecOS);
1007 // Fill our buffer with the section data.
1008 Asm.writeSectionData(&MCSec, Layout);
1009 // Reset the stream back to what it was before.
1010 setStream(OldStream);
1011
1012 // Calculate our CRC with an initial value of '0', this is not how
1013 // JamCRC is specified but it aligns with the expected output.
1014 JamCRC JC(/*Init=*/0x00000000U);
1015 JC.update(Buf);
1016
1017 // Write the section contents to the object file.
1018 getStream() << Buf;
1019
1020 // Update the section definition auxiliary symbol to record the CRC.
1021 COFFSection *Sec = SectionMap[&MCSec];
1022 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
1023 assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
1024 AuxSymbol &SecDef = AuxSyms[0];
1025 SecDef.Aux.SectionDefinition.CheckSum = JC.getCRC();
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001026 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001027
Rui Ueyama62376782017-02-16 01:06:45 +00001028 if (!Sec.Relocations.empty()) {
1029 assert(getStream().tell() == Sec.Header.PointerToRelocations &&
1030 "Section::PointerToRelocations is insane!");
Michael J. Spencerd6283772010-09-27 08:58:26 +00001031
Rui Ueyama62376782017-02-16 01:06:45 +00001032 if (Sec.Relocations.size() >= 0xffff) {
1033 // In case of overflow, write actual relocation count as first
1034 // relocation. Including the synthetic reloc itself (+ 1).
1035 COFF::relocation R;
1036 R.VirtualAddress = Sec.Relocations.size() + 1;
1037 R.SymbolTableIndex = 0;
1038 R.Type = 0;
1039 WriteRelocation(R);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001040 }
1041
Rui Ueyama62376782017-02-16 01:06:45 +00001042 for (const auto &Relocation : Sec.Relocations)
1043 WriteRelocation(Relocation.Data);
1044 } else
1045 assert(Sec.Header.PointerToRelocations == 0 &&
1046 "Section::PointerToRelocations is insane!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001047 }
1048
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001049 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001050 "Header::PointerToSymbolTable is insane!");
1051
Yaron Keren56919ef2014-12-04 08:30:39 +00001052 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001053 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001054 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001055
Rafael Espindola39751af2016-10-04 22:43:25 +00001056 Strings.write(getStream());
Chris Lattner2c52b792010-07-11 22:07:02 +00001057}
1058
Rafael Espindola11e9e212015-05-27 14:37:12 +00001059MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1060 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001061
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001062// Pin the vtable to this file.
1063void MCWinCOFFObjectTargetWriter::anchor() {}
1064
Chris Lattner2c52b792010-07-11 22:07:02 +00001065//------------------------------------------------------------------------------
1066// WinCOFFObjectWriter factory function
1067
Rafael Espindola37099d92015-04-09 18:08:15 +00001068MCObjectWriter *
1069llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001070 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001071 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001072}