blob: 974668a3916c96a09eb524fc102cf72fab1e16c7 [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.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000339void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000340 MCAssembler &Assembler,
341 const MCAsmLayout &Layout) {
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000342 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000343 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
344 COFFSection *Sec = nullptr;
345 if (Base && Base->getFragment()) {
346 Sec = SectionMap[Base->getFragment()->getParent()];
347 if (coff_symbol->Section && coff_symbol->Section != Sec)
348 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;
Pete Cooper6bf1f302015-06-08 17:17:19 +0000352 if (cast<MCSymbolCOFF>(Symbol).isWeakExternal()) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000353 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
354
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000355 COFFSymbol *WeakDefault = getLinkedSymbol(Symbol);
356 if (!WeakDefault) {
Yaron Keren075759a2015-03-30 15:42:36 +0000357 std::string WeakName = (".weak." + Symbol.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
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000366 coff_symbol->Other = WeakDefault;
367
Michael J. Spencer17990d52010-10-16 08:25:57 +0000368 // Setup the Weak External auxiliary symbol.
369 coff_symbol->Aux.resize(1);
370 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
371 coff_symbol->Aux[0].AuxType = ATWeakExternal;
372 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
373 coff_symbol->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)
Reid Klecknerc1e76212013-09-17 23:18:05 +0000377 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola30c080a2016-05-26 18:48:23 +0000378 else
379 coff_symbol->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000380 Local = coff_symbol;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000381 }
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000382
383 if (Local) {
384 Local->Data.Value = getSymbolValue(Symbol, Layout);
385
386 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(Symbol);
387 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) {
392 bool IsExternal = Symbol.isExternal() ||
393 (!Symbol.getFragment() && !Symbol.isVariable());
394
395 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
396 : COFF::IMAGE_SYM_CLASS_STATIC;
397 }
398 }
399
400 coff_symbol->MC = &Symbol;
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) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000430 if (S.Name.size() > COFF::NameSize) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000431 uint64_t StringTableEntry = Strings.getOffset(S.Name);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000432
David Majnemerf088f522016-01-24 20:46:11 +0000433 if (StringTableEntry <= Max7DecimalOffset) {
434 SmallVector<char, COFF::NameSize> Buffer;
435 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
436 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
437
438 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
Nico Rieck01143f92014-02-25 09:50:40 +0000439 } else if (StringTableEntry <= MaxBase64Offset) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000440 // Starting with 10,000,000, offsets are encoded as base64.
441 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000442 } else {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000443 report_fatal_error("COFF string table is greater than 64 GB.");
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000444 }
David Majnemerf088f522016-01-24 20:46:11 +0000445 } else {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000446 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
David Majnemerf088f522016-01-24 20:46:11 +0000447 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000448}
449
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000450void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
451 if (S.Name.size() > COFF::NameSize)
452 S.set_name_offset(Strings.getOffset(S.Name));
453 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000454 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000455}
456
Michael J. Spencerd6283772010-09-27 08:58:26 +0000457bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000458 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
459 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000460}
461
462//------------------------------------------------------------------------------
463// entity writing methods
464
465void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000466 if (UseBigObj) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000467 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
468 writeLE16(0xFFFF);
469 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
470 writeLE16(Header.Machine);
471 writeLE32(Header.TimeDateStamp);
472 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
473 writeLE32(0);
474 writeLE32(0);
475 writeLE32(0);
476 writeLE32(0);
477 writeLE32(Header.NumberOfSections);
478 writeLE32(Header.PointerToSymbolTable);
479 writeLE32(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000480 } else {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000481 writeLE16(Header.Machine);
482 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
483 writeLE32(Header.TimeDateStamp);
484 writeLE32(Header.PointerToSymbolTable);
485 writeLE32(Header.NumberOfSymbols);
486 writeLE16(Header.SizeOfOptionalHeader);
487 writeLE16(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000488 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000489}
490
David Blaikief564ab62014-04-15 05:25:03 +0000491void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000492 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
493 writeLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000494 if (UseBigObj)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000495 writeLE32(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000496 else
Jim Grosbach36e60e92015-06-04 22:24:41 +0000497 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
498 writeLE16(S.Data.Type);
499 write8(S.Data.StorageClass);
500 write8(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000501 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000502}
503
504void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000505 const COFFSymbol::AuxiliarySymbols &S) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000506 for (const AuxSymbol &i : S) {
507 switch (i.AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000508 case ATFunctionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000509 writeLE32(i.Aux.FunctionDefinition.TagIndex);
510 writeLE32(i.Aux.FunctionDefinition.TotalSize);
511 writeLE32(i.Aux.FunctionDefinition.PointerToLinenumber);
512 writeLE32(i.Aux.FunctionDefinition.PointerToNextFunction);
513 WriteZeros(sizeof(i.Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000514 if (UseBigObj)
515 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000516 break;
517 case ATbfAndefSymbol:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000518 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused1));
519 writeLE16(i.Aux.bfAndefSymbol.Linenumber);
520 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused2));
521 writeLE32(i.Aux.bfAndefSymbol.PointerToNextFunction);
522 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused3));
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 ATWeakExternal:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000527 writeLE32(i.Aux.WeakExternal.TagIndex);
528 writeLE32(i.Aux.WeakExternal.Characteristics);
529 WriteZeros(sizeof(i.Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000530 if (UseBigObj)
531 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000532 break;
533 case ATFile:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000534 writeBytes(
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000535 StringRef(reinterpret_cast<const char *>(&i.Aux),
David Majnemer4d571592014-09-15 19:42:42 +0000536 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000537 break;
538 case ATSectionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000539 writeLE32(i.Aux.SectionDefinition.Length);
540 writeLE16(i.Aux.SectionDefinition.NumberOfRelocations);
541 writeLE16(i.Aux.SectionDefinition.NumberOfLinenumbers);
542 writeLE32(i.Aux.SectionDefinition.CheckSum);
543 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
544 write8(i.Aux.SectionDefinition.Selection);
545 WriteZeros(sizeof(i.Aux.SectionDefinition.unused));
546 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000547 if (UseBigObj)
548 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000549 break;
550 }
551 }
552}
553
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000554void WinCOFFObjectWriter::writeSectionHeader(const COFF::section &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000555 writeBytes(StringRef(S.Name, COFF::NameSize));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000556
Jim Grosbach36e60e92015-06-04 22:24:41 +0000557 writeLE32(S.VirtualSize);
558 writeLE32(S.VirtualAddress);
559 writeLE32(S.SizeOfRawData);
560 writeLE32(S.PointerToRawData);
561 writeLE32(S.PointerToRelocations);
562 writeLE32(S.PointerToLineNumbers);
563 writeLE16(S.NumberOfRelocations);
564 writeLE16(S.NumberOfLineNumbers);
565 writeLE32(S.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000566}
567
568void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000569 writeLE32(R.VirtualAddress);
570 writeLE32(R.SymbolTableIndex);
571 writeLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000572}
573
574////////////////////////////////////////////////////////////////////////////////
575// MCObjectWriter interface implementations
576
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000577void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000578 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000579 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000580 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000581 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000582 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000583
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000584 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000585 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000586 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000587}
588
Jim Grosbach36e60e92015-06-04 22:24:41 +0000589bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000590 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000591 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000592 // MS LINK expects to be able to replace all references to a function with a
593 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
594 // away any relocations to functions.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000595 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
David Majnemer03e2cc32015-12-21 22:09:27 +0000596 if (Asm.isIncrementalLinkerCompatible() &&
597 (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000598 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000599 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000600 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000601}
602
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000603bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000604 if (!Sym.isExternal())
Rafael Espindola88af4112015-04-17 11:27:13 +0000605 return false;
606
Rafael Espindola88af4112015-04-17 11:27:13 +0000607 if (!Sym.isInSection())
608 return false;
609
610 const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
611 if (!Sec.getCOMDATSymbol())
612 return false;
613
614 // It looks like for COFF it is invalid to replace a reference to a global
615 // in a comdat with a reference to a local.
616 // FIXME: Add a specification reference if available.
617 return true;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000618}
619
Jim Grosbach36e60e92015-06-04 22:24:41 +0000620void WinCOFFObjectWriter::recordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000621 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
622 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000623 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000624
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000625 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000626 if (!A.isRegistered()) {
627 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000628 Twine("symbol '") + A.getName() +
629 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000630 return;
631 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000632 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000633 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000634 Twine("assembler label '") + A.getName() +
635 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000636 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000637 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000638
Rafael Espindola7549f872015-05-26 00:36:57 +0000639 MCSection *Section = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000640
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000641 // Mark this symbol as requiring an entry in the symbol table.
Rafael Espindola7549f872015-05-26 00:36:57 +0000642 assert(SectionMap.find(Section) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000643 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000644
Rafael Espindola7549f872015-05-26 00:36:57 +0000645 COFFSection *coff_section = SectionMap[Section];
Rafael Espindolaed164772011-04-20 14:01:45 +0000646 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000647 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000648
David Majnemera45a1762014-01-06 07:39:46 +0000649 if (SymB) {
650 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000651 if (!B->getFragment()) {
652 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000653 Fixup.getLoc(),
654 Twine("symbol '") + B->getName() +
655 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000656 return;
657 }
David Majnemera45a1762014-01-06 07:39:46 +0000658
Oliver Stannard9be59af2015-11-17 10:00:43 +0000659 if (!A.getFragment()) {
660 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000661 Fixup.getLoc(),
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000662 Twine("symbol '") + A.getName() +
David Majnemera45a1762014-01-06 07:39:46 +0000663 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000664 return;
665 }
David Majnemera45a1762014-01-06 07:39:46 +0000666
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000667 CrossSection = &A.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000668
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000669 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000670 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000671
672 // In the case where we have SymbA and SymB, we just need to store the delta
673 // between the two symbols. Update FixedValue to account for the delta, and
674 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000675 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000676 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000677 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000678 return;
David Majnemer1de30942015-02-09 06:31:31 +0000679 }
680
681 // Offset of the relocation in the section
682 int64_t OffsetOfRelocation =
683 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
684
Andy Ayers9e5c8512015-05-14 01:10:41 +0000685 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000686 } else {
687 FixedValue = Target.getConstant();
688 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000689
690 COFFRelocation Reloc;
691
Daniel Dunbar727be432010-07-31 21:08:54 +0000692 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000693 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000694
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000695 // Turn relocations for temporary symbols into section relocations.
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000696 if (A.isTemporary() || CrossSection) {
697 MCSection *TargetSection = &A.getSection();
698 assert(
699 SectionMap.find(TargetSection) != SectionMap.end() &&
700 "Section must already have been defined in executePostLayoutBinding!");
701 Reloc.Symb = SectionMap[TargetSection]->Symbol;
702 FixedValue += Layout.getSymbolOffset(A);
703 } else {
704 assert(
705 SymbolMap.find(&A) != SymbolMap.end() &&
706 "Symbol must already have been defined in executePostLayoutBinding!");
707 Reloc.Symb = SymbolMap[&A];
708 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000709
710 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000711
712 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000713 Reloc.Data.Type = TargetObjectWriter->getRelocType(
714 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000715
716 // FIXME: Can anyone explain what this does other than adjust for the size
717 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000718 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
719 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
720 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
721 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000722 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000723
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000724 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
725 switch (Reloc.Data.Type) {
726 case COFF::IMAGE_REL_ARM_ABSOLUTE:
727 case COFF::IMAGE_REL_ARM_ADDR32:
728 case COFF::IMAGE_REL_ARM_ADDR32NB:
729 case COFF::IMAGE_REL_ARM_TOKEN:
730 case COFF::IMAGE_REL_ARM_SECTION:
731 case COFF::IMAGE_REL_ARM_SECREL:
732 break;
733 case COFF::IMAGE_REL_ARM_BRANCH11:
734 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000735 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
736 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
737 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000738 case COFF::IMAGE_REL_ARM_BRANCH24:
739 case COFF::IMAGE_REL_ARM_BLX24:
740 case COFF::IMAGE_REL_ARM_MOV32A:
741 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
742 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000743 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000744 // generate the relocations however the rest of the MSVC toolchain is
745 // unable to handle it.
746 llvm_unreachable("unsupported relocation");
747 break;
748 case COFF::IMAGE_REL_ARM_MOV32T:
749 break;
750 case COFF::IMAGE_REL_ARM_BRANCH20T:
751 case COFF::IMAGE_REL_ARM_BRANCH24T:
752 case COFF::IMAGE_REL_ARM_BLX23T:
753 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
754 // perform a 4 byte adjustment to the relocation. Relative branches are
755 // offset by 4 on ARM, however, because there is no RELA relocations, all
756 // branches are offset by 4.
757 FixedValue = FixedValue + 4;
758 break;
759 }
760 }
761
Simon Pilgrimf2fbf432016-11-20 13:47:59 +0000762 // The fixed value never makes sense for section indices, ignore it.
David Majnemer408b5e62016-02-05 01:55:49 +0000763 if (Fixup.getKind() == FK_SecRel_2)
764 FixedValue = 0;
765
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000766 if (TargetObjectWriter->recordRelocation(Fixup))
767 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000768}
769
Jim Grosbach36e60e92015-06-04 22:24:41 +0000770void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000771 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000772 size_t SectionsSize = Sections.size();
773 if (SectionsSize > static_cast<size_t>(INT32_MAX))
774 report_fatal_error(
775 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000776
David Majnemer4d571592014-09-15 19:42:42 +0000777 // Assign symbol and section indexes and offsets.
778 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
779
780 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
781
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000782 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000783 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000784 for (const auto &Section : Sections) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000785 Section->Number = Number;
786 Section->Symbol->Data.SectionNumber = Number;
787 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000788 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000789 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000790
David Majnemer4d571592014-09-15 19:42:42 +0000791 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000792 Header.NumberOfSymbols = 0;
793
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000794 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000795 // round up to calculate the number of auxiliary symbols required
796 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000797 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000798
799 COFFSymbol *file = createSymbol(".file");
800 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
801 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
802 file->Aux.resize(Count);
803
804 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000805 unsigned Length = Name.size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000806 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000807 Aux.AuxType = ATFile;
808
809 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000810 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000811 Length = Length - SymbolSize;
812 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000813 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000814 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
815 break;
816 }
817
818 Offset += SymbolSize;
819 }
820 }
821
David Majnemer9ab5ff12014-08-28 04:02:50 +0000822 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000823 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000824 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000825 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000826 Symbol->setIndex(Header.NumberOfSymbols++);
827 // Update auxiliary symbol info.
828 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
829 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000830 }
831
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000832 // Build string table.
833 for (const auto &S : Sections)
834 if (S->Name.size() > COFF::NameSize)
835 Strings.add(S->Name);
836 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000837 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000838 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000839 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000840
841 // Set names.
842 for (const auto &S : Sections)
843 SetSectionName(*S);
844 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000845 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000846
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000847 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000848 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000849 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000850 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000851 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
852 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000853 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000854 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000855 }
856 }
857
Nico Riecka37acf72013-07-06 12:13:10 +0000858 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000859 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000860 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000861 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
862 continue;
863
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000864 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +0000865
Rafael Espindola0766ae02014-06-06 19:26:12 +0000866 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
867 assert(COMDAT);
868 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
869 assert(COMDATSymbol);
870 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000871 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000872 report_fatal_error(
873 Twine("Missing associated COMDAT section for section ") +
874 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000875
876 // Skip this section if the associated section is unused.
877 if (Assoc->Number == -1)
878 continue;
879
David Majnemer4eecd302015-05-30 04:56:02 +0000880 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +0000881 }
882
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000883 // Assign file offsets to COFF object file structures.
884
Manuel Klimek272d3f12015-11-18 15:24:17 +0000885 unsigned offset = getInitialOffset();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000886
David Majnemer4d571592014-09-15 19:42:42 +0000887 if (UseBigObj)
888 offset += COFF::Header32Size;
889 else
890 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000891 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000892
Yaron Keren56919ef2014-12-04 08:30:39 +0000893 for (const auto &Section : Asm) {
Rafael Espindolaa554c052015-05-25 23:14:17 +0000894 COFFSection *Sec = SectionMap[&Section];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000895
Michael J. Spencerd6283772010-09-27 08:58:26 +0000896 if (Sec->Number == -1)
897 continue;
898
Rafael Espindola5a1e80b2015-05-26 02:00:36 +0000899 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000900
Michael J. Spencerd6283772010-09-27 08:58:26 +0000901 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000902 // Align the section data to a four byte boundary.
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000903 offset = alignTo(offset, 4);
David Majnemerab2b25b2015-02-11 22:51:55 +0000904 Sec->Header.PointerToRawData = offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000905
906 offset += Sec->Header.SizeOfRawData;
907 }
908
Eugene Zelenko1d435522017-02-07 23:02:00 +0000909 if (!Sec->Relocations.empty()) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000910 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
911
912 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000913 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000914 // size is found in reloc #0. Microsoft tools understand this.
915 Sec->Header.NumberOfRelocations = 0xffff;
916 } else {
917 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
918 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000919 Sec->Header.PointerToRelocations = offset;
920
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000921 if (RelocationsOverflow) {
922 // Reloc #0 will contain actual count, so make room for it.
923 offset += COFF::RelocationSize;
924 }
925
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000926 offset += COFF::RelocationSize * Sec->Relocations.size();
927
Yaron Keren56919ef2014-12-04 08:30:39 +0000928 for (auto &Relocation : Sec->Relocations) {
David Majnemer4eecd302015-05-30 04:56:02 +0000929 assert(Relocation.Symb->getIndex() != -1);
930 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000931 }
932 }
933
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000934 assert(Sec->Symbol->Aux.size() == 1 &&
935 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000936 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000937 assert(Aux.AuxType == ATSectionDefinition &&
938 "Section's symbol's aux symbol must be a Section Definition!");
939 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
940 Aux.Aux.SectionDefinition.NumberOfRelocations =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000941 Sec->Header.NumberOfRelocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000942 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000943 Sec->Header.NumberOfLineNumbers;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000944 }
945
946 Header.PointerToSymbolTable = offset;
947
David Majnemer088ba022015-09-01 23:46:11 +0000948 // MS LINK expects to be able to use this timestamp to implement their
949 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +0000950 if (Asm.isIncrementalLinkerCompatible()) {
951 std::time_t Now = time(nullptr);
952 if (Now < 0 || !isUInt<32>(Now))
953 Now = UINT32_MAX;
954 Header.TimeDateStamp = Now;
955 } else {
Nico Weber891419a2016-01-06 19:05:19 +0000956 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +0000957 Header.TimeDateStamp = 0;
958 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +0000959
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000960 // Write it all to disk...
961 WriteFileHeader(Header);
962
963 {
964 sections::iterator i, ie;
Rafael Espindola63702e22015-06-01 01:30:01 +0000965 MCAssembler::iterator j, je;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000966
Yaron Keren56919ef2014-12-04 08:30:39 +0000967 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000968 if (Section->Number != -1) {
969 if (Section->Relocations.size() >= 0xffff)
970 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000971 writeSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000972 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000973 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000974
David Majnemer6ddc6362015-09-01 21:23:58 +0000975 SmallVector<char, 128> SectionContents;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000976 for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
977 je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000978 (i != ie) && (j != je); ++i, ++j) {
979
980 if ((*i)->Number == -1)
981 continue;
982
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000983 if ((*i)->Header.PointerToRawData != 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +0000984 assert(getStream().tell() <= (*i)->Header.PointerToRawData &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000985 "Section::PointerToRawData is insane!");
986
David Majnemerabdb2d2a2015-09-01 16:19:03 +0000987 unsigned SectionDataPadding =
988 (*i)->Header.PointerToRawData - getStream().tell();
David Majnemer3df3c612015-02-11 22:22:30 +0000989 assert(SectionDataPadding < 4 &&
990 "Should only need at most three bytes of padding!");
991
992 WriteZeros(SectionDataPadding);
993
David Majnemer6ddc6362015-09-01 21:23:58 +0000994 // Save the contents of the section to a temporary buffer, we need this
995 // to CRC the data before we dump it into the object file.
996 SectionContents.clear();
997 raw_svector_ostream VecOS(SectionContents);
998 raw_pwrite_stream &OldStream = getStream();
999 // Redirect the output stream to our buffer.
1000 setStream(VecOS);
1001 // Fill our buffer with the section data.
Rafael Espindola64acc7f2015-05-26 02:17:21 +00001002 Asm.writeSectionData(&*j, Layout);
David Majnemer6ddc6362015-09-01 21:23:58 +00001003 // Reset the stream back to what it was before.
1004 setStream(OldStream);
1005
1006 // Calculate our CRC with an initial value of '0', this is not how
1007 // JamCRC is specified but it aligns with the expected output.
1008 JamCRC JC(/*Init=*/0x00000000U);
1009 JC.update(SectionContents);
1010
1011 // Write the section contents to the object file.
1012 getStream() << SectionContents;
1013
1014 // Update the section definition auxiliary symbol to record the CRC.
1015 COFFSection *Sec = SectionMap[&*j];
1016 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
1017 assert(AuxSyms.size() == 1 &&
1018 AuxSyms[0].AuxType == ATSectionDefinition);
1019 AuxSymbol &SecDef = AuxSyms[0];
1020 SecDef.Aux.SectionDefinition.CheckSum = JC.getCRC();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001021 }
1022
Eugene Zelenko1d435522017-02-07 23:02:00 +00001023 if (!(*i)->Relocations.empty()) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001024 assert(getStream().tell() == (*i)->Header.PointerToRelocations &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001025 "Section::PointerToRelocations is insane!");
1026
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001027 if ((*i)->Relocations.size() >= 0xffff) {
1028 // In case of overflow, write actual relocation count as first
1029 // relocation. Including the synthetic reloc itself (+ 1).
1030 COFF::relocation r;
1031 r.VirtualAddress = (*i)->Relocations.size() + 1;
1032 r.SymbolTableIndex = 0;
1033 r.Type = 0;
1034 WriteRelocation(r);
1035 }
1036
Yaron Keren56919ef2014-12-04 08:30:39 +00001037 for (const auto &Relocation : (*i)->Relocations)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001038 WriteRelocation(Relocation.Data);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001039 } else
1040 assert((*i)->Header.PointerToRelocations == 0 &&
1041 "Section::PointerToRelocations is insane!");
1042 }
1043 }
1044
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001045 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001046 "Header::PointerToSymbolTable is insane!");
1047
Yaron Keren56919ef2014-12-04 08:30:39 +00001048 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001049 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001050 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001051
Rafael Espindola39751af2016-10-04 22:43:25 +00001052 Strings.write(getStream());
Chris Lattner2c52b792010-07-11 22:07:02 +00001053}
1054
Rafael Espindola11e9e212015-05-27 14:37:12 +00001055MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1056 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001057
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001058// Pin the vtable to this file.
1059void MCWinCOFFObjectTargetWriter::anchor() {}
1060
Chris Lattner2c52b792010-07-11 22:07:02 +00001061//------------------------------------------------------------------------------
1062// WinCOFFObjectWriter factory function
1063
Rafael Espindola37099d92015-04-09 18:08:15 +00001064MCObjectWriter *
1065llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001066 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001067 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001068}