blob: e41a6bdfca7b3d9bcc804f2850ab35643fb2aa21 [file] [log] [blame]
Chris Lattner2c52b792010-07-11 22:07:02 +00001//===-- llvm/MC/WinCOFFObjectWriter.cpp -------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains an implementation of a Win32 COFF object file writer.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "WinCOFFObjectWriter"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000015
Rafael Espindola908d2ed2011-12-24 02:14:02 +000016#include "llvm/MC/MCWinCOFFObjectWriter.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000017#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringRef.h"
Nico Riecka37acf72013-07-06 12:13:10 +000020#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCAsmLayout.h"
22#include "llvm/MC/MCAssembler.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCExpr.h"
25#include "llvm/MC/MCObjectWriter.h"
26#include "llvm/MC/MCSection.h"
27#include "llvm/MC/MCSectionCOFF.h"
28#include "llvm/MC/MCSymbol.h"
29#include "llvm/MC/MCValue.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000030#include "llvm/Support/COFF.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000033#include "llvm/Support/TimeValue.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000034#include <cstdio>
35
Chris Lattner2c52b792010-07-11 22:07:02 +000036using namespace llvm;
37
38namespace {
Dmitri Gribenko226fea52013-01-13 16:01:15 +000039typedef SmallString<COFF::NameSize> name;
Chris Lattner2c52b792010-07-11 22:07:02 +000040
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000041enum AuxiliaryType {
42 ATFunctionDefinition,
43 ATbfAndefSymbol,
44 ATWeakExternal,
45 ATFile,
46 ATSectionDefinition
47};
Chris Lattner2c52b792010-07-11 22:07:02 +000048
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000049struct AuxSymbol {
50 AuxiliaryType AuxType;
51 COFF::Auxiliary Aux;
52};
Chris Lattner2c52b792010-07-11 22:07:02 +000053
Michael J. Spencerd6283772010-09-27 08:58:26 +000054class COFFSymbol;
55class COFFSection;
56
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000057class COFFSymbol {
58public:
59 COFF::symbol Data;
Chris Lattner2c52b792010-07-11 22:07:02 +000060
Dmitri Gribenko226fea52013-01-13 16:01:15 +000061 typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
Chris Lattner2c52b792010-07-11 22:07:02 +000062
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000063 name Name;
Michael J. Spencerce2e5352010-09-27 21:17:39 +000064 int Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000065 AuxiliarySymbols Aux;
66 COFFSymbol *Other;
Michael J. Spencerd6283772010-09-27 08:58:26 +000067 COFFSection *Section;
68 int Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000069
70 MCSymbolData const *MCData;
71
Dmitri Gribenko226fea52013-01-13 16:01:15 +000072 COFFSymbol(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000073 size_t size() const;
74 void set_name_offset(uint32_t Offset);
Michael J. Spencerd6283772010-09-27 08:58:26 +000075
76 bool should_keep() const;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000077};
78
79// This class contains staging data for a COFF relocation entry.
80struct COFFRelocation {
81 COFF::relocation Data;
82 COFFSymbol *Symb;
83
84 COFFRelocation() : Symb(NULL) {}
85 static size_t size() { return COFF::RelocationSize; }
86};
87
88typedef std::vector<COFFRelocation> relocations;
89
90class COFFSection {
91public:
92 COFF::section Header;
93
94 std::string Name;
Michael J. Spencerce2e5352010-09-27 21:17:39 +000095 int Number;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000096 MCSectionData const *MCData;
Michael J. Spencerd6283772010-09-27 08:58:26 +000097 COFFSymbol *Symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000098 relocations Relocations;
99
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000100 COFFSection(StringRef name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000101 static size_t size();
102};
103
104// This class holds the COFF string table.
105class StringTable {
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000106 typedef StringMap<size_t> map;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000107 map Map;
108
109 void update_length();
110public:
111 std::vector<char> Data;
112
113 StringTable();
114 size_t size() const;
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000115 size_t insert(StringRef String);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000116};
117
118class WinCOFFObjectWriter : public MCObjectWriter {
119public:
120
121 typedef std::vector<COFFSymbol*> symbols;
122 typedef std::vector<COFFSection*> sections;
123
Michael J. Spencer17990d52010-10-16 08:25:57 +0000124 typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
125 typedef DenseMap<MCSection const *, COFFSection *> section_map;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000126
Ahmed Charles56440fd2014-03-06 05:51:42 +0000127 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000128
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000129 // Root level file contents.
130 COFF::header Header;
131 sections Sections;
132 symbols Symbols;
133 StringTable Strings;
134
135 // Maps used during object file creation.
136 section_map SectionMap;
137 symbol_map SymbolMap;
138
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000139 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_ostream &OS);
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000140 virtual ~WinCOFFObjectWriter();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000141
Michael J. Spencer17990d52010-10-16 08:25:57 +0000142 COFFSymbol *createSymbol(StringRef Name);
143 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol * Symbol);
144 COFFSection *createSection(StringRef Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000145
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000146 template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000147 object_t *createCOFFEntity(StringRef Name, list_t &List);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000148
149 void DefineSection(MCSectionData const &SectionData);
Reid Klecknerc1e76212013-09-17 23:18:05 +0000150 void DefineSymbol(MCSymbolData const &SymbolData, MCAssembler &Assembler,
151 const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000152
Michael J. Spencerd6283772010-09-27 08:58:26 +0000153 void MakeSymbolReal(COFFSymbol &S, size_t Index);
154 void MakeSectionReal(COFFSection &S, size_t Number);
155
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000156 bool ExportSymbol(MCSymbolData const &SymbolData, MCAssembler &Asm);
157
Michael J. Spencerd6283772010-09-27 08:58:26 +0000158 bool IsPhysicalSection(COFFSection *S);
159
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000160 // Entity writing methods.
161
162 void WriteFileHeader(const COFF::header &Header);
163 void WriteSymbol(const COFFSymbol *S);
164 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
165 void WriteSectionHeader(const COFF::section &S);
166 void WriteRelocation(const COFF::relocation &R);
167
168 // MCObjectWriter interface implementation.
169
Craig Topper34a61bc2014-03-08 07:02:02 +0000170 void ExecutePostLayoutBinding(MCAssembler &Asm,
171 const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000172
Craig Topper34a61bc2014-03-08 07:02:02 +0000173 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
174 const MCFragment *Fragment, const MCFixup &Fixup,
175 MCValue Target, uint64_t &FixedValue) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000176
Craig Topper34a61bc2014-03-08 07:02:02 +0000177 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000178};
Chris Lattner2c52b792010-07-11 22:07:02 +0000179}
180
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000181static inline void write_uint32_le(void *Data, uint32_t const &Value) {
182 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data);
183 Ptr[0] = (Value & 0x000000FF) >> 0;
184 Ptr[1] = (Value & 0x0000FF00) >> 8;
185 Ptr[2] = (Value & 0x00FF0000) >> 16;
186 Ptr[3] = (Value & 0xFF000000) >> 24;
187}
188
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000189//------------------------------------------------------------------------------
190// Symbol class implementation
191
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000192COFFSymbol::COFFSymbol(StringRef name)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000193 : Name(name.begin(), name.end())
194 , Other(NULL)
195 , Section(NULL)
196 , Relocations(0)
197 , MCData(NULL) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000198 memset(&Data, 0, sizeof(Data));
199}
200
201size_t COFFSymbol::size() const {
202 return COFF::SymbolSize + (Data.NumberOfAuxSymbols * COFF::SymbolSize);
203}
204
205// In the case that the name does not fit within 8 bytes, the offset
206// into the string table is stored in the last 4 bytes instead, leaving
207// the first 4 bytes as 0.
208void COFFSymbol::set_name_offset(uint32_t Offset) {
209 write_uint32_le(Data.Name + 0, 0);
210 write_uint32_le(Data.Name + 4, Offset);
211}
212
Michael J. Spencerd6283772010-09-27 08:58:26 +0000213/// logic to decide if the symbol should be reported in the symbol table
214bool COFFSymbol::should_keep() const {
215 // no section means its external, keep it
216 if (Section == NULL)
217 return true;
218
219 // if it has relocations pointing at it, keep it
220 if (Relocations > 0) {
221 assert(Section->Number != -1 && "Sections with relocations must be real!");
222 return true;
223 }
224
225 // if the section its in is being droped, drop it
226 if (Section->Number == -1)
227 return false;
228
229 // if it is the section symbol, keep it
230 if (Section->Symbol == this)
231 return true;
232
233 // if its temporary, drop it
234 if (MCData && MCData->getSymbol().isTemporary())
235 return false;
236
237 // otherwise, keep it
238 return true;
239}
240
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000241//------------------------------------------------------------------------------
242// Section class implementation
243
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000244COFFSection::COFFSection(StringRef name)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000245 : Name(name)
246 , MCData(NULL)
247 , Symbol(NULL) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000248 memset(&Header, 0, sizeof(Header));
249}
250
251size_t COFFSection::size() {
252 return COFF::SectionSize;
253}
254
255//------------------------------------------------------------------------------
256// StringTable class implementation
257
258/// Write the length of the string table into Data.
259/// The length of the string table includes uint32 length header.
260void StringTable::update_length() {
261 write_uint32_le(&Data.front(), Data.size());
262}
263
264StringTable::StringTable() {
265 // The string table data begins with the length of the entire string table
266 // including the length header. Allocate space for this header.
267 Data.resize(4);
Michael J. Spencer5ca95bc2011-11-08 19:52:32 +0000268 update_length();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000269}
270
271size_t StringTable::size() const {
272 return Data.size();
273}
274
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000275/// Add String to the table iff it is not already there.
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000276/// @returns the index into the string table where the string is now located.
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000277size_t StringTable::insert(StringRef String) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000278 map::iterator i = Map.find(String);
279
280 if (i != Map.end())
281 return i->second;
282
283 size_t Offset = Data.size();
284
285 // Insert string data into string table.
286 Data.insert(Data.end(), String.begin(), String.end());
287 Data.push_back('\0');
288
289 // Put a reference to it in the map.
290 Map[String] = Offset;
291
292 // Update the internal length field.
293 update_length();
294
295 return Offset;
296}
297
298//------------------------------------------------------------------------------
299// WinCOFFObjectWriter class implementation
300
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000301WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
302 raw_ostream &OS)
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000303 : MCObjectWriter(OS, true)
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000304 , TargetObjectWriter(MOTW) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000305 memset(&Header, 0, sizeof(Header));
Michael J. Spencer377aa202010-08-21 05:58:13 +0000306
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000307 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000308}
309
Benjamin Kramerc2997dd2010-07-29 11:57:59 +0000310WinCOFFObjectWriter::~WinCOFFObjectWriter() {
311 for (symbols::iterator I = Symbols.begin(), E = Symbols.end(); I != E; ++I)
312 delete *I;
313 for (sections::iterator I = Sections.begin(), E = Sections.end(); I != E; ++I)
314 delete *I;
315}
316
Michael J. Spencer17990d52010-10-16 08:25:57 +0000317COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000318 return createCOFFEntity<COFFSymbol>(Name, Symbols);
319}
320
Michael J. Spencer17990d52010-10-16 08:25:57 +0000321COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol * Symbol){
322 symbol_map::iterator i = SymbolMap.find(Symbol);
323 if (i != SymbolMap.end())
324 return i->second;
325 COFFSymbol *RetSymbol
326 = createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
327 SymbolMap[Symbol] = RetSymbol;
328 return RetSymbol;
329}
330
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000331COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000332 return createCOFFEntity<COFFSection>(Name, Sections);
333}
334
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000335/// A template used to lookup or create a symbol/section, and initialize it if
336/// needed.
337template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000338object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name,
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000339 list_t &List) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000340 object_t *Object = new object_t(Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000341
342 List.push_back(Object);
343
344 return Object;
345}
346
347/// This function takes a section data object from the assembler
348/// and creates the associated COFF section staging object.
349void WinCOFFObjectWriter::DefineSection(MCSectionData const &SectionData) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000350 assert(SectionData.getSection().getVariant() == MCSection::SV_COFF
Alp Tokerf907b892013-12-05 05:44:44 +0000351 && "Got non-COFF section in the COFF backend!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000352 // FIXME: Not sure how to verify this (at least in a debug build).
353 MCSectionCOFF const &Sec =
354 static_cast<MCSectionCOFF const &>(SectionData.getSection());
355
356 COFFSection *coff_section = createSection(Sec.getSectionName());
357 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
358
Michael J. Spencerd6283772010-09-27 08:58:26 +0000359 coff_section->Symbol = coff_symbol;
360 coff_symbol->Section = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000361 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000362
363 // In this case the auxiliary symbol is a Section Definition.
364 coff_symbol->Aux.resize(1);
365 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
366 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000367 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
368
369 coff_section->Header.Characteristics = Sec.getCharacteristics();
370
371 uint32_t &Characteristics = coff_section->Header.Characteristics;
372 switch (SectionData.getAlignment()) {
373 case 1: Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES; break;
374 case 2: Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES; break;
375 case 4: Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES; break;
376 case 8: Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES; break;
377 case 16: Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES; break;
378 case 32: Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES; break;
379 case 64: Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES; break;
380 case 128: Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES; break;
381 case 256: Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES; break;
382 case 512: Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES; break;
383 case 1024: Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES; break;
384 case 2048: Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES; break;
385 case 4096: Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES; break;
386 case 8192: Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES; break;
387 default:
388 llvm_unreachable("unsupported section alignment");
389 }
390
391 // Bind internal COFF section to MC section.
392 coff_section->MCData = &SectionData;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000393 SectionMap[&SectionData.getSection()] = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000394}
395
396/// This function takes a section data object from the assembler
397/// and creates the associated COFF symbol staging object.
Peter Collingbourne89886872013-04-22 18:48:56 +0000398void WinCOFFObjectWriter::DefineSymbol(MCSymbolData const &SymbolData,
Reid Klecknerc1e76212013-09-17 23:18:05 +0000399 MCAssembler &Assembler,
400 const MCAsmLayout &Layout) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000401 MCSymbol const &Symbol = SymbolData.getSymbol();
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000402 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Peter Collingbourne89886872013-04-22 18:48:56 +0000403 SymbolMap[&Symbol] = coff_symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000404
Michael J. Spencer17990d52010-10-16 08:25:57 +0000405 if (SymbolData.getFlags() & COFF::SF_WeakExternal) {
406 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
407
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000408 if (Symbol.isVariable()) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000409 const MCSymbolRefExpr *SymRef =
410 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000411
Peter Collingbourne89886872013-04-22 18:48:56 +0000412 if (!SymRef)
413 report_fatal_error("Weak externals may only alias symbols");
Michael J. Spencer17990d52010-10-16 08:25:57 +0000414
Peter Collingbourne89886872013-04-22 18:48:56 +0000415 coff_symbol->Other = GetOrCreateCOFFSymbol(&SymRef->getSymbol());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000416 } else {
417 std::string WeakName = std::string(".weak.")
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000418 + Symbol.getName().str()
Michael J. Spencer17990d52010-10-16 08:25:57 +0000419 + ".default";
420 COFFSymbol *WeakDefault = createSymbol(WeakName);
421 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
422 WeakDefault->Data.StorageClass = COFF::IMAGE_SYM_CLASS_EXTERNAL;
423 WeakDefault->Data.Type = 0;
424 WeakDefault->Data.Value = 0;
425 coff_symbol->Other = WeakDefault;
426 }
427
428 // Setup the Weak External auxiliary symbol.
429 coff_symbol->Aux.resize(1);
430 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
431 coff_symbol->Aux[0].AuxType = ATWeakExternal;
432 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
433 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
434 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000435
436 coff_symbol->MCData = &SymbolData;
437 } else {
438 const MCSymbolData &ResSymData =
439 Assembler.getSymbolData(Symbol.AliasedSymbol());
440
Reid Klecknerc1e76212013-09-17 23:18:05 +0000441 if (Symbol.isVariable()) {
442 int64_t Addr;
443 if (Symbol.getVariableValue()->EvaluateAsAbsolute(Addr, Layout))
444 coff_symbol->Data.Value = Addr;
445 }
446
Peter Collingbourne89886872013-04-22 18:48:56 +0000447 coff_symbol->Data.Type = (ResSymData.getFlags() & 0x0000FFFF) >> 0;
448 coff_symbol->Data.StorageClass = (ResSymData.getFlags() & 0x00FF0000) >> 16;
449
450 // If no storage class was specified in the streamer, define it here.
451 if (coff_symbol->Data.StorageClass == 0) {
452 bool external = ResSymData.isExternal() || (ResSymData.Fragment == NULL);
453
454 coff_symbol->Data.StorageClass =
455 external ? COFF::IMAGE_SYM_CLASS_EXTERNAL : COFF::IMAGE_SYM_CLASS_STATIC;
456 }
457
Reid Klecknerc1e76212013-09-17 23:18:05 +0000458 if (Symbol.isAbsolute() || Symbol.AliasedSymbol().isVariable())
459 coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
460 else if (ResSymData.Fragment != NULL)
Peter Collingbourne89886872013-04-22 18:48:56 +0000461 coff_symbol->Section =
462 SectionMap[&ResSymData.Fragment->getParent()->getSection()];
463
464 coff_symbol->MCData = &ResSymData;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000465 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000466}
467
Nico Rieck01143f92014-02-25 09:50:40 +0000468// Maximum offsets for different string table entry encodings.
469static const unsigned Max6DecimalOffset = 999999;
470static const unsigned Max7DecimalOffset = 9999999;
471static const uint64_t MaxBase64Offset = 0xFFFFFFFFFULL; // 64^6, including 0
472
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000473// Encode a string table entry offset in base 64, padded to 6 chars, and
474// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
475// Buffer must be at least 8 bytes large. No terminating null appended.
476static void encodeBase64StringEntry(char* Buffer, uint64_t Value) {
Nico Rieck01143f92014-02-25 09:50:40 +0000477 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000478 "Illegal section name encoding for value");
479
480 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
481 "abcdefghijklmnopqrstuvwxyz"
482 "0123456789+/";
483
484 Buffer[0] = '/';
485 Buffer[1] = '/';
486
487 char* Ptr = Buffer + 7;
488 for (unsigned i = 0; i < 6; ++i) {
489 unsigned Rem = Value % 64;
490 Value /= 64;
491 *(Ptr--) = Alphabet[Rem];
492 }
493}
494
Michael J. Spencerd6283772010-09-27 08:58:26 +0000495/// making a section real involves assigned it a number and putting
496/// name into the string table if needed
497void WinCOFFObjectWriter::MakeSectionReal(COFFSection &S, size_t Number) {
498 if (S.Name.size() > COFF::NameSize) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000499 uint64_t StringTableEntry = Strings.insert(S.Name.c_str());
Michael J. Spencerd6283772010-09-27 08:58:26 +0000500
Nico Rieck01143f92014-02-25 09:50:40 +0000501 if (StringTableEntry <= Max6DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000502 std::sprintf(S.Header.Name, "/%d", unsigned(StringTableEntry));
Nico Rieck01143f92014-02-25 09:50:40 +0000503 } else if (StringTableEntry <= Max7DecimalOffset) {
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000504 // With seven digits, we have to skip the terminating null. Because
505 // sprintf always appends it, we use a larger temporary buffer.
506 char buffer[9] = { };
507 std::sprintf(buffer, "/%d", unsigned(StringTableEntry));
508 std::memcpy(S.Header.Name, buffer, 8);
Nico Rieck01143f92014-02-25 09:50:40 +0000509 } else if (StringTableEntry <= MaxBase64Offset) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000510 // Starting with 10,000,000, offsets are encoded as base64.
511 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000512 } else {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000513 report_fatal_error("COFF string table is greater than 64 GB.");
Nico Rieck2c9c89b2013-07-29 12:30:12 +0000514 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000515 } else
516 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
517
518 S.Number = Number;
519 S.Symbol->Data.SectionNumber = S.Number;
520 S.Symbol->Aux[0].Aux.SectionDefinition.Number = S.Number;
521}
522
523void WinCOFFObjectWriter::MakeSymbolReal(COFFSymbol &S, size_t Index) {
524 if (S.Name.size() > COFF::NameSize) {
525 size_t StringTableEntry = Strings.insert(S.Name.c_str());
526
527 S.set_name_offset(StringTableEntry);
528 } else
529 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
530 S.Index = Index;
531}
532
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000533bool WinCOFFObjectWriter::ExportSymbol(MCSymbolData const &SymbolData,
534 MCAssembler &Asm) {
535 // This doesn't seem to be right. Strings referred to from the .data section
536 // need symbols so they can be linked to code in the .text section right?
537
538 // return Asm.isSymbolLinkerVisible (&SymbolData);
539
540 // For now, all non-variable symbols are exported,
541 // the linker will sort the rest out for us.
542 return SymbolData.isExternal() || !SymbolData.getSymbol().isVariable();
543}
544
Michael J. Spencerd6283772010-09-27 08:58:26 +0000545bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
546 return (S->Header.Characteristics
547 & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000548}
549
550//------------------------------------------------------------------------------
551// entity writing methods
552
553void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
554 WriteLE16(Header.Machine);
555 WriteLE16(Header.NumberOfSections);
556 WriteLE32(Header.TimeDateStamp);
557 WriteLE32(Header.PointerToSymbolTable);
558 WriteLE32(Header.NumberOfSymbols);
559 WriteLE16(Header.SizeOfOptionalHeader);
560 WriteLE16(Header.Characteristics);
561}
562
563void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol *S) {
564 WriteBytes(StringRef(S->Data.Name, COFF::NameSize));
565 WriteLE32(S->Data.Value);
566 WriteLE16(S->Data.SectionNumber);
567 WriteLE16(S->Data.Type);
568 Write8(S->Data.StorageClass);
569 Write8(S->Data.NumberOfAuxSymbols);
570 WriteAuxiliarySymbols(S->Aux);
571}
572
573void WinCOFFObjectWriter::WriteAuxiliarySymbols(
574 const COFFSymbol::AuxiliarySymbols &S) {
575 for(COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
576 i != e; ++i) {
577 switch(i->AuxType) {
578 case ATFunctionDefinition:
579 WriteLE32(i->Aux.FunctionDefinition.TagIndex);
580 WriteLE32(i->Aux.FunctionDefinition.TotalSize);
581 WriteLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
582 WriteLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
583 WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
584 break;
585 case ATbfAndefSymbol:
586 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
587 WriteLE16(i->Aux.bfAndefSymbol.Linenumber);
588 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
589 WriteLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
590 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
591 break;
592 case ATWeakExternal:
593 WriteLE32(i->Aux.WeakExternal.TagIndex);
594 WriteLE32(i->Aux.WeakExternal.Characteristics);
595 WriteZeros(sizeof(i->Aux.WeakExternal.unused));
596 break;
597 case ATFile:
598 WriteBytes(StringRef(reinterpret_cast<const char *>(i->Aux.File.FileName),
599 sizeof(i->Aux.File.FileName)));
600 break;
601 case ATSectionDefinition:
602 WriteLE32(i->Aux.SectionDefinition.Length);
603 WriteLE16(i->Aux.SectionDefinition.NumberOfRelocations);
604 WriteLE16(i->Aux.SectionDefinition.NumberOfLinenumbers);
605 WriteLE32(i->Aux.SectionDefinition.CheckSum);
606 WriteLE16(i->Aux.SectionDefinition.Number);
607 Write8(i->Aux.SectionDefinition.Selection);
608 WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
609 break;
610 }
611 }
612}
613
614void WinCOFFObjectWriter::WriteSectionHeader(const COFF::section &S) {
615 WriteBytes(StringRef(S.Name, COFF::NameSize));
616
617 WriteLE32(S.VirtualSize);
618 WriteLE32(S.VirtualAddress);
619 WriteLE32(S.SizeOfRawData);
620 WriteLE32(S.PointerToRawData);
621 WriteLE32(S.PointerToRelocations);
622 WriteLE32(S.PointerToLineNumbers);
623 WriteLE16(S.NumberOfRelocations);
624 WriteLE16(S.NumberOfLineNumbers);
625 WriteLE32(S.Characteristics);
626}
627
628void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
629 WriteLE32(R.VirtualAddress);
630 WriteLE32(R.SymbolTableIndex);
631 WriteLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000632}
633
634////////////////////////////////////////////////////////////////////////////////
635// MCObjectWriter interface implementations
636
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000637void WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
638 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000639 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000640 // entries in the staging area.
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000641
642 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e; i++)
643 DefineSection(*i);
644
645 for (MCAssembler::const_symbol_iterator i = Asm.symbol_begin(),
Reid Kleckner3ea536f2013-09-17 21:24:44 +0000646 e = Asm.symbol_end();
Reid Kleckner8b2ad2a2013-11-18 23:08:12 +0000647 i != e; i++) {
648 if (ExportSymbol(*i, Asm)) {
649 DefineSymbol(*i, Asm, Layout);
650 }
651 }
Chris Lattner2c52b792010-07-11 22:07:02 +0000652}
653
654void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm,
655 const MCAsmLayout &Layout,
656 const MCFragment *Fragment,
657 const MCFixup &Fixup,
658 MCValue Target,
659 uint64_t &FixedValue) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000660 assert(Target.getSymA() != NULL && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000661
Reid Klecknerdae7b4e2013-07-15 19:41:21 +0000662 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
663 const MCSymbol &A = Symbol.AliasedSymbol();
Timur Iskhodzhanov3e4ac4e2014-01-30 21:13:05 +0000664 if (!Asm.hasSymbolData(A))
665 Asm.getContext().FatalError(
666 Fixup.getLoc(),
667 Twine("symbol '") + A.getName() + "' can not be undefined");
668
Reid Klecknerdae7b4e2013-07-15 19:41:21 +0000669 MCSymbolData &A_SD = Asm.getSymbolData(A);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000670
671 MCSectionData const *SectionData = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000672
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000673 // Mark this symbol as requiring an entry in the symbol table.
Michael J. Spencer17990d52010-10-16 08:25:57 +0000674 assert(SectionMap.find(&SectionData->getSection()) != SectionMap.end() &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000675 "Section must already have been defined in ExecutePostLayoutBinding!");
Michael J. Spencer17990d52010-10-16 08:25:57 +0000676 assert(SymbolMap.find(&A_SD.getSymbol()) != SymbolMap.end() &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000677 "Symbol must already have been defined in ExecutePostLayoutBinding!");
678
Michael J. Spencer17990d52010-10-16 08:25:57 +0000679 COFFSection *coff_section = SectionMap[&SectionData->getSection()];
680 COFFSymbol *coff_symbol = SymbolMap[&A_SD.getSymbol()];
Rafael Espindolaed164772011-04-20 14:01:45 +0000681 const MCSymbolRefExpr *SymB = Target.getSymB();
David Majnemera45a1762014-01-06 07:39:46 +0000682 bool CrossSection = false;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000683
David Majnemera45a1762014-01-06 07:39:46 +0000684 if (SymB) {
685 const MCSymbol *B = &SymB->getSymbol();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000686 MCSymbolData &B_SD = Asm.getSymbolData(*B);
David Majnemera45a1762014-01-06 07:39:46 +0000687 if (!B_SD.getFragment())
688 Asm.getContext().FatalError(
689 Fixup.getLoc(),
690 Twine("symbol '") + B->getName() +
691 "' can not be undefined in a subtraction expression");
692
693 if (!A_SD.getFragment())
694 Asm.getContext().FatalError(
695 Fixup.getLoc(),
696 Twine("symbol '") + Symbol.getName() +
697 "' can not be undefined in a subtraction expression");
698
699 CrossSection = &Symbol.getSection() != &B->getSection();
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000700
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000701 // Offset of the symbol in the section
702 int64_t a = Layout.getSymbolOffset(&B_SD);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000703
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000704 // Ofeset of the relocation in the section
705 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
706
707 FixedValue = b - a;
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000708 // In the case where we have SymbA and SymB, we just need to store the delta
709 // between the two symbols. Update FixedValue to account for the delta, and
710 // skip recording the relocation.
Rafael Espindolaed164772011-04-20 14:01:45 +0000711 if (!CrossSection)
712 return;
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000713 } else {
714 FixedValue = Target.getConstant();
715 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000716
717 COFFRelocation Reloc;
718
Daniel Dunbar727be432010-07-31 21:08:54 +0000719 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000720 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000721
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000722 // Turn relocations for temporary symbols into section relocations.
Rafael Espindolaed164772011-04-20 14:01:45 +0000723 if (coff_symbol->MCData->getSymbol().isTemporary() || CrossSection) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000724 Reloc.Symb = coff_symbol->Section->Symbol;
Michael J. Spencera3b34ed2010-10-05 19:48:03 +0000725 FixedValue += Layout.getFragmentOffset(coff_symbol->MCData->Fragment)
726 + coff_symbol->MCData->getOffset();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000727 } else
728 Reloc.Symb = coff_symbol;
729
730 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000731
732 Reloc.Data.VirtualAddress += Fixup.getOffset();
Nico Rieck1da45292013-04-10 23:28:17 +0000733 Reloc.Data.Type = TargetObjectWriter->getRelocType(Target, Fixup,
734 CrossSection);
Rafael Espindolae61724a2011-12-22 22:21:47 +0000735
736 // FIXME: Can anyone explain what this does other than adjust for the size
737 // of the offset?
738 if (Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32 ||
739 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32)
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000740 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000741
742 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000743}
744
Rafael Espindolabce26a12010-10-05 15:11:03 +0000745void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000746 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000747 // Assign symbol and section indexes and offsets.
Michael J. Spencerd6283772010-09-27 08:58:26 +0000748 Header.NumberOfSections = 0;
749
Nico Riecka37acf72013-07-06 12:13:10 +0000750 DenseMap<COFFSection *, uint16_t> SectionIndices;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000751 for (sections::iterator i = Sections.begin(),
752 e = Sections.end(); i != e; i++) {
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000753 if (Layout.getSectionAddressSize((*i)->MCData) > 0) {
Nico Riecka37acf72013-07-06 12:13:10 +0000754 size_t Number = ++Header.NumberOfSections;
755 SectionIndices[*i] = Number;
756 MakeSectionReal(**i, Number);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000757 } else {
758 (*i)->Number = -1;
759 }
760 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000761
762 Header.NumberOfSymbols = 0;
763
764 for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++) {
765 COFFSymbol *coff_symbol = *i;
766 MCSymbolData const *SymbolData = coff_symbol->MCData;
767
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000768 // Update section number & offset for symbols that have them.
769 if ((SymbolData != NULL) && (SymbolData->Fragment != NULL)) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000770 assert(coff_symbol->Section != NULL);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000771
Michael J. Spencerd6283772010-09-27 08:58:26 +0000772 coff_symbol->Data.SectionNumber = coff_symbol->Section->Number;
Michael J. Spencer54cfd422010-08-03 05:02:46 +0000773 coff_symbol->Data.Value = Layout.getFragmentOffset(SymbolData->Fragment)
774 + SymbolData->Offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000775 }
776
Michael J. Spencerd6283772010-09-27 08:58:26 +0000777 if (coff_symbol->should_keep()) {
778 MakeSymbolReal(*coff_symbol, Header.NumberOfSymbols++);
779
780 // Update auxiliary symbol info.
781 coff_symbol->Data.NumberOfAuxSymbols = coff_symbol->Aux.size();
782 Header.NumberOfSymbols += coff_symbol->Data.NumberOfAuxSymbols;
783 } else
784 coff_symbol->Index = -1;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000785 }
786
787 // Fixup weak external references.
788 for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000789 COFFSymbol *coff_symbol = *i;
790 if (coff_symbol->Other != NULL) {
791 assert(coff_symbol->Index != -1);
792 assert(coff_symbol->Aux.size() == 1 &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000793 "Symbol must contain one aux symbol!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000794 assert(coff_symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000795 "Symbol's aux symbol must be a Weak External!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000796 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = coff_symbol->Other->Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000797 }
798 }
799
Nico Riecka37acf72013-07-06 12:13:10 +0000800 // Fixup associative COMDAT sections.
801 for (sections::iterator i = Sections.begin(),
802 e = Sections.end(); i != e; i++) {
803 if ((*i)->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
804 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
805 continue;
806
807 const MCSectionCOFF &MCSec = static_cast<const MCSectionCOFF &>(
808 (*i)->MCData->getSection());
809
810 COFFSection *Assoc = SectionMap.lookup(MCSec.getAssocSection());
811 if (!Assoc) {
812 report_fatal_error(Twine("Missing associated COMDAT section ") +
813 MCSec.getAssocSection()->getSectionName() +
814 " for section " + MCSec.getSectionName());
815 }
816
817 // Skip this section if the associated section is unused.
818 if (Assoc->Number == -1)
819 continue;
820
821 (*i)->Symbol->Aux[0].Aux.SectionDefinition.Number = SectionIndices[Assoc];
822 }
823
824
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000825 // Assign file offsets to COFF object file structures.
826
827 unsigned offset = 0;
828
829 offset += COFF::HeaderSize;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000830 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000831
832 for (MCAssembler::const_iterator i = Asm.begin(),
833 e = Asm.end();
834 i != e; i++) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000835 COFFSection *Sec = SectionMap[&i->getSection()];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000836
Michael J. Spencerd6283772010-09-27 08:58:26 +0000837 if (Sec->Number == -1)
838 continue;
839
Michael J. Spencera6a984b2010-10-09 16:04:45 +0000840 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(i);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000841
Michael J. Spencerd6283772010-09-27 08:58:26 +0000842 if (IsPhysicalSection(Sec)) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000843 Sec->Header.PointerToRawData = offset;
844
845 offset += Sec->Header.SizeOfRawData;
846 }
847
848 if (Sec->Relocations.size() > 0) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000849 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
850
851 if (RelocationsOverflow) {
852 // Signal overflow by setting NumberOfSections to max value. Actual
853 // size is found in reloc #0. Microsoft tools understand this.
854 Sec->Header.NumberOfRelocations = 0xffff;
855 } else {
856 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
857 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000858 Sec->Header.PointerToRelocations = offset;
859
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000860 if (RelocationsOverflow) {
861 // Reloc #0 will contain actual count, so make room for it.
862 offset += COFF::RelocationSize;
863 }
864
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000865 offset += COFF::RelocationSize * Sec->Relocations.size();
866
867 for (relocations::iterator cr = Sec->Relocations.begin(),
868 er = Sec->Relocations.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000869 cr != er; ++cr) {
870 assert((*cr).Symb->Index != -1);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000871 (*cr).Data.SymbolTableIndex = (*cr).Symb->Index;
872 }
873 }
874
Michael J. Spencerd6283772010-09-27 08:58:26 +0000875 assert(Sec->Symbol->Aux.size() == 1
876 && "Section's symbol must have one aux!");
877 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000878 assert(Aux.AuxType == ATSectionDefinition &&
879 "Section's symbol's aux symbol must be a Section Definition!");
880 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
881 Aux.Aux.SectionDefinition.NumberOfRelocations =
882 Sec->Header.NumberOfRelocations;
883 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
884 Sec->Header.NumberOfLineNumbers;
885 }
886
887 Header.PointerToSymbolTable = offset;
888
Rafael Espindola5ac4ad22013-12-04 02:26:54 +0000889 // We want a deterministic output. It looks like GNU as also writes 0 in here.
Rafael Espindola9201e6b2013-12-04 02:02:55 +0000890 Header.TimeDateStamp = 0;
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +0000891
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000892 // Write it all to disk...
893 WriteFileHeader(Header);
894
895 {
896 sections::iterator i, ie;
897 MCAssembler::const_iterator j, je;
898
899 for (i = Sections.begin(), ie = Sections.end(); i != ie; i++)
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000900 if ((*i)->Number != -1) {
901 if ((*i)->Relocations.size() >= 0xffff) {
902 (*i)->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
903 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000904 WriteSectionHeader((*i)->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000905 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000906
907 for (i = Sections.begin(), ie = Sections.end(),
908 j = Asm.begin(), je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000909 (i != ie) && (j != je); ++i, ++j) {
910
911 if ((*i)->Number == -1)
912 continue;
913
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000914 if ((*i)->Header.PointerToRawData != 0) {
915 assert(OS.tell() == (*i)->Header.PointerToRawData &&
916 "Section::PointerToRawData is insane!");
917
Jim Grosbach18e2fe42011-12-06 00:03:48 +0000918 Asm.writeSectionData(j, Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000919 }
920
921 if ((*i)->Relocations.size() > 0) {
922 assert(OS.tell() == (*i)->Header.PointerToRelocations &&
923 "Section::PointerToRelocations is insane!");
924
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000925 if ((*i)->Relocations.size() >= 0xffff) {
926 // In case of overflow, write actual relocation count as first
927 // relocation. Including the synthetic reloc itself (+ 1).
928 COFF::relocation r;
929 r.VirtualAddress = (*i)->Relocations.size() + 1;
930 r.SymbolTableIndex = 0;
931 r.Type = 0;
932 WriteRelocation(r);
933 }
934
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000935 for (relocations::const_iterator k = (*i)->Relocations.begin(),
936 ke = (*i)->Relocations.end();
937 k != ke; k++) {
938 WriteRelocation(k->Data);
939 }
940 } else
941 assert((*i)->Header.PointerToRelocations == 0 &&
942 "Section::PointerToRelocations is insane!");
943 }
944 }
945
946 assert(OS.tell() == Header.PointerToSymbolTable &&
947 "Header::PointerToSymbolTable is insane!");
948
949 for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000950 if ((*i)->Index != -1)
951 WriteSymbol(*i);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000952
953 OS.write((char const *)&Strings.Data.front(), Strings.Data.size());
Chris Lattner2c52b792010-07-11 22:07:02 +0000954}
955
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000956MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_) :
957 Machine(Machine_) {
958}
959
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000960// Pin the vtable to this file.
961void MCWinCOFFObjectTargetWriter::anchor() {}
962
Chris Lattner2c52b792010-07-11 22:07:02 +0000963//------------------------------------------------------------------------------
964// WinCOFFObjectWriter factory function
965
966namespace llvm {
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000967 MCObjectWriter *createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
968 raw_ostream &OS) {
969 return new WinCOFFObjectWriter(MOTW, OS);
Chris Lattner2c52b792010-07-11 22:07:02 +0000970 }
Michael J. Spencerc2129372010-07-26 03:01:28 +0000971}