blob: ce8b2005e869daee794b6b2043ed38527f3a3488 [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);
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000182 uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout,
183 const MCSection &MCSec);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000184 void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
185 const COFFSection &Sec, const MCSection &MCSec);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000186
187 // MCObjectWriter interface implementation.
188
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000189 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper34a61bc2014-03-08 07:02:02 +0000190 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000191
Jim Grosbach36e60e92015-06-04 22:24:41 +0000192 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000193 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000194 const MCFragment &FB, bool InSet,
195 bool IsPCRel) const override;
196
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000197 bool isWeak(const MCSymbol &Sym) const override;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000198
Jim Grosbach36e60e92015-06-04 22:24:41 +0000199 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000200 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000201 MCValue Target, bool &IsPCRel,
202 uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000203
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000204 void createFileSymbols(MCAssembler &Asm);
205 void assignSectionNumbers();
206 void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout);
207
Jim Grosbach36e60e92015-06-04 22:24:41 +0000208 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000209};
Eugene Zelenko1d435522017-02-07 23:02:00 +0000210
211} // end anonymous namespace
Chris Lattner2c52b792010-07-11 22:07:02 +0000212
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000213//------------------------------------------------------------------------------
214// Symbol class implementation
215
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000216// In the case that the name does not fit within 8 bytes, the offset
217// into the string table is stored in the last 4 bytes instead, leaving
218// the first 4 bytes as 0.
219void COFFSymbol::set_name_offset(uint32_t Offset) {
Rui Ueyama86e3ef92017-02-14 23:28:19 +0000220 write32le(Data.Name + 0, 0);
221 write32le(Data.Name + 4, Offset);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000222}
223
224//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000225// WinCOFFObjectWriter class implementation
226
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000227WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000228 raw_pwrite_stream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000229 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000230 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000231}
232
Michael J. Spencer17990d52010-10-16 08:25:57 +0000233COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000234 Symbols.push_back(make_unique<COFFSymbol>(Name));
235 return Symbols.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000236}
237
Yaron Keren56919ef2014-12-04 08:30:39 +0000238COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Rui Ueyama0fcdb482017-02-14 23:47:34 +0000239 COFFSymbol *&Ret = SymbolMap[Symbol];
240 if (!Ret)
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000241 Ret = createSymbol(Symbol->getName());
Rui Ueyama0fcdb482017-02-14 23:47:34 +0000242 return Ret;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000243}
244
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000245COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000246 Sections.emplace_back(make_unique<COFFSection>(Name));
247 return Sections.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000248}
249
Rui Ueyama24e27b42017-02-15 00:15:54 +0000250static uint32_t getAlignment(const MCSectionCOFF &Sec) {
251 switch (Sec.getAlignment()) {
252 case 1:
253 return COFF::IMAGE_SCN_ALIGN_1BYTES;
254 case 2:
255 return COFF::IMAGE_SCN_ALIGN_2BYTES;
256 case 4:
257 return COFF::IMAGE_SCN_ALIGN_4BYTES;
258 case 8:
259 return COFF::IMAGE_SCN_ALIGN_8BYTES;
260 case 16:
261 return COFF::IMAGE_SCN_ALIGN_16BYTES;
262 case 32:
263 return COFF::IMAGE_SCN_ALIGN_32BYTES;
264 case 64:
265 return COFF::IMAGE_SCN_ALIGN_64BYTES;
266 case 128:
267 return COFF::IMAGE_SCN_ALIGN_128BYTES;
268 case 256:
269 return COFF::IMAGE_SCN_ALIGN_256BYTES;
270 case 512:
271 return COFF::IMAGE_SCN_ALIGN_512BYTES;
272 case 1024:
273 return COFF::IMAGE_SCN_ALIGN_1024BYTES;
274 case 2048:
275 return COFF::IMAGE_SCN_ALIGN_2048BYTES;
276 case 4096:
277 return COFF::IMAGE_SCN_ALIGN_4096BYTES;
278 case 8192:
279 return COFF::IMAGE_SCN_ALIGN_8192BYTES;
280 }
281 llvm_unreachable("unsupported section alignment");
282}
283
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000284/// This function takes a section data object from the assembler
285/// and creates the associated COFF section staging object.
Rui Ueyama09786c42017-02-15 00:28:48 +0000286void WinCOFFObjectWriter::defineSection(const MCSectionCOFF &MCSec) {
287 COFFSection *Section = createSection(MCSec.getSectionName());
288 COFFSymbol *Symbol = createSymbol(MCSec.getSectionName());
289 Section->Symbol = Symbol;
290 Symbol->Section = Section;
291 Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Rui Ueyama24e27b42017-02-15 00:15:54 +0000292
293 // Create a COMDAT symbol if needed.
Rui Ueyama09786c42017-02-15 00:28:48 +0000294 if (MCSec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
295 if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
Rafael Espindola0766ae02014-06-06 19:26:12 +0000296 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
297 if (COMDATSymbol->Section)
298 report_fatal_error("two sections have the same comdat");
Rui Ueyama09786c42017-02-15 00:28:48 +0000299 COMDATSymbol->Section = Section;
Rafael Espindola0766ae02014-06-06 19:26:12 +0000300 }
301 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000302
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000303 // In this case the auxiliary symbol is a Section Definition.
Rui Ueyama09786c42017-02-15 00:28:48 +0000304 Symbol->Aux.resize(1);
305 Symbol->Aux[0] = {};
306 Symbol->Aux[0].AuxType = ATSectionDefinition;
307 Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000308
Rui Ueyama09786c42017-02-15 00:28:48 +0000309 // Set section alignment.
310 Section->Header.Characteristics = MCSec.getCharacteristics();
311 Section->Header.Characteristics |= getAlignment(MCSec);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000312
313 // Bind internal COFF section to MC section.
Rui Ueyama09786c42017-02-15 00:28:48 +0000314 Section->MCSection = &MCSec;
315 SectionMap[&MCSec] = Section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000316}
317
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000318static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000319 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000320 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000321 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000322
323 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000324 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000325 return 0;
326
327 return Res;
328}
329
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000330COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
331 if (!Symbol.isVariable())
332 return nullptr;
333
334 const MCSymbolRefExpr *SymRef =
335 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
336 if (!SymRef)
337 return nullptr;
338
339 const MCSymbol &Aliasee = SymRef->getSymbol();
340 if (!Aliasee.isUndefined())
341 return nullptr;
342 return GetOrCreateCOFFSymbol(&Aliasee);
343}
344
David Majnemera9bdb322014-04-08 22:33:40 +0000345/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000346/// and creates the associated COFF symbol staging object.
Rui Ueyama789c4222017-02-15 01:09:01 +0000347void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000348 MCAssembler &Assembler,
349 const MCAsmLayout &Layout) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000350 COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
351 const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000352 COFFSection *Sec = nullptr;
353 if (Base && Base->getFragment()) {
354 Sec = SectionMap[Base->getFragment()->getParent()];
Rui Ueyama789c4222017-02-15 01:09:01 +0000355 if (Sym->Section && Sym->Section != Sec)
Rafael Espindola30c080a2016-05-26 18:48:23 +0000356 report_fatal_error("conflicting sections for symbol");
357 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000358
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000359 COFFSymbol *Local = nullptr;
Rui Ueyama789c4222017-02-15 01:09:01 +0000360 if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
361 Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000362
Rui Ueyama789c4222017-02-15 01:09:01 +0000363 COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000364 if (!WeakDefault) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000365 std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000366 WeakDefault = createSymbol(WeakName);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000367 if (!Sec)
368 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
369 else
370 WeakDefault->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000371 Local = WeakDefault;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000372 }
373
Rui Ueyama789c4222017-02-15 01:09:01 +0000374 Sym->Other = WeakDefault;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000375
Michael J. Spencer17990d52010-10-16 08:25:57 +0000376 // Setup the Weak External auxiliary symbol.
Rui Ueyama789c4222017-02-15 01:09:01 +0000377 Sym->Aux.resize(1);
378 memset(&Sym->Aux[0], 0, sizeof(Sym->Aux[0]));
379 Sym->Aux[0].AuxType = ATWeakExternal;
380 Sym->Aux[0].Aux.WeakExternal.TagIndex = 0;
381 Sym->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000382 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000383 } else {
Rafael Espindola30c080a2016-05-26 18:48:23 +0000384 if (!Base)
Rui Ueyama789c4222017-02-15 01:09:01 +0000385 Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola30c080a2016-05-26 18:48:23 +0000386 else
Rui Ueyama789c4222017-02-15 01:09:01 +0000387 Sym->Section = Sec;
388 Local = Sym;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000389 }
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000390
391 if (Local) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000392 Local->Data.Value = getSymbolValue(MCSym, Layout);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000393
Rui Ueyama789c4222017-02-15 01:09:01 +0000394 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000395 Local->Data.Type = SymbolCOFF.getType();
396 Local->Data.StorageClass = SymbolCOFF.getClass();
397
398 // If no storage class was specified in the streamer, define it here.
399 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000400 bool IsExternal = MCSym.isExternal() ||
401 (!MCSym.getFragment() && !MCSym.isVariable());
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000402
403 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
404 : COFF::IMAGE_SYM_CLASS_STATIC;
405 }
406 }
407
Rui Ueyama789c4222017-02-15 01:09:01 +0000408 Sym->MC = &MCSym;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000409}
410
Nico Rieck01143f92014-02-25 09:50:40 +0000411// Maximum offsets for different string table entry encodings.
David Majnemerf088f522016-01-24 20:46:11 +0000412enum : unsigned { Max7DecimalOffset = 9999999U };
413enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
Nico Rieck01143f92014-02-25 09:50:40 +0000414
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000415// Encode a string table entry offset in base 64, padded to 6 chars, and
416// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
417// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000418static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000419 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000420 "Illegal section name encoding for value");
421
422 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
423 "abcdefghijklmnopqrstuvwxyz"
424 "0123456789+/";
425
426 Buffer[0] = '/';
427 Buffer[1] = '/';
428
Rafael Espindola11e9e212015-05-27 14:37:12 +0000429 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000430 for (unsigned i = 0; i < 6; ++i) {
431 unsigned Rem = Value % 64;
432 Value /= 64;
433 *(Ptr--) = Alphabet[Rem];
434 }
435}
436
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000437void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000438 if (S.Name.size() <= COFF::NameSize) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000439 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000440 return;
David Majnemerf088f522016-01-24 20:46:11 +0000441 }
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000442
443 uint64_t StringTableEntry = Strings.getOffset(S.Name);
444 if (StringTableEntry <= Max7DecimalOffset) {
Rui Ueyama4b58f5772017-02-15 01:48:33 +0000445 SmallVector<char, COFF::NameSize> Buffer;
446 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
447 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
448 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000449 return;
450 }
451 if (StringTableEntry <= MaxBase64Offset) {
452 // Starting with 10,000,000, offsets are encoded as base64.
453 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
454 return;
455 }
456 report_fatal_error("COFF string table is greater than 64 GB.");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000457}
458
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000459void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
460 if (S.Name.size() > COFF::NameSize)
461 S.set_name_offset(Strings.getOffset(S.Name));
462 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000463 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000464}
465
Michael J. Spencerd6283772010-09-27 08:58:26 +0000466bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000467 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
468 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000469}
470
471//------------------------------------------------------------------------------
472// entity writing methods
473
474void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000475 if (UseBigObj) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000476 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
477 writeLE16(0xFFFF);
478 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
479 writeLE16(Header.Machine);
480 writeLE32(Header.TimeDateStamp);
481 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
482 writeLE32(0);
483 writeLE32(0);
484 writeLE32(0);
485 writeLE32(0);
486 writeLE32(Header.NumberOfSections);
487 writeLE32(Header.PointerToSymbolTable);
488 writeLE32(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000489 } else {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000490 writeLE16(Header.Machine);
491 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
492 writeLE32(Header.TimeDateStamp);
493 writeLE32(Header.PointerToSymbolTable);
494 writeLE32(Header.NumberOfSymbols);
495 writeLE16(Header.SizeOfOptionalHeader);
496 writeLE16(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000497 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000498}
499
David Blaikief564ab62014-04-15 05:25:03 +0000500void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000501 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
502 writeLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000503 if (UseBigObj)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000504 writeLE32(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000505 else
Jim Grosbach36e60e92015-06-04 22:24:41 +0000506 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
507 writeLE16(S.Data.Type);
508 write8(S.Data.StorageClass);
509 write8(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000510 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000511}
512
513void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000514 const COFFSymbol::AuxiliarySymbols &S) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000515 for (const AuxSymbol &i : S) {
516 switch (i.AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000517 case ATFunctionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000518 writeLE32(i.Aux.FunctionDefinition.TagIndex);
519 writeLE32(i.Aux.FunctionDefinition.TotalSize);
520 writeLE32(i.Aux.FunctionDefinition.PointerToLinenumber);
521 writeLE32(i.Aux.FunctionDefinition.PointerToNextFunction);
522 WriteZeros(sizeof(i.Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000523 if (UseBigObj)
524 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000525 break;
526 case ATbfAndefSymbol:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000527 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused1));
528 writeLE16(i.Aux.bfAndefSymbol.Linenumber);
529 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused2));
530 writeLE32(i.Aux.bfAndefSymbol.PointerToNextFunction);
531 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000532 if (UseBigObj)
533 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000534 break;
535 case ATWeakExternal:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000536 writeLE32(i.Aux.WeakExternal.TagIndex);
537 writeLE32(i.Aux.WeakExternal.Characteristics);
538 WriteZeros(sizeof(i.Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000539 if (UseBigObj)
540 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000541 break;
542 case ATFile:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000543 writeBytes(
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000544 StringRef(reinterpret_cast<const char *>(&i.Aux),
David Majnemer4d571592014-09-15 19:42:42 +0000545 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000546 break;
547 case ATSectionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000548 writeLE32(i.Aux.SectionDefinition.Length);
549 writeLE16(i.Aux.SectionDefinition.NumberOfRelocations);
550 writeLE16(i.Aux.SectionDefinition.NumberOfLinenumbers);
551 writeLE32(i.Aux.SectionDefinition.CheckSum);
552 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
553 write8(i.Aux.SectionDefinition.Selection);
554 WriteZeros(sizeof(i.Aux.SectionDefinition.unused));
555 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000556 if (UseBigObj)
557 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000558 break;
559 }
560 }
561}
562
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000563void WinCOFFObjectWriter::writeSectionHeader(const COFF::section &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000564 writeBytes(StringRef(S.Name, COFF::NameSize));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000565
Jim Grosbach36e60e92015-06-04 22:24:41 +0000566 writeLE32(S.VirtualSize);
567 writeLE32(S.VirtualAddress);
568 writeLE32(S.SizeOfRawData);
569 writeLE32(S.PointerToRawData);
570 writeLE32(S.PointerToRelocations);
571 writeLE32(S.PointerToLineNumbers);
572 writeLE16(S.NumberOfRelocations);
573 writeLE16(S.NumberOfLineNumbers);
574 writeLE32(S.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000575}
576
577void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000578 writeLE32(R.VirtualAddress);
579 writeLE32(R.SymbolTableIndex);
580 writeLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000581}
582
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000583// Write MCSec's contents. What this function does is essentially
584// "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
585// because it needs to compute a CRC.
586uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler &Asm,
587 const MCAsmLayout &Layout,
588 const MCSection &MCSec) {
589 // Save the contents of the section to a temporary buffer, we need this
590 // to CRC the data before we dump it into the object file.
591 SmallVector<char, 128> Buf;
592 raw_svector_ostream VecOS(Buf);
593 raw_pwrite_stream &OldStream = getStream();
594
595 // Redirect the output stream to our buffer and fill our buffer with
596 // the section data.
597 setStream(VecOS);
598 Asm.writeSectionData(&MCSec, Layout);
599
600 // Reset the stream back to what it was before.
601 setStream(OldStream);
602
603 // Write the section contents to the object file.
604 getStream() << Buf;
605
606 // Calculate our CRC with an initial value of '0', this is not how
607 // JamCRC is specified but it aligns with the expected output.
608 JamCRC JC(/*Init=*/0);
609 JC.update(Buf);
610 return JC.getCRC();
611}
612
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000613void WinCOFFObjectWriter::writeSection(MCAssembler &Asm,
614 const MCAsmLayout &Layout,
615 const COFFSection &Sec,
616 const MCSection &MCSec) {
617 if (Sec.Number == -1)
618 return;
619
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000620 // Write the section contents.
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000621 if (Sec.Header.PointerToRawData != 0) {
622 assert(getStream().tell() <= Sec.Header.PointerToRawData &&
623 "Section::PointerToRawData is insane!");
624
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000625 unsigned PaddingSize = Sec.Header.PointerToRawData - getStream().tell();
626 assert(PaddingSize < 4 &&
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000627 "Should only need at most three bytes of padding!");
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000628 WriteZeros(PaddingSize);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000629
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000630 uint32_t CRC = writeSectionContents(Asm, Layout, MCSec);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000631
632 // Update the section definition auxiliary symbol to record the CRC.
633 COFFSection *Sec = SectionMap[&MCSec];
634 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
635 assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
636 AuxSymbol &SecDef = AuxSyms[0];
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000637 SecDef.Aux.SectionDefinition.CheckSum = CRC;
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000638 }
639
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000640 // Write relocations for this section.
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000641 if (Sec.Relocations.empty()) {
642 assert(Sec.Header.PointerToRelocations == 0 &&
643 "Section::PointerToRelocations is insane!");
644 return;
645 }
646
647 assert(getStream().tell() == Sec.Header.PointerToRelocations &&
648 "Section::PointerToRelocations is insane!");
649
650 if (Sec.Relocations.size() >= 0xffff) {
651 // In case of overflow, write actual relocation count as first
652 // relocation. Including the synthetic reloc itself (+ 1).
653 COFF::relocation R;
654 R.VirtualAddress = Sec.Relocations.size() + 1;
655 R.SymbolTableIndex = 0;
656 R.Type = 0;
657 WriteRelocation(R);
658 }
659
660 for (const auto &Relocation : Sec.Relocations)
661 WriteRelocation(Relocation.Data);
662}
663
Chris Lattner2c52b792010-07-11 22:07:02 +0000664////////////////////////////////////////////////////////////////////////////////
665// MCObjectWriter interface implementations
666
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000667void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000668 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000669 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000670 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000671 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000672 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000673
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000674 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000675 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000676 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000677}
678
Jim Grosbach36e60e92015-06-04 22:24:41 +0000679bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000680 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000681 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000682 // MS LINK expects to be able to replace all references to a function with a
683 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
684 // away any relocations to functions.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000685 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
David Majnemer03e2cc32015-12-21 22:09:27 +0000686 if (Asm.isIncrementalLinkerCompatible() &&
687 (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000688 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000689 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000690 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000691}
692
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000693bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000694 if (!Sym.isExternal())
Rafael Espindola88af4112015-04-17 11:27:13 +0000695 return false;
696
Rafael Espindola88af4112015-04-17 11:27:13 +0000697 if (!Sym.isInSection())
698 return false;
699
700 const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
701 if (!Sec.getCOMDATSymbol())
702 return false;
703
704 // It looks like for COFF it is invalid to replace a reference to a global
705 // in a comdat with a reference to a local.
706 // FIXME: Add a specification reference if available.
707 return true;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000708}
709
Jim Grosbach36e60e92015-06-04 22:24:41 +0000710void WinCOFFObjectWriter::recordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000711 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
712 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000713 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000714
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000715 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000716 if (!A.isRegistered()) {
717 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000718 Twine("symbol '") + A.getName() +
719 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000720 return;
721 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000722 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000723 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000724 Twine("assembler label '") + A.getName() +
725 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000726 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000727 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000728
Rui Ueyama62376782017-02-16 01:06:45 +0000729 MCSection *MCSec = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000730
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000731 // Mark this symbol as requiring an entry in the symbol table.
Rui Ueyama62376782017-02-16 01:06:45 +0000732 assert(SectionMap.find(MCSec) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000733 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000734
Rui Ueyama62376782017-02-16 01:06:45 +0000735 COFFSection *Sec = SectionMap[MCSec];
Rafael Espindolaed164772011-04-20 14:01:45 +0000736 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000737 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000738
David Majnemera45a1762014-01-06 07:39:46 +0000739 if (SymB) {
740 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000741 if (!B->getFragment()) {
742 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000743 Fixup.getLoc(),
744 Twine("symbol '") + B->getName() +
745 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000746 return;
747 }
David Majnemera45a1762014-01-06 07:39:46 +0000748
Oliver Stannard9be59af2015-11-17 10:00:43 +0000749 if (!A.getFragment()) {
750 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000751 Fixup.getLoc(),
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000752 Twine("symbol '") + A.getName() +
David Majnemera45a1762014-01-06 07:39:46 +0000753 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000754 return;
755 }
David Majnemera45a1762014-01-06 07:39:46 +0000756
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000757 CrossSection = &A.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000758
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000759 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000760 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000761
762 // In the case where we have SymbA and SymB, we just need to store the delta
763 // between the two symbols. Update FixedValue to account for the delta, and
764 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000765 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000766 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000767 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000768 return;
David Majnemer1de30942015-02-09 06:31:31 +0000769 }
770
771 // Offset of the relocation in the section
772 int64_t OffsetOfRelocation =
773 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
774
Andy Ayers9e5c8512015-05-14 01:10:41 +0000775 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000776 } else {
777 FixedValue = Target.getConstant();
778 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000779
780 COFFRelocation Reloc;
781
Daniel Dunbar727be432010-07-31 21:08:54 +0000782 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000783 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000784
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000785 // Turn relocations for temporary symbols into section relocations.
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000786 if (A.isTemporary() || CrossSection) {
787 MCSection *TargetSection = &A.getSection();
788 assert(
789 SectionMap.find(TargetSection) != SectionMap.end() &&
790 "Section must already have been defined in executePostLayoutBinding!");
791 Reloc.Symb = SectionMap[TargetSection]->Symbol;
792 FixedValue += Layout.getSymbolOffset(A);
793 } else {
794 assert(
795 SymbolMap.find(&A) != SymbolMap.end() &&
796 "Symbol must already have been defined in executePostLayoutBinding!");
797 Reloc.Symb = SymbolMap[&A];
798 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000799
800 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000801
802 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000803 Reloc.Data.Type = TargetObjectWriter->getRelocType(
804 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000805
806 // FIXME: Can anyone explain what this does other than adjust for the size
807 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000808 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
809 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
810 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
811 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000812 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000813
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000814 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
815 switch (Reloc.Data.Type) {
816 case COFF::IMAGE_REL_ARM_ABSOLUTE:
817 case COFF::IMAGE_REL_ARM_ADDR32:
818 case COFF::IMAGE_REL_ARM_ADDR32NB:
819 case COFF::IMAGE_REL_ARM_TOKEN:
820 case COFF::IMAGE_REL_ARM_SECTION:
821 case COFF::IMAGE_REL_ARM_SECREL:
822 break;
823 case COFF::IMAGE_REL_ARM_BRANCH11:
824 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000825 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
826 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
827 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000828 case COFF::IMAGE_REL_ARM_BRANCH24:
829 case COFF::IMAGE_REL_ARM_BLX24:
830 case COFF::IMAGE_REL_ARM_MOV32A:
831 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
832 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000833 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000834 // generate the relocations however the rest of the MSVC toolchain is
835 // unable to handle it.
836 llvm_unreachable("unsupported relocation");
837 break;
838 case COFF::IMAGE_REL_ARM_MOV32T:
839 break;
840 case COFF::IMAGE_REL_ARM_BRANCH20T:
841 case COFF::IMAGE_REL_ARM_BRANCH24T:
842 case COFF::IMAGE_REL_ARM_BLX23T:
843 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
844 // perform a 4 byte adjustment to the relocation. Relative branches are
845 // offset by 4 on ARM, however, because there is no RELA relocations, all
846 // branches are offset by 4.
847 FixedValue = FixedValue + 4;
848 break;
849 }
850 }
851
Simon Pilgrimf2fbf432016-11-20 13:47:59 +0000852 // The fixed value never makes sense for section indices, ignore it.
David Majnemer408b5e62016-02-05 01:55:49 +0000853 if (Fixup.getKind() == FK_SecRel_2)
854 FixedValue = 0;
855
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000856 if (TargetObjectWriter->recordRelocation(Fixup))
Rui Ueyama62376782017-02-16 01:06:45 +0000857 Sec->Relocations.push_back(Reloc);
858}
859
860static std::time_t getTime() {
861 std::time_t Now = time(nullptr);
862 if (Now < 0 || !isUInt<32>(Now))
863 return UINT32_MAX;
864 return Now;
Chris Lattner2c52b792010-07-11 22:07:02 +0000865}
866
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000867// Create .file symbols.
868void WinCOFFObjectWriter::createFileSymbols(MCAssembler &Asm) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000869 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000870 // round up to calculate the number of auxiliary symbols required
871 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000872 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000873
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000874 COFFSymbol *File = createSymbol(".file");
875 File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
876 File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
877 File->Aux.resize(Count);
David Majnemer4d571592014-09-15 19:42:42 +0000878
879 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000880 unsigned Length = Name.size();
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000881 for (auto &Aux : File->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000882 Aux.AuxType = ATFile;
883
884 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000885 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000886 Length = Length - SymbolSize;
887 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000888 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000889 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
890 break;
891 }
892
893 Offset += SymbolSize;
894 }
895 }
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000896}
897
898void WinCOFFObjectWriter::assignSectionNumbers() {
899 size_t I = 1;
900 for (const auto &Section : Sections) {
901 Section->Number = I;
902 Section->Symbol->Data.SectionNumber = I;
903 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = I;
904 ++I;
905 }
906}
907
908// Assign file offsets to COFF object file structures.
909void WinCOFFObjectWriter::assignFileOffsets(MCAssembler &Asm,
910 const MCAsmLayout &Layout) {
911 unsigned Offset = getInitialOffset();
912
913 Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
914 Offset += COFF::SectionSize * Header.NumberOfSections;
915
916 for (const auto &Section : Asm) {
917 COFFSection *Sec = SectionMap[&Section];
918
919 if (Sec->Number == -1)
920 continue;
921
922 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
923
924 if (IsPhysicalSection(Sec)) {
925 // Align the section data to a four byte boundary.
926 Offset = alignTo(Offset, 4);
927 Sec->Header.PointerToRawData = Offset;
928
929 Offset += Sec->Header.SizeOfRawData;
930 }
931
932 if (!Sec->Relocations.empty()) {
933 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
934
935 if (RelocationsOverflow) {
936 // Signal overflow by setting NumberOfRelocations to max value. Actual
937 // size is found in reloc #0. Microsoft tools understand this.
938 Sec->Header.NumberOfRelocations = 0xffff;
939 } else {
940 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
941 }
942 Sec->Header.PointerToRelocations = Offset;
943
944 if (RelocationsOverflow) {
945 // Reloc #0 will contain actual count, so make room for it.
946 Offset += COFF::RelocationSize;
947 }
948
949 Offset += COFF::RelocationSize * Sec->Relocations.size();
950
951 for (auto &Relocation : Sec->Relocations) {
952 assert(Relocation.Symb->getIndex() != -1);
953 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
954 }
955 }
956
957 assert(Sec->Symbol->Aux.size() == 1 &&
958 "Section's symbol must have one aux!");
959 AuxSymbol &Aux = Sec->Symbol->Aux[0];
960 assert(Aux.AuxType == ATSectionDefinition &&
961 "Section's symbol's aux symbol must be a Section Definition!");
962 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
963 Aux.Aux.SectionDefinition.NumberOfRelocations =
964 Sec->Header.NumberOfRelocations;
965 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
966 Sec->Header.NumberOfLineNumbers;
967 }
968
969 Header.PointerToSymbolTable = Offset;
970}
971
972void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
973 const MCAsmLayout &Layout) {
974 if (Sections.size() > INT32_MAX)
975 report_fatal_error(
976 "PE COFF object files can't have more than 2147483647 sections");
977
978 UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
979 Header.NumberOfSections = Sections.size();
980 Header.NumberOfSymbols = 0;
981
982 assignSectionNumbers();
983 createFileSymbols(Asm);
David Majnemer4d571592014-09-15 19:42:42 +0000984
David Majnemer9ab5ff12014-08-28 04:02:50 +0000985 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000986 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000987 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000988 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000989 Symbol->setIndex(Header.NumberOfSymbols++);
990 // Update auxiliary symbol info.
991 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
992 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000993 }
994
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000995 // Build string table.
996 for (const auto &S : Sections)
997 if (S->Name.size() > COFF::NameSize)
998 Strings.add(S->Name);
999 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +00001000 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001001 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +00001002 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001003
1004 // Set names.
1005 for (const auto &S : Sections)
1006 SetSectionName(*S);
1007 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +00001008 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001009
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001010 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +00001011 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001012 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +00001013 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001014 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
1015 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001016 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +00001017 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001018 }
1019 }
1020
Nico Riecka37acf72013-07-06 12:13:10 +00001021 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +00001022 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001023 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +00001024 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1025 continue;
1026
Rafael Espindolaf59264f2015-05-27 14:45:54 +00001027 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +00001028
Rafael Espindola0766ae02014-06-06 19:26:12 +00001029 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
1030 assert(COMDAT);
1031 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
1032 assert(COMDATSymbol);
1033 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001034 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +00001035 report_fatal_error(
1036 Twine("Missing associated COMDAT section for section ") +
1037 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +00001038
1039 // Skip this section if the associated section is unused.
1040 if (Assoc->Number == -1)
1041 continue;
1042
David Majnemer4eecd302015-05-30 04:56:02 +00001043 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +00001044 }
1045
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001046 assignFileOffsets(Asm, Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001047
David Majnemer088ba022015-09-01 23:46:11 +00001048 // MS LINK expects to be able to use this timestamp to implement their
1049 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +00001050 if (Asm.isIncrementalLinkerCompatible()) {
Rui Ueyama62376782017-02-16 01:06:45 +00001051 Header.TimeDateStamp = getTime();
David Majnemer03e2cc32015-12-21 22:09:27 +00001052 } else {
Nico Weber891419a2016-01-06 19:05:19 +00001053 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +00001054 Header.TimeDateStamp = 0;
1055 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +00001056
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001057 // Write it all to disk...
1058 WriteFileHeader(Header);
1059
Rui Ueyama62376782017-02-16 01:06:45 +00001060 for (auto &Section : Sections) {
1061 if (Section->Number != -1) {
1062 if (Section->Relocations.size() >= 0xffff)
1063 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
1064 writeSectionHeader(Section->Header);
1065 }
1066 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001067
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001068 // Write section contents.
1069 sections::iterator I = Sections.begin();
1070 sections::iterator IE = Sections.end();
1071 MCAssembler::iterator J = Asm.begin();
1072 MCAssembler::iterator JE = Asm.end();
1073 for (; I != IE && J != JE; ++I, ++J)
1074 writeSection(Asm, Layout, **I, *J);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001075
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001076 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001077 "Header::PointerToSymbolTable is insane!");
1078
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001079 // Write a symbol table.
Yaron Keren56919ef2014-12-04 08:30:39 +00001080 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001081 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001082 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001083
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001084 // Write a string table, which completes the entire COFF file.
Rafael Espindola39751af2016-10-04 22:43:25 +00001085 Strings.write(getStream());
Chris Lattner2c52b792010-07-11 22:07:02 +00001086}
1087
Rafael Espindola11e9e212015-05-27 14:37:12 +00001088MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1089 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001090
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001091// Pin the vtable to this file.
1092void MCWinCOFFObjectTargetWriter::anchor() {}
1093
Chris Lattner2c52b792010-07-11 22:07:02 +00001094//------------------------------------------------------------------------------
1095// WinCOFFObjectWriter factory function
1096
Rafael Espindola37099d92015-04-09 18:08:15 +00001097MCObjectWriter *
1098llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001099 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001100 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001101}