blob: afc5c6a14d118e1ef855b0c2c6ae2149328b86a5 [file] [log] [blame]
Chris Lattner2c52b792010-07-11 22:07:02 +00001//===-- llvm/MC/WinCOFFObjectWriter.cpp -------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains an implementation of a Win32 COFF object file writer.
11//
12//===----------------------------------------------------------------------===//
13
Rafael Espindola908d2ed2011-12-24 02:14:02 +000014#include "llvm/MC/MCWinCOFFObjectWriter.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000015#include "llvm/ADT/DenseMap.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000016#include "llvm/ADT/STLExtras.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000017#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
Nico Riecka37acf72013-07-06 12:13:10 +000019#include "llvm/ADT/Twine.h"
David Majnemer18663f82015-12-21 08:03:07 +000020#include "llvm/Config/config.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCAsmLayout.h"
22#include "llvm/MC/MCAssembler.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCExpr.h"
David Majnemer4eecd302015-05-30 04:56:02 +000025#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/MC/MCObjectWriter.h"
27#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCSectionCOFF.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000029#include "llvm/MC/MCSymbolCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/MC/MCValue.h"
Hans Wennborgf26bfc12014-09-29 22:43:20 +000031#include "llvm/MC/StringTableBuilder.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000032#include "llvm/Support/COFF.h"
33#include "llvm/Support/Debug.h"
Hans Wennborgba80b5d2014-09-28 00:22:27 +000034#include "llvm/Support/Endian.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000035#include "llvm/Support/ErrorHandling.h"
David Majnemer6ddc6362015-09-01 21:23:58 +000036#include "llvm/Support/JamCRC.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000037#include <cstdio>
David Majnemer088ba022015-09-01 23:46:11 +000038#include <ctime>
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000039
Chris Lattner2c52b792010-07-11 22:07:02 +000040using namespace llvm;
41
Chandler Carruthf58e3762014-04-22 03:04:17 +000042#define DEBUG_TYPE "WinCOFFObjectWriter"
43
Chris Lattner2c52b792010-07-11 22:07:02 +000044namespace {
Dmitri Gribenko226fea52013-01-13 16:01:15 +000045typedef SmallString<COFF::NameSize> name;
Chris Lattner2c52b792010-07-11 22:07:02 +000046
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000047enum AuxiliaryType {
48 ATFunctionDefinition,
49 ATbfAndefSymbol,
50 ATWeakExternal,
51 ATFile,
52 ATSectionDefinition
53};
Chris Lattner2c52b792010-07-11 22:07:02 +000054
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000055struct AuxSymbol {
Rafael Espindola11e9e212015-05-27 14:37:12 +000056 AuxiliaryType AuxType;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000057 COFF::Auxiliary Aux;
58};
Chris Lattner2c52b792010-07-11 22:07:02 +000059
Michael J. Spencerd6283772010-09-27 08:58:26 +000060class COFFSymbol;
61class COFFSection;
62
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000063class COFFSymbol {
64public:
65 COFF::symbol Data;
Chris Lattner2c52b792010-07-11 22:07:02 +000066
Dmitri Gribenko226fea52013-01-13 16:01:15 +000067 typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
Chris Lattner2c52b792010-07-11 22:07:02 +000068
Rafael Espindola11e9e212015-05-27 14:37:12 +000069 name Name;
70 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000071 AuxiliarySymbols Aux;
Rafael Espindola11e9e212015-05-27 14:37:12 +000072 COFFSymbol *Other;
73 COFFSection *Section;
74 int Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000075
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +000076 const MCSymbol *MC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000077
Dmitri Gribenko226fea52013-01-13 16:01:15 +000078 COFFSymbol(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000079 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000080
David Majnemer4eecd302015-05-30 04:56:02 +000081 int64_t getIndex() const { return Index; }
82 void setIndex(int Value) {
83 Index = Value;
84 if (MC)
85 MC->setIndex(static_cast<uint32_t>(Value));
86 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000087};
88
89// This class contains staging data for a COFF relocation entry.
90struct COFFRelocation {
91 COFF::relocation Data;
Rafael Espindola11e9e212015-05-27 14:37:12 +000092 COFFSymbol *Symb;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000093
Craig Topperbb694de2014-04-13 04:57:38 +000094 COFFRelocation() : Symb(nullptr) {}
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000095 static size_t size() { return COFF::RelocationSize; }
96};
97
98typedef std::vector<COFFRelocation> relocations;
99
100class COFFSection {
101public:
102 COFF::section Header;
103
Rafael Espindola11e9e212015-05-27 14:37:12 +0000104 std::string Name;
105 int Number;
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000106 MCSectionCOFF const *MCSection;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000107 COFFSymbol *Symbol;
108 relocations Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000109
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000110 COFFSection(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000111};
112
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000113class WinCOFFObjectWriter : public MCObjectWriter {
114public:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000115 typedef std::vector<std::unique_ptr<COFFSymbol>> symbols;
David Blaikief564ab62014-04-15 05:25:03 +0000116 typedef std::vector<std::unique_ptr<COFFSection>> sections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000117
Rafael Espindola11e9e212015-05-27 14:37:12 +0000118 typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000119 typedef DenseMap<MCSection const *, COFFSection *> section_map;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000120
Ahmed Charles56440fd2014-03-06 05:51:42 +0000121 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000122
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000123 // Root level file contents.
124 COFF::header Header;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000125 sections Sections;
126 symbols Symbols;
Rafael Espindola21956e42015-10-23 21:48:05 +0000127 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000128
129 // Maps used during object file creation.
130 section_map SectionMap;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000131 symbol_map SymbolMap;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000132
David Majnemer4d571592014-09-15 19:42:42 +0000133 bool UseBigObj;
134
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000135 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000136
Yaron Kerencca43c12014-09-16 21:31:04 +0000137 void reset() override {
138 memset(&Header, 0, sizeof(Header));
139 Header.Machine = TargetObjectWriter->getMachine();
140 Sections.clear();
141 Symbols.clear();
142 Strings.clear();
143 SectionMap.clear();
144 SymbolMap.clear();
145 MCObjectWriter::reset();
146 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000147
Michael J. Spencer17990d52010-10-16 08:25:57 +0000148 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola11e9e212015-05-27 14:37:12 +0000149 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000150 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000151
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000152 template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000153 object_t *createCOFFEntity(StringRef Name, list_t &List);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000154
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000155 void defineSection(MCSectionCOFF const &Sec);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000156
157 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000158 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000159 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000160
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000161 void SetSymbolName(COFFSymbol &S);
162 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000163
Michael J. Spencerd6283772010-09-27 08:58:26 +0000164 bool IsPhysicalSection(COFFSection *S);
165
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000166 // Entity writing methods.
167
168 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000169 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000170 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000171 void writeSectionHeader(const COFF::section &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000172 void WriteRelocation(const COFF::relocation &R);
173
174 // MCObjectWriter interface implementation.
175
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000176 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper34a61bc2014-03-08 07:02:02 +0000177 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000178
Jim Grosbach36e60e92015-06-04 22:24:41 +0000179 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000180 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000181 const MCFragment &FB, bool InSet,
182 bool IsPCRel) const override;
183
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000184 bool isWeak(const MCSymbol &Sym) const override;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000185
Jim Grosbach36e60e92015-06-04 22:24:41 +0000186 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000187 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000188 MCValue Target, bool &IsPCRel,
189 uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000190
Jim Grosbach36e60e92015-06-04 22:24:41 +0000191 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000192};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000193}
Chris Lattner2c52b792010-07-11 22:07:02 +0000194
Hans Wennborgba80b5d2014-09-28 00:22:27 +0000195static inline void write_uint32_le(void *Data, uint32_t Value) {
196 support::endian::write<uint32_t, support::little, support::unaligned>(Data,
197 Value);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000198}
199
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000200//------------------------------------------------------------------------------
201// Symbol class implementation
202
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000203COFFSymbol::COFFSymbol(StringRef name)
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000204 : Name(name.begin(), name.end()), Other(nullptr), Section(nullptr),
205 Relocations(0), MC(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000206 memset(&Data, 0, sizeof(Data));
207}
208
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000209// In the case that the name does not fit within 8 bytes, the offset
210// into the string table is stored in the last 4 bytes instead, leaving
211// the first 4 bytes as 0.
212void COFFSymbol::set_name_offset(uint32_t Offset) {
213 write_uint32_le(Data.Name + 0, 0);
214 write_uint32_le(Data.Name + 4, Offset);
215}
216
217//------------------------------------------------------------------------------
218// Section class implementation
219
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000220COFFSection::COFFSection(StringRef name)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000221 : Name(name), MCSection(nullptr), Symbol(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000222 memset(&Header, 0, sizeof(Header));
223}
224
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000225//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000226// WinCOFFObjectWriter class implementation
227
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000228WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000229 raw_pwrite_stream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000230 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000231 memset(&Header, 0, sizeof(Header));
Michael J. Spencer377aa202010-08-21 05:58:13 +0000232
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000233 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000234}
235
Michael J. Spencer17990d52010-10-16 08:25:57 +0000236COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000237 return createCOFFEntity<COFFSymbol>(Name, Symbols);
238}
239
Yaron Keren56919ef2014-12-04 08:30:39 +0000240COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000241 symbol_map::iterator i = SymbolMap.find(Symbol);
242 if (i != SymbolMap.end())
243 return i->second;
Yaron Keren56919ef2014-12-04 08:30:39 +0000244 COFFSymbol *RetSymbol =
245 createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000246 SymbolMap[Symbol] = RetSymbol;
247 return RetSymbol;
248}
249
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000250COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000251 return createCOFFEntity<COFFSection>(Name, Sections);
252}
253
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000254/// A template used to lookup or create a symbol/section, and initialize it if
255/// needed.
256template <typename object_t, typename list_t>
Rafael Espindola11e9e212015-05-27 14:37:12 +0000257object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name, list_t &List) {
David Blaikief564ab62014-04-15 05:25:03 +0000258 List.push_back(make_unique<object_t>(Name));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000259
David Blaikief564ab62014-04-15 05:25:03 +0000260 return List.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000261}
262
263/// This function takes a section data object from the assembler
264/// and creates the associated COFF section staging object.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000265void WinCOFFObjectWriter::defineSection(MCSectionCOFF const &Sec) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000266 COFFSection *coff_section = createSection(Sec.getSectionName());
Rafael Espindola11e9e212015-05-27 14:37:12 +0000267 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
Rafael Espindola0766ae02014-06-06 19:26:12 +0000268 if (Sec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
269 if (const MCSymbol *S = Sec.getCOMDATSymbol()) {
270 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
271 if (COMDATSymbol->Section)
272 report_fatal_error("two sections have the same comdat");
273 COMDATSymbol->Section = coff_section;
274 }
275 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000276
Michael J. Spencerd6283772010-09-27 08:58:26 +0000277 coff_section->Symbol = coff_symbol;
278 coff_symbol->Section = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000279 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000280
281 // In this case the auxiliary symbol is a Section Definition.
282 coff_symbol->Aux.resize(1);
283 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
284 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000285 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
286
287 coff_section->Header.Characteristics = Sec.getCharacteristics();
288
289 uint32_t &Characteristics = coff_section->Header.Characteristics;
Rafael Espindola967d6a62015-05-21 21:02:35 +0000290 switch (Sec.getAlignment()) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000291 case 1:
292 Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES;
293 break;
294 case 2:
295 Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES;
296 break;
297 case 4:
298 Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES;
299 break;
300 case 8:
301 Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES;
302 break;
303 case 16:
304 Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES;
305 break;
306 case 32:
307 Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES;
308 break;
309 case 64:
310 Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES;
311 break;
312 case 128:
313 Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES;
314 break;
315 case 256:
316 Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES;
317 break;
318 case 512:
319 Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES;
320 break;
321 case 1024:
322 Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES;
323 break;
324 case 2048:
325 Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES;
326 break;
327 case 4096:
328 Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES;
329 break;
330 case 8192:
331 Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES;
332 break;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000333 default:
334 llvm_unreachable("unsupported section alignment");
335 }
336
337 // Bind internal COFF section to MC section.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000338 coff_section->MCSection = &Sec;
339 SectionMap[&Sec] = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000340}
341
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000342static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000343 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000344 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000345 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000346
347 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000348 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000349 return 0;
350
351 return Res;
352}
353
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000354COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
355 if (!Symbol.isVariable())
356 return nullptr;
357
358 const MCSymbolRefExpr *SymRef =
359 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
360 if (!SymRef)
361 return nullptr;
362
363 const MCSymbol &Aliasee = SymRef->getSymbol();
364 if (!Aliasee.isUndefined())
365 return nullptr;
366 return GetOrCreateCOFFSymbol(&Aliasee);
367}
368
David Majnemera9bdb322014-04-08 22:33:40 +0000369/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000370/// and creates the associated COFF symbol staging object.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000371void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000372 MCAssembler &Assembler,
373 const MCAsmLayout &Layout) {
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000374 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000375 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
376 COFFSection *Sec = nullptr;
377 if (Base && Base->getFragment()) {
378 Sec = SectionMap[Base->getFragment()->getParent()];
379 if (coff_symbol->Section && coff_symbol->Section != Sec)
380 report_fatal_error("conflicting sections for symbol");
381 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000382
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000383 COFFSymbol *Local = nullptr;
Pete Cooper6bf1f302015-06-08 17:17:19 +0000384 if (cast<MCSymbolCOFF>(Symbol).isWeakExternal()) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000385 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
386
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000387 COFFSymbol *WeakDefault = getLinkedSymbol(Symbol);
388 if (!WeakDefault) {
Yaron Keren075759a2015-03-30 15:42:36 +0000389 std::string WeakName = (".weak." + Symbol.getName() + ".default").str();
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000390 WeakDefault = createSymbol(WeakName);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000391 if (!Sec)
392 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
393 else
394 WeakDefault->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000395 Local = WeakDefault;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000396 }
397
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000398 coff_symbol->Other = WeakDefault;
399
Michael J. Spencer17990d52010-10-16 08:25:57 +0000400 // Setup the Weak External auxiliary symbol.
401 coff_symbol->Aux.resize(1);
402 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
403 coff_symbol->Aux[0].AuxType = ATWeakExternal;
404 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
405 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000406 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000407 } else {
Rafael Espindola30c080a2016-05-26 18:48:23 +0000408 if (!Base)
Reid Klecknerc1e76212013-09-17 23:18:05 +0000409 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola30c080a2016-05-26 18:48:23 +0000410 else
411 coff_symbol->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000412 Local = coff_symbol;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000413 }
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000414
415 if (Local) {
416 Local->Data.Value = getSymbolValue(Symbol, Layout);
417
418 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(Symbol);
419 Local->Data.Type = SymbolCOFF.getType();
420 Local->Data.StorageClass = SymbolCOFF.getClass();
421
422 // If no storage class was specified in the streamer, define it here.
423 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
424 bool IsExternal = Symbol.isExternal() ||
425 (!Symbol.getFragment() && !Symbol.isVariable());
426
427 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
428 : COFF::IMAGE_SYM_CLASS_STATIC;
429 }
430 }
431
432 coff_symbol->MC = &Symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000433}
434
Nico Rieck01143f92014-02-25 09:50:40 +0000435// Maximum offsets for different string table entry encodings.
David Majnemerf088f522016-01-24 20:46:11 +0000436enum : unsigned { Max7DecimalOffset = 9999999U };
437enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
Nico Rieck01143f92014-02-25 09:50:40 +0000438
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000439// Encode a string table entry offset in base 64, padded to 6 chars, and
440// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
441// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000442static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000443 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000444 "Illegal section name encoding for value");
445
446 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
447 "abcdefghijklmnopqrstuvwxyz"
448 "0123456789+/";
449
450 Buffer[0] = '/';
451 Buffer[1] = '/';
452
Rafael Espindola11e9e212015-05-27 14:37:12 +0000453 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000454 for (unsigned i = 0; i < 6; ++i) {
455 unsigned Rem = Value % 64;
456 Value /= 64;
457 *(Ptr--) = Alphabet[Rem];
458 }
459}
460
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000461void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000462 if (S.Name.size() > COFF::NameSize) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000463 uint64_t StringTableEntry = Strings.getOffset(S.Name);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000464
David Majnemerf088f522016-01-24 20:46:11 +0000465 if (StringTableEntry <= Max7DecimalOffset) {
466 SmallVector<char, COFF::NameSize> Buffer;
467 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
468 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
469
470 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
Nico Rieck01143f92014-02-25 09:50:40 +0000471 } else if (StringTableEntry <= MaxBase64Offset) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000472 // Starting with 10,000,000, offsets are encoded as base64.
473 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000474 } else {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000475 report_fatal_error("COFF string table is greater than 64 GB.");
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000476 }
David Majnemerf088f522016-01-24 20:46:11 +0000477 } else {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000478 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
David Majnemerf088f522016-01-24 20:46:11 +0000479 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000480}
481
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000482void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
483 if (S.Name.size() > COFF::NameSize)
484 S.set_name_offset(Strings.getOffset(S.Name));
485 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000486 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000487}
488
Michael J. Spencerd6283772010-09-27 08:58:26 +0000489bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000490 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
491 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000492}
493
494//------------------------------------------------------------------------------
495// entity writing methods
496
497void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000498 if (UseBigObj) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000499 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
500 writeLE16(0xFFFF);
501 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
502 writeLE16(Header.Machine);
503 writeLE32(Header.TimeDateStamp);
504 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
505 writeLE32(0);
506 writeLE32(0);
507 writeLE32(0);
508 writeLE32(0);
509 writeLE32(Header.NumberOfSections);
510 writeLE32(Header.PointerToSymbolTable);
511 writeLE32(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000512 } else {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000513 writeLE16(Header.Machine);
514 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
515 writeLE32(Header.TimeDateStamp);
516 writeLE32(Header.PointerToSymbolTable);
517 writeLE32(Header.NumberOfSymbols);
518 writeLE16(Header.SizeOfOptionalHeader);
519 writeLE16(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000520 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000521}
522
David Blaikief564ab62014-04-15 05:25:03 +0000523void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000524 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
525 writeLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000526 if (UseBigObj)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000527 writeLE32(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000528 else
Jim Grosbach36e60e92015-06-04 22:24:41 +0000529 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
530 writeLE16(S.Data.Type);
531 write8(S.Data.StorageClass);
532 write8(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000533 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000534}
535
536void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000537 const COFFSymbol::AuxiliarySymbols &S) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000538 for (const AuxSymbol &i : S) {
539 switch (i.AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000540 case ATFunctionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000541 writeLE32(i.Aux.FunctionDefinition.TagIndex);
542 writeLE32(i.Aux.FunctionDefinition.TotalSize);
543 writeLE32(i.Aux.FunctionDefinition.PointerToLinenumber);
544 writeLE32(i.Aux.FunctionDefinition.PointerToNextFunction);
545 WriteZeros(sizeof(i.Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000546 if (UseBigObj)
547 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000548 break;
549 case ATbfAndefSymbol:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000550 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused1));
551 writeLE16(i.Aux.bfAndefSymbol.Linenumber);
552 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused2));
553 writeLE32(i.Aux.bfAndefSymbol.PointerToNextFunction);
554 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000555 if (UseBigObj)
556 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000557 break;
558 case ATWeakExternal:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000559 writeLE32(i.Aux.WeakExternal.TagIndex);
560 writeLE32(i.Aux.WeakExternal.Characteristics);
561 WriteZeros(sizeof(i.Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000562 if (UseBigObj)
563 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000564 break;
565 case ATFile:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000566 writeBytes(
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000567 StringRef(reinterpret_cast<const char *>(&i.Aux),
David Majnemer4d571592014-09-15 19:42:42 +0000568 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000569 break;
570 case ATSectionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000571 writeLE32(i.Aux.SectionDefinition.Length);
572 writeLE16(i.Aux.SectionDefinition.NumberOfRelocations);
573 writeLE16(i.Aux.SectionDefinition.NumberOfLinenumbers);
574 writeLE32(i.Aux.SectionDefinition.CheckSum);
575 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
576 write8(i.Aux.SectionDefinition.Selection);
577 WriteZeros(sizeof(i.Aux.SectionDefinition.unused));
578 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000579 if (UseBigObj)
580 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000581 break;
582 }
583 }
584}
585
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000586void WinCOFFObjectWriter::writeSectionHeader(const COFF::section &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000587 writeBytes(StringRef(S.Name, COFF::NameSize));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000588
Jim Grosbach36e60e92015-06-04 22:24:41 +0000589 writeLE32(S.VirtualSize);
590 writeLE32(S.VirtualAddress);
591 writeLE32(S.SizeOfRawData);
592 writeLE32(S.PointerToRawData);
593 writeLE32(S.PointerToRelocations);
594 writeLE32(S.PointerToLineNumbers);
595 writeLE16(S.NumberOfRelocations);
596 writeLE16(S.NumberOfLineNumbers);
597 writeLE32(S.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000598}
599
600void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000601 writeLE32(R.VirtualAddress);
602 writeLE32(R.SymbolTableIndex);
603 writeLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000604}
605
606////////////////////////////////////////////////////////////////////////////////
607// MCObjectWriter interface implementations
608
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000609void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000610 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000611 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000612 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000613 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000614 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000615
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000616 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000617 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000618 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000619}
620
Jim Grosbach36e60e92015-06-04 22:24:41 +0000621bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000622 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000623 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000624 // MS LINK expects to be able to replace all references to a function with a
625 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
626 // away any relocations to functions.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000627 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
David Majnemer03e2cc32015-12-21 22:09:27 +0000628 if (Asm.isIncrementalLinkerCompatible() &&
629 (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000630 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000631 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000632 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000633}
634
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000635bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000636 if (!Sym.isExternal())
Rafael Espindola88af4112015-04-17 11:27:13 +0000637 return false;
638
Rafael Espindola88af4112015-04-17 11:27:13 +0000639 if (!Sym.isInSection())
640 return false;
641
642 const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
643 if (!Sec.getCOMDATSymbol())
644 return false;
645
646 // It looks like for COFF it is invalid to replace a reference to a global
647 // in a comdat with a reference to a local.
648 // FIXME: Add a specification reference if available.
649 return true;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000650}
651
Jim Grosbach36e60e92015-06-04 22:24:41 +0000652void WinCOFFObjectWriter::recordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000653 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
654 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000655 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000656
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000657 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000658 if (!A.isRegistered()) {
659 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000660 Twine("symbol '") + A.getName() +
661 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000662 return;
663 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000664 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000665 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000666 Twine("assembler label '") + A.getName() +
667 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000668 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000669 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000670
Rafael Espindola7549f872015-05-26 00:36:57 +0000671 MCSection *Section = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000672
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000673 // Mark this symbol as requiring an entry in the symbol table.
Rafael Espindola7549f872015-05-26 00:36:57 +0000674 assert(SectionMap.find(Section) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000675 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000676
Rafael Espindola7549f872015-05-26 00:36:57 +0000677 COFFSection *coff_section = SectionMap[Section];
Rafael Espindolaed164772011-04-20 14:01:45 +0000678 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000679 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000680
David Majnemera45a1762014-01-06 07:39:46 +0000681 if (SymB) {
682 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000683 if (!B->getFragment()) {
684 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000685 Fixup.getLoc(),
686 Twine("symbol '") + B->getName() +
687 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000688 return;
689 }
David Majnemera45a1762014-01-06 07:39:46 +0000690
Oliver Stannard9be59af2015-11-17 10:00:43 +0000691 if (!A.getFragment()) {
692 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000693 Fixup.getLoc(),
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000694 Twine("symbol '") + A.getName() +
David Majnemera45a1762014-01-06 07:39:46 +0000695 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000696 return;
697 }
David Majnemera45a1762014-01-06 07:39:46 +0000698
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000699 CrossSection = &A.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000700
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000701 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000702 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000703
704 // In the case where we have SymbA and SymB, we just need to store the delta
705 // between the two symbols. Update FixedValue to account for the delta, and
706 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000707 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000708 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000709 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000710 return;
David Majnemer1de30942015-02-09 06:31:31 +0000711 }
712
713 // Offset of the relocation in the section
714 int64_t OffsetOfRelocation =
715 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
716
Andy Ayers9e5c8512015-05-14 01:10:41 +0000717 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000718 } else {
719 FixedValue = Target.getConstant();
720 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000721
722 COFFRelocation Reloc;
723
Daniel Dunbar727be432010-07-31 21:08:54 +0000724 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000725 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000726
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000727 // Turn relocations for temporary symbols into section relocations.
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000728 if (A.isTemporary() || CrossSection) {
729 MCSection *TargetSection = &A.getSection();
730 assert(
731 SectionMap.find(TargetSection) != SectionMap.end() &&
732 "Section must already have been defined in executePostLayoutBinding!");
733 Reloc.Symb = SectionMap[TargetSection]->Symbol;
734 FixedValue += Layout.getSymbolOffset(A);
735 } else {
736 assert(
737 SymbolMap.find(&A) != SymbolMap.end() &&
738 "Symbol must already have been defined in executePostLayoutBinding!");
739 Reloc.Symb = SymbolMap[&A];
740 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000741
742 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000743
744 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000745 Reloc.Data.Type = TargetObjectWriter->getRelocType(
746 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000747
748 // FIXME: Can anyone explain what this does other than adjust for the size
749 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000750 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
751 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
752 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
753 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000754 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000755
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000756 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
757 switch (Reloc.Data.Type) {
758 case COFF::IMAGE_REL_ARM_ABSOLUTE:
759 case COFF::IMAGE_REL_ARM_ADDR32:
760 case COFF::IMAGE_REL_ARM_ADDR32NB:
761 case COFF::IMAGE_REL_ARM_TOKEN:
762 case COFF::IMAGE_REL_ARM_SECTION:
763 case COFF::IMAGE_REL_ARM_SECREL:
764 break;
765 case COFF::IMAGE_REL_ARM_BRANCH11:
766 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000767 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
768 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
769 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000770 case COFF::IMAGE_REL_ARM_BRANCH24:
771 case COFF::IMAGE_REL_ARM_BLX24:
772 case COFF::IMAGE_REL_ARM_MOV32A:
773 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
774 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000775 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000776 // generate the relocations however the rest of the MSVC toolchain is
777 // unable to handle it.
778 llvm_unreachable("unsupported relocation");
779 break;
780 case COFF::IMAGE_REL_ARM_MOV32T:
781 break;
782 case COFF::IMAGE_REL_ARM_BRANCH20T:
783 case COFF::IMAGE_REL_ARM_BRANCH24T:
784 case COFF::IMAGE_REL_ARM_BLX23T:
785 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
786 // perform a 4 byte adjustment to the relocation. Relative branches are
787 // offset by 4 on ARM, however, because there is no RELA relocations, all
788 // branches are offset by 4.
789 FixedValue = FixedValue + 4;
790 break;
791 }
792 }
793
Simon Pilgrimf2fbf432016-11-20 13:47:59 +0000794 // The fixed value never makes sense for section indices, ignore it.
David Majnemer408b5e62016-02-05 01:55:49 +0000795 if (Fixup.getKind() == FK_SecRel_2)
796 FixedValue = 0;
797
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000798 if (TargetObjectWriter->recordRelocation(Fixup))
799 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000800}
801
Jim Grosbach36e60e92015-06-04 22:24:41 +0000802void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000803 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000804 size_t SectionsSize = Sections.size();
805 if (SectionsSize > static_cast<size_t>(INT32_MAX))
806 report_fatal_error(
807 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000808
David Majnemer4d571592014-09-15 19:42:42 +0000809 // Assign symbol and section indexes and offsets.
810 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
811
812 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
813
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000814 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000815 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000816 for (const auto &Section : Sections) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000817 Section->Number = Number;
818 Section->Symbol->Data.SectionNumber = Number;
819 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000820 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000821 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000822
David Majnemer4d571592014-09-15 19:42:42 +0000823 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000824 Header.NumberOfSymbols = 0;
825
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000826 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000827 // round up to calculate the number of auxiliary symbols required
828 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000829 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000830
831 COFFSymbol *file = createSymbol(".file");
832 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
833 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
834 file->Aux.resize(Count);
835
836 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000837 unsigned Length = Name.size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000838 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000839 Aux.AuxType = ATFile;
840
841 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000842 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000843 Length = Length - SymbolSize;
844 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000845 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000846 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
847 break;
848 }
849
850 Offset += SymbolSize;
851 }
852 }
853
David Majnemer9ab5ff12014-08-28 04:02:50 +0000854 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000855 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000856 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000857 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000858 Symbol->setIndex(Header.NumberOfSymbols++);
859 // Update auxiliary symbol info.
860 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
861 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000862 }
863
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000864 // Build string table.
865 for (const auto &S : Sections)
866 if (S->Name.size() > COFF::NameSize)
867 Strings.add(S->Name);
868 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000869 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000870 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000871 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000872
873 // Set names.
874 for (const auto &S : Sections)
875 SetSectionName(*S);
876 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000877 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000878
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000879 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000880 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000881 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000882 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000883 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
884 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000885 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000886 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000887 }
888 }
889
Nico Riecka37acf72013-07-06 12:13:10 +0000890 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000891 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000892 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000893 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
894 continue;
895
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000896 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +0000897
Rafael Espindola0766ae02014-06-06 19:26:12 +0000898 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
899 assert(COMDAT);
900 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
901 assert(COMDATSymbol);
902 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000903 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000904 report_fatal_error(
905 Twine("Missing associated COMDAT section for section ") +
906 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000907
908 // Skip this section if the associated section is unused.
909 if (Assoc->Number == -1)
910 continue;
911
David Majnemer4eecd302015-05-30 04:56:02 +0000912 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +0000913 }
914
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000915 // Assign file offsets to COFF object file structures.
916
Manuel Klimek272d3f12015-11-18 15:24:17 +0000917 unsigned offset = getInitialOffset();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000918
David Majnemer4d571592014-09-15 19:42:42 +0000919 if (UseBigObj)
920 offset += COFF::Header32Size;
921 else
922 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000923 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000924
Yaron Keren56919ef2014-12-04 08:30:39 +0000925 for (const auto &Section : Asm) {
Rafael Espindolaa554c052015-05-25 23:14:17 +0000926 COFFSection *Sec = SectionMap[&Section];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000927
Michael J. Spencerd6283772010-09-27 08:58:26 +0000928 if (Sec->Number == -1)
929 continue;
930
Rafael Espindola5a1e80b2015-05-26 02:00:36 +0000931 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000932
Michael J. Spencerd6283772010-09-27 08:58:26 +0000933 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000934 // Align the section data to a four byte boundary.
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000935 offset = alignTo(offset, 4);
David Majnemerab2b25b2015-02-11 22:51:55 +0000936 Sec->Header.PointerToRawData = offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000937
938 offset += Sec->Header.SizeOfRawData;
939 }
940
941 if (Sec->Relocations.size() > 0) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000942 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
943
944 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000945 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000946 // size is found in reloc #0. Microsoft tools understand this.
947 Sec->Header.NumberOfRelocations = 0xffff;
948 } else {
949 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
950 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000951 Sec->Header.PointerToRelocations = offset;
952
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000953 if (RelocationsOverflow) {
954 // Reloc #0 will contain actual count, so make room for it.
955 offset += COFF::RelocationSize;
956 }
957
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000958 offset += COFF::RelocationSize * Sec->Relocations.size();
959
Yaron Keren56919ef2014-12-04 08:30:39 +0000960 for (auto &Relocation : Sec->Relocations) {
David Majnemer4eecd302015-05-30 04:56:02 +0000961 assert(Relocation.Symb->getIndex() != -1);
962 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000963 }
964 }
965
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000966 assert(Sec->Symbol->Aux.size() == 1 &&
967 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000968 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000969 assert(Aux.AuxType == ATSectionDefinition &&
970 "Section's symbol's aux symbol must be a Section Definition!");
971 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
972 Aux.Aux.SectionDefinition.NumberOfRelocations =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000973 Sec->Header.NumberOfRelocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000974 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000975 Sec->Header.NumberOfLineNumbers;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000976 }
977
978 Header.PointerToSymbolTable = offset;
979
David Majnemer088ba022015-09-01 23:46:11 +0000980 // MS LINK expects to be able to use this timestamp to implement their
981 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +0000982 if (Asm.isIncrementalLinkerCompatible()) {
983 std::time_t Now = time(nullptr);
984 if (Now < 0 || !isUInt<32>(Now))
985 Now = UINT32_MAX;
986 Header.TimeDateStamp = Now;
987 } else {
Nico Weber891419a2016-01-06 19:05:19 +0000988 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +0000989 Header.TimeDateStamp = 0;
990 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +0000991
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000992 // Write it all to disk...
993 WriteFileHeader(Header);
994
995 {
996 sections::iterator i, ie;
Rafael Espindola63702e22015-06-01 01:30:01 +0000997 MCAssembler::iterator j, je;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000998
Yaron Keren56919ef2014-12-04 08:30:39 +0000999 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001000 if (Section->Number != -1) {
1001 if (Section->Relocations.size() >= 0xffff)
1002 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Jim Grosbach56ed0bb2015-06-04 23:25:54 +00001003 writeSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001004 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001005 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001006
David Majnemer6ddc6362015-09-01 21:23:58 +00001007 SmallVector<char, 128> SectionContents;
Rafael Espindola11e9e212015-05-27 14:37:12 +00001008 for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
1009 je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +00001010 (i != ie) && (j != je); ++i, ++j) {
1011
1012 if ((*i)->Number == -1)
1013 continue;
1014
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001015 if ((*i)->Header.PointerToRawData != 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001016 assert(getStream().tell() <= (*i)->Header.PointerToRawData &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001017 "Section::PointerToRawData is insane!");
1018
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001019 unsigned SectionDataPadding =
1020 (*i)->Header.PointerToRawData - getStream().tell();
David Majnemer3df3c612015-02-11 22:22:30 +00001021 assert(SectionDataPadding < 4 &&
1022 "Should only need at most three bytes of padding!");
1023
1024 WriteZeros(SectionDataPadding);
1025
David Majnemer6ddc6362015-09-01 21:23:58 +00001026 // Save the contents of the section to a temporary buffer, we need this
1027 // to CRC the data before we dump it into the object file.
1028 SectionContents.clear();
1029 raw_svector_ostream VecOS(SectionContents);
1030 raw_pwrite_stream &OldStream = getStream();
1031 // Redirect the output stream to our buffer.
1032 setStream(VecOS);
1033 // Fill our buffer with the section data.
Rafael Espindola64acc7f2015-05-26 02:17:21 +00001034 Asm.writeSectionData(&*j, Layout);
David Majnemer6ddc6362015-09-01 21:23:58 +00001035 // Reset the stream back to what it was before.
1036 setStream(OldStream);
1037
1038 // Calculate our CRC with an initial value of '0', this is not how
1039 // JamCRC is specified but it aligns with the expected output.
1040 JamCRC JC(/*Init=*/0x00000000U);
1041 JC.update(SectionContents);
1042
1043 // Write the section contents to the object file.
1044 getStream() << SectionContents;
1045
1046 // Update the section definition auxiliary symbol to record the CRC.
1047 COFFSection *Sec = SectionMap[&*j];
1048 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
1049 assert(AuxSyms.size() == 1 &&
1050 AuxSyms[0].AuxType == ATSectionDefinition);
1051 AuxSymbol &SecDef = AuxSyms[0];
1052 SecDef.Aux.SectionDefinition.CheckSum = JC.getCRC();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001053 }
1054
1055 if ((*i)->Relocations.size() > 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001056 assert(getStream().tell() == (*i)->Header.PointerToRelocations &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001057 "Section::PointerToRelocations is insane!");
1058
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001059 if ((*i)->Relocations.size() >= 0xffff) {
1060 // In case of overflow, write actual relocation count as first
1061 // relocation. Including the synthetic reloc itself (+ 1).
1062 COFF::relocation r;
1063 r.VirtualAddress = (*i)->Relocations.size() + 1;
1064 r.SymbolTableIndex = 0;
1065 r.Type = 0;
1066 WriteRelocation(r);
1067 }
1068
Yaron Keren56919ef2014-12-04 08:30:39 +00001069 for (const auto &Relocation : (*i)->Relocations)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001070 WriteRelocation(Relocation.Data);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001071 } else
1072 assert((*i)->Header.PointerToRelocations == 0 &&
1073 "Section::PointerToRelocations is insane!");
1074 }
1075 }
1076
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001077 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001078 "Header::PointerToSymbolTable is insane!");
1079
Yaron Keren56919ef2014-12-04 08:30:39 +00001080 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001081 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001082 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001083
Rafael Espindola39751af2016-10-04 22:43:25 +00001084 Strings.write(getStream());
Chris Lattner2c52b792010-07-11 22:07:02 +00001085}
1086
Rafael Espindola11e9e212015-05-27 14:37:12 +00001087MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1088 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001089
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001090// Pin the vtable to this file.
1091void MCWinCOFFObjectTargetWriter::anchor() {}
1092
Chris Lattner2c52b792010-07-11 22:07:02 +00001093//------------------------------------------------------------------------------
1094// WinCOFFObjectWriter factory function
1095
Rafael Espindola37099d92015-04-09 18:08:15 +00001096MCObjectWriter *
1097llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001098 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001099 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001100}