blob: 86d76fbceb58e5d665a78c320fc7ad61fd6152ed [file] [log] [blame]
Eugene Zelenko1d435522017-02-07 23:02:00 +00001//===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
Chris Lattner2c52b792010-07-11 22:07:02 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains an implementation of a Win32 COFF object file writer.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000014#include "llvm/ADT/DenseMap.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/SmallVector.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/ADT/STLExtras.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000018#include "llvm/ADT/StringRef.h"
Nico Riecka37acf72013-07-06 12:13:10 +000019#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCAsmLayout.h"
21#include "llvm/MC/MCAssembler.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000024#include "llvm/MC/MCFixup.h"
25#include "llvm/MC/MCFragment.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/MC/MCObjectWriter.h"
27#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCSectionCOFF.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000029#include "llvm/MC/MCSymbol.h"
Pete Cooperad9f9c32015-06-08 17:17:12 +000030#include "llvm/MC/MCSymbolCOFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/MC/MCValue.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000032#include "llvm/MC/MCWinCOFFObjectWriter.h"
Hans Wennborgf26bfc12014-09-29 22:43:20 +000033#include "llvm/MC/StringTableBuilder.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000034#include "llvm/Support/Casting.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000035#include "llvm/Support/COFF.h"
Hans Wennborgba80b5d2014-09-28 00:22:27 +000036#include "llvm/Support/Endian.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000037#include "llvm/Support/ErrorHandling.h"
David Majnemer6ddc6362015-09-01 21:23:58 +000038#include "llvm/Support/JamCRC.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000039#include "llvm/Support/MathExtras.h"
40#include "llvm/Support/raw_ostream.h"
41#include <cassert>
42#include <cstddef>
43#include <cstdint>
44#include <cstring>
David Majnemer088ba022015-09-01 23:46:11 +000045#include <ctime>
Eugene Zelenko1d435522017-02-07 23:02:00 +000046#include <memory>
47#include <string>
48#include <vector>
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000049
Chris Lattner2c52b792010-07-11 22:07:02 +000050using namespace llvm;
51
Chandler Carruthf58e3762014-04-22 03:04:17 +000052#define DEBUG_TYPE "WinCOFFObjectWriter"
53
Chris Lattner2c52b792010-07-11 22:07:02 +000054namespace {
Eugene Zelenko1d435522017-02-07 23:02:00 +000055
Dmitri Gribenko226fea52013-01-13 16:01:15 +000056typedef SmallString<COFF::NameSize> name;
Chris Lattner2c52b792010-07-11 22:07:02 +000057
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000058enum AuxiliaryType {
59 ATFunctionDefinition,
60 ATbfAndefSymbol,
61 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:
75 COFF::symbol Data;
Chris Lattner2c52b792010-07-11 22:07:02 +000076
Dmitri Gribenko226fea52013-01-13 16:01:15 +000077 typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
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
Dmitri Gribenko226fea52013-01-13 16:01:15 +000087 COFFSymbol(StringRef 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
109typedef std::vector<COFFRelocation> relocations;
110
111class COFFSection {
112public:
113 COFF::section Header;
114
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
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000121 COFFSection(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000122};
123
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000124class WinCOFFObjectWriter : public MCObjectWriter {
125public:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000126 typedef std::vector<std::unique_ptr<COFFSymbol>> symbols;
David Blaikief564ab62014-04-15 05:25:03 +0000127 typedef std::vector<std::unique_ptr<COFFSection>> sections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000128
Rafael Espindola11e9e212015-05-27 14:37:12 +0000129 typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000130 typedef DenseMap<MCSection const *, COFFSection *> section_map;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000131
Ahmed Charles56440fd2014-03-06 05:51:42 +0000132 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000133
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000134 // Root level file contents.
135 COFF::header Header;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000136 sections Sections;
137 symbols Symbols;
Rafael Espindola21956e42015-10-23 21:48:05 +0000138 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000139
140 // Maps used during object file creation.
141 section_map SectionMap;
Rafael Espindola11e9e212015-05-27 14:37:12 +0000142 symbol_map SymbolMap;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000143
David Majnemer4d571592014-09-15 19:42:42 +0000144 bool UseBigObj;
145
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000146 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
Rafael Espindola37099d92015-04-09 18:08:15 +0000147
Yaron Kerencca43c12014-09-16 21:31:04 +0000148 void reset() override {
149 memset(&Header, 0, sizeof(Header));
150 Header.Machine = TargetObjectWriter->getMachine();
151 Sections.clear();
152 Symbols.clear();
153 Strings.clear();
154 SectionMap.clear();
155 SymbolMap.clear();
156 MCObjectWriter::reset();
157 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000158
Michael J. Spencer17990d52010-10-16 08:25:57 +0000159 COFFSymbol *createSymbol(StringRef Name);
Rafael Espindola11e9e212015-05-27 14:37:12 +0000160 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000161 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000162
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000163 template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000164 object_t *createCOFFEntity(StringRef Name, list_t &List);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000165
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000166 void defineSection(MCSectionCOFF const &Sec);
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000167
168 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000169 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000170 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000171
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000172 void SetSymbolName(COFFSymbol &S);
173 void SetSectionName(COFFSection &S);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000174
Michael J. Spencerd6283772010-09-27 08:58:26 +0000175 bool IsPhysicalSection(COFFSection *S);
176
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000177 // Entity writing methods.
178
179 void WriteFileHeader(const COFF::header &Header);
David Blaikief564ab62014-04-15 05:25:03 +0000180 void WriteSymbol(const COFFSymbol &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000181 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000182 void writeSectionHeader(const COFF::section &S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000183 void WriteRelocation(const COFF::relocation &R);
184
185 // MCObjectWriter interface implementation.
186
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000187 void executePostLayoutBinding(MCAssembler &Asm,
Craig Topper34a61bc2014-03-08 07:02:02 +0000188 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000189
Jim Grosbach36e60e92015-06-04 22:24:41 +0000190 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000191 const MCSymbol &SymA,
David Majnemer2cc4bc772014-11-11 08:43:57 +0000192 const MCFragment &FB, bool InSet,
193 bool IsPCRel) const override;
194
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000195 bool isWeak(const MCSymbol &Sym) const override;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000196
Jim Grosbach36e60e92015-06-04 22:24:41 +0000197 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000198 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000199 MCValue Target, bool &IsPCRel,
200 uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000201
Jim Grosbach36e60e92015-06-04 22:24:41 +0000202 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000203};
Eugene Zelenko1d435522017-02-07 23:02:00 +0000204
205} // end anonymous namespace
Chris Lattner2c52b792010-07-11 22:07:02 +0000206
Hans Wennborgba80b5d2014-09-28 00:22:27 +0000207static inline void write_uint32_le(void *Data, uint32_t Value) {
208 support::endian::write<uint32_t, support::little, support::unaligned>(Data,
209 Value);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000210}
211
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000212//------------------------------------------------------------------------------
213// Symbol class implementation
214
Eugene Zelenko1d435522017-02-07 23:02:00 +0000215COFFSymbol::COFFSymbol(StringRef name) : Name(name.begin(), name.end()) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000216 memset(&Data, 0, sizeof(Data));
217}
218
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000219// In the case that the name does not fit within 8 bytes, the offset
220// into the string table is stored in the last 4 bytes instead, leaving
221// the first 4 bytes as 0.
222void COFFSymbol::set_name_offset(uint32_t Offset) {
223 write_uint32_le(Data.Name + 0, 0);
224 write_uint32_le(Data.Name + 4, Offset);
225}
226
227//------------------------------------------------------------------------------
228// Section class implementation
229
Eugene Zelenko1d435522017-02-07 23:02:00 +0000230COFFSection::COFFSection(StringRef name) : Name(name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000231 memset(&Header, 0, sizeof(Header));
232}
233
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000234//------------------------------------------------------------------------------
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000235// WinCOFFObjectWriter class implementation
236
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000237WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000238 raw_pwrite_stream &OS)
David Majnemer4d571592014-09-15 19:42:42 +0000239 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000240 memset(&Header, 0, sizeof(Header));
Michael J. Spencer377aa202010-08-21 05:58:13 +0000241
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000242 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000243}
244
Michael J. Spencer17990d52010-10-16 08:25:57 +0000245COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000246 return createCOFFEntity<COFFSymbol>(Name, Symbols);
247}
248
Yaron Keren56919ef2014-12-04 08:30:39 +0000249COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000250 symbol_map::iterator i = SymbolMap.find(Symbol);
251 if (i != SymbolMap.end())
252 return i->second;
Yaron Keren56919ef2014-12-04 08:30:39 +0000253 COFFSymbol *RetSymbol =
254 createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
Michael J. Spencer17990d52010-10-16 08:25:57 +0000255 SymbolMap[Symbol] = RetSymbol;
256 return RetSymbol;
257}
258
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000259COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000260 return createCOFFEntity<COFFSection>(Name, Sections);
261}
262
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000263/// A template used to lookup or create a symbol/section, and initialize it if
264/// needed.
265template <typename object_t, typename list_t>
Rafael Espindola11e9e212015-05-27 14:37:12 +0000266object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name, list_t &List) {
David Blaikief564ab62014-04-15 05:25:03 +0000267 List.push_back(make_unique<object_t>(Name));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000268
David Blaikief564ab62014-04-15 05:25:03 +0000269 return List.back().get();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000270}
271
272/// This function takes a section data object from the assembler
273/// and creates the associated COFF section staging object.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000274void WinCOFFObjectWriter::defineSection(MCSectionCOFF const &Sec) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000275 COFFSection *coff_section = createSection(Sec.getSectionName());
Rafael Espindola11e9e212015-05-27 14:37:12 +0000276 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
Rafael Espindola0766ae02014-06-06 19:26:12 +0000277 if (Sec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
278 if (const MCSymbol *S = Sec.getCOMDATSymbol()) {
279 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
280 if (COMDATSymbol->Section)
281 report_fatal_error("two sections have the same comdat");
282 COMDATSymbol->Section = coff_section;
283 }
284 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000285
Michael J. Spencerd6283772010-09-27 08:58:26 +0000286 coff_section->Symbol = coff_symbol;
287 coff_symbol->Section = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000288 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000289
290 // In this case the auxiliary symbol is a Section Definition.
291 coff_symbol->Aux.resize(1);
292 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
293 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000294 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
295
296 coff_section->Header.Characteristics = Sec.getCharacteristics();
297
298 uint32_t &Characteristics = coff_section->Header.Characteristics;
Rafael Espindola967d6a62015-05-21 21:02:35 +0000299 switch (Sec.getAlignment()) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000300 case 1:
301 Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES;
302 break;
303 case 2:
304 Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES;
305 break;
306 case 4:
307 Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES;
308 break;
309 case 8:
310 Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES;
311 break;
312 case 16:
313 Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES;
314 break;
315 case 32:
316 Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES;
317 break;
318 case 64:
319 Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES;
320 break;
321 case 128:
322 Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES;
323 break;
324 case 256:
325 Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES;
326 break;
327 case 512:
328 Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES;
329 break;
330 case 1024:
331 Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES;
332 break;
333 case 2048:
334 Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES;
335 break;
336 case 4096:
337 Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES;
338 break;
339 case 8192:
340 Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES;
341 break;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000342 default:
343 llvm_unreachable("unsupported section alignment");
344 }
345
346 // Bind internal COFF section to MC section.
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000347 coff_section->MCSection = &Sec;
348 SectionMap[&Sec] = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000349}
350
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000351static uint64_t getSymbolValue(const MCSymbol &Symbol,
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000352 const MCAsmLayout &Layout) {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000353 if (Symbol.isCommon() && Symbol.isExternal())
Rafael Espindola14672502015-05-29 17:48:04 +0000354 return Symbol.getCommonSize();
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000355
356 uint64_t Res;
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000357 if (!Layout.getSymbolOffset(Symbol, Res))
Rafael Espindolaff68cb72014-05-01 00:10:17 +0000358 return 0;
359
360 return Res;
361}
362
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000363COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
364 if (!Symbol.isVariable())
365 return nullptr;
366
367 const MCSymbolRefExpr *SymRef =
368 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
369 if (!SymRef)
370 return nullptr;
371
372 const MCSymbol &Aliasee = SymRef->getSymbol();
373 if (!Aliasee.isUndefined())
374 return nullptr;
375 return GetOrCreateCOFFSymbol(&Aliasee);
376}
377
David Majnemera9bdb322014-04-08 22:33:40 +0000378/// This function takes a symbol data object from the assembler
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000379/// and creates the associated COFF symbol staging object.
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000380void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000381 MCAssembler &Assembler,
382 const MCAsmLayout &Layout) {
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000383 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000384 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
385 COFFSection *Sec = nullptr;
386 if (Base && Base->getFragment()) {
387 Sec = SectionMap[Base->getFragment()->getParent()];
388 if (coff_symbol->Section && coff_symbol->Section != Sec)
389 report_fatal_error("conflicting sections for symbol");
390 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000391
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000392 COFFSymbol *Local = nullptr;
Pete Cooper6bf1f302015-06-08 17:17:19 +0000393 if (cast<MCSymbolCOFF>(Symbol).isWeakExternal()) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000394 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
395
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000396 COFFSymbol *WeakDefault = getLinkedSymbol(Symbol);
397 if (!WeakDefault) {
Yaron Keren075759a2015-03-30 15:42:36 +0000398 std::string WeakName = (".weak." + Symbol.getName() + ".default").str();
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000399 WeakDefault = createSymbol(WeakName);
Rafael Espindola30c080a2016-05-26 18:48:23 +0000400 if (!Sec)
401 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
402 else
403 WeakDefault->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000404 Local = WeakDefault;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000405 }
406
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000407 coff_symbol->Other = WeakDefault;
408
Michael J. Spencer17990d52010-10-16 08:25:57 +0000409 // Setup the Weak External auxiliary symbol.
410 coff_symbol->Aux.resize(1);
411 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
412 coff_symbol->Aux[0].AuxType = ATWeakExternal;
413 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
414 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000415 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000416 } else {
Rafael Espindola30c080a2016-05-26 18:48:23 +0000417 if (!Base)
Reid Klecknerc1e76212013-09-17 23:18:05 +0000418 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
Rafael Espindola30c080a2016-05-26 18:48:23 +0000419 else
420 coff_symbol->Section = Sec;
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000421 Local = coff_symbol;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000422 }
Rafael Espindola732eeaf22016-05-26 20:31:00 +0000423
424 if (Local) {
425 Local->Data.Value = getSymbolValue(Symbol, Layout);
426
427 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(Symbol);
428 Local->Data.Type = SymbolCOFF.getType();
429 Local->Data.StorageClass = SymbolCOFF.getClass();
430
431 // If no storage class was specified in the streamer, define it here.
432 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
433 bool IsExternal = Symbol.isExternal() ||
434 (!Symbol.getFragment() && !Symbol.isVariable());
435
436 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
437 : COFF::IMAGE_SYM_CLASS_STATIC;
438 }
439 }
440
441 coff_symbol->MC = &Symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000442}
443
Nico Rieck01143f92014-02-25 09:50:40 +0000444// Maximum offsets for different string table entry encodings.
David Majnemerf088f522016-01-24 20:46:11 +0000445enum : unsigned { Max7DecimalOffset = 9999999U };
446enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
Nico Rieck01143f92014-02-25 09:50:40 +0000447
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000448// Encode a string table entry offset in base 64, padded to 6 chars, and
449// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
450// Buffer must be at least 8 bytes large. No terminating null appended.
Rafael Espindola11e9e212015-05-27 14:37:12 +0000451static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000452 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000453 "Illegal section name encoding for value");
454
455 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
456 "abcdefghijklmnopqrstuvwxyz"
457 "0123456789+/";
458
459 Buffer[0] = '/';
460 Buffer[1] = '/';
461
Rafael Espindola11e9e212015-05-27 14:37:12 +0000462 char *Ptr = Buffer + 7;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000463 for (unsigned i = 0; i < 6; ++i) {
464 unsigned Rem = Value % 64;
465 Value /= 64;
466 *(Ptr--) = Alphabet[Rem];
467 }
468}
469
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000470void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000471 if (S.Name.size() > COFF::NameSize) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000472 uint64_t StringTableEntry = Strings.getOffset(S.Name);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000473
David Majnemerf088f522016-01-24 20:46:11 +0000474 if (StringTableEntry <= Max7DecimalOffset) {
475 SmallVector<char, COFF::NameSize> Buffer;
476 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
477 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
478
479 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
Nico Rieck01143f92014-02-25 09:50:40 +0000480 } else if (StringTableEntry <= MaxBase64Offset) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000481 // Starting with 10,000,000, offsets are encoded as base64.
482 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000483 } else {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000484 report_fatal_error("COFF string table is greater than 64 GB.");
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000485 }
David Majnemerf088f522016-01-24 20:46:11 +0000486 } else {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000487 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
David Majnemerf088f522016-01-24 20:46:11 +0000488 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000489}
490
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000491void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
492 if (S.Name.size() > COFF::NameSize)
493 S.set_name_offset(Strings.getOffset(S.Name));
494 else
Michael J. Spencerd6283772010-09-27 08:58:26 +0000495 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000496}
497
Michael J. Spencerd6283772010-09-27 08:58:26 +0000498bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
Rafael Espindola11e9e212015-05-27 14:37:12 +0000499 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
500 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000501}
502
503//------------------------------------------------------------------------------
504// entity writing methods
505
506void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
David Majnemer4d571592014-09-15 19:42:42 +0000507 if (UseBigObj) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000508 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
509 writeLE16(0xFFFF);
510 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
511 writeLE16(Header.Machine);
512 writeLE32(Header.TimeDateStamp);
513 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
514 writeLE32(0);
515 writeLE32(0);
516 writeLE32(0);
517 writeLE32(0);
518 writeLE32(Header.NumberOfSections);
519 writeLE32(Header.PointerToSymbolTable);
520 writeLE32(Header.NumberOfSymbols);
David Majnemer4d571592014-09-15 19:42:42 +0000521 } else {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000522 writeLE16(Header.Machine);
523 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
524 writeLE32(Header.TimeDateStamp);
525 writeLE32(Header.PointerToSymbolTable);
526 writeLE32(Header.NumberOfSymbols);
527 writeLE16(Header.SizeOfOptionalHeader);
528 writeLE16(Header.Characteristics);
David Majnemer4d571592014-09-15 19:42:42 +0000529 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000530}
531
David Blaikief564ab62014-04-15 05:25:03 +0000532void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000533 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
534 writeLE32(S.Data.Value);
David Majnemer4d571592014-09-15 19:42:42 +0000535 if (UseBigObj)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000536 writeLE32(S.Data.SectionNumber);
David Majnemer4d571592014-09-15 19:42:42 +0000537 else
Jim Grosbach36e60e92015-06-04 22:24:41 +0000538 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
539 writeLE16(S.Data.Type);
540 write8(S.Data.StorageClass);
541 write8(S.Data.NumberOfAuxSymbols);
David Blaikief564ab62014-04-15 05:25:03 +0000542 WriteAuxiliarySymbols(S.Aux);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000543}
544
545void WinCOFFObjectWriter::WriteAuxiliarySymbols(
Rafael Espindola11e9e212015-05-27 14:37:12 +0000546 const COFFSymbol::AuxiliarySymbols &S) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000547 for (const AuxSymbol &i : S) {
548 switch (i.AuxType) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000549 case ATFunctionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000550 writeLE32(i.Aux.FunctionDefinition.TagIndex);
551 writeLE32(i.Aux.FunctionDefinition.TotalSize);
552 writeLE32(i.Aux.FunctionDefinition.PointerToLinenumber);
553 writeLE32(i.Aux.FunctionDefinition.PointerToNextFunction);
554 WriteZeros(sizeof(i.Aux.FunctionDefinition.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000555 if (UseBigObj)
556 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000557 break;
558 case ATbfAndefSymbol:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000559 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused1));
560 writeLE16(i.Aux.bfAndefSymbol.Linenumber);
561 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused2));
562 writeLE32(i.Aux.bfAndefSymbol.PointerToNextFunction);
563 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused3));
David Majnemer4d571592014-09-15 19:42:42 +0000564 if (UseBigObj)
565 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000566 break;
567 case ATWeakExternal:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000568 writeLE32(i.Aux.WeakExternal.TagIndex);
569 writeLE32(i.Aux.WeakExternal.Characteristics);
570 WriteZeros(sizeof(i.Aux.WeakExternal.unused));
David Majnemer4d571592014-09-15 19:42:42 +0000571 if (UseBigObj)
572 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000573 break;
574 case ATFile:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000575 writeBytes(
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000576 StringRef(reinterpret_cast<const char *>(&i.Aux),
David Majnemer4d571592014-09-15 19:42:42 +0000577 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000578 break;
579 case ATSectionDefinition:
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000580 writeLE32(i.Aux.SectionDefinition.Length);
581 writeLE16(i.Aux.SectionDefinition.NumberOfRelocations);
582 writeLE16(i.Aux.SectionDefinition.NumberOfLinenumbers);
583 writeLE32(i.Aux.SectionDefinition.CheckSum);
584 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
585 write8(i.Aux.SectionDefinition.Selection);
586 WriteZeros(sizeof(i.Aux.SectionDefinition.unused));
587 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
David Majnemer4d571592014-09-15 19:42:42 +0000588 if (UseBigObj)
589 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000590 break;
591 }
592 }
593}
594
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000595void WinCOFFObjectWriter::writeSectionHeader(const COFF::section &S) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000596 writeBytes(StringRef(S.Name, COFF::NameSize));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000597
Jim Grosbach36e60e92015-06-04 22:24:41 +0000598 writeLE32(S.VirtualSize);
599 writeLE32(S.VirtualAddress);
600 writeLE32(S.SizeOfRawData);
601 writeLE32(S.PointerToRawData);
602 writeLE32(S.PointerToRelocations);
603 writeLE32(S.PointerToLineNumbers);
604 writeLE16(S.NumberOfRelocations);
605 writeLE16(S.NumberOfLineNumbers);
606 writeLE32(S.Characteristics);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000607}
608
609void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
Jim Grosbach36e60e92015-06-04 22:24:41 +0000610 writeLE32(R.VirtualAddress);
611 writeLE32(R.SymbolTableIndex);
612 writeLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000613}
614
615////////////////////////////////////////////////////////////////////////////////
616// MCObjectWriter interface implementations
617
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000618void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000619 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000620 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000621 // entries in the staging area.
Yaron Keren56919ef2014-12-04 08:30:39 +0000622 for (const auto &Section : Asm)
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000623 defineSection(static_cast<const MCSectionCOFF &>(Section));
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000624
Duncan P. N. Exon Smithf48de1c2015-05-16 00:35:24 +0000625 for (const MCSymbol &Symbol : Asm.symbols())
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000626 if (!Symbol.isTemporary())
Duncan P. N. Exon Smithe8fb3a22015-05-20 19:34:08 +0000627 DefineSymbol(Symbol, Asm, Layout);
Chris Lattner2c52b792010-07-11 22:07:02 +0000628}
629
Jim Grosbach36e60e92015-06-04 22:24:41 +0000630bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
Duncan P. N. Exon Smithd81ba532015-05-16 01:01:55 +0000631 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000632 bool InSet, bool IsPCRel) const {
David Majnemer2cc4bc772014-11-11 08:43:57 +0000633 // MS LINK expects to be able to replace all references to a function with a
634 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
635 // away any relocations to functions.
Pete Cooperad9f9c32015-06-08 17:17:12 +0000636 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
David Majnemer03e2cc32015-12-21 22:09:27 +0000637 if (Asm.isIncrementalLinkerCompatible() &&
638 (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
David Majnemer2cc4bc772014-11-11 08:43:57 +0000639 return false;
Jim Grosbach36e60e92015-06-04 22:24:41 +0000640 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
Rafael Espindola35d61892015-04-17 21:15:17 +0000641 InSet, IsPCRel);
David Majnemer2cc4bc772014-11-11 08:43:57 +0000642}
643
Duncan P. N. Exon Smith5266ad92015-05-20 15:10:03 +0000644bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000645 if (!Sym.isExternal())
Rafael Espindola88af4112015-04-17 11:27:13 +0000646 return false;
647
Rafael Espindola88af4112015-04-17 11:27:13 +0000648 if (!Sym.isInSection())
649 return false;
650
651 const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
652 if (!Sec.getCOMDATSymbol())
653 return false;
654
655 // It looks like for COFF it is invalid to replace a reference to a global
656 // in a comdat with a reference to a local.
657 // FIXME: Add a specification reference if available.
658 return true;
Rafael Espindolaaeed3cb2015-03-26 21:11:00 +0000659}
660
Jim Grosbach36e60e92015-06-04 22:24:41 +0000661void WinCOFFObjectWriter::recordRelocation(
Rafael Espindola26585542015-01-19 21:11:14 +0000662 MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
663 const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
Craig Topperbb694de2014-04-13 04:57:38 +0000664 assert(Target.getSymA() && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000665
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000666 const MCSymbol &A = Target.getSymA()->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000667 if (!A.isRegistered()) {
668 Asm.getContext().reportError(Fixup.getLoc(),
Rafael Espindola11e9e212015-05-27 14:37:12 +0000669 Twine("symbol '") + A.getName() +
670 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000671 return;
672 }
Reid Kleckner85dfb682015-09-16 16:26:29 +0000673 if (A.isTemporary() && A.isUndefined()) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000674 Asm.getContext().reportError(Fixup.getLoc(),
Reid Kleckner85dfb682015-09-16 16:26:29 +0000675 Twine("assembler label '") + A.getName() +
676 "' can not be undefined");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000677 return;
Reid Kleckner85dfb682015-09-16 16:26:29 +0000678 }
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000679
Rafael Espindola7549f872015-05-26 00:36:57 +0000680 MCSection *Section = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000681
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000682 // Mark this symbol as requiring an entry in the symbol table.
Rafael Espindola7549f872015-05-26 00:36:57 +0000683 assert(SectionMap.find(Section) != SectionMap.end() &&
Jim Grosbach56ed0bb2015-06-04 23:25:54 +0000684 "Section must already have been defined in executePostLayoutBinding!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000685
Rafael Espindola7549f872015-05-26 00:36:57 +0000686 COFFSection *coff_section = SectionMap[Section];
Rafael Espindolaed164772011-04-20 14:01:45 +0000687 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000688 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000689
David Majnemera45a1762014-01-06 07:39:46 +0000690 if (SymB) {
691 const MCSymbol *B = &SymB->getSymbol();
Oliver Stannard9be59af2015-11-17 10:00:43 +0000692 if (!B->getFragment()) {
693 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000694 Fixup.getLoc(),
695 Twine("symbol '") + B->getName() +
696 "' 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
Oliver Stannard9be59af2015-11-17 10:00:43 +0000700 if (!A.getFragment()) {
701 Asm.getContext().reportError(
David Majnemera45a1762014-01-06 07:39:46 +0000702 Fixup.getLoc(),
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000703 Twine("symbol '") + A.getName() +
David Majnemera45a1762014-01-06 07:39:46 +0000704 "' can not be undefined in a subtraction expression");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000705 return;
706 }
David Majnemera45a1762014-01-06 07:39:46 +0000707
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000708 CrossSection = &A.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000709
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000710 // Offset of the symbol in the section
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000711 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000712
713 // In the case where we have SymbA and SymB, we just need to store the delta
714 // between the two symbols. Update FixedValue to account for the delta, and
715 // skip recording the relocation.
David Majnemer1de30942015-02-09 06:31:31 +0000716 if (!CrossSection) {
Duncan P. N. Exon Smith2a404832015-05-19 23:53:20 +0000717 int64_t OffsetOfA = Layout.getSymbolOffset(A);
David Majnemer1de30942015-02-09 06:31:31 +0000718 FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
Rafael Espindolaed164772011-04-20 14:01:45 +0000719 return;
David Majnemer1de30942015-02-09 06:31:31 +0000720 }
721
722 // Offset of the relocation in the section
723 int64_t OffsetOfRelocation =
724 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
725
Andy Ayers9e5c8512015-05-14 01:10:41 +0000726 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000727 } else {
728 FixedValue = Target.getConstant();
729 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000730
731 COFFRelocation Reloc;
732
Daniel Dunbar727be432010-07-31 21:08:54 +0000733 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000734 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000735
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000736 // Turn relocations for temporary symbols into section relocations.
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000737 if (A.isTemporary() || CrossSection) {
738 MCSection *TargetSection = &A.getSection();
739 assert(
740 SectionMap.find(TargetSection) != SectionMap.end() &&
741 "Section must already have been defined in executePostLayoutBinding!");
742 Reloc.Symb = SectionMap[TargetSection]->Symbol;
743 FixedValue += Layout.getSymbolOffset(A);
744 } else {
745 assert(
746 SymbolMap.find(&A) != SymbolMap.end() &&
747 "Symbol must already have been defined in executePostLayoutBinding!");
748 Reloc.Symb = SymbolMap[&A];
749 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000750
751 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000752
753 Reloc.Data.VirtualAddress += Fixup.getOffset();
Rafael Espindola11e9e212015-05-27 14:37:12 +0000754 Reloc.Data.Type = TargetObjectWriter->getRelocType(
755 Target, Fixup, CrossSection, Asm.getBackend());
Rafael Espindolae61724a2011-12-22 22:21:47 +0000756
757 // FIXME: Can anyone explain what this does other than adjust for the size
758 // of the offset?
Saleem Abdulrasool2c080512014-04-13 20:47:55 +0000759 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
760 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
761 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
762 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000763 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000764
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000765 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
766 switch (Reloc.Data.Type) {
767 case COFF::IMAGE_REL_ARM_ABSOLUTE:
768 case COFF::IMAGE_REL_ARM_ADDR32:
769 case COFF::IMAGE_REL_ARM_ADDR32NB:
770 case COFF::IMAGE_REL_ARM_TOKEN:
771 case COFF::IMAGE_REL_ARM_SECTION:
772 case COFF::IMAGE_REL_ARM_SECREL:
773 break;
774 case COFF::IMAGE_REL_ARM_BRANCH11:
775 case COFF::IMAGE_REL_ARM_BLX11:
Rafael Espindola11e9e212015-05-27 14:37:12 +0000776 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
777 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
778 // for Windows CE).
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000779 case COFF::IMAGE_REL_ARM_BRANCH24:
780 case COFF::IMAGE_REL_ARM_BLX24:
781 case COFF::IMAGE_REL_ARM_MOV32A:
782 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
783 // only used for ARM mode code, which is documented as being unsupported
Alp Tokerbeaca192014-05-15 01:52:21 +0000784 // by Windows on ARM. Empirical proof indicates that masm is able to
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000785 // generate the relocations however the rest of the MSVC toolchain is
786 // unable to handle it.
787 llvm_unreachable("unsupported relocation");
788 break;
789 case COFF::IMAGE_REL_ARM_MOV32T:
790 break;
791 case COFF::IMAGE_REL_ARM_BRANCH20T:
792 case COFF::IMAGE_REL_ARM_BRANCH24T:
793 case COFF::IMAGE_REL_ARM_BLX23T:
794 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
795 // perform a 4 byte adjustment to the relocation. Relative branches are
796 // offset by 4 on ARM, however, because there is no RELA relocations, all
797 // branches are offset by 4.
798 FixedValue = FixedValue + 4;
799 break;
800 }
801 }
802
Simon Pilgrimf2fbf432016-11-20 13:47:59 +0000803 // The fixed value never makes sense for section indices, ignore it.
David Majnemer408b5e62016-02-05 01:55:49 +0000804 if (Fixup.getKind() == FK_SecRel_2)
805 FixedValue = 0;
806
Saleem Abdulrasool54bed122014-05-21 23:17:50 +0000807 if (TargetObjectWriter->recordRelocation(Fixup))
808 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000809}
810
Jim Grosbach36e60e92015-06-04 22:24:41 +0000811void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000812 const MCAsmLayout &Layout) {
David Majnemer4d571592014-09-15 19:42:42 +0000813 size_t SectionsSize = Sections.size();
814 if (SectionsSize > static_cast<size_t>(INT32_MAX))
815 report_fatal_error(
816 "PE COFF object files can't have more than 2147483647 sections");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000817
David Majnemer4d571592014-09-15 19:42:42 +0000818 // Assign symbol and section indexes and offsets.
819 int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
820
821 UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
822
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000823 // Assign section numbers.
David Majnemer4d571592014-09-15 19:42:42 +0000824 size_t Number = 1;
David Majnemer9ab5ff12014-08-28 04:02:50 +0000825 for (const auto &Section : Sections) {
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000826 Section->Number = Number;
827 Section->Symbol->Data.SectionNumber = Number;
828 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
David Majnemer4d571592014-09-15 19:42:42 +0000829 ++Number;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000830 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000831
David Majnemer4d571592014-09-15 19:42:42 +0000832 Header.NumberOfSections = NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000833 Header.NumberOfSymbols = 0;
834
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000835 for (const std::string &Name : Asm.getFileNames()) {
David Majnemer4d571592014-09-15 19:42:42 +0000836 // round up to calculate the number of auxiliary symbols required
837 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000838 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
David Majnemer4d571592014-09-15 19:42:42 +0000839
840 COFFSymbol *file = createSymbol(".file");
841 file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
842 file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
843 file->Aux.resize(Count);
844
845 unsigned Offset = 0;
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000846 unsigned Length = Name.size();
Yaron Keren56919ef2014-12-04 08:30:39 +0000847 for (auto &Aux : file->Aux) {
David Majnemer4d571592014-09-15 19:42:42 +0000848 Aux.AuxType = ATFile;
849
850 if (Length > SymbolSize) {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000851 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
David Majnemer4d571592014-09-15 19:42:42 +0000852 Length = Length - SymbolSize;
853 } else {
Rafael Espindola66f3c9c2015-05-28 18:03:20 +0000854 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
David Majnemer4d571592014-09-15 19:42:42 +0000855 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
856 break;
857 }
858
859 Offset += SymbolSize;
860 }
861 }
862
David Majnemer9ab5ff12014-08-28 04:02:50 +0000863 for (auto &Symbol : Symbols) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000864 // Update section number & offset for symbols that have them.
Rafael Espindola575f79a2014-05-01 13:37:57 +0000865 if (Symbol->Section)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000866 Symbol->Data.SectionNumber = Symbol->Section->Number;
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000867 Symbol->setIndex(Header.NumberOfSymbols++);
868 // Update auxiliary symbol info.
869 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
870 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000871 }
872
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000873 // Build string table.
874 for (const auto &S : Sections)
875 if (S->Name.size() > COFF::NameSize)
876 Strings.add(S->Name);
877 for (const auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000878 if (S->Name.size() > COFF::NameSize)
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000879 Strings.add(S->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000880 Strings.finalize();
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000881
882 // Set names.
883 for (const auto &S : Sections)
884 SetSectionName(*S);
885 for (auto &S : Symbols)
Peter Collingbourne8359a6a2015-11-26 23:29:27 +0000886 SetSymbolName(*S);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000887
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000888 // Fixup weak external references.
Yaron Keren56919ef2014-12-04 08:30:39 +0000889 for (auto &Symbol : Symbols) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000890 if (Symbol->Other) {
David Majnemer4eecd302015-05-30 04:56:02 +0000891 assert(Symbol->getIndex() != -1);
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000892 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
893 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000894 "Symbol's aux symbol must be a Weak External!");
David Majnemer4eecd302015-05-30 04:56:02 +0000895 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000896 }
897 }
898
Nico Riecka37acf72013-07-06 12:13:10 +0000899 // Fixup associative COMDAT sections.
Yaron Keren56919ef2014-12-04 08:30:39 +0000900 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000901 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
Nico Riecka37acf72013-07-06 12:13:10 +0000902 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
903 continue;
904
Rafael Espindolaf59264f2015-05-27 14:45:54 +0000905 const MCSectionCOFF &MCSec = *Section->MCSection;
Nico Riecka37acf72013-07-06 12:13:10 +0000906
Rafael Espindola0766ae02014-06-06 19:26:12 +0000907 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
908 assert(COMDAT);
909 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
910 assert(COMDATSymbol);
911 COFFSection *Assoc = COMDATSymbol->Section;
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000912 if (!Assoc)
Rafael Espindola0766ae02014-06-06 19:26:12 +0000913 report_fatal_error(
914 Twine("Missing associated COMDAT section for section ") +
915 MCSec.getSectionName());
Nico Riecka37acf72013-07-06 12:13:10 +0000916
917 // Skip this section if the associated section is unused.
918 if (Assoc->Number == -1)
919 continue;
920
David Majnemer4eecd302015-05-30 04:56:02 +0000921 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
Nico Riecka37acf72013-07-06 12:13:10 +0000922 }
923
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000924 // Assign file offsets to COFF object file structures.
925
Manuel Klimek272d3f12015-11-18 15:24:17 +0000926 unsigned offset = getInitialOffset();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000927
David Majnemer4d571592014-09-15 19:42:42 +0000928 if (UseBigObj)
929 offset += COFF::Header32Size;
930 else
931 offset += COFF::Header16Size;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000932 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000933
Yaron Keren56919ef2014-12-04 08:30:39 +0000934 for (const auto &Section : Asm) {
Rafael Espindolaa554c052015-05-25 23:14:17 +0000935 COFFSection *Sec = SectionMap[&Section];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000936
Michael J. Spencerd6283772010-09-27 08:58:26 +0000937 if (Sec->Number == -1)
938 continue;
939
Rafael Espindola5a1e80b2015-05-26 02:00:36 +0000940 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000941
Michael J. Spencerd6283772010-09-27 08:58:26 +0000942 if (IsPhysicalSection(Sec)) {
David Majnemer3df3c612015-02-11 22:22:30 +0000943 // Align the section data to a four byte boundary.
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000944 offset = alignTo(offset, 4);
David Majnemerab2b25b2015-02-11 22:51:55 +0000945 Sec->Header.PointerToRawData = offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000946
947 offset += Sec->Header.SizeOfRawData;
948 }
949
Eugene Zelenko1d435522017-02-07 23:02:00 +0000950 if (!Sec->Relocations.empty()) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000951 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
952
953 if (RelocationsOverflow) {
David Majnemer9ab5ff12014-08-28 04:02:50 +0000954 // Signal overflow by setting NumberOfRelocations to max value. Actual
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000955 // size is found in reloc #0. Microsoft tools understand this.
956 Sec->Header.NumberOfRelocations = 0xffff;
957 } else {
958 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
959 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000960 Sec->Header.PointerToRelocations = offset;
961
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000962 if (RelocationsOverflow) {
963 // Reloc #0 will contain actual count, so make room for it.
964 offset += COFF::RelocationSize;
965 }
966
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000967 offset += COFF::RelocationSize * Sec->Relocations.size();
968
Yaron Keren56919ef2014-12-04 08:30:39 +0000969 for (auto &Relocation : Sec->Relocations) {
David Majnemer4eecd302015-05-30 04:56:02 +0000970 assert(Relocation.Symb->getIndex() != -1);
971 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000972 }
973 }
974
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +0000975 assert(Sec->Symbol->Aux.size() == 1 &&
976 "Section's symbol must have one aux!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000977 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000978 assert(Aux.AuxType == ATSectionDefinition &&
979 "Section's symbol's aux symbol must be a Section Definition!");
980 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
981 Aux.Aux.SectionDefinition.NumberOfRelocations =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000982 Sec->Header.NumberOfRelocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000983 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
Rafael Espindola11e9e212015-05-27 14:37:12 +0000984 Sec->Header.NumberOfLineNumbers;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000985 }
986
987 Header.PointerToSymbolTable = offset;
988
David Majnemer088ba022015-09-01 23:46:11 +0000989 // MS LINK expects to be able to use this timestamp to implement their
990 // /INCREMENTAL feature.
David Majnemer03e2cc32015-12-21 22:09:27 +0000991 if (Asm.isIncrementalLinkerCompatible()) {
992 std::time_t Now = time(nullptr);
993 if (Now < 0 || !isUInt<32>(Now))
994 Now = UINT32_MAX;
995 Header.TimeDateStamp = Now;
996 } else {
Nico Weber891419a2016-01-06 19:05:19 +0000997 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
David Majnemer03e2cc32015-12-21 22:09:27 +0000998 Header.TimeDateStamp = 0;
999 }
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +00001000
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001001 // Write it all to disk...
1002 WriteFileHeader(Header);
1003
1004 {
1005 sections::iterator i, ie;
Rafael Espindola63702e22015-06-01 01:30:01 +00001006 MCAssembler::iterator j, je;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001007
Yaron Keren56919ef2014-12-04 08:30:39 +00001008 for (auto &Section : Sections) {
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001009 if (Section->Number != -1) {
1010 if (Section->Relocations.size() >= 0xffff)
1011 Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
Jim Grosbach56ed0bb2015-06-04 23:25:54 +00001012 writeSectionHeader(Section->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001013 }
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001014 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001015
David Majnemer6ddc6362015-09-01 21:23:58 +00001016 SmallVector<char, 128> SectionContents;
Rafael Espindola11e9e212015-05-27 14:37:12 +00001017 for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
1018 je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +00001019 (i != ie) && (j != je); ++i, ++j) {
1020
1021 if ((*i)->Number == -1)
1022 continue;
1023
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001024 if ((*i)->Header.PointerToRawData != 0) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001025 assert(getStream().tell() <= (*i)->Header.PointerToRawData &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001026 "Section::PointerToRawData is insane!");
1027
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001028 unsigned SectionDataPadding =
1029 (*i)->Header.PointerToRawData - getStream().tell();
David Majnemer3df3c612015-02-11 22:22:30 +00001030 assert(SectionDataPadding < 4 &&
1031 "Should only need at most three bytes of padding!");
1032
1033 WriteZeros(SectionDataPadding);
1034
David Majnemer6ddc6362015-09-01 21:23:58 +00001035 // Save the contents of the section to a temporary buffer, we need this
1036 // to CRC the data before we dump it into the object file.
1037 SectionContents.clear();
1038 raw_svector_ostream VecOS(SectionContents);
1039 raw_pwrite_stream &OldStream = getStream();
1040 // Redirect the output stream to our buffer.
1041 setStream(VecOS);
1042 // Fill our buffer with the section data.
Rafael Espindola64acc7f2015-05-26 02:17:21 +00001043 Asm.writeSectionData(&*j, Layout);
David Majnemer6ddc6362015-09-01 21:23:58 +00001044 // Reset the stream back to what it was before.
1045 setStream(OldStream);
1046
1047 // Calculate our CRC with an initial value of '0', this is not how
1048 // JamCRC is specified but it aligns with the expected output.
1049 JamCRC JC(/*Init=*/0x00000000U);
1050 JC.update(SectionContents);
1051
1052 // Write the section contents to the object file.
1053 getStream() << SectionContents;
1054
1055 // Update the section definition auxiliary symbol to record the CRC.
1056 COFFSection *Sec = SectionMap[&*j];
1057 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
1058 assert(AuxSyms.size() == 1 &&
1059 AuxSyms[0].AuxType == ATSectionDefinition);
1060 AuxSymbol &SecDef = AuxSyms[0];
1061 SecDef.Aux.SectionDefinition.CheckSum = JC.getCRC();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001062 }
1063
Eugene Zelenko1d435522017-02-07 23:02:00 +00001064 if (!(*i)->Relocations.empty()) {
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001065 assert(getStream().tell() == (*i)->Header.PointerToRelocations &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001066 "Section::PointerToRelocations is insane!");
1067
Michael J. Spencerfa39bd22012-03-15 09:03:03 +00001068 if ((*i)->Relocations.size() >= 0xffff) {
1069 // In case of overflow, write actual relocation count as first
1070 // relocation. Including the synthetic reloc itself (+ 1).
1071 COFF::relocation r;
1072 r.VirtualAddress = (*i)->Relocations.size() + 1;
1073 r.SymbolTableIndex = 0;
1074 r.Type = 0;
1075 WriteRelocation(r);
1076 }
1077
Yaron Keren56919ef2014-12-04 08:30:39 +00001078 for (const auto &Relocation : (*i)->Relocations)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001079 WriteRelocation(Relocation.Data);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001080 } else
1081 assert((*i)->Header.PointerToRelocations == 0 &&
1082 "Section::PointerToRelocations is insane!");
1083 }
1084 }
1085
David Majnemerabdb2d2a2015-09-01 16:19:03 +00001086 assert(getStream().tell() == Header.PointerToSymbolTable &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001087 "Header::PointerToSymbolTable is insane!");
1088
Yaron Keren56919ef2014-12-04 08:30:39 +00001089 for (auto &Symbol : Symbols)
David Majnemer4eecd302015-05-30 04:56:02 +00001090 if (Symbol->getIndex() != -1)
Saleem Abdulrasool09ced5f2014-04-28 03:34:48 +00001091 WriteSymbol(*Symbol);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +00001092
Rafael Espindola39751af2016-10-04 22:43:25 +00001093 Strings.write(getStream());
Chris Lattner2c52b792010-07-11 22:07:02 +00001094}
1095
Rafael Espindola11e9e212015-05-27 14:37:12 +00001096MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1097 : Machine(Machine_) {}
Rafael Espindola908d2ed2011-12-24 02:14:02 +00001098
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +00001099// Pin the vtable to this file.
1100void MCWinCOFFObjectTargetWriter::anchor() {}
1101
Chris Lattner2c52b792010-07-11 22:07:02 +00001102//------------------------------------------------------------------------------
1103// WinCOFFObjectWriter factory function
1104
Rafael Espindola37099d92015-04-09 18:08:15 +00001105MCObjectWriter *
1106llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001107 raw_pwrite_stream &OS) {
Rafael Espindola37099d92015-04-09 18:08:15 +00001108 return new WinCOFFObjectWriter(MOTW, OS);
Michael J. Spencerc2129372010-07-26 03:01:28 +00001109}