blob: b5dac8233910101d98a55a714a280a75273f2c32 [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 Ueyama24e27b42017-02-15 00:15:54 +0000278void WinCOFFObjectWriter::defineSection(const MCSectionCOFF &Sec) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000279 COFFSection *coff_section = createSection(Sec.getSectionName());
Rafael Espindola11e9e212015-05-27 14:37:12 +0000280 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
Rui Ueyama24e27b42017-02-15 00:15:54 +0000281
282 // Create a COMDAT symbol if needed.
Rafael Espindola0766ae02014-06-06 19:26:12 +0000283 if (Sec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
284 if (const MCSymbol *S = Sec.getCOMDATSymbol()) {
285 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
286 if (COMDATSymbol->Section)
287 report_fatal_error("two sections have the same comdat");
288 COMDATSymbol->Section = coff_section;
289 }
290 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000291
Michael J. Spencerd6283772010-09-27 08:58:26 +0000292 coff_section->Symbol = coff_symbol;
293 coff_symbol->Section = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000294 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000295
296 // In this case the auxiliary symbol is a Section Definition.
297 coff_symbol->Aux.resize(1);
298 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
299 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000300 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
301
302 coff_section->Header.Characteristics = Sec.getCharacteristics();
303
304 uint32_t &Characteristics = coff_section->Header.Characteristics;
Rui Ueyama24e27b42017-02-15 00:15:54 +0000305 Characteristics |= getAlignment(Sec);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000306
307 // Bind internal COFF section to MC section.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000308 coff_section->MCSection = &Sec;
309 SectionMap[&Sec] = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000310}
311
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000312static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000313 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000314 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000315 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000316
317 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000318 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000319 return 0;
320
321 return Res;
322}
323
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000324COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
325 if (!Symbol.isVariable())
326 return nullptr;
327
328 const MCSymbolRefExpr *SymRef =
329 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
330 if (!SymRef)
331 return nullptr;
332
333 const MCSymbol &Aliasee = SymRef->getSymbol();
334 if (!Aliasee.isUndefined())
335 return nullptr;
336 return GetOrCreateCOFFSymbol(&Aliasee);
337}
338
David Majnemera9bdb322014-04-08 22:33:40 +0000339/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000340/// and creates the associated COFF symbol staging object.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000341void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000342 MCAssembler &Assembler,
343 const MCAsmLayout &Layout) {
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000344 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000345 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
346 COFFSection *Sec = nullptr;
347 if (Base && Base->getFragment()) {
348 Sec = SectionMap[Base->getFragment()->getParent()];
349 if (coff_symbol->Section && coff_symbol->Section != Sec)
350 report_fatal_error("conflicting sections for symbol");
351 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000352
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000353 COFFSymbol *Local = nullptr;
Pete Cooper6bf1f302015-06-08 17:17:19 +0000354 if (cast<MCSymbolCOFF>(Symbol).isWeakExternal()) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000355 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
356
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000357 COFFSymbol *WeakDefault = getLinkedSymbol(Symbol);
358 if (!WeakDefault) {
Yaron Keren075759a2015-03-30 15:42:36 +0000359 std::string WeakName = (".weak." + Symbol.getName() + ".default").str();
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000360 WeakDefault = createSymbol(WeakName);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000361 if (!Sec)
362 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
363 else
364 WeakDefault->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000365 Local = WeakDefault;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000366 }
367
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000368 coff_symbol->Other = WeakDefault;
369
Michael J. Spencer17990d52010-10-16 08:25:57 +0000370 // Setup the Weak External auxiliary symbol.
371 coff_symbol->Aux.resize(1);
372 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
373 coff_symbol->Aux[0].AuxType = ATWeakExternal;
374 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
375 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000376 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000377 } else {
Rafael Espindola30c080a2016-05-26 18:48:23 +0000378 if (!Base)
Reid Klecknerc1e76212013-09-17 23:18:05 +0000379 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola30c080a2016-05-26 18:48:23 +0000380 else
381 coff_symbol->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000382 Local = coff_symbol;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000383 }
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000384
385 if (Local) {
386 Local->Data.Value = getSymbolValue(Symbol, Layout);
387
388 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(Symbol);
389 Local->Data.Type = SymbolCOFF.getType();
390 Local->Data.StorageClass = SymbolCOFF.getClass();
391
392 // If no storage class was specified in the streamer, define it here.
393 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
394 bool IsExternal = Symbol.isExternal() ||
395 (!Symbol.getFragment() && !Symbol.isVariable());
396
397 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
398 : COFF::IMAGE_SYM_CLASS_STATIC;
399 }
400 }
401
402 coff_symbol->MC = &Symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000403}
404
Nico Rieck01143f92014-02-25 09:50:40 +0000405// Maximum offsets for different string table entry encodings.
David Majnemerf088f522016-01-24 20:46:11 +0000406enum : unsigned { Max7DecimalOffset = 9999999U };
407enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
Nico Rieck01143f92014-02-25 09:50:40 +0000408
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000409// Encode a string table entry offset in base 64, padded to 6 chars, and
410// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
411// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000412static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000413 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000414 "Illegal section name encoding for value");
415
416 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
417 "abcdefghijklmnopqrstuvwxyz"
418 "0123456789+/";
419
420 Buffer[0] = '/';
421 Buffer[1] = '/';
422
Rafael Espindola11e9e212015-05-27 14:37:12 +0000423 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000424 for (unsigned i = 0; i < 6; ++i) {
425 unsigned Rem = Value % 64;
426 Value /= 64;
427 *(Ptr--) = Alphabet[Rem];
428 }
429}
430
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000431void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000432 if (S.Name.size() > COFF::NameSize) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000433 uint64_t StringTableEntry = Strings.getOffset(S.Name);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000434
David Majnemerf088f522016-01-24 20:46:11 +0000435 if (StringTableEntry <= Max7DecimalOffset) {
436 SmallVector<char, COFF::NameSize> Buffer;
437 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
438 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
439
440 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
Nico Rieck01143f92014-02-25 09:50:40 +0000441 } else if (StringTableEntry <= MaxBase64Offset) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000442 // Starting with 10,000,000, offsets are encoded as base64.
443 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000444 } else {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000445 report_fatal_error("COFF string table is greater than 64 GB.");
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000446 }
David Majnemerf088f522016-01-24 20:46:11 +0000447 } else {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000448 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
David Majnemerf088f522016-01-24 20:46:11 +0000449 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000450}
451
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000452void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
453 if (S.Name.size() > COFF::NameSize)
454 S.set_name_offset(Strings.getOffset(S.Name));
455 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000456 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000457}
458
Michael J. Spencerd6283772010-09-27 08:58:26 +0000459bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000460 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
461 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000462}
463
464//------------------------------------------------------------------------------
465// entity writing methods
466
467void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000468 if (UseBigObj) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000469 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
470 writeLE16(0xFFFF);
471 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
472 writeLE16(Header.Machine);
473 writeLE32(Header.TimeDateStamp);
474 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
475 writeLE32(0);
476 writeLE32(0);
477 writeLE32(0);
478 writeLE32(0);
479 writeLE32(Header.NumberOfSections);
480 writeLE32(Header.PointerToSymbolTable);
481 writeLE32(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000482 } else {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000483 writeLE16(Header.Machine);
484 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
485 writeLE32(Header.TimeDateStamp);
486 writeLE32(Header.PointerToSymbolTable);
487 writeLE32(Header.NumberOfSymbols);
488 writeLE16(Header.SizeOfOptionalHeader);
489 writeLE16(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000490 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000491}
492
David Blaikief564ab62014-04-15 05:25:03 +0000493void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000494 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
495 writeLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000496 if (UseBigObj)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000497 writeLE32(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000498 else
Jim Grosbach36e60e92015-06-04 22:24:41 +0000499 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
500 writeLE16(S.Data.Type);
501 write8(S.Data.StorageClass);
502 write8(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000503 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000504}
505
506void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000507 const COFFSymbol::AuxiliarySymbols &S) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000508 for (const AuxSymbol &i : S) {
509 switch (i.AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000510 case ATFunctionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000511 writeLE32(i.Aux.FunctionDefinition.TagIndex);
512 writeLE32(i.Aux.FunctionDefinition.TotalSize);
513 writeLE32(i.Aux.FunctionDefinition.PointerToLinenumber);
514 writeLE32(i.Aux.FunctionDefinition.PointerToNextFunction);
515 WriteZeros(sizeof(i.Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000516 if (UseBigObj)
517 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000518 break;
519 case ATbfAndefSymbol:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000520 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused1));
521 writeLE16(i.Aux.bfAndefSymbol.Linenumber);
522 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused2));
523 writeLE32(i.Aux.bfAndefSymbol.PointerToNextFunction);
524 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000525 if (UseBigObj)
526 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000527 break;
528 case ATWeakExternal:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000529 writeLE32(i.Aux.WeakExternal.TagIndex);
530 writeLE32(i.Aux.WeakExternal.Characteristics);
531 WriteZeros(sizeof(i.Aux.WeakExternal.unused));
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 ATFile:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000536 writeBytes(
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000537 StringRef(reinterpret_cast<const char *>(&i.Aux),
David Majnemer4d571592014-09-15 19:42:42 +0000538 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000539 break;
540 case ATSectionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000541 writeLE32(i.Aux.SectionDefinition.Length);
542 writeLE16(i.Aux.SectionDefinition.NumberOfRelocations);
543 writeLE16(i.Aux.SectionDefinition.NumberOfLinenumbers);
544 writeLE32(i.Aux.SectionDefinition.CheckSum);
545 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
546 write8(i.Aux.SectionDefinition.Selection);
547 WriteZeros(sizeof(i.Aux.SectionDefinition.unused));
548 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000549 if (UseBigObj)
550 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000551 break;
552 }
553 }
554}
555
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000556void WinCOFFObjectWriter::writeSectionHeader(const COFF::section &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000557 writeBytes(StringRef(S.Name, COFF::NameSize));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000558
Jim Grosbach36e60e92015-06-04 22:24:41 +0000559 writeLE32(S.VirtualSize);
560 writeLE32(S.VirtualAddress);
561 writeLE32(S.SizeOfRawData);
562 writeLE32(S.PointerToRawData);
563 writeLE32(S.PointerToRelocations);
564 writeLE32(S.PointerToLineNumbers);
565 writeLE16(S.NumberOfRelocations);
566 writeLE16(S.NumberOfLineNumbers);
567 writeLE32(S.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000568}
569
570void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000571 writeLE32(R.VirtualAddress);
572 writeLE32(R.SymbolTableIndex);
573 writeLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000574}
575
576////////////////////////////////////////////////////////////////////////////////
577// MCObjectWriter interface implementations
578
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000579void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000580 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000581 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000582 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000583 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000584 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000585
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000586 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000587 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000588 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000589}
590
Jim Grosbach36e60e92015-06-04 22:24:41 +0000591bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000592 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000593 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000594 // MS LINK expects to be able to replace all references to a function with a
595 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
596 // away any relocations to functions.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000597 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
David Majnemer03e2cc32015-12-21 22:09:27 +0000598 if (Asm.isIncrementalLinkerCompatible() &&
599 (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000600 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000601 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000602 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000603}
604
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000605bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000606 if (!Sym.isExternal())
Rafael Espindola88af4112015-04-17 11:27:13 +0000607 return false;
608
Rafael Espindola88af4112015-04-17 11:27:13 +0000609 if (!Sym.isInSection())
610 return false;
611
612 const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
613 if (!Sec.getCOMDATSymbol())
614 return false;
615
616 // It looks like for COFF it is invalid to replace a reference to a global
617 // in a comdat with a reference to a local.
618 // FIXME: Add a specification reference if available.
619 return true;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000620}
621
Jim Grosbach36e60e92015-06-04 22:24:41 +0000622void WinCOFFObjectWriter::recordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000623 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
624 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000625 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000626
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000627 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000628 if (!A.isRegistered()) {
629 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000630 Twine("symbol '") + A.getName() +
631 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000632 return;
633 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000634 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000635 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000636 Twine("assembler label '") + A.getName() +
637 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000638 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000639 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000640
Rafael Espindola7549f872015-05-26 00:36:57 +0000641 MCSection *Section = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000642
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000643 // Mark this symbol as requiring an entry in the symbol table.
Rafael Espindola7549f872015-05-26 00:36:57 +0000644 assert(SectionMap.find(Section) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000645 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000646
Rafael Espindola7549f872015-05-26 00:36:57 +0000647 COFFSection *coff_section = SectionMap[Section];
Rafael Espindolaed164772011-04-20 14:01:45 +0000648 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000649 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000650
David Majnemera45a1762014-01-06 07:39:46 +0000651 if (SymB) {
652 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000653 if (!B->getFragment()) {
654 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000655 Fixup.getLoc(),
656 Twine("symbol '") + B->getName() +
657 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000658 return;
659 }
David Majnemera45a1762014-01-06 07:39:46 +0000660
Oliver Stannard9be59af2015-11-17 10:00:43 +0000661 if (!A.getFragment()) {
662 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000663 Fixup.getLoc(),
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000664 Twine("symbol '") + A.getName() +
David Majnemera45a1762014-01-06 07:39:46 +0000665 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000666 return;
667 }
David Majnemera45a1762014-01-06 07:39:46 +0000668
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000669 CrossSection = &A.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000670
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000671 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000672 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000673
674 // In the case where we have SymbA and SymB, we just need to store the delta
675 // between the two symbols. Update FixedValue to account for the delta, and
676 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000677 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000678 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000679 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000680 return;
David Majnemer1de30942015-02-09 06:31:31 +0000681 }
682
683 // Offset of the relocation in the section
684 int64_t OffsetOfRelocation =
685 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
686
Andy Ayers9e5c8512015-05-14 01:10:41 +0000687 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000688 } else {
689 FixedValue = Target.getConstant();
690 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000691
692 COFFRelocation Reloc;
693
Daniel Dunbar727be432010-07-31 21:08:54 +0000694 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000695 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000696
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000697 // Turn relocations for temporary symbols into section relocations.
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000698 if (A.isTemporary() || CrossSection) {
699 MCSection *TargetSection = &A.getSection();
700 assert(
701 SectionMap.find(TargetSection) != SectionMap.end() &&
702 "Section must already have been defined in executePostLayoutBinding!");
703 Reloc.Symb = SectionMap[TargetSection]->Symbol;
704 FixedValue += Layout.getSymbolOffset(A);
705 } else {
706 assert(
707 SymbolMap.find(&A) != SymbolMap.end() &&
708 "Symbol must already have been defined in executePostLayoutBinding!");
709 Reloc.Symb = SymbolMap[&A];
710 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000711
712 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000713
714 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000715 Reloc.Data.Type = TargetObjectWriter->getRelocType(
716 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000717
718 // FIXME: Can anyone explain what this does other than adjust for the size
719 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000720 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
721 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
722 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
723 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000724 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000725
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000726 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
727 switch (Reloc.Data.Type) {
728 case COFF::IMAGE_REL_ARM_ABSOLUTE:
729 case COFF::IMAGE_REL_ARM_ADDR32:
730 case COFF::IMAGE_REL_ARM_ADDR32NB:
731 case COFF::IMAGE_REL_ARM_TOKEN:
732 case COFF::IMAGE_REL_ARM_SECTION:
733 case COFF::IMAGE_REL_ARM_SECREL:
734 break;
735 case COFF::IMAGE_REL_ARM_BRANCH11:
736 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000737 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
738 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
739 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000740 case COFF::IMAGE_REL_ARM_BRANCH24:
741 case COFF::IMAGE_REL_ARM_BLX24:
742 case COFF::IMAGE_REL_ARM_MOV32A:
743 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
744 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000745 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000746 // generate the relocations however the rest of the MSVC toolchain is
747 // unable to handle it.
748 llvm_unreachable("unsupported relocation");
749 break;
750 case COFF::IMAGE_REL_ARM_MOV32T:
751 break;
752 case COFF::IMAGE_REL_ARM_BRANCH20T:
753 case COFF::IMAGE_REL_ARM_BRANCH24T:
754 case COFF::IMAGE_REL_ARM_BLX23T:
755 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
756 // perform a 4 byte adjustment to the relocation. Relative branches are
757 // offset by 4 on ARM, however, because there is no RELA relocations, all
758 // branches are offset by 4.
759 FixedValue = FixedValue + 4;
760 break;
761 }
762 }
763
Simon Pilgrimf2fbf432016-11-20 13:47:59 +0000764 // The fixed value never makes sense for section indices, ignore it.
David Majnemer408b5e62016-02-05 01:55:49 +0000765 if (Fixup.getKind() == FK_SecRel_2)
766 FixedValue = 0;
767
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000768 if (TargetObjectWriter->recordRelocation(Fixup))
769 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000770}
771
Jim Grosbach36e60e92015-06-04 22:24:41 +0000772void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000773 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000774 size_t SectionsSize = Sections.size();
775 if (SectionsSize > static_cast<size_t>(INT32_MAX))
776 report_fatal_error(
777 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000778
David Majnemer4d571592014-09-15 19:42:42 +0000779 // Assign symbol and section indexes and offsets.
780 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
781
782 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
783
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000784 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000785 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000786 for (const auto &Section : Sections) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000787 Section->Number = Number;
788 Section->Symbol->Data.SectionNumber = Number;
789 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000790 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000791 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000792
David Majnemer4d571592014-09-15 19:42:42 +0000793 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000794 Header.NumberOfSymbols = 0;
795
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000796 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000797 // round up to calculate the number of auxiliary symbols required
798 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000799 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000800
801 COFFSymbol *file = createSymbol(".file");
802 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
803 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
804 file->Aux.resize(Count);
805
806 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000807 unsigned Length = Name.size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000808 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000809 Aux.AuxType = ATFile;
810
811 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000812 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000813 Length = Length - SymbolSize;
814 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000815 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000816 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
817 break;
818 }
819
820 Offset += SymbolSize;
821 }
822 }
823
David Majnemer9ab5ff12014-08-28 04:02:50 +0000824 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000825 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000826 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000827 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000828 Symbol->setIndex(Header.NumberOfSymbols++);
829 // Update auxiliary symbol info.
830 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
831 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000832 }
833
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000834 // Build string table.
835 for (const auto &S : Sections)
836 if (S->Name.size() > COFF::NameSize)
837 Strings.add(S->Name);
838 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000839 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000840 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000841 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000842
843 // Set names.
844 for (const auto &S : Sections)
845 SetSectionName(*S);
846 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000847 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000848
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000849 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000850 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000851 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000852 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000853 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
854 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000855 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000856 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000857 }
858 }
859
Nico Riecka37acf72013-07-06 12:13:10 +0000860 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000861 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000862 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000863 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
864 continue;
865
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000866 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +0000867
Rafael Espindola0766ae02014-06-06 19:26:12 +0000868 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
869 assert(COMDAT);
870 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
871 assert(COMDATSymbol);
872 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000873 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000874 report_fatal_error(
875 Twine("Missing associated COMDAT section for section ") +
876 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000877
878 // Skip this section if the associated section is unused.
879 if (Assoc->Number == -1)
880 continue;
881
David Majnemer4eecd302015-05-30 04:56:02 +0000882 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +0000883 }
884
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000885 // Assign file offsets to COFF object file structures.
886
Manuel Klimek272d3f12015-11-18 15:24:17 +0000887 unsigned offset = getInitialOffset();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000888
David Majnemer4d571592014-09-15 19:42:42 +0000889 if (UseBigObj)
890 offset += COFF::Header32Size;
891 else
892 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000893 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000894
Yaron Keren56919ef2014-12-04 08:30:39 +0000895 for (const auto &Section : Asm) {
Rafael Espindolaa554c052015-05-25 23:14:17 +0000896 COFFSection *Sec = SectionMap[&Section];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000897
Michael J. Spencerd6283772010-09-27 08:58:26 +0000898 if (Sec->Number == -1)
899 continue;
900
Rafael Espindola5a1e80b2015-05-26 02:00:36 +0000901 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000902
Michael J. Spencerd6283772010-09-27 08:58:26 +0000903 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000904 // Align the section data to a four byte boundary.
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000905 offset = alignTo(offset, 4);
David Majnemerab2b25b2015-02-11 22:51:55 +0000906 Sec->Header.PointerToRawData = offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000907
908 offset += Sec->Header.SizeOfRawData;
909 }
910
Eugene Zelenko1d435522017-02-07 23:02:00 +0000911 if (!Sec->Relocations.empty()) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000912 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
913
914 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000915 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000916 // size is found in reloc #0. Microsoft tools understand this.
917 Sec->Header.NumberOfRelocations = 0xffff;
918 } else {
919 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
920 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000921 Sec->Header.PointerToRelocations = offset;
922
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000923 if (RelocationsOverflow) {
924 // Reloc #0 will contain actual count, so make room for it.
925 offset += COFF::RelocationSize;
926 }
927
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000928 offset += COFF::RelocationSize * Sec->Relocations.size();
929
Yaron Keren56919ef2014-12-04 08:30:39 +0000930 for (auto &Relocation : Sec->Relocations) {
David Majnemer4eecd302015-05-30 04:56:02 +0000931 assert(Relocation.Symb->getIndex() != -1);
932 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000933 }
934 }
935
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000936 assert(Sec->Symbol->Aux.size() == 1 &&
937 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000938 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000939 assert(Aux.AuxType == ATSectionDefinition &&
940 "Section's symbol's aux symbol must be a Section Definition!");
941 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
942 Aux.Aux.SectionDefinition.NumberOfRelocations =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000943 Sec->Header.NumberOfRelocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000944 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000945 Sec->Header.NumberOfLineNumbers;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000946 }
947
948 Header.PointerToSymbolTable = offset;
949
David Majnemer088ba022015-09-01 23:46:11 +0000950 // MS LINK expects to be able to use this timestamp to implement their
951 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +0000952 if (Asm.isIncrementalLinkerCompatible()) {
953 std::time_t Now = time(nullptr);
954 if (Now < 0 || !isUInt<32>(Now))
955 Now = UINT32_MAX;
956 Header.TimeDateStamp = Now;
957 } else {
Nico Weber891419a2016-01-06 19:05:19 +0000958 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +0000959 Header.TimeDateStamp = 0;
960 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +0000961
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000962 // Write it all to disk...
963 WriteFileHeader(Header);
964
965 {
966 sections::iterator i, ie;
Rafael Espindola63702e22015-06-01 01:30:01 +0000967 MCAssembler::iterator j, je;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000968
Yaron Keren56919ef2014-12-04 08:30:39 +0000969 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000970 if (Section->Number != -1) {
971 if (Section->Relocations.size() >= 0xffff)
972 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000973 writeSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000974 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000975 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000976
David Majnemer6ddc6362015-09-01 21:23:58 +0000977 SmallVector<char, 128> SectionContents;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000978 for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
979 je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000980 (i != ie) && (j != je); ++i, ++j) {
981
982 if ((*i)->Number == -1)
983 continue;
984
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000985 if ((*i)->Header.PointerToRawData != 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +0000986 assert(getStream().tell() <= (*i)->Header.PointerToRawData &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000987 "Section::PointerToRawData is insane!");
988
David Majnemerabdb2d2a2015-09-01 16:19:03 +0000989 unsigned SectionDataPadding =
990 (*i)->Header.PointerToRawData - getStream().tell();
David Majnemer3df3c612015-02-11 22:22:30 +0000991 assert(SectionDataPadding < 4 &&
992 "Should only need at most three bytes of padding!");
993
994 WriteZeros(SectionDataPadding);
995
David Majnemer6ddc6362015-09-01 21:23:58 +0000996 // Save the contents of the section to a temporary buffer, we need this
997 // to CRC the data before we dump it into the object file.
998 SectionContents.clear();
999 raw_svector_ostream VecOS(SectionContents);
1000 raw_pwrite_stream &OldStream = getStream();
1001 // Redirect the output stream to our buffer.
1002 setStream(VecOS);
1003 // Fill our buffer with the section data.
Rafael Espindola64acc7f2015-05-26 02:17:21 +00001004 Asm.writeSectionData(&*j, Layout);
David Majnemer6ddc6362015-09-01 21:23:58 +00001005 // Reset the stream back to what it was before.
1006 setStream(OldStream);
1007
1008 // Calculate our CRC with an initial value of '0', this is not how
1009 // JamCRC is specified but it aligns with the expected output.
1010 JamCRC JC(/*Init=*/0x00000000U);
1011 JC.update(SectionContents);
1012
1013 // Write the section contents to the object file.
1014 getStream() << SectionContents;
1015
1016 // Update the section definition auxiliary symbol to record the CRC.
1017 COFFSection *Sec = SectionMap[&*j];
1018 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
1019 assert(AuxSyms.size() == 1 &&
1020 AuxSyms[0].AuxType == ATSectionDefinition);
1021 AuxSymbol &SecDef = AuxSyms[0];
1022 SecDef.Aux.SectionDefinition.CheckSum = JC.getCRC();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001023 }
1024
Eugene Zelenko1d435522017-02-07 23:02:00 +00001025 if (!(*i)->Relocations.empty()) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001026 assert(getStream().tell() == (*i)->Header.PointerToRelocations &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001027 "Section::PointerToRelocations is insane!");
1028
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001029 if ((*i)->Relocations.size() >= 0xffff) {
1030 // In case of overflow, write actual relocation count as first
1031 // relocation. Including the synthetic reloc itself (+ 1).
1032 COFF::relocation r;
1033 r.VirtualAddress = (*i)->Relocations.size() + 1;
1034 r.SymbolTableIndex = 0;
1035 r.Type = 0;
1036 WriteRelocation(r);
1037 }
1038
Yaron Keren56919ef2014-12-04 08:30:39 +00001039 for (const auto &Relocation : (*i)->Relocations)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001040 WriteRelocation(Relocation.Data);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001041 } else
1042 assert((*i)->Header.PointerToRelocations == 0 &&
1043 "Section::PointerToRelocations is insane!");
1044 }
1045 }
1046
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001047 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001048 "Header::PointerToSymbolTable is insane!");
1049
Yaron Keren56919ef2014-12-04 08:30:39 +00001050 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001051 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001052 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001053
Rafael Espindola39751af2016-10-04 22:43:25 +00001054 Strings.write(getStream());
Chris Lattner2c52b792010-07-11 22:07:02 +00001055}
1056
Rafael Espindola11e9e212015-05-27 14:37:12 +00001057MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1058 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001059
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001060// Pin the vtable to this file.
1061void MCWinCOFFObjectTargetWriter::anchor() {}
1062
Chris Lattner2c52b792010-07-11 22:07:02 +00001063//------------------------------------------------------------------------------
1064// WinCOFFObjectWriter factory function
1065
Rafael Espindola37099d92015-04-09 18:08:15 +00001066MCObjectWriter *
1067llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001068 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001069 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001070}