blob: 518b59ee244d04f05522f0afe1ebb7e147aa9bab [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"
Rafael Espindola908d2ed2011-12-24 02:14:02 +000018#include "llvm/ADT/OwningPtr.h"
Michael J. Spencerb5fc1382010-07-26 02:17:32 +000019#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/StringRef.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
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000127 llvm::OwningPtr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
128
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);
Benjamin Kramerc2997dd2010-07-29 11:57:59 +0000140 ~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);
Peter Collingbourne89886872013-04-22 18:48:56 +0000150 void DefineSymbol(MCSymbolData const &SymbolData,
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000151 MCAssembler &Assembler);
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
156 bool ExportSection(COFFSection const *S);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000157 bool ExportSymbol(MCSymbolData const &SymbolData, MCAssembler &Asm);
158
Michael J. Spencerd6283772010-09-27 08:58:26 +0000159 bool IsPhysicalSection(COFFSection *S);
160
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000161 // Entity writing methods.
162
163 void WriteFileHeader(const COFF::header &Header);
164 void WriteSymbol(const COFFSymbol *S);
165 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
166 void WriteSectionHeader(const COFF::section &S);
167 void WriteRelocation(const COFF::relocation &R);
168
169 // MCObjectWriter interface implementation.
170
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000171 void ExecutePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000172
173 void RecordRelocation(const MCAssembler &Asm,
174 const MCAsmLayout &Layout,
175 const MCFragment *Fragment,
176 const MCFixup &Fixup,
177 MCValue Target,
178 uint64_t &FixedValue);
179
Rafael Espindolabce26a12010-10-05 15:11:03 +0000180 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000181};
Chris Lattner2c52b792010-07-11 22:07:02 +0000182}
183
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000184static inline void write_uint32_le(void *Data, uint32_t const &Value) {
185 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data);
186 Ptr[0] = (Value & 0x000000FF) >> 0;
187 Ptr[1] = (Value & 0x0000FF00) >> 8;
188 Ptr[2] = (Value & 0x00FF0000) >> 16;
189 Ptr[3] = (Value & 0xFF000000) >> 24;
190}
191
192static inline void write_uint16_le(void *Data, uint16_t const &Value) {
193 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data);
194 Ptr[0] = (Value & 0x00FF) >> 0;
195 Ptr[1] = (Value & 0xFF00) >> 8;
196}
197
198static inline void write_uint8_le(void *Data, uint8_t const &Value) {
199 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data);
200 Ptr[0] = (Value & 0xFF) >> 0;
201}
202
203//------------------------------------------------------------------------------
204// Symbol class implementation
205
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000206COFFSymbol::COFFSymbol(StringRef name)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000207 : Name(name.begin(), name.end())
208 , Other(NULL)
209 , Section(NULL)
210 , Relocations(0)
211 , MCData(NULL) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000212 memset(&Data, 0, sizeof(Data));
213}
214
215size_t COFFSymbol::size() const {
216 return COFF::SymbolSize + (Data.NumberOfAuxSymbols * COFF::SymbolSize);
217}
218
219// 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
Michael J. Spencerd6283772010-09-27 08:58:26 +0000227/// logic to decide if the symbol should be reported in the symbol table
228bool COFFSymbol::should_keep() const {
229 // no section means its external, keep it
230 if (Section == NULL)
231 return true;
232
233 // if it has relocations pointing at it, keep it
234 if (Relocations > 0) {
235 assert(Section->Number != -1 && "Sections with relocations must be real!");
236 return true;
237 }
238
239 // if the section its in is being droped, drop it
240 if (Section->Number == -1)
241 return false;
242
243 // if it is the section symbol, keep it
244 if (Section->Symbol == this)
245 return true;
246
247 // if its temporary, drop it
248 if (MCData && MCData->getSymbol().isTemporary())
249 return false;
250
251 // otherwise, keep it
252 return true;
253}
254
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000255//------------------------------------------------------------------------------
256// Section class implementation
257
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000258COFFSection::COFFSection(StringRef name)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000259 : Name(name)
260 , MCData(NULL)
261 , Symbol(NULL) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000262 memset(&Header, 0, sizeof(Header));
263}
264
265size_t COFFSection::size() {
266 return COFF::SectionSize;
267}
268
269//------------------------------------------------------------------------------
270// StringTable class implementation
271
272/// Write the length of the string table into Data.
273/// The length of the string table includes uint32 length header.
274void StringTable::update_length() {
275 write_uint32_le(&Data.front(), Data.size());
276}
277
278StringTable::StringTable() {
279 // The string table data begins with the length of the entire string table
280 // including the length header. Allocate space for this header.
281 Data.resize(4);
Michael J. Spencer5ca95bc2011-11-08 19:52:32 +0000282 update_length();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000283}
284
285size_t StringTable::size() const {
286 return Data.size();
287}
288
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000289/// Add String to the table iff it is not already there.
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000290/// @returns the index into the string table where the string is now located.
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000291size_t StringTable::insert(StringRef String) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000292 map::iterator i = Map.find(String);
293
294 if (i != Map.end())
295 return i->second;
296
297 size_t Offset = Data.size();
298
299 // Insert string data into string table.
300 Data.insert(Data.end(), String.begin(), String.end());
301 Data.push_back('\0');
302
303 // Put a reference to it in the map.
304 Map[String] = Offset;
305
306 // Update the internal length field.
307 update_length();
308
309 return Offset;
310}
311
312//------------------------------------------------------------------------------
313// WinCOFFObjectWriter class implementation
314
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000315WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
316 raw_ostream &OS)
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000317 : MCObjectWriter(OS, true)
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000318 , TargetObjectWriter(MOTW) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000319 memset(&Header, 0, sizeof(Header));
Michael J. Spencer377aa202010-08-21 05:58:13 +0000320
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000321 Header.Machine = TargetObjectWriter->getMachine();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000322}
323
Benjamin Kramerc2997dd2010-07-29 11:57:59 +0000324WinCOFFObjectWriter::~WinCOFFObjectWriter() {
325 for (symbols::iterator I = Symbols.begin(), E = Symbols.end(); I != E; ++I)
326 delete *I;
327 for (sections::iterator I = Sections.begin(), E = Sections.end(); I != E; ++I)
328 delete *I;
329}
330
Michael J. Spencer17990d52010-10-16 08:25:57 +0000331COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000332 return createCOFFEntity<COFFSymbol>(Name, Symbols);
333}
334
Michael J. Spencer17990d52010-10-16 08:25:57 +0000335COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol * Symbol){
336 symbol_map::iterator i = SymbolMap.find(Symbol);
337 if (i != SymbolMap.end())
338 return i->second;
339 COFFSymbol *RetSymbol
340 = createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
341 SymbolMap[Symbol] = RetSymbol;
342 return RetSymbol;
343}
344
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000345COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000346 return createCOFFEntity<COFFSection>(Name, Sections);
347}
348
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000349/// A template used to lookup or create a symbol/section, and initialize it if
350/// needed.
351template <typename object_t, typename list_t>
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000352object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name,
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000353 list_t &List) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000354 object_t *Object = new object_t(Name);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000355
356 List.push_back(Object);
357
358 return Object;
359}
360
361/// This function takes a section data object from the assembler
362/// and creates the associated COFF section staging object.
363void WinCOFFObjectWriter::DefineSection(MCSectionData const &SectionData) {
Michael J. Spencerbe52c622010-10-09 11:00:37 +0000364 assert(SectionData.getSection().getVariant() == MCSection::SV_COFF
365 && "Got non COFF section in the COFF backend!");
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000366 // FIXME: Not sure how to verify this (at least in a debug build).
367 MCSectionCOFF const &Sec =
368 static_cast<MCSectionCOFF const &>(SectionData.getSection());
369
370 COFFSection *coff_section = createSection(Sec.getSectionName());
371 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
372
Michael J. Spencerd6283772010-09-27 08:58:26 +0000373 coff_section->Symbol = coff_symbol;
374 coff_symbol->Section = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000375 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000376
377 // In this case the auxiliary symbol is a Section Definition.
378 coff_symbol->Aux.resize(1);
379 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
380 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000381 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
382
383 coff_section->Header.Characteristics = Sec.getCharacteristics();
384
385 uint32_t &Characteristics = coff_section->Header.Characteristics;
386 switch (SectionData.getAlignment()) {
387 case 1: Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES; break;
388 case 2: Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES; break;
389 case 4: Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES; break;
390 case 8: Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES; break;
391 case 16: Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES; break;
392 case 32: Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES; break;
393 case 64: Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES; break;
394 case 128: Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES; break;
395 case 256: Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES; break;
396 case 512: Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES; break;
397 case 1024: Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES; break;
398 case 2048: Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES; break;
399 case 4096: Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES; break;
400 case 8192: Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES; break;
401 default:
402 llvm_unreachable("unsupported section alignment");
403 }
404
405 // Bind internal COFF section to MC section.
406 coff_section->MCData = &SectionData;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000407 SectionMap[&SectionData.getSection()] = coff_section;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000408}
409
410/// This function takes a section data object from the assembler
411/// and creates the associated COFF symbol staging object.
Peter Collingbourne89886872013-04-22 18:48:56 +0000412void WinCOFFObjectWriter::DefineSymbol(MCSymbolData const &SymbolData,
Michael J. Spencerd6283772010-09-27 08:58:26 +0000413 MCAssembler &Assembler) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000414 MCSymbol const &Symbol = SymbolData.getSymbol();
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000415 COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
Peter Collingbourne89886872013-04-22 18:48:56 +0000416 SymbolMap[&Symbol] = coff_symbol;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000417
Michael J. Spencer17990d52010-10-16 08:25:57 +0000418 if (SymbolData.getFlags() & COFF::SF_WeakExternal) {
419 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
420
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000421 if (Symbol.isVariable()) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000422 const MCSymbolRefExpr *SymRef =
423 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000424
Peter Collingbourne89886872013-04-22 18:48:56 +0000425 if (!SymRef)
426 report_fatal_error("Weak externals may only alias symbols");
Michael J. Spencer17990d52010-10-16 08:25:57 +0000427
Peter Collingbourne89886872013-04-22 18:48:56 +0000428 coff_symbol->Other = GetOrCreateCOFFSymbol(&SymRef->getSymbol());
Michael J. Spencer17990d52010-10-16 08:25:57 +0000429 } else {
430 std::string WeakName = std::string(".weak.")
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000431 + Symbol.getName().str()
Michael J. Spencer17990d52010-10-16 08:25:57 +0000432 + ".default";
433 COFFSymbol *WeakDefault = createSymbol(WeakName);
434 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
435 WeakDefault->Data.StorageClass = COFF::IMAGE_SYM_CLASS_EXTERNAL;
436 WeakDefault->Data.Type = 0;
437 WeakDefault->Data.Value = 0;
438 coff_symbol->Other = WeakDefault;
439 }
440
441 // Setup the Weak External auxiliary symbol.
442 coff_symbol->Aux.resize(1);
443 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
444 coff_symbol->Aux[0].AuxType = ATWeakExternal;
445 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
446 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
447 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
Peter Collingbourne89886872013-04-22 18:48:56 +0000448
449 coff_symbol->MCData = &SymbolData;
450 } else {
451 const MCSymbolData &ResSymData =
452 Assembler.getSymbolData(Symbol.AliasedSymbol());
453
454 coff_symbol->Data.Type = (ResSymData.getFlags() & 0x0000FFFF) >> 0;
455 coff_symbol->Data.StorageClass = (ResSymData.getFlags() & 0x00FF0000) >> 16;
456
457 // If no storage class was specified in the streamer, define it here.
458 if (coff_symbol->Data.StorageClass == 0) {
459 bool external = ResSymData.isExternal() || (ResSymData.Fragment == NULL);
460
461 coff_symbol->Data.StorageClass =
462 external ? COFF::IMAGE_SYM_CLASS_EXTERNAL : COFF::IMAGE_SYM_CLASS_STATIC;
463 }
464
465 if (ResSymData.Fragment != NULL)
466 coff_symbol->Section =
467 SectionMap[&ResSymData.Fragment->getParent()->getSection()];
468
469 coff_symbol->MCData = &ResSymData;
Michael J. Spencer17990d52010-10-16 08:25:57 +0000470 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000471}
472
Michael J. Spencerd6283772010-09-27 08:58:26 +0000473/// making a section real involves assigned it a number and putting
474/// name into the string table if needed
475void WinCOFFObjectWriter::MakeSectionReal(COFFSection &S, size_t Number) {
476 if (S.Name.size() > COFF::NameSize) {
477 size_t StringTableEntry = Strings.insert(S.Name.c_str());
478
479 // FIXME: Why is this number 999999? This number is never mentioned in the
480 // spec. I'm assuming this is due to the printed value needing to fit into
481 // the S.Header.Name field. In which case why not 9999999 (7 9's instead of
482 // 6)? The spec does not state if this entry should be null terminated in
483 // this case, and thus this seems to be the best way to do it. I think I
484 // just solved my own FIXME...
485 if (StringTableEntry > 999999)
486 report_fatal_error("COFF string table is greater than 999999 bytes.");
487
488 std::sprintf(S.Header.Name, "/%d", unsigned(StringTableEntry));
489 } else
490 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
491
492 S.Number = Number;
493 S.Symbol->Data.SectionNumber = S.Number;
494 S.Symbol->Aux[0].Aux.SectionDefinition.Number = S.Number;
495}
496
497void WinCOFFObjectWriter::MakeSymbolReal(COFFSymbol &S, size_t Index) {
498 if (S.Name.size() > COFF::NameSize) {
499 size_t StringTableEntry = Strings.insert(S.Name.c_str());
500
501 S.set_name_offset(StringTableEntry);
502 } else
503 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
504 S.Index = Index;
505}
506
507bool WinCOFFObjectWriter::ExportSection(COFFSection const *S) {
508 return !S->MCData->getFragmentList().empty();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000509}
510
511bool WinCOFFObjectWriter::ExportSymbol(MCSymbolData const &SymbolData,
512 MCAssembler &Asm) {
513 // This doesn't seem to be right. Strings referred to from the .data section
514 // need symbols so they can be linked to code in the .text section right?
515
516 // return Asm.isSymbolLinkerVisible (&SymbolData);
517
Michael J. Spencerd6283772010-09-27 08:58:26 +0000518 // For now, all non-variable symbols are exported,
519 // the linker will sort the rest out for us.
Michael J. Spencer17990d52010-10-16 08:25:57 +0000520 return SymbolData.isExternal() || !SymbolData.getSymbol().isVariable();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000521}
522
523bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
524 return (S->Header.Characteristics
525 & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000526}
527
528//------------------------------------------------------------------------------
529// entity writing methods
530
531void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
532 WriteLE16(Header.Machine);
533 WriteLE16(Header.NumberOfSections);
534 WriteLE32(Header.TimeDateStamp);
535 WriteLE32(Header.PointerToSymbolTable);
536 WriteLE32(Header.NumberOfSymbols);
537 WriteLE16(Header.SizeOfOptionalHeader);
538 WriteLE16(Header.Characteristics);
539}
540
541void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol *S) {
542 WriteBytes(StringRef(S->Data.Name, COFF::NameSize));
543 WriteLE32(S->Data.Value);
544 WriteLE16(S->Data.SectionNumber);
545 WriteLE16(S->Data.Type);
546 Write8(S->Data.StorageClass);
547 Write8(S->Data.NumberOfAuxSymbols);
548 WriteAuxiliarySymbols(S->Aux);
549}
550
551void WinCOFFObjectWriter::WriteAuxiliarySymbols(
552 const COFFSymbol::AuxiliarySymbols &S) {
553 for(COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
554 i != e; ++i) {
555 switch(i->AuxType) {
556 case ATFunctionDefinition:
557 WriteLE32(i->Aux.FunctionDefinition.TagIndex);
558 WriteLE32(i->Aux.FunctionDefinition.TotalSize);
559 WriteLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
560 WriteLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
561 WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
562 break;
563 case ATbfAndefSymbol:
564 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
565 WriteLE16(i->Aux.bfAndefSymbol.Linenumber);
566 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
567 WriteLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
568 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
569 break;
570 case ATWeakExternal:
571 WriteLE32(i->Aux.WeakExternal.TagIndex);
572 WriteLE32(i->Aux.WeakExternal.Characteristics);
573 WriteZeros(sizeof(i->Aux.WeakExternal.unused));
574 break;
575 case ATFile:
576 WriteBytes(StringRef(reinterpret_cast<const char *>(i->Aux.File.FileName),
577 sizeof(i->Aux.File.FileName)));
578 break;
579 case ATSectionDefinition:
580 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(i->Aux.SectionDefinition.Number);
585 Write8(i->Aux.SectionDefinition.Selection);
586 WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
587 break;
588 }
589 }
590}
591
592void WinCOFFObjectWriter::WriteSectionHeader(const COFF::section &S) {
593 WriteBytes(StringRef(S.Name, COFF::NameSize));
594
595 WriteLE32(S.VirtualSize);
596 WriteLE32(S.VirtualAddress);
597 WriteLE32(S.SizeOfRawData);
598 WriteLE32(S.PointerToRawData);
599 WriteLE32(S.PointerToRelocations);
600 WriteLE32(S.PointerToLineNumbers);
601 WriteLE16(S.NumberOfRelocations);
602 WriteLE16(S.NumberOfLineNumbers);
603 WriteLE32(S.Characteristics);
604}
605
606void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
607 WriteLE32(R.VirtualAddress);
608 WriteLE32(R.SymbolTableIndex);
609 WriteLE16(R.Type);
Chris Lattner2c52b792010-07-11 22:07:02 +0000610}
611
612////////////////////////////////////////////////////////////////////////////////
613// MCObjectWriter interface implementations
614
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000615void WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
616 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000617 // "Define" each section & symbol. This creates section & symbol
Michael J. Spencerd6283772010-09-27 08:58:26 +0000618 // entries in the staging area.
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000619
620 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e; i++)
621 DefineSection(*i);
622
623 for (MCAssembler::const_symbol_iterator i = Asm.symbol_begin(),
624 e = Asm.symbol_end(); i != e; i++) {
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000625 if (ExportSymbol(*i, Asm)) {
Peter Collingbourne89886872013-04-22 18:48:56 +0000626 DefineSymbol(*i, Asm);
Michael J. Spencer54b24e12013-01-29 22:10:07 +0000627 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000628 }
Chris Lattner2c52b792010-07-11 22:07:02 +0000629}
630
631void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm,
632 const MCAsmLayout &Layout,
633 const MCFragment *Fragment,
634 const MCFixup &Fixup,
635 MCValue Target,
636 uint64_t &FixedValue) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000637 assert(Target.getSymA() != NULL && "Relocation must reference a symbol!");
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000638
639 const MCSymbol *A = &Target.getSymA()->getSymbol();
640 MCSymbolData &A_SD = Asm.getSymbolData(*A);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000641
642 MCSectionData const *SectionData = Fragment->getParent();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000643
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000644 // Mark this symbol as requiring an entry in the symbol table.
Michael J. Spencer17990d52010-10-16 08:25:57 +0000645 assert(SectionMap.find(&SectionData->getSection()) != SectionMap.end() &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000646 "Section must already have been defined in ExecutePostLayoutBinding!");
Michael J. Spencer17990d52010-10-16 08:25:57 +0000647 assert(SymbolMap.find(&A_SD.getSymbol()) != SymbolMap.end() &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000648 "Symbol must already have been defined in ExecutePostLayoutBinding!");
649
Michael J. Spencer17990d52010-10-16 08:25:57 +0000650 COFFSection *coff_section = SectionMap[&SectionData->getSection()];
651 COFFSymbol *coff_symbol = SymbolMap[&A_SD.getSymbol()];
Rafael Espindolaed164772011-04-20 14:01:45 +0000652 const MCSymbolRefExpr *SymA = Target.getSymA();
653 const MCSymbolRefExpr *SymB = Target.getSymB();
654 const bool CrossSection = SymB &&
655 &SymA->getSymbol().getSection() != &SymB->getSymbol().getSection();
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000656
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000657 if (Target.getSymB()) {
658 const MCSymbol *B = &Target.getSymB()->getSymbol();
659 MCSymbolData &B_SD = Asm.getSymbolData(*B);
660
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000661 // Offset of the symbol in the section
662 int64_t a = Layout.getSymbolOffset(&B_SD);
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000663
Rafael Espindolac3dc4862011-04-21 18:36:50 +0000664 // Ofeset of the relocation in the section
665 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
666
667 FixedValue = b - a;
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000668 // In the case where we have SymbA and SymB, we just need to store the delta
669 // between the two symbols. Update FixedValue to account for the delta, and
670 // skip recording the relocation.
Rafael Espindolaed164772011-04-20 14:01:45 +0000671 if (!CrossSection)
672 return;
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000673 } else {
674 FixedValue = Target.getConstant();
675 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000676
677 COFFRelocation Reloc;
678
Daniel Dunbar727be432010-07-31 21:08:54 +0000679 Reloc.Data.SymbolTableIndex = 0;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000680 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
Michael J. Spencerd6283772010-09-27 08:58:26 +0000681
Michael J. Spencera65d17a2010-10-05 19:48:12 +0000682 // Turn relocations for temporary symbols into section relocations.
Rafael Espindolaed164772011-04-20 14:01:45 +0000683 if (coff_symbol->MCData->getSymbol().isTemporary() || CrossSection) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000684 Reloc.Symb = coff_symbol->Section->Symbol;
Michael J. Spencera3b34ed2010-10-05 19:48:03 +0000685 FixedValue += Layout.getFragmentOffset(coff_symbol->MCData->Fragment)
686 + coff_symbol->MCData->getOffset();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000687 } else
688 Reloc.Symb = coff_symbol;
689
690 ++Reloc.Symb->Relocations;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000691
692 Reloc.Data.VirtualAddress += Fixup.getOffset();
Nico Rieck1da45292013-04-10 23:28:17 +0000693 Reloc.Data.Type = TargetObjectWriter->getRelocType(Target, Fixup,
694 CrossSection);
Rafael Espindolae61724a2011-12-22 22:21:47 +0000695
696 // FIXME: Can anyone explain what this does other than adjust for the size
697 // of the offset?
698 if (Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32 ||
699 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32)
Michael J. Spencerccd28d02010-08-24 21:04:52 +0000700 FixedValue += 4;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000701
702 coff_section->Relocations.push_back(Reloc);
Chris Lattner2c52b792010-07-11 22:07:02 +0000703}
704
Rafael Espindolabce26a12010-10-05 15:11:03 +0000705void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
Chris Lattner2c52b792010-07-11 22:07:02 +0000706 const MCAsmLayout &Layout) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000707 // Assign symbol and section indexes and offsets.
Michael J. Spencerd6283772010-09-27 08:58:26 +0000708 Header.NumberOfSections = 0;
709
710 for (sections::iterator i = Sections.begin(),
711 e = Sections.end(); i != e; i++) {
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000712 if (Layout.getSectionAddressSize((*i)->MCData) > 0) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000713 MakeSectionReal(**i, ++Header.NumberOfSections);
714 } else {
715 (*i)->Number = -1;
716 }
717 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000718
719 Header.NumberOfSymbols = 0;
720
721 for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++) {
722 COFFSymbol *coff_symbol = *i;
723 MCSymbolData const *SymbolData = coff_symbol->MCData;
724
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000725 // Update section number & offset for symbols that have them.
726 if ((SymbolData != NULL) && (SymbolData->Fragment != NULL)) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000727 assert(coff_symbol->Section != NULL);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000728
Michael J. Spencerd6283772010-09-27 08:58:26 +0000729 coff_symbol->Data.SectionNumber = coff_symbol->Section->Number;
Michael J. Spencer54cfd422010-08-03 05:02:46 +0000730 coff_symbol->Data.Value = Layout.getFragmentOffset(SymbolData->Fragment)
731 + SymbolData->Offset;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000732 }
733
Michael J. Spencerd6283772010-09-27 08:58:26 +0000734 if (coff_symbol->should_keep()) {
735 MakeSymbolReal(*coff_symbol, Header.NumberOfSymbols++);
736
737 // Update auxiliary symbol info.
738 coff_symbol->Data.NumberOfAuxSymbols = coff_symbol->Aux.size();
739 Header.NumberOfSymbols += coff_symbol->Data.NumberOfAuxSymbols;
740 } else
741 coff_symbol->Index = -1;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000742 }
743
744 // Fixup weak external references.
745 for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++) {
Michael J. Spencerd6283772010-09-27 08:58:26 +0000746 COFFSymbol *coff_symbol = *i;
747 if (coff_symbol->Other != NULL) {
748 assert(coff_symbol->Index != -1);
749 assert(coff_symbol->Aux.size() == 1 &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000750 "Symbol must contain one aux symbol!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000751 assert(coff_symbol->Aux[0].AuxType == ATWeakExternal &&
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000752 "Symbol's aux symbol must be a Weak External!");
Michael J. Spencerd6283772010-09-27 08:58:26 +0000753 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = coff_symbol->Other->Index;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000754 }
755 }
756
757 // Assign file offsets to COFF object file structures.
758
759 unsigned offset = 0;
760
761 offset += COFF::HeaderSize;
Michael J. Spencerd6283772010-09-27 08:58:26 +0000762 offset += COFF::SectionSize * Header.NumberOfSections;
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000763
764 for (MCAssembler::const_iterator i = Asm.begin(),
765 e = Asm.end();
766 i != e; i++) {
Michael J. Spencer17990d52010-10-16 08:25:57 +0000767 COFFSection *Sec = SectionMap[&i->getSection()];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000768
Michael J. Spencerd6283772010-09-27 08:58:26 +0000769 if (Sec->Number == -1)
770 continue;
771
Michael J. Spencera6a984b2010-10-09 16:04:45 +0000772 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(i);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000773
Michael J. Spencerd6283772010-09-27 08:58:26 +0000774 if (IsPhysicalSection(Sec)) {
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000775 Sec->Header.PointerToRawData = offset;
776
777 offset += Sec->Header.SizeOfRawData;
778 }
779
780 if (Sec->Relocations.size() > 0) {
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000781 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
782
783 if (RelocationsOverflow) {
784 // Signal overflow by setting NumberOfSections to max value. Actual
785 // size is found in reloc #0. Microsoft tools understand this.
786 Sec->Header.NumberOfRelocations = 0xffff;
787 } else {
788 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
789 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000790 Sec->Header.PointerToRelocations = offset;
791
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000792 if (RelocationsOverflow) {
793 // Reloc #0 will contain actual count, so make room for it.
794 offset += COFF::RelocationSize;
795 }
796
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000797 offset += COFF::RelocationSize * Sec->Relocations.size();
798
799 for (relocations::iterator cr = Sec->Relocations.begin(),
800 er = Sec->Relocations.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000801 cr != er; ++cr) {
802 assert((*cr).Symb->Index != -1);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000803 (*cr).Data.SymbolTableIndex = (*cr).Symb->Index;
804 }
805 }
806
Michael J. Spencerd6283772010-09-27 08:58:26 +0000807 assert(Sec->Symbol->Aux.size() == 1
808 && "Section's symbol must have one aux!");
809 AuxSymbol &Aux = Sec->Symbol->Aux[0];
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000810 assert(Aux.AuxType == ATSectionDefinition &&
811 "Section's symbol's aux symbol must be a Section Definition!");
812 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
813 Aux.Aux.SectionDefinition.NumberOfRelocations =
814 Sec->Header.NumberOfRelocations;
815 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
816 Sec->Header.NumberOfLineNumbers;
817 }
818
819 Header.PointerToSymbolTable = offset;
820
Michael J. Spencera6cfbeb2010-08-03 04:43:33 +0000821 Header.TimeDateStamp = sys::TimeValue::now().toEpochTime();
822
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000823 // Write it all to disk...
824 WriteFileHeader(Header);
825
826 {
827 sections::iterator i, ie;
828 MCAssembler::const_iterator j, je;
829
830 for (i = Sections.begin(), ie = Sections.end(); i != ie; i++)
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000831 if ((*i)->Number != -1) {
832 if ((*i)->Relocations.size() >= 0xffff) {
833 (*i)->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
834 }
Michael J. Spencerd6283772010-09-27 08:58:26 +0000835 WriteSectionHeader((*i)->Header);
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000836 }
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000837
838 for (i = Sections.begin(), ie = Sections.end(),
839 j = Asm.begin(), je = Asm.end();
Michael J. Spencerd6283772010-09-27 08:58:26 +0000840 (i != ie) && (j != je); ++i, ++j) {
841
842 if ((*i)->Number == -1)
843 continue;
844
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000845 if ((*i)->Header.PointerToRawData != 0) {
846 assert(OS.tell() == (*i)->Header.PointerToRawData &&
847 "Section::PointerToRawData is insane!");
848
Jim Grosbach18e2fe42011-12-06 00:03:48 +0000849 Asm.writeSectionData(j, Layout);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000850 }
851
852 if ((*i)->Relocations.size() > 0) {
853 assert(OS.tell() == (*i)->Header.PointerToRelocations &&
854 "Section::PointerToRelocations is insane!");
855
Michael J. Spencerfa39bd22012-03-15 09:03:03 +0000856 if ((*i)->Relocations.size() >= 0xffff) {
857 // In case of overflow, write actual relocation count as first
858 // relocation. Including the synthetic reloc itself (+ 1).
859 COFF::relocation r;
860 r.VirtualAddress = (*i)->Relocations.size() + 1;
861 r.SymbolTableIndex = 0;
862 r.Type = 0;
863 WriteRelocation(r);
864 }
865
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000866 for (relocations::const_iterator k = (*i)->Relocations.begin(),
867 ke = (*i)->Relocations.end();
868 k != ke; k++) {
869 WriteRelocation(k->Data);
870 }
871 } else
872 assert((*i)->Header.PointerToRelocations == 0 &&
873 "Section::PointerToRelocations is insane!");
874 }
875 }
876
877 assert(OS.tell() == Header.PointerToSymbolTable &&
878 "Header::PointerToSymbolTable is insane!");
879
880 for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++)
Michael J. Spencerd6283772010-09-27 08:58:26 +0000881 if ((*i)->Index != -1)
882 WriteSymbol(*i);
Michael J. Spencerb5fc1382010-07-26 02:17:32 +0000883
884 OS.write((char const *)&Strings.Data.front(), Strings.Data.size());
Chris Lattner2c52b792010-07-11 22:07:02 +0000885}
886
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000887MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_) :
888 Machine(Machine_) {
889}
890
Chris Lattner2c52b792010-07-11 22:07:02 +0000891//------------------------------------------------------------------------------
892// WinCOFFObjectWriter factory function
893
894namespace llvm {
Rafael Espindola908d2ed2011-12-24 02:14:02 +0000895 MCObjectWriter *createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
896 raw_ostream &OS) {
897 return new WinCOFFObjectWriter(MOTW, OS);
Chris Lattner2c52b792010-07-11 22:07:02 +0000898 }
Michael J. Spencerc2129372010-07-26 03:01:28 +0000899}