blob: 1dfb1cf440f34a073626bb08bb0c06fe85330cc8 [file] [log] [blame]
Eugene Zelenko1d435522017-02-07 23:02:00 +00001//===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
Chris Lattner2c52b792010-07-11 22:07:02 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner2c52b792010-07-11 22:07:02 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains an implementation of a Win32 COFF object file writer.
10//
11//===----------------------------------------------------------------------===//
12
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000013#include "llvm/ADT/DenseMap.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/ADT/STLExtras.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/SmallVector.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000017#include "llvm/ADT/StringRef.h"
Nico Riecka37acf72013-07-06 12:13:10 +000018#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000019#include "llvm/BinaryFormat/COFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCAsmLayout.h"
21#include "llvm/MC/MCAssembler.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000024#include "llvm/MC/MCFixup.h"
25#include "llvm/MC/MCFragment.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/MC/MCObjectWriter.h"
27#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCSectionCOFF.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000029#include "llvm/MC/MCSymbol.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000030#include "llvm/MC/MCSymbolCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/MC/MCValue.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000032#include "llvm/MC/MCWinCOFFObjectWriter.h"
Hans Wennborgf26bfc12014-09-29 22:43:20 +000033#include "llvm/MC/StringTableBuilder.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000034#include "llvm/Support/Casting.h"
Hans Wennborgba80b5d2014-09-28 00:22:27 +000035#include "llvm/Support/Endian.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000036#include "llvm/Support/ErrorHandling.h"
David Majnemer6ddc6362015-09-01 21:23:58 +000037#include "llvm/Support/JamCRC.h"
Peter Collingbournebc3089f2018-08-22 23:58:16 +000038#include "llvm/Support/LEB128.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000039#include "llvm/Support/MathExtras.h"
40#include "llvm/Support/raw_ostream.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000041#include <algorithm>
Eugene Zelenko1d435522017-02-07 23:02:00 +000042#include <cassert>
43#include <cstddef>
44#include <cstdint>
45#include <cstring>
David Majnemer088ba022015-09-01 23:46:11 +000046#include <ctime>
Eugene Zelenko1d435522017-02-07 23:02:00 +000047#include <memory>
48#include <string>
49#include <vector>
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000050
Chris Lattner2c52b792010-07-11 22:07:02 +000051using namespace llvm;
Rui Ueyama86e3ef92017-02-14 23:28:19 +000052using llvm::support::endian::write32le;
Chris Lattner2c52b792010-07-11 22:07:02 +000053
Chandler Carruthf58e3762014-04-22 03:04:17 +000054#define DEBUG_TYPE "WinCOFFObjectWriter"
55
Chris Lattner2c52b792010-07-11 22:07:02 +000056namespace {
Eugene Zelenko1d435522017-02-07 23:02:00 +000057
Eugene Zelenko7975b992017-04-26 22:31:39 +000058using name = SmallString<COFF::NameSize>;
Chris Lattner2c52b792010-07-11 22:07:02 +000059
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000060enum AuxiliaryType {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000061 ATWeakExternal,
62 ATFile,
63 ATSectionDefinition
64};
Chris Lattner2c52b792010-07-11 22:07:02 +000065
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000066struct AuxSymbol {
Rafael Espindola11e9e212015-05-27 14:37:12 +000067 AuxiliaryType AuxType;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000068 COFF::Auxiliary Aux;
69};
Chris Lattner2c52b792010-07-11 22:07:02 +000070
Michael J. Spencerd6283772010-09-27 08:58:26 +000071class COFFSection;
72
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000073class COFFSymbol {
74public:
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +000075 COFF::symbol Data = {};
Chris Lattner2c52b792010-07-11 22:07:02 +000076
Eugene Zelenko7975b992017-04-26 22:31:39 +000077 using AuxiliarySymbols = SmallVector<AuxSymbol, 1>;
Chris Lattner2c52b792010-07-11 22:07:02 +000078
Rafael Espindola11e9e212015-05-27 14:37:12 +000079 name Name;
80 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000081 AuxiliarySymbols Aux;
Eugene Zelenko1d435522017-02-07 23:02:00 +000082 COFFSymbol *Other = nullptr;
83 COFFSection *Section = nullptr;
84 int Relocations = 0;
85 const MCSymbol *MC = nullptr;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000086
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +000087 COFFSymbol(StringRef Name) : Name(Name) {}
Eugene Zelenko1d435522017-02-07 23:02:00 +000088
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000089 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000090
David Majnemer4eecd302015-05-30 04:56:02 +000091 int64_t getIndex() const { return Index; }
92 void setIndex(int Value) {
93 Index = Value;
94 if (MC)
95 MC->setIndex(static_cast<uint32_t>(Value));
96 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000097};
98
99// This class contains staging data for a COFF relocation entry.
100struct COFFRelocation {
101 COFF::relocation Data;
Eugene Zelenko1d435522017-02-07 23:02:00 +0000102 COFFSymbol *Symb = nullptr;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000103
Eugene Zelenko1d435522017-02-07 23:02:00 +0000104 COFFRelocation() = default;
105
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000106 static size_t size() { return COFF::RelocationSize; }
107};
108
Eugene Zelenko7975b992017-04-26 22:31:39 +0000109using relocations = std::vector<COFFRelocation>;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000110
111class COFFSection {
112public:
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000113 COFF::section Header = {};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000114
Rafael Espindola11e9e212015-05-27 14:37:12 +0000115 std::string Name;
116 int Number;
Eugene Zelenko1d435522017-02-07 23:02:00 +0000117 MCSectionCOFF const *MCSection = nullptr;
118 COFFSymbol *Symbol = nullptr;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000119 relocations Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000120
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000121 COFFSection(StringRef Name) : Name(Name) {}
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000122};
123
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000124class WinCOFFObjectWriter : public MCObjectWriter {
125public:
Peter Collingbournef17b1492018-05-21 18:17:42 +0000126 support::endian::Writer W;
127
Eugene Zelenko7975b992017-04-26 22:31:39 +0000128 using symbols = std::vector<std::unique_ptr<COFFSymbol>>;
129 using sections = std::vector<std::unique_ptr<COFFSection>>;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000130
Eugene Zelenko7975b992017-04-26 22:31:39 +0000131 using symbol_map = DenseMap<MCSymbol const *, COFFSymbol *>;
132 using section_map = DenseMap<MCSection const *, COFFSection *>;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000133
Ahmed Charles56440fd2014-03-06 05:51:42 +0000134 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000135
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000136 // Root level file contents.
Rui Ueyamacbb4e7c2017-02-14 23:28:01 +0000137 COFF::header Header = {};
Rafael Espindola11e9e212015-05-27 14:37:12 +0000138 sections Sections;
139 symbols Symbols;
Rafael Espindola21956e42015-10-23 21:48:05 +0000140 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000141
142 // Maps used during object file creation.
143 section_map SectionMap;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000144 symbol_map SymbolMap;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000145
David Majnemer4d571592014-09-15 19:42:42 +0000146 bool UseBigObj;
147
Peter Collingbournebc3089f2018-08-22 23:58:16 +0000148 bool EmitAddrsigSection = false;
149 MCSectionCOFF *AddrsigSection;
150 std::vector<const MCSymbol *> AddrsigSyms;
151
Lang Hames77dff392017-10-10 00:50:29 +0000152 WinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
153 raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000154
Yaron Kerencca43c12014-09-16 21:31:04 +0000155 void reset() override {
156 memset(&Header, 0, sizeof(Header));
157 Header.Machine = TargetObjectWriter->getMachine();
158 Sections.clear();
159 Symbols.clear();
160 Strings.clear();
161 SectionMap.clear();
162 SymbolMap.clear();
163 MCObjectWriter::reset();
164 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000165
Michael J. Spencer17990d52010-10-16 08:25:57 +0000166 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola11e9e212015-05-27 14:37:12 +0000167 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000168 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000169
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000170 void defineSection(MCSectionCOFF const &Sec);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000171
172 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000173 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000174 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000175
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000176 void SetSymbolName(COFFSymbol &S);
177 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000178
Michael J. Spencerd6283772010-09-27 08:58:26 +0000179 bool IsPhysicalSection(COFFSection *S);
180
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000181 // Entity writing methods.
182
183 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000184 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000185 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
Rui Ueyamaac20c172017-02-17 17:32:54 +0000186 void writeSectionHeaders();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000187 void WriteRelocation(const COFF::relocation &R);
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000188 uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout,
189 const MCSection &MCSec);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000190 void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
191 const COFFSection &Sec, const MCSection &MCSec);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000192
193 // MCObjectWriter interface implementation.
194
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000195 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper34a61bc2014-03-08 07:02:02 +0000196 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000197
Jim Grosbach36e60e92015-06-04 22:24:41 +0000198 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000199 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000200 const MCFragment &FB, bool InSet,
201 bool IsPCRel) const override;
202
Jim Grosbach36e60e92015-06-04 22:24:41 +0000203 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000204 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindolaceecfe5b2017-07-11 23:56:10 +0000205 MCValue Target, uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000206
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000207 void createFileSymbols(MCAssembler &Asm);
208 void assignSectionNumbers();
209 void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout);
210
Peter Collingbournebc3089f2018-08-22 23:58:16 +0000211 void emitAddrsigSection() override { EmitAddrsigSection = true; }
212 void addAddrsigSymbol(const MCSymbol *Sym) override {
213 AddrsigSyms.push_back(Sym);
214 }
215
Peter Collingbourne438390f2018-05-21 18:23:50 +0000216 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000217};
Eugene Zelenko1d435522017-02-07 23:02:00 +0000218
219} // end anonymous namespace
Chris Lattner2c52b792010-07-11 22:07:02 +0000220
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000221//------------------------------------------------------------------------------
222// Symbol class implementation
223
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000224// In the case that the name does not fit within 8 bytes, the offset
225// into the string table is stored in the last 4 bytes instead, leaving
226// the first 4 bytes as 0.
227void COFFSymbol::set_name_offset(uint32_t Offset) {
Rui Ueyama86e3ef92017-02-14 23:28:19 +0000228 write32le(Data.Name + 0, 0);
229 write32le(Data.Name + 4, Offset);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000230}
231
232//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000233// WinCOFFObjectWriter class implementation
234
Lang Hames77dff392017-10-10 00:50:29 +0000235WinCOFFObjectWriter::WinCOFFObjectWriter(
236 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS)
Peter Collingbourne59a6fc42018-05-21 18:28:57 +0000237 : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000238 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000239}
240
Michael J. Spencer17990d52010-10-16 08:25:57 +0000241COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000242 Symbols.push_back(make_unique<COFFSymbol>(Name));
243 return Symbols.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000244}
245
Yaron Keren56919ef2014-12-04 08:30:39 +0000246COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Rui Ueyama0fcdb482017-02-14 23:47:34 +0000247 COFFSymbol *&Ret = SymbolMap[Symbol];
248 if (!Ret)
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000249 Ret = createSymbol(Symbol->getName());
Rui Ueyama0fcdb482017-02-14 23:47:34 +0000250 return Ret;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000251}
252
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000253COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Rui Ueyamadfc8aa82017-02-14 23:58:19 +0000254 Sections.emplace_back(make_unique<COFFSection>(Name));
255 return Sections.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000256}
257
Rui Ueyama24e27b42017-02-15 00:15:54 +0000258static uint32_t getAlignment(const MCSectionCOFF &Sec) {
259 switch (Sec.getAlignment()) {
260 case 1:
261 return COFF::IMAGE_SCN_ALIGN_1BYTES;
262 case 2:
263 return COFF::IMAGE_SCN_ALIGN_2BYTES;
264 case 4:
265 return COFF::IMAGE_SCN_ALIGN_4BYTES;
266 case 8:
267 return COFF::IMAGE_SCN_ALIGN_8BYTES;
268 case 16:
269 return COFF::IMAGE_SCN_ALIGN_16BYTES;
270 case 32:
271 return COFF::IMAGE_SCN_ALIGN_32BYTES;
272 case 64:
273 return COFF::IMAGE_SCN_ALIGN_64BYTES;
274 case 128:
275 return COFF::IMAGE_SCN_ALIGN_128BYTES;
276 case 256:
277 return COFF::IMAGE_SCN_ALIGN_256BYTES;
278 case 512:
279 return COFF::IMAGE_SCN_ALIGN_512BYTES;
280 case 1024:
281 return COFF::IMAGE_SCN_ALIGN_1024BYTES;
282 case 2048:
283 return COFF::IMAGE_SCN_ALIGN_2048BYTES;
284 case 4096:
285 return COFF::IMAGE_SCN_ALIGN_4096BYTES;
286 case 8192:
287 return COFF::IMAGE_SCN_ALIGN_8192BYTES;
288 }
289 llvm_unreachable("unsupported section alignment");
290}
291
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000292/// This function takes a section data object from the assembler
293/// and creates the associated COFF section staging object.
Rui Ueyama09786c42017-02-15 00:28:48 +0000294void WinCOFFObjectWriter::defineSection(const MCSectionCOFF &MCSec) {
295 COFFSection *Section = createSection(MCSec.getSectionName());
296 COFFSymbol *Symbol = createSymbol(MCSec.getSectionName());
297 Section->Symbol = Symbol;
298 Symbol->Section = Section;
299 Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Rui Ueyama24e27b42017-02-15 00:15:54 +0000300
301 // Create a COMDAT symbol if needed.
Rui Ueyama09786c42017-02-15 00:28:48 +0000302 if (MCSec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
303 if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
Rafael Espindola0766ae02014-06-06 19:26:12 +0000304 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
305 if (COMDATSymbol->Section)
306 report_fatal_error("two sections have the same comdat");
Rui Ueyama09786c42017-02-15 00:28:48 +0000307 COMDATSymbol->Section = Section;
Rafael Espindola0766ae02014-06-06 19:26:12 +0000308 }
309 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000310
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000311 // In this case the auxiliary symbol is a Section Definition.
Rui Ueyama09786c42017-02-15 00:28:48 +0000312 Symbol->Aux.resize(1);
313 Symbol->Aux[0] = {};
314 Symbol->Aux[0].AuxType = ATSectionDefinition;
315 Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000316
Rui Ueyama09786c42017-02-15 00:28:48 +0000317 // Set section alignment.
318 Section->Header.Characteristics = MCSec.getCharacteristics();
319 Section->Header.Characteristics |= getAlignment(MCSec);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000320
321 // Bind internal COFF section to MC section.
Rui Ueyama09786c42017-02-15 00:28:48 +0000322 Section->MCSection = &MCSec;
323 SectionMap[&MCSec] = Section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000324}
325
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000326static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000327 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000328 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000329 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000330
331 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000332 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000333 return 0;
334
335 return Res;
336}
337
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000338COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
339 if (!Symbol.isVariable())
340 return nullptr;
341
342 const MCSymbolRefExpr *SymRef =
343 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
344 if (!SymRef)
345 return nullptr;
346
347 const MCSymbol &Aliasee = SymRef->getSymbol();
348 if (!Aliasee.isUndefined())
349 return nullptr;
350 return GetOrCreateCOFFSymbol(&Aliasee);
351}
352
David Majnemera9bdb322014-04-08 22:33:40 +0000353/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000354/// and creates the associated COFF symbol staging object.
Rui Ueyama789c4222017-02-15 01:09:01 +0000355void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000356 MCAssembler &Assembler,
357 const MCAsmLayout &Layout) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000358 COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
359 const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000360 COFFSection *Sec = nullptr;
361 if (Base && Base->getFragment()) {
362 Sec = SectionMap[Base->getFragment()->getParent()];
Rui Ueyama789c4222017-02-15 01:09:01 +0000363 if (Sym->Section && Sym->Section != Sec)
Rafael Espindola30c080a2016-05-26 18:48:23 +0000364 report_fatal_error("conflicting sections for symbol");
365 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000366
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000367 COFFSymbol *Local = nullptr;
Rui Ueyama789c4222017-02-15 01:09:01 +0000368 if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
369 Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000370
Rui Ueyama789c4222017-02-15 01:09:01 +0000371 COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000372 if (!WeakDefault) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000373 std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000374 WeakDefault = createSymbol(WeakName);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000375 if (!Sec)
376 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
377 else
378 WeakDefault->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000379 Local = WeakDefault;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000380 }
381
Rui Ueyama789c4222017-02-15 01:09:01 +0000382 Sym->Other = WeakDefault;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000383
Michael J. Spencer17990d52010-10-16 08:25:57 +0000384 // Setup the Weak External auxiliary symbol.
Rui Ueyama789c4222017-02-15 01:09:01 +0000385 Sym->Aux.resize(1);
386 memset(&Sym->Aux[0], 0, sizeof(Sym->Aux[0]));
387 Sym->Aux[0].AuxType = ATWeakExternal;
388 Sym->Aux[0].Aux.WeakExternal.TagIndex = 0;
389 Sym->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000390 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000391 } else {
Rafael Espindola30c080a2016-05-26 18:48:23 +0000392 if (!Base)
Rui Ueyama789c4222017-02-15 01:09:01 +0000393 Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola30c080a2016-05-26 18:48:23 +0000394 else
Rui Ueyama789c4222017-02-15 01:09:01 +0000395 Sym->Section = Sec;
396 Local = Sym;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000397 }
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000398
399 if (Local) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000400 Local->Data.Value = getSymbolValue(MCSym, Layout);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000401
Rui Ueyama789c4222017-02-15 01:09:01 +0000402 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000403 Local->Data.Type = SymbolCOFF.getType();
404 Local->Data.StorageClass = SymbolCOFF.getClass();
405
406 // If no storage class was specified in the streamer, define it here.
407 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
Rui Ueyama789c4222017-02-15 01:09:01 +0000408 bool IsExternal = MCSym.isExternal() ||
409 (!MCSym.getFragment() && !MCSym.isVariable());
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000410
411 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
412 : COFF::IMAGE_SYM_CLASS_STATIC;
413 }
414 }
415
Rui Ueyama789c4222017-02-15 01:09:01 +0000416 Sym->MC = &MCSym;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000417}
418
Nico Rieck01143f92014-02-25 09:50:40 +0000419// Maximum offsets for different string table entry encodings.
David Majnemerf088f522016-01-24 20:46:11 +0000420enum : unsigned { Max7DecimalOffset = 9999999U };
421enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
Nico Rieck01143f92014-02-25 09:50:40 +0000422
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000423// Encode a string table entry offset in base 64, padded to 6 chars, and
424// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
425// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000426static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000427 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000428 "Illegal section name encoding for value");
429
430 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
431 "abcdefghijklmnopqrstuvwxyz"
432 "0123456789+/";
433
434 Buffer[0] = '/';
435 Buffer[1] = '/';
436
Rafael Espindola11e9e212015-05-27 14:37:12 +0000437 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000438 for (unsigned i = 0; i < 6; ++i) {
439 unsigned Rem = Value % 64;
440 Value /= 64;
441 *(Ptr--) = Alphabet[Rem];
442 }
443}
444
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000445void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000446 if (S.Name.size() <= COFF::NameSize) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000447 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000448 return;
David Majnemerf088f522016-01-24 20:46:11 +0000449 }
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000450
451 uint64_t StringTableEntry = Strings.getOffset(S.Name);
452 if (StringTableEntry <= Max7DecimalOffset) {
Rui Ueyama4b58f5772017-02-15 01:48:33 +0000453 SmallVector<char, COFF::NameSize> Buffer;
454 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
455 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
456 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
Rui Ueyamaa39d1482017-02-15 01:09:20 +0000457 return;
458 }
459 if (StringTableEntry <= MaxBase64Offset) {
460 // Starting with 10,000,000, offsets are encoded as base64.
461 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
462 return;
463 }
464 report_fatal_error("COFF string table is greater than 64 GB.");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000465}
466
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000467void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
468 if (S.Name.size() > COFF::NameSize)
469 S.set_name_offset(Strings.getOffset(S.Name));
470 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000471 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000472}
473
Michael J. Spencerd6283772010-09-27 08:58:26 +0000474bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000475 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
476 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000477}
478
479//------------------------------------------------------------------------------
480// entity writing methods
481
482void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000483 if (UseBigObj) {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000484 W.write<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
485 W.write<uint16_t>(0xFFFF);
486 W.write<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion);
487 W.write<uint16_t>(Header.Machine);
488 W.write<uint32_t>(Header.TimeDateStamp);
489 W.OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic));
490 W.write<uint32_t>(0);
491 W.write<uint32_t>(0);
492 W.write<uint32_t>(0);
493 W.write<uint32_t>(0);
494 W.write<uint32_t>(Header.NumberOfSections);
495 W.write<uint32_t>(Header.PointerToSymbolTable);
496 W.write<uint32_t>(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000497 } else {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000498 W.write<uint16_t>(Header.Machine);
499 W.write<uint16_t>(static_cast<int16_t>(Header.NumberOfSections));
500 W.write<uint32_t>(Header.TimeDateStamp);
501 W.write<uint32_t>(Header.PointerToSymbolTable);
502 W.write<uint32_t>(Header.NumberOfSymbols);
503 W.write<uint16_t>(Header.SizeOfOptionalHeader);
504 W.write<uint16_t>(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000505 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000506}
507
David Blaikief564ab62014-04-15 05:25:03 +0000508void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000509 W.OS.write(S.Data.Name, COFF::NameSize);
510 W.write<uint32_t>(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000511 if (UseBigObj)
Peter Collingbournef17b1492018-05-21 18:17:42 +0000512 W.write<uint32_t>(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000513 else
Peter Collingbournef17b1492018-05-21 18:17:42 +0000514 W.write<uint16_t>(static_cast<int16_t>(S.Data.SectionNumber));
515 W.write<uint16_t>(S.Data.Type);
516 W.OS << char(S.Data.StorageClass);
517 W.OS << char(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000518 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000519}
520
521void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000522 const COFFSymbol::AuxiliarySymbols &S) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000523 for (const AuxSymbol &i : S) {
524 switch (i.AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000525 case ATWeakExternal:
Peter Collingbournef17b1492018-05-21 18:17:42 +0000526 W.write<uint32_t>(i.Aux.WeakExternal.TagIndex);
527 W.write<uint32_t>(i.Aux.WeakExternal.Characteristics);
528 W.OS.write_zeros(sizeof(i.Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000529 if (UseBigObj)
Peter Collingbournef17b1492018-05-21 18:17:42 +0000530 W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000531 break;
532 case ATFile:
Peter Collingbournef17b1492018-05-21 18:17:42 +0000533 W.OS.write(reinterpret_cast<const char *>(&i.Aux),
534 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000535 break;
536 case ATSectionDefinition:
Peter Collingbournef17b1492018-05-21 18:17:42 +0000537 W.write<uint32_t>(i.Aux.SectionDefinition.Length);
538 W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfRelocations);
539 W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfLinenumbers);
540 W.write<uint32_t>(i.Aux.SectionDefinition.CheckSum);
541 W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
542 W.OS << char(i.Aux.SectionDefinition.Selection);
543 W.OS.write_zeros(sizeof(i.Aux.SectionDefinition.unused));
544 W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000545 if (UseBigObj)
Peter Collingbournef17b1492018-05-21 18:17:42 +0000546 W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000547 break;
548 }
549 }
550}
551
Rui Ueyamaac20c172017-02-17 17:32:54 +0000552// Write the section header.
553void WinCOFFObjectWriter::writeSectionHeaders() {
554 // Section numbers must be monotonically increasing in the section
555 // header, but our Sections array is not sorted by section number,
556 // so make a copy of Sections and sort it.
557 std::vector<COFFSection *> Arr;
558 for (auto &Section : Sections)
559 Arr.push_back(Section.get());
Fangrui Song0cac7262018-09-27 02:13:45 +0000560 llvm::sort(Arr, [](const COFFSection *A, const COFFSection *B) {
561 return A->Number < B->Number;
562 });
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000563
Rui Ueyamaac20c172017-02-17 17:32:54 +0000564 for (auto &Section : Arr) {
565 if (Section->Number == -1)
566 continue;
567
568 COFF::section &S = Section->Header;
569 if (Section->Relocations.size() >= 0xffff)
570 S.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Peter Collingbournef17b1492018-05-21 18:17:42 +0000571 W.OS.write(S.Name, COFF::NameSize);
572 W.write<uint32_t>(S.VirtualSize);
573 W.write<uint32_t>(S.VirtualAddress);
574 W.write<uint32_t>(S.SizeOfRawData);
575 W.write<uint32_t>(S.PointerToRawData);
576 W.write<uint32_t>(S.PointerToRelocations);
577 W.write<uint32_t>(S.PointerToLineNumbers);
578 W.write<uint16_t>(S.NumberOfRelocations);
579 W.write<uint16_t>(S.NumberOfLineNumbers);
580 W.write<uint32_t>(S.Characteristics);
Rui Ueyamaac20c172017-02-17 17:32:54 +0000581 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000582}
583
584void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000585 W.write<uint32_t>(R.VirtualAddress);
586 W.write<uint32_t>(R.SymbolTableIndex);
587 W.write<uint16_t>(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000588}
589
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000590// Write MCSec's contents. What this function does is essentially
591// "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
592// because it needs to compute a CRC.
593uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler &Asm,
594 const MCAsmLayout &Layout,
595 const MCSection &MCSec) {
596 // Save the contents of the section to a temporary buffer, we need this
597 // to CRC the data before we dump it into the object file.
598 SmallVector<char, 128> Buf;
599 raw_svector_ostream VecOS(Buf);
Peter Collingbourne147db3e2018-05-21 18:11:35 +0000600 Asm.writeSectionData(VecOS, &MCSec, Layout);
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000601
602 // Write the section contents to the object file.
Peter Collingbournef17b1492018-05-21 18:17:42 +0000603 W.OS << Buf;
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000604
605 // Calculate our CRC with an initial value of '0', this is not how
606 // JamCRC is specified but it aligns with the expected output.
607 JamCRC JC(/*Init=*/0);
608 JC.update(Buf);
609 return JC.getCRC();
610}
611
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000612void WinCOFFObjectWriter::writeSection(MCAssembler &Asm,
613 const MCAsmLayout &Layout,
614 const COFFSection &Sec,
615 const MCSection &MCSec) {
616 if (Sec.Number == -1)
617 return;
618
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000619 // Write the section contents.
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000620 if (Sec.Header.PointerToRawData != 0) {
Peter Collingbournea67161f2018-08-23 05:39:36 +0000621 assert(W.OS.tell() == Sec.Header.PointerToRawData &&
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000622 "Section::PointerToRawData is insane!");
623
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000624 uint32_t CRC = writeSectionContents(Asm, Layout, MCSec);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000625
626 // Update the section definition auxiliary symbol to record the CRC.
627 COFFSection *Sec = SectionMap[&MCSec];
628 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
629 assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
630 AuxSymbol &SecDef = AuxSyms[0];
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000631 SecDef.Aux.SectionDefinition.CheckSum = CRC;
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000632 }
633
Rui Ueyama26ca0bd2017-02-16 02:56:06 +0000634 // Write relocations for this section.
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000635 if (Sec.Relocations.empty()) {
636 assert(Sec.Header.PointerToRelocations == 0 &&
637 "Section::PointerToRelocations is insane!");
638 return;
639 }
640
Peter Collingbournef17b1492018-05-21 18:17:42 +0000641 assert(W.OS.tell() == Sec.Header.PointerToRelocations &&
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000642 "Section::PointerToRelocations is insane!");
643
644 if (Sec.Relocations.size() >= 0xffff) {
645 // In case of overflow, write actual relocation count as first
646 // relocation. Including the synthetic reloc itself (+ 1).
647 COFF::relocation R;
648 R.VirtualAddress = Sec.Relocations.size() + 1;
649 R.SymbolTableIndex = 0;
650 R.Type = 0;
651 WriteRelocation(R);
652 }
653
654 for (const auto &Relocation : Sec.Relocations)
655 WriteRelocation(Relocation.Data);
656}
657
Chris Lattner2c52b792010-07-11 22:07:02 +0000658////////////////////////////////////////////////////////////////////////////////
659// MCObjectWriter interface implementations
660
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000661void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000662 const MCAsmLayout &Layout) {
Peter Collingbournebc3089f2018-08-22 23:58:16 +0000663 if (EmitAddrsigSection) {
664 AddrsigSection = Asm.getContext().getCOFFSection(
665 ".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE,
666 SectionKind::getMetadata());
667 Asm.registerSection(*AddrsigSection);
668 }
669
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000670 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000671 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000672 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000673 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000674
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000675 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000676 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000677 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000678}
679
Jim Grosbach36e60e92015-06-04 22:24:41 +0000680bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000681 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000682 bool InSet, bool IsPCRel) const {
Reid Kleckner5a5ac652018-03-14 19:24:32 +0000683 // Don't drop relocations between functions, even if they are in the same text
684 // section. Multiple Visual C++ linker features depend on having the
685 // relocations present. The /INCREMENTAL flag will cause these relocations to
686 // point to thunks, and the /GUARD:CF flag assumes that it can use relocations
687 // to approximate the set of all address taken functions. LLD's implementation
688 // of /GUARD:CF also relies on the existance of these relocations.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000689 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
Reid Kleckner5a5ac652018-03-14 19:24:32 +0000690 if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000691 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000692 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000693 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000694}
695
Rafael Espindolaceecfe5b2017-07-11 23:56:10 +0000696void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
697 const MCAsmLayout &Layout,
698 const MCFragment *Fragment,
699 const MCFixup &Fixup, MCValue Target,
700 uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000701 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000702
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000703 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000704 if (!A.isRegistered()) {
705 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000706 Twine("symbol '") + A.getName() +
707 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000708 return;
709 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000710 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000711 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000712 Twine("assembler label '") + A.getName() +
713 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000714 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000715 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000716
Rui Ueyama62376782017-02-16 01:06:45 +0000717 MCSection *MCSec = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000718
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000719 // Mark this symbol as requiring an entry in the symbol table.
Rui Ueyama62376782017-02-16 01:06:45 +0000720 assert(SectionMap.find(MCSec) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000721 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000722
Rui Ueyama62376782017-02-16 01:06:45 +0000723 COFFSection *Sec = SectionMap[MCSec];
Rafael Espindolaed164772011-04-20 14:01:45 +0000724 const MCSymbolRefExpr *SymB = Target.getSymB();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000725
David Majnemera45a1762014-01-06 07:39:46 +0000726 if (SymB) {
727 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000728 if (!B->getFragment()) {
729 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000730 Fixup.getLoc(),
731 Twine("symbol '") + B->getName() +
732 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000733 return;
734 }
David Majnemera45a1762014-01-06 07:39:46 +0000735
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000736 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000737 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000738
David Majnemer1de30942015-02-09 06:31:31 +0000739 // Offset of the relocation in the section
740 int64_t OffsetOfRelocation =
741 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
742
Andy Ayers9e5c8512015-05-14 01:10:41 +0000743 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000744 } else {
745 FixedValue = Target.getConstant();
746 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000747
748 COFFRelocation Reloc;
749
Daniel Dunbar727be432010-07-31 21:08:54 +0000750 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000751 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000752
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000753 // Turn relocations for temporary symbols into section relocations.
Rafael Espindolad2edd132017-06-22 21:57:04 +0000754 if (A.isTemporary()) {
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000755 MCSection *TargetSection = &A.getSection();
756 assert(
757 SectionMap.find(TargetSection) != SectionMap.end() &&
758 "Section must already have been defined in executePostLayoutBinding!");
759 Reloc.Symb = SectionMap[TargetSection]->Symbol;
760 FixedValue += Layout.getSymbolOffset(A);
761 } else {
762 assert(
763 SymbolMap.find(&A) != SymbolMap.end() &&
764 "Symbol must already have been defined in executePostLayoutBinding!");
765 Reloc.Symb = SymbolMap[&A];
766 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000767
768 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000769
770 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola58173b92017-06-23 04:07:44 +0000771 Reloc.Data.Type = TargetObjectWriter->getRelocType(
772 Asm.getContext(), Target, Fixup, SymB, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000773
774 // FIXME: Can anyone explain what this does other than adjust for the size
775 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000776 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
777 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
778 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
779 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000780 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000781
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000782 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
783 switch (Reloc.Data.Type) {
784 case COFF::IMAGE_REL_ARM_ABSOLUTE:
785 case COFF::IMAGE_REL_ARM_ADDR32:
786 case COFF::IMAGE_REL_ARM_ADDR32NB:
787 case COFF::IMAGE_REL_ARM_TOKEN:
788 case COFF::IMAGE_REL_ARM_SECTION:
789 case COFF::IMAGE_REL_ARM_SECREL:
790 break;
791 case COFF::IMAGE_REL_ARM_BRANCH11:
792 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000793 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
794 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
795 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000796 case COFF::IMAGE_REL_ARM_BRANCH24:
797 case COFF::IMAGE_REL_ARM_BLX24:
798 case COFF::IMAGE_REL_ARM_MOV32A:
799 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
800 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000801 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000802 // generate the relocations however the rest of the MSVC toolchain is
803 // unable to handle it.
804 llvm_unreachable("unsupported relocation");
805 break;
806 case COFF::IMAGE_REL_ARM_MOV32T:
807 break;
808 case COFF::IMAGE_REL_ARM_BRANCH20T:
809 case COFF::IMAGE_REL_ARM_BRANCH24T:
810 case COFF::IMAGE_REL_ARM_BLX23T:
811 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
812 // perform a 4 byte adjustment to the relocation. Relative branches are
813 // offset by 4 on ARM, however, because there is no RELA relocations, all
814 // branches are offset by 4.
815 FixedValue = FixedValue + 4;
816 break;
817 }
818 }
819
Simon Pilgrimf2fbf432016-11-20 13:47:59 +0000820 // The fixed value never makes sense for section indices, ignore it.
David Majnemer408b5e62016-02-05 01:55:49 +0000821 if (Fixup.getKind() == FK_SecRel_2)
822 FixedValue = 0;
823
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000824 if (TargetObjectWriter->recordRelocation(Fixup))
Rui Ueyama62376782017-02-16 01:06:45 +0000825 Sec->Relocations.push_back(Reloc);
826}
827
828static std::time_t getTime() {
829 std::time_t Now = time(nullptr);
830 if (Now < 0 || !isUInt<32>(Now))
831 return UINT32_MAX;
832 return Now;
Chris Lattner2c52b792010-07-11 22:07:02 +0000833}
834
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000835// Create .file symbols.
836void WinCOFFObjectWriter::createFileSymbols(MCAssembler &Asm) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000837 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000838 // round up to calculate the number of auxiliary symbols required
839 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000840 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000841
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000842 COFFSymbol *File = createSymbol(".file");
843 File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
844 File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
845 File->Aux.resize(Count);
David Majnemer4d571592014-09-15 19:42:42 +0000846
847 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000848 unsigned Length = Name.size();
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000849 for (auto &Aux : File->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000850 Aux.AuxType = ATFile;
851
852 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000853 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000854 Length = Length - SymbolSize;
855 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000856 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000857 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
858 break;
859 }
860
861 Offset += SymbolSize;
862 }
863 }
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000864}
865
Rui Ueyamaac20c172017-02-17 17:32:54 +0000866static bool isAssociative(const COFFSection &Section) {
867 return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
868 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
869}
870
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000871void WinCOFFObjectWriter::assignSectionNumbers() {
872 size_t I = 1;
Rui Ueyamaac20c172017-02-17 17:32:54 +0000873 auto Assign = [&](COFFSection &Section) {
874 Section.Number = I;
875 Section.Symbol->Data.SectionNumber = I;
876 Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I;
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000877 ++I;
Rui Ueyamaac20c172017-02-17 17:32:54 +0000878 };
879
880 // Although it is not explicitly requested by the Microsoft COFF spec,
881 // we should avoid emitting forward associative section references,
882 // because MSVC link.exe as of 2017 cannot handle that.
883 for (const std::unique_ptr<COFFSection> &Section : Sections)
884 if (!isAssociative(*Section))
885 Assign(*Section);
886 for (const std::unique_ptr<COFFSection> &Section : Sections)
887 if (isAssociative(*Section))
888 Assign(*Section);
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000889}
890
891// Assign file offsets to COFF object file structures.
892void WinCOFFObjectWriter::assignFileOffsets(MCAssembler &Asm,
893 const MCAsmLayout &Layout) {
Peter Collingbournef17b1492018-05-21 18:17:42 +0000894 unsigned Offset = W.OS.tell();
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000895
896 Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
897 Offset += COFF::SectionSize * Header.NumberOfSections;
898
899 for (const auto &Section : Asm) {
900 COFFSection *Sec = SectionMap[&Section];
901
902 if (Sec->Number == -1)
903 continue;
904
905 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
906
907 if (IsPhysicalSection(Sec)) {
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000908 Sec->Header.PointerToRawData = Offset;
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000909 Offset += Sec->Header.SizeOfRawData;
910 }
911
912 if (!Sec->Relocations.empty()) {
913 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
914
915 if (RelocationsOverflow) {
916 // Signal overflow by setting NumberOfRelocations to max value. Actual
917 // size is found in reloc #0. Microsoft tools understand this.
918 Sec->Header.NumberOfRelocations = 0xffff;
919 } else {
920 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
921 }
922 Sec->Header.PointerToRelocations = Offset;
923
924 if (RelocationsOverflow) {
925 // Reloc #0 will contain actual count, so make room for it.
926 Offset += COFF::RelocationSize;
927 }
928
929 Offset += COFF::RelocationSize * Sec->Relocations.size();
930
931 for (auto &Relocation : Sec->Relocations) {
932 assert(Relocation.Symb->getIndex() != -1);
933 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
934 }
935 }
936
937 assert(Sec->Symbol->Aux.size() == 1 &&
938 "Section's symbol must have one aux!");
939 AuxSymbol &Aux = Sec->Symbol->Aux[0];
940 assert(Aux.AuxType == ATSectionDefinition &&
941 "Section's symbol's aux symbol must be a Section Definition!");
942 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
943 Aux.Aux.SectionDefinition.NumberOfRelocations =
944 Sec->Header.NumberOfRelocations;
945 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
946 Sec->Header.NumberOfLineNumbers;
947 }
948
949 Header.PointerToSymbolTable = Offset;
950}
951
Peter Collingbourne438390f2018-05-21 18:23:50 +0000952uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
953 const MCAsmLayout &Layout) {
954 uint64_t StartOffset = W.OS.tell();
955
Rui Ueyamaaf20f102017-02-16 02:35:48 +0000956 if (Sections.size() > INT32_MAX)
957 report_fatal_error(
958 "PE COFF object files can't have more than 2147483647 sections");
959
960 UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
961 Header.NumberOfSections = Sections.size();
962 Header.NumberOfSymbols = 0;
963
964 assignSectionNumbers();
965 createFileSymbols(Asm);
David Majnemer4d571592014-09-15 19:42:42 +0000966
David Majnemer9ab5ff12014-08-28 04:02:50 +0000967 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000968 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000969 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000970 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000971 Symbol->setIndex(Header.NumberOfSymbols++);
972 // Update auxiliary symbol info.
973 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
974 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000975 }
976
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000977 // Build string table.
978 for (const auto &S : Sections)
979 if (S->Name.size() > COFF::NameSize)
980 Strings.add(S->Name);
981 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000982 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000983 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000984 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000985
986 // Set names.
987 for (const auto &S : Sections)
988 SetSectionName(*S);
989 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000990 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000991
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000992 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000993 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000994 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000995 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000996 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
997 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000998 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000999 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001000 }
1001 }
1002
Nico Riecka37acf72013-07-06 12:13:10 +00001003 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +00001004 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001005 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +00001006 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1007 continue;
1008
Rafael Espindolaf59264f2015-05-27 14:45:54 +00001009 const MCSectionCOFF &MCSec = *Section->MCSection;
Reid Kleckner602c0da2018-08-16 21:34:41 +00001010 const MCSymbol *AssocMCSym = MCSec.getCOMDATSymbol();
1011 assert(AssocMCSym);
Nico Riecka37acf72013-07-06 12:13:10 +00001012
Reid Kleckner602c0da2018-08-16 21:34:41 +00001013 // It's an error to try to associate with an undefined symbol or a symbol
1014 // without a section.
1015 if (!AssocMCSym->isInSection()) {
1016 Asm.getContext().reportError(
1017 SMLoc(), Twine("cannot make section ") + MCSec.getSectionName() +
1018 Twine(" associative with sectionless symbol ") +
1019 AssocMCSym->getName());
1020 continue;
1021 }
1022
1023 const auto *AssocMCSec = cast<MCSectionCOFF>(&AssocMCSym->getSection());
1024 assert(SectionMap.count(AssocMCSec));
1025 COFFSection *AssocSec = SectionMap[AssocMCSec];
Nico Riecka37acf72013-07-06 12:13:10 +00001026
1027 // Skip this section if the associated section is unused.
Reid Kleckner602c0da2018-08-16 21:34:41 +00001028 if (AssocSec->Number == -1)
Nico Riecka37acf72013-07-06 12:13:10 +00001029 continue;
1030
Reid Kleckner602c0da2018-08-16 21:34:41 +00001031 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = AssocSec->Number;
Nico Riecka37acf72013-07-06 12:13:10 +00001032 }
1033
Peter Collingbournebc3089f2018-08-22 23:58:16 +00001034 // Create the contents of the .llvm_addrsig section.
1035 if (EmitAddrsigSection) {
1036 auto Frag = new MCDataFragment(AddrsigSection);
Peter Collingbourne6579c812018-08-23 06:57:49 +00001037 Frag->setLayoutOrder(0);
Peter Collingbournebc3089f2018-08-22 23:58:16 +00001038 raw_svector_ostream OS(Frag->getContents());
1039 for (const MCSymbol *S : AddrsigSyms) {
1040 if (!S->isTemporary()) {
1041 encodeULEB128(S->getIndex(), OS);
1042 continue;
1043 }
1044
1045 MCSection *TargetSection = &S->getSection();
1046 assert(SectionMap.find(TargetSection) != SectionMap.end() &&
1047 "Section must already have been defined in "
1048 "executePostLayoutBinding!");
1049 encodeULEB128(SectionMap[TargetSection]->Symbol->getIndex(), OS);
1050 }
1051 }
1052
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001053 assignFileOffsets(Asm, Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001054
David Majnemer088ba022015-09-01 23:46:11 +00001055 // MS LINK expects to be able to use this timestamp to implement their
1056 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +00001057 if (Asm.isIncrementalLinkerCompatible()) {
Rui Ueyama62376782017-02-16 01:06:45 +00001058 Header.TimeDateStamp = getTime();
David Majnemer03e2cc32015-12-21 22:09:27 +00001059 } else {
Nico Weber891419a2016-01-06 19:05:19 +00001060 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +00001061 Header.TimeDateStamp = 0;
1062 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +00001063
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001064 // Write it all to disk...
1065 WriteFileHeader(Header);
Rui Ueyamaac20c172017-02-17 17:32:54 +00001066 writeSectionHeaders();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001067
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001068 // Write section contents.
1069 sections::iterator I = Sections.begin();
1070 sections::iterator IE = Sections.end();
1071 MCAssembler::iterator J = Asm.begin();
1072 MCAssembler::iterator JE = Asm.end();
1073 for (; I != IE && J != JE; ++I, ++J)
1074 writeSection(Asm, Layout, **I, *J);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001075
Peter Collingbournef17b1492018-05-21 18:17:42 +00001076 assert(W.OS.tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001077 "Header::PointerToSymbolTable is insane!");
1078
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001079 // Write a symbol table.
Yaron Keren56919ef2014-12-04 08:30:39 +00001080 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001081 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001082 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001083
Rui Ueyamaaf20f102017-02-16 02:35:48 +00001084 // Write a string table, which completes the entire COFF file.
Peter Collingbournef17b1492018-05-21 18:17:42 +00001085 Strings.write(W.OS);
Peter Collingbourne438390f2018-05-21 18:23:50 +00001086
1087 return W.OS.tell() - StartOffset;
Chris Lattner2c52b792010-07-11 22:07:02 +00001088}
1089
Rafael Espindola11e9e212015-05-27 14:37:12 +00001090MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1091 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001092
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001093// Pin the vtable to this file.
1094void MCWinCOFFObjectTargetWriter::anchor() {}
1095
Chris Lattner2c52b792010-07-11 22:07:02 +00001096//------------------------------------------------------------------------------
1097// WinCOFFObjectWriter factory function
1098
Lang Hames60fbc7c2017-10-10 16:28:07 +00001099std::unique_ptr<MCObjectWriter> llvm::createWinCOFFObjectWriter(
Lang Hames77dff392017-10-10 00:50:29 +00001100 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) {
Lang Hames60fbc7c2017-10-10 16:28:07 +00001101 return llvm::make_unique<WinCOFFObjectWriter>(std::move(MOTW), OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001102}