blob: 819d446d216d44443fd510827e7fcdbeb7652c83 [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. Spencer447762d2010-11-29 18:16:10 +000037#include "llvm/Support/TimeValue.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000038#include <cstdio>
David Majnemer088ba022015-09-01 23:46:11 +000039#include <ctime>
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000040
Chris Lattner2c52b792010-07-11 22:07:02 +000041using namespace llvm;
42
Chandler Carruthf58e3762014-04-22 03:04:17 +000043#define DEBUG_TYPE "WinCOFFObjectWriter"
44
Chris Lattner2c52b792010-07-11 22:07:02 +000045namespace {
Dmitri Gribenko226fea52013-01-13 16:01:15 +000046typedef SmallString<COFF::NameSize> name;
Chris Lattner2c52b792010-07-11 22:07:02 +000047
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000048enum AuxiliaryType {
49 ATFunctionDefinition,
50 ATbfAndefSymbol,
51 ATWeakExternal,
52 ATFile,
53 ATSectionDefinition
54};
Chris Lattner2c52b792010-07-11 22:07:02 +000055
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000056struct AuxSymbol {
Rafael Espindola11e9e212015-05-27 14:37:12 +000057 AuxiliaryType AuxType;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000058 COFF::Auxiliary Aux;
59};
Chris Lattner2c52b792010-07-11 22:07:02 +000060
Michael J. Spencerd6283772010-09-27 08:58:26 +000061class COFFSymbol;
62class COFFSection;
63
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000064class COFFSymbol {
65public:
66 COFF::symbol Data;
Chris Lattner2c52b792010-07-11 22:07:02 +000067
Dmitri Gribenko226fea52013-01-13 16:01:15 +000068 typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
Chris Lattner2c52b792010-07-11 22:07:02 +000069
Rafael Espindola11e9e212015-05-27 14:37:12 +000070 name Name;
71 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000072 AuxiliarySymbols Aux;
Rafael Espindola11e9e212015-05-27 14:37:12 +000073 COFFSymbol *Other;
74 COFFSection *Section;
75 int Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000076
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +000077 const MCSymbol *MC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000078
Dmitri Gribenko226fea52013-01-13 16:01:15 +000079 COFFSymbol(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000080 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000081
David Majnemer4eecd302015-05-30 04:56:02 +000082 int64_t getIndex() const { return Index; }
83 void setIndex(int Value) {
84 Index = Value;
85 if (MC)
86 MC->setIndex(static_cast<uint32_t>(Value));
87 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000088};
89
90// This class contains staging data for a COFF relocation entry.
91struct COFFRelocation {
92 COFF::relocation Data;
Rafael Espindola11e9e212015-05-27 14:37:12 +000093 COFFSymbol *Symb;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000094
Craig Topperbb694de2014-04-13 04:57:38 +000095 COFFRelocation() : Symb(nullptr) {}
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000096 static size_t size() { return COFF::RelocationSize; }
97};
98
99typedef std::vector<COFFRelocation> relocations;
100
101class COFFSection {
102public:
103 COFF::section Header;
104
Rafael Espindola11e9e212015-05-27 14:37:12 +0000105 std::string Name;
106 int Number;
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000107 MCSectionCOFF const *MCSection;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000108 COFFSymbol *Symbol;
109 relocations Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000110
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000111 COFFSection(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000112};
113
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000114class WinCOFFObjectWriter : public MCObjectWriter {
115public:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000116 typedef std::vector<std::unique_ptr<COFFSymbol>> symbols;
David Blaikief564ab62014-04-15 05:25:03 +0000117 typedef std::vector<std::unique_ptr<COFFSection>> sections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000118
Rafael Espindola11e9e212015-05-27 14:37:12 +0000119 typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000120 typedef DenseMap<MCSection const *, COFFSection *> section_map;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000121
Ahmed Charles56440fd2014-03-06 05:51:42 +0000122 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000123
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000124 // Root level file contents.
125 COFF::header Header;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000126 sections Sections;
127 symbols Symbols;
Rafael Espindola21956e42015-10-23 21:48:05 +0000128 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000129
130 // Maps used during object file creation.
131 section_map SectionMap;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000132 symbol_map SymbolMap;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000133
David Majnemer4d571592014-09-15 19:42:42 +0000134 bool UseBigObj;
135
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000136 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000137
Yaron Kerencca43c12014-09-16 21:31:04 +0000138 void reset() override {
139 memset(&Header, 0, sizeof(Header));
140 Header.Machine = TargetObjectWriter->getMachine();
141 Sections.clear();
142 Symbols.clear();
143 Strings.clear();
144 SectionMap.clear();
145 SymbolMap.clear();
146 MCObjectWriter::reset();
147 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000148
Michael J. Spencer17990d52010-10-16 08:25:57 +0000149 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola11e9e212015-05-27 14:37:12 +0000150 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000151 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000152
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000153 template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000154 object_t *createCOFFEntity(StringRef Name, list_t &List);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000155
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000156 void defineSection(MCSectionCOFF const &Sec);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000157
158 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000159 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000160 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000161
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000162 void SetSymbolName(COFFSymbol &S);
163 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000164
Michael J. Spencerd6283772010-09-27 08:58:26 +0000165 bool IsPhysicalSection(COFFSection *S);
166
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000167 // Entity writing methods.
168
169 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000170 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000171 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000172 void writeSectionHeader(const COFF::section &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000173 void WriteRelocation(const COFF::relocation &R);
174
175 // MCObjectWriter interface implementation.
176
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000177 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper34a61bc2014-03-08 07:02:02 +0000178 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000179
Jim Grosbach36e60e92015-06-04 22:24:41 +0000180 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000181 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000182 const MCFragment &FB, bool InSet,
183 bool IsPCRel) const override;
184
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000185 bool isWeak(const MCSymbol &Sym) const override;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000186
Jim Grosbach36e60e92015-06-04 22:24:41 +0000187 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000188 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000189 MCValue Target, bool &IsPCRel,
190 uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000191
Jim Grosbach36e60e92015-06-04 22:24:41 +0000192 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000193};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000194}
Chris Lattner2c52b792010-07-11 22:07:02 +0000195
Hans Wennborgba80b5d2014-09-28 00:22:27 +0000196static inline void write_uint32_le(void *Data, uint32_t Value) {
197 support::endian::write<uint32_t, support::little, support::unaligned>(Data,
198 Value);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000199}
200
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000201//------------------------------------------------------------------------------
202// Symbol class implementation
203
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000204COFFSymbol::COFFSymbol(StringRef name)
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000205 : Name(name.begin(), name.end()), Other(nullptr), Section(nullptr),
206 Relocations(0), MC(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000207 memset(&Data, 0, sizeof(Data));
208}
209
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000210// In the case that the name does not fit within 8 bytes, the offset
211// into the string table is stored in the last 4 bytes instead, leaving
212// the first 4 bytes as 0.
213void COFFSymbol::set_name_offset(uint32_t Offset) {
214 write_uint32_le(Data.Name + 0, 0);
215 write_uint32_le(Data.Name + 4, Offset);
216}
217
218//------------------------------------------------------------------------------
219// Section class implementation
220
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000221COFFSection::COFFSection(StringRef name)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000222 : Name(name), MCSection(nullptr), Symbol(nullptr) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000223 memset(&Header, 0, sizeof(Header));
224}
225
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000226//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000227// WinCOFFObjectWriter class implementation
228
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000229WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000230 raw_pwrite_stream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000231 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000232 memset(&Header, 0, sizeof(Header));
Michael J. Spencer377aa202010-08-21 05:58:13 +0000233
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000234 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000235}
236
Michael J. Spencer17990d52010-10-16 08:25:57 +0000237COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000238 return createCOFFEntity<COFFSymbol>(Name, Symbols);
239}
240
Yaron Keren56919ef2014-12-04 08:30:39 +0000241COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000242 symbol_map::iterator i = SymbolMap.find(Symbol);
243 if (i != SymbolMap.end())
244 return i->second;
Yaron Keren56919ef2014-12-04 08:30:39 +0000245 COFFSymbol *RetSymbol =
246 createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000247 SymbolMap[Symbol] = RetSymbol;
248 return RetSymbol;
249}
250
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000251COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000252 return createCOFFEntity<COFFSection>(Name, Sections);
253}
254
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000255/// A template used to lookup or create a symbol/section, and initialize it if
256/// needed.
257template <typename object_t, typename list_t>
Rafael Espindola11e9e212015-05-27 14:37:12 +0000258object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name, list_t &List) {
David Blaikief564ab62014-04-15 05:25:03 +0000259 List.push_back(make_unique<object_t>(Name));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000260
David Blaikief564ab62014-04-15 05:25:03 +0000261 return List.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000262}
263
264/// This function takes a section data object from the assembler
265/// and creates the associated COFF section staging object.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000266void WinCOFFObjectWriter::defineSection(MCSectionCOFF const &Sec) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000267 COFFSection *coff_section = createSection(Sec.getSectionName());
Rafael Espindola11e9e212015-05-27 14:37:12 +0000268 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
Rafael Espindola0766ae02014-06-06 19:26:12 +0000269 if (Sec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
270 if (const MCSymbol *S = Sec.getCOMDATSymbol()) {
271 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
272 if (COMDATSymbol->Section)
273 report_fatal_error("two sections have the same comdat");
274 COMDATSymbol->Section = coff_section;
275 }
276 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000277
Michael J. Spencerd6283772010-09-27 08:58:26 +0000278 coff_section->Symbol = coff_symbol;
279 coff_symbol->Section = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000280 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000281
282 // In this case the auxiliary symbol is a Section Definition.
283 coff_symbol->Aux.resize(1);
284 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
285 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000286 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
287
288 coff_section->Header.Characteristics = Sec.getCharacteristics();
289
290 uint32_t &Characteristics = coff_section->Header.Characteristics;
Rafael Espindola967d6a62015-05-21 21:02:35 +0000291 switch (Sec.getAlignment()) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000292 case 1:
293 Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES;
294 break;
295 case 2:
296 Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES;
297 break;
298 case 4:
299 Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES;
300 break;
301 case 8:
302 Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES;
303 break;
304 case 16:
305 Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES;
306 break;
307 case 32:
308 Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES;
309 break;
310 case 64:
311 Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES;
312 break;
313 case 128:
314 Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES;
315 break;
316 case 256:
317 Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES;
318 break;
319 case 512:
320 Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES;
321 break;
322 case 1024:
323 Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES;
324 break;
325 case 2048:
326 Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES;
327 break;
328 case 4096:
329 Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES;
330 break;
331 case 8192:
332 Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES;
333 break;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000334 default:
335 llvm_unreachable("unsupported section alignment");
336 }
337
338 // Bind internal COFF section to MC section.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000339 coff_section->MCSection = &Sec;
340 SectionMap[&Sec] = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000341}
342
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000343static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000344 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000345 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000346 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000347
348 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000349 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000350 return 0;
351
352 return Res;
353}
354
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000355COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
356 if (!Symbol.isVariable())
357 return nullptr;
358
359 const MCSymbolRefExpr *SymRef =
360 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
361 if (!SymRef)
362 return nullptr;
363
364 const MCSymbol &Aliasee = SymRef->getSymbol();
365 if (!Aliasee.isUndefined())
366 return nullptr;
367 return GetOrCreateCOFFSymbol(&Aliasee);
368}
369
David Majnemera9bdb322014-04-08 22:33:40 +0000370/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000371/// and creates the associated COFF symbol staging object.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000372void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000373 MCAssembler &Assembler,
374 const MCAsmLayout &Layout) {
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000375 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000376 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
377 COFFSection *Sec = nullptr;
378 if (Base && Base->getFragment()) {
379 Sec = SectionMap[Base->getFragment()->getParent()];
380 if (coff_symbol->Section && coff_symbol->Section != Sec)
381 report_fatal_error("conflicting sections for symbol");
382 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000383
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000384 COFFSymbol *Local = nullptr;
Pete Cooper6bf1f302015-06-08 17:17:19 +0000385 if (cast<MCSymbolCOFF>(Symbol).isWeakExternal()) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000386 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
387
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000388 COFFSymbol *WeakDefault = getLinkedSymbol(Symbol);
389 if (!WeakDefault) {
Yaron Keren075759a2015-03-30 15:42:36 +0000390 std::string WeakName = (".weak." + Symbol.getName() + ".default").str();
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000391 WeakDefault = createSymbol(WeakName);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000392 if (!Sec)
393 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
394 else
395 WeakDefault->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000396 Local = WeakDefault;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000397 }
398
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000399 coff_symbol->Other = WeakDefault;
400
Michael J. Spencer17990d52010-10-16 08:25:57 +0000401 // Setup the Weak External auxiliary symbol.
402 coff_symbol->Aux.resize(1);
403 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
404 coff_symbol->Aux[0].AuxType = ATWeakExternal;
405 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
406 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000407 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000408 } else {
Rafael Espindola30c080a2016-05-26 18:48:23 +0000409 if (!Base)
Reid Klecknerc1e76212013-09-17 23:18:05 +0000410 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola30c080a2016-05-26 18:48:23 +0000411 else
412 coff_symbol->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000413 Local = coff_symbol;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000414 }
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000415
416 if (Local) {
417 Local->Data.Value = getSymbolValue(Symbol, Layout);
418
419 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(Symbol);
420 Local->Data.Type = SymbolCOFF.getType();
421 Local->Data.StorageClass = SymbolCOFF.getClass();
422
423 // If no storage class was specified in the streamer, define it here.
424 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
425 bool IsExternal = Symbol.isExternal() ||
426 (!Symbol.getFragment() && !Symbol.isVariable());
427
428 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
429 : COFF::IMAGE_SYM_CLASS_STATIC;
430 }
431 }
432
433 coff_symbol->MC = &Symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000434}
435
Nico Rieck01143f92014-02-25 09:50:40 +0000436// Maximum offsets for different string table entry encodings.
David Majnemerf088f522016-01-24 20:46:11 +0000437enum : unsigned { Max7DecimalOffset = 9999999U };
438enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
Nico Rieck01143f92014-02-25 09:50:40 +0000439
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000440// Encode a string table entry offset in base 64, padded to 6 chars, and
441// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
442// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000443static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000444 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000445 "Illegal section name encoding for value");
446
447 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
448 "abcdefghijklmnopqrstuvwxyz"
449 "0123456789+/";
450
451 Buffer[0] = '/';
452 Buffer[1] = '/';
453
Rafael Espindola11e9e212015-05-27 14:37:12 +0000454 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000455 for (unsigned i = 0; i < 6; ++i) {
456 unsigned Rem = Value % 64;
457 Value /= 64;
458 *(Ptr--) = Alphabet[Rem];
459 }
460}
461
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000462void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000463 if (S.Name.size() > COFF::NameSize) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000464 uint64_t StringTableEntry = Strings.getOffset(S.Name);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000465
David Majnemerf088f522016-01-24 20:46:11 +0000466 if (StringTableEntry <= Max7DecimalOffset) {
467 SmallVector<char, COFF::NameSize> Buffer;
468 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
469 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
470
471 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
Nico Rieck01143f92014-02-25 09:50:40 +0000472 } else if (StringTableEntry <= MaxBase64Offset) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000473 // Starting with 10,000,000, offsets are encoded as base64.
474 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000475 } else {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000476 report_fatal_error("COFF string table is greater than 64 GB.");
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000477 }
David Majnemerf088f522016-01-24 20:46:11 +0000478 } else {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000479 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
David Majnemerf088f522016-01-24 20:46:11 +0000480 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000481}
482
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000483void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
484 if (S.Name.size() > COFF::NameSize)
485 S.set_name_offset(Strings.getOffset(S.Name));
486 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000487 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000488}
489
Michael J. Spencerd6283772010-09-27 08:58:26 +0000490bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000491 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
492 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000493}
494
495//------------------------------------------------------------------------------
496// entity writing methods
497
498void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000499 if (UseBigObj) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000500 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
501 writeLE16(0xFFFF);
502 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
503 writeLE16(Header.Machine);
504 writeLE32(Header.TimeDateStamp);
505 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
506 writeLE32(0);
507 writeLE32(0);
508 writeLE32(0);
509 writeLE32(0);
510 writeLE32(Header.NumberOfSections);
511 writeLE32(Header.PointerToSymbolTable);
512 writeLE32(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000513 } else {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000514 writeLE16(Header.Machine);
515 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
516 writeLE32(Header.TimeDateStamp);
517 writeLE32(Header.PointerToSymbolTable);
518 writeLE32(Header.NumberOfSymbols);
519 writeLE16(Header.SizeOfOptionalHeader);
520 writeLE16(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000521 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000522}
523
David Blaikief564ab62014-04-15 05:25:03 +0000524void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000525 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
526 writeLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000527 if (UseBigObj)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000528 writeLE32(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000529 else
Jim Grosbach36e60e92015-06-04 22:24:41 +0000530 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
531 writeLE16(S.Data.Type);
532 write8(S.Data.StorageClass);
533 write8(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000534 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000535}
536
537void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000538 const COFFSymbol::AuxiliarySymbols &S) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000539 for (const AuxSymbol &i : S) {
540 switch (i.AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000541 case ATFunctionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000542 writeLE32(i.Aux.FunctionDefinition.TagIndex);
543 writeLE32(i.Aux.FunctionDefinition.TotalSize);
544 writeLE32(i.Aux.FunctionDefinition.PointerToLinenumber);
545 writeLE32(i.Aux.FunctionDefinition.PointerToNextFunction);
546 WriteZeros(sizeof(i.Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000547 if (UseBigObj)
548 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000549 break;
550 case ATbfAndefSymbol:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000551 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused1));
552 writeLE16(i.Aux.bfAndefSymbol.Linenumber);
553 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused2));
554 writeLE32(i.Aux.bfAndefSymbol.PointerToNextFunction);
555 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000556 if (UseBigObj)
557 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000558 break;
559 case ATWeakExternal:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000560 writeLE32(i.Aux.WeakExternal.TagIndex);
561 writeLE32(i.Aux.WeakExternal.Characteristics);
562 WriteZeros(sizeof(i.Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000563 if (UseBigObj)
564 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000565 break;
566 case ATFile:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000567 writeBytes(
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000568 StringRef(reinterpret_cast<const char *>(&i.Aux),
David Majnemer4d571592014-09-15 19:42:42 +0000569 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000570 break;
571 case ATSectionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000572 writeLE32(i.Aux.SectionDefinition.Length);
573 writeLE16(i.Aux.SectionDefinition.NumberOfRelocations);
574 writeLE16(i.Aux.SectionDefinition.NumberOfLinenumbers);
575 writeLE32(i.Aux.SectionDefinition.CheckSum);
576 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
577 write8(i.Aux.SectionDefinition.Selection);
578 WriteZeros(sizeof(i.Aux.SectionDefinition.unused));
579 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000580 if (UseBigObj)
581 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000582 break;
583 }
584 }
585}
586
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000587void WinCOFFObjectWriter::writeSectionHeader(const COFF::section &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000588 writeBytes(StringRef(S.Name, COFF::NameSize));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000589
Jim Grosbach36e60e92015-06-04 22:24:41 +0000590 writeLE32(S.VirtualSize);
591 writeLE32(S.VirtualAddress);
592 writeLE32(S.SizeOfRawData);
593 writeLE32(S.PointerToRawData);
594 writeLE32(S.PointerToRelocations);
595 writeLE32(S.PointerToLineNumbers);
596 writeLE16(S.NumberOfRelocations);
597 writeLE16(S.NumberOfLineNumbers);
598 writeLE32(S.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000599}
600
601void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000602 writeLE32(R.VirtualAddress);
603 writeLE32(R.SymbolTableIndex);
604 writeLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000605}
606
607////////////////////////////////////////////////////////////////////////////////
608// MCObjectWriter interface implementations
609
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000610void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000611 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000612 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000613 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000614 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000615 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000616
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000617 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000618 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000619 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000620}
621
Jim Grosbach36e60e92015-06-04 22:24:41 +0000622bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000623 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000624 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000625 // MS LINK expects to be able to replace all references to a function with a
626 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
627 // away any relocations to functions.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000628 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
David Majnemer03e2cc32015-12-21 22:09:27 +0000629 if (Asm.isIncrementalLinkerCompatible() &&
630 (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000631 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000632 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000633 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000634}
635
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000636bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000637 if (!Sym.isExternal())
Rafael Espindola88af4112015-04-17 11:27:13 +0000638 return false;
639
Rafael Espindola88af4112015-04-17 11:27:13 +0000640 if (!Sym.isInSection())
641 return false;
642
643 const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
644 if (!Sec.getCOMDATSymbol())
645 return false;
646
647 // It looks like for COFF it is invalid to replace a reference to a global
648 // in a comdat with a reference to a local.
649 // FIXME: Add a specification reference if available.
650 return true;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000651}
652
Jim Grosbach36e60e92015-06-04 22:24:41 +0000653void WinCOFFObjectWriter::recordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000654 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
655 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000656 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000657
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000658 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000659 if (!A.isRegistered()) {
660 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000661 Twine("symbol '") + A.getName() +
662 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000663 return;
664 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000665 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000666 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000667 Twine("assembler label '") + A.getName() +
668 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000669 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000670 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000671
Rafael Espindola7549f872015-05-26 00:36:57 +0000672 MCSection *Section = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000673
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000674 // Mark this symbol as requiring an entry in the symbol table.
Rafael Espindola7549f872015-05-26 00:36:57 +0000675 assert(SectionMap.find(Section) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000676 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000677
Rafael Espindola7549f872015-05-26 00:36:57 +0000678 COFFSection *coff_section = SectionMap[Section];
Rafael Espindolaed164772011-04-20 14:01:45 +0000679 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000680 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000681
David Majnemera45a1762014-01-06 07:39:46 +0000682 if (SymB) {
683 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000684 if (!B->getFragment()) {
685 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000686 Fixup.getLoc(),
687 Twine("symbol '") + B->getName() +
688 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000689 return;
690 }
David Majnemera45a1762014-01-06 07:39:46 +0000691
Oliver Stannard9be59af2015-11-17 10:00:43 +0000692 if (!A.getFragment()) {
693 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000694 Fixup.getLoc(),
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000695 Twine("symbol '") + A.getName() +
David Majnemera45a1762014-01-06 07:39:46 +0000696 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000697 return;
698 }
David Majnemera45a1762014-01-06 07:39:46 +0000699
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000700 CrossSection = &A.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000701
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000702 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000703 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000704
705 // In the case where we have SymbA and SymB, we just need to store the delta
706 // between the two symbols. Update FixedValue to account for the delta, and
707 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000708 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000709 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000710 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000711 return;
David Majnemer1de30942015-02-09 06:31:31 +0000712 }
713
714 // Offset of the relocation in the section
715 int64_t OffsetOfRelocation =
716 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
717
Andy Ayers9e5c8512015-05-14 01:10:41 +0000718 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000719 } else {
720 FixedValue = Target.getConstant();
721 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000722
723 COFFRelocation Reloc;
724
Daniel Dunbar727be432010-07-31 21:08:54 +0000725 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000726 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000727
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000728 // Turn relocations for temporary symbols into section relocations.
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000729 if (A.isTemporary() || CrossSection) {
730 MCSection *TargetSection = &A.getSection();
731 assert(
732 SectionMap.find(TargetSection) != SectionMap.end() &&
733 "Section must already have been defined in executePostLayoutBinding!");
734 Reloc.Symb = SectionMap[TargetSection]->Symbol;
735 FixedValue += Layout.getSymbolOffset(A);
736 } else {
737 assert(
738 SymbolMap.find(&A) != SymbolMap.end() &&
739 "Symbol must already have been defined in executePostLayoutBinding!");
740 Reloc.Symb = SymbolMap[&A];
741 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000742
743 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000744
745 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000746 Reloc.Data.Type = TargetObjectWriter->getRelocType(
747 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000748
749 // FIXME: Can anyone explain what this does other than adjust for the size
750 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000751 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
752 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
753 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
754 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000755 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000756
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000757 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
758 switch (Reloc.Data.Type) {
759 case COFF::IMAGE_REL_ARM_ABSOLUTE:
760 case COFF::IMAGE_REL_ARM_ADDR32:
761 case COFF::IMAGE_REL_ARM_ADDR32NB:
762 case COFF::IMAGE_REL_ARM_TOKEN:
763 case COFF::IMAGE_REL_ARM_SECTION:
764 case COFF::IMAGE_REL_ARM_SECREL:
765 break;
766 case COFF::IMAGE_REL_ARM_BRANCH11:
767 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000768 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
769 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
770 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000771 case COFF::IMAGE_REL_ARM_BRANCH24:
772 case COFF::IMAGE_REL_ARM_BLX24:
773 case COFF::IMAGE_REL_ARM_MOV32A:
774 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
775 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000776 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000777 // generate the relocations however the rest of the MSVC toolchain is
778 // unable to handle it.
779 llvm_unreachable("unsupported relocation");
780 break;
781 case COFF::IMAGE_REL_ARM_MOV32T:
782 break;
783 case COFF::IMAGE_REL_ARM_BRANCH20T:
784 case COFF::IMAGE_REL_ARM_BRANCH24T:
785 case COFF::IMAGE_REL_ARM_BLX23T:
786 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
787 // perform a 4 byte adjustment to the relocation. Relative branches are
788 // offset by 4 on ARM, however, because there is no RELA relocations, all
789 // branches are offset by 4.
790 FixedValue = FixedValue + 4;
791 break;
792 }
793 }
794
David Majnemer408b5e62016-02-05 01:55:49 +0000795 // The fixed value never makes sense for section indicies, ignore it.
796 if (Fixup.getKind() == FK_SecRel_2)
797 FixedValue = 0;
798
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000799 if (TargetObjectWriter->recordRelocation(Fixup))
800 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000801}
802
Jim Grosbach36e60e92015-06-04 22:24:41 +0000803void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000804 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000805 size_t SectionsSize = Sections.size();
806 if (SectionsSize > static_cast<size_t>(INT32_MAX))
807 report_fatal_error(
808 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000809
David Majnemer4d571592014-09-15 19:42:42 +0000810 // Assign symbol and section indexes and offsets.
811 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
812
813 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
814
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000815 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000816 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000817 for (const auto &Section : Sections) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000818 Section->Number = Number;
819 Section->Symbol->Data.SectionNumber = Number;
820 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000821 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000822 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000823
David Majnemer4d571592014-09-15 19:42:42 +0000824 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000825 Header.NumberOfSymbols = 0;
826
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000827 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000828 // round up to calculate the number of auxiliary symbols required
829 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000830 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000831
832 COFFSymbol *file = createSymbol(".file");
833 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
834 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
835 file->Aux.resize(Count);
836
837 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000838 unsigned Length = Name.size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000839 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000840 Aux.AuxType = ATFile;
841
842 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000843 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000844 Length = Length - SymbolSize;
845 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000846 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000847 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
848 break;
849 }
850
851 Offset += SymbolSize;
852 }
853 }
854
David Majnemer9ab5ff12014-08-28 04:02:50 +0000855 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000856 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000857 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000858 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000859 Symbol->setIndex(Header.NumberOfSymbols++);
860 // Update auxiliary symbol info.
861 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
862 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000863 }
864
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000865 // Build string table.
866 for (const auto &S : Sections)
867 if (S->Name.size() > COFF::NameSize)
868 Strings.add(S->Name);
869 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000870 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000871 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000872 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000873
874 // Set names.
875 for (const auto &S : Sections)
876 SetSectionName(*S);
877 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000878 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000879
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000880 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000881 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000882 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000883 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000884 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
885 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000886 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000887 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000888 }
889 }
890
Nico Riecka37acf72013-07-06 12:13:10 +0000891 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000892 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000893 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000894 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
895 continue;
896
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000897 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +0000898
Rafael Espindola0766ae02014-06-06 19:26:12 +0000899 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
900 assert(COMDAT);
901 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
902 assert(COMDATSymbol);
903 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000904 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000905 report_fatal_error(
906 Twine("Missing associated COMDAT section for section ") +
907 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000908
909 // Skip this section if the associated section is unused.
910 if (Assoc->Number == -1)
911 continue;
912
David Majnemer4eecd302015-05-30 04:56:02 +0000913 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +0000914 }
915
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000916 // Assign file offsets to COFF object file structures.
917
Manuel Klimek272d3f12015-11-18 15:24:17 +0000918 unsigned offset = getInitialOffset();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000919
David Majnemer4d571592014-09-15 19:42:42 +0000920 if (UseBigObj)
921 offset += COFF::Header32Size;
922 else
923 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000924 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000925
Yaron Keren56919ef2014-12-04 08:30:39 +0000926 for (const auto &Section : Asm) {
Rafael Espindolaa554c052015-05-25 23:14:17 +0000927 COFFSection *Sec = SectionMap[&Section];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000928
Michael J. Spencerd6283772010-09-27 08:58:26 +0000929 if (Sec->Number == -1)
930 continue;
931
Rafael Espindola5a1e80b2015-05-26 02:00:36 +0000932 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000933
Michael J. Spencerd6283772010-09-27 08:58:26 +0000934 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000935 // Align the section data to a four byte boundary.
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000936 offset = alignTo(offset, 4);
David Majnemerab2b25b2015-02-11 22:51:55 +0000937 Sec->Header.PointerToRawData = offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000938
939 offset += Sec->Header.SizeOfRawData;
940 }
941
942 if (Sec->Relocations.size() > 0) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000943 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
944
945 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000946 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000947 // size is found in reloc #0. Microsoft tools understand this.
948 Sec->Header.NumberOfRelocations = 0xffff;
949 } else {
950 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
951 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000952 Sec->Header.PointerToRelocations = offset;
953
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000954 if (RelocationsOverflow) {
955 // Reloc #0 will contain actual count, so make room for it.
956 offset += COFF::RelocationSize;
957 }
958
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000959 offset += COFF::RelocationSize * Sec->Relocations.size();
960
Yaron Keren56919ef2014-12-04 08:30:39 +0000961 for (auto &Relocation : Sec->Relocations) {
David Majnemer4eecd302015-05-30 04:56:02 +0000962 assert(Relocation.Symb->getIndex() != -1);
963 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000964 }
965 }
966
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000967 assert(Sec->Symbol->Aux.size() == 1 &&
968 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000969 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000970 assert(Aux.AuxType == ATSectionDefinition &&
971 "Section's symbol's aux symbol must be a Section Definition!");
972 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
973 Aux.Aux.SectionDefinition.NumberOfRelocations =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000974 Sec->Header.NumberOfRelocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000975 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000976 Sec->Header.NumberOfLineNumbers;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000977 }
978
979 Header.PointerToSymbolTable = offset;
980
David Majnemer088ba022015-09-01 23:46:11 +0000981 // MS LINK expects to be able to use this timestamp to implement their
982 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +0000983 if (Asm.isIncrementalLinkerCompatible()) {
984 std::time_t Now = time(nullptr);
985 if (Now < 0 || !isUInt<32>(Now))
986 Now = UINT32_MAX;
987 Header.TimeDateStamp = Now;
988 } else {
Nico Weber891419a2016-01-06 19:05:19 +0000989 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +0000990 Header.TimeDateStamp = 0;
991 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +0000992
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000993 // Write it all to disk...
994 WriteFileHeader(Header);
995
996 {
997 sections::iterator i, ie;
Rafael Espindola63702e22015-06-01 01:30:01 +0000998 MCAssembler::iterator j, je;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000999
Yaron Keren56919ef2014-12-04 08:30:39 +00001000 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001001 if (Section->Number != -1) {
1002 if (Section->Relocations.size() >= 0xffff)
1003 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Jim Grosbach56ed0bb2015-06-04 23:25:54 +00001004 writeSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001005 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001006 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001007
David Majnemer6ddc6362015-09-01 21:23:58 +00001008 SmallVector<char, 128> SectionContents;
Rafael Espindola11e9e212015-05-27 14:37:12 +00001009 for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
1010 je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +00001011 (i != ie) && (j != je); ++i, ++j) {
1012
1013 if ((*i)->Number == -1)
1014 continue;
1015
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001016 if ((*i)->Header.PointerToRawData != 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001017 assert(getStream().tell() <= (*i)->Header.PointerToRawData &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001018 "Section::PointerToRawData is insane!");
1019
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001020 unsigned SectionDataPadding =
1021 (*i)->Header.PointerToRawData - getStream().tell();
David Majnemer3df3c612015-02-11 22:22:30 +00001022 assert(SectionDataPadding < 4 &&
1023 "Should only need at most three bytes of padding!");
1024
1025 WriteZeros(SectionDataPadding);
1026
David Majnemer6ddc6362015-09-01 21:23:58 +00001027 // Save the contents of the section to a temporary buffer, we need this
1028 // to CRC the data before we dump it into the object file.
1029 SectionContents.clear();
1030 raw_svector_ostream VecOS(SectionContents);
1031 raw_pwrite_stream &OldStream = getStream();
1032 // Redirect the output stream to our buffer.
1033 setStream(VecOS);
1034 // Fill our buffer with the section data.
Rafael Espindola64acc7f2015-05-26 02:17:21 +00001035 Asm.writeSectionData(&*j, Layout);
David Majnemer6ddc6362015-09-01 21:23:58 +00001036 // Reset the stream back to what it was before.
1037 setStream(OldStream);
1038
1039 // Calculate our CRC with an initial value of '0', this is not how
1040 // JamCRC is specified but it aligns with the expected output.
1041 JamCRC JC(/*Init=*/0x00000000U);
1042 JC.update(SectionContents);
1043
1044 // Write the section contents to the object file.
1045 getStream() << SectionContents;
1046
1047 // Update the section definition auxiliary symbol to record the CRC.
1048 COFFSection *Sec = SectionMap[&*j];
1049 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
1050 assert(AuxSyms.size() == 1 &&
1051 AuxSyms[0].AuxType == ATSectionDefinition);
1052 AuxSymbol &SecDef = AuxSyms[0];
1053 SecDef.Aux.SectionDefinition.CheckSum = JC.getCRC();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001054 }
1055
1056 if ((*i)->Relocations.size() > 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001057 assert(getStream().tell() == (*i)->Header.PointerToRelocations &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001058 "Section::PointerToRelocations is insane!");
1059
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001060 if ((*i)->Relocations.size() >= 0xffff) {
1061 // In case of overflow, write actual relocation count as first
1062 // relocation. Including the synthetic reloc itself (+ 1).
1063 COFF::relocation r;
1064 r.VirtualAddress = (*i)->Relocations.size() + 1;
1065 r.SymbolTableIndex = 0;
1066 r.Type = 0;
1067 WriteRelocation(r);
1068 }
1069
Yaron Keren56919ef2014-12-04 08:30:39 +00001070 for (const auto &Relocation : (*i)->Relocations)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001071 WriteRelocation(Relocation.Data);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001072 } else
1073 assert((*i)->Header.PointerToRelocations == 0 &&
1074 "Section::PointerToRelocations is insane!");
1075 }
1076 }
1077
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001078 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001079 "Header::PointerToSymbolTable is insane!");
1080
Yaron Keren56919ef2014-12-04 08:30:39 +00001081 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001082 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001083 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001084
Rafael Espindola39751af2016-10-04 22:43:25 +00001085 Strings.write(getStream());
Chris Lattner2c52b792010-07-11 22:07:02 +00001086}
1087
Rafael Espindola11e9e212015-05-27 14:37:12 +00001088MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1089 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001090
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001091// Pin the vtable to this file.
1092void MCWinCOFFObjectTargetWriter::anchor() {}
1093
Chris Lattner2c52b792010-07-11 22:07:02 +00001094//------------------------------------------------------------------------------
1095// WinCOFFObjectWriter factory function
1096
Rafael Espindola37099d92015-04-09 18:08:15 +00001097MCObjectWriter *
1098llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001099 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001100 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001101}