1 //===-- llvm/MC/WinCOFFObjectWriter.cpp -------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains an implementation of a Win32 COFF object file writer.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "WinCOFFObjectWriter"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSection.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCValue.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCAsmLayout.h"
24 #include "llvm/MC/MCSectionCOFF.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/StringMap.h"
28 #include "llvm/ADT/StringRef.h"
30 #include "llvm/Support/COFF.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/System/TimeValue.h"
36 #include "../Target/X86/X86FixupKinds.h"
43 typedef llvm::SmallString<COFF::NameSize> name;
54 AuxiliaryType AuxType;
62 typedef llvm::SmallVector<AuxSymbol, 1> AuxiliarySymbols;
69 MCSymbolData const *MCData;
71 COFFSymbol(llvm::StringRef name, size_t index);
73 void set_name_offset(uint32_t Offset);
76 // This class contains staging data for a COFF relocation entry.
77 struct COFFRelocation {
78 COFF::relocation Data;
81 COFFRelocation() : Symb(NULL) {}
82 static size_t size() { return COFF::RelocationSize; }
85 typedef std::vector<COFFRelocation> relocations;
93 MCSectionData const *MCData;
95 relocations Relocations;
97 COFFSection(llvm::StringRef name, size_t Index);
101 // This class holds the COFF string table.
103 typedef llvm::StringMap<size_t> map;
106 void update_length();
108 std::vector<char> Data;
112 size_t insert(llvm::StringRef String);
115 class WinCOFFObjectWriter : public MCObjectWriter {
118 typedef std::vector<COFFSymbol*> symbols;
119 typedef std::vector<COFFSection*> sections;
121 typedef StringMap<COFFSymbol *> name_symbol_map;
122 typedef StringMap<COFFSection *> name_section_map;
124 typedef DenseMap<MCSymbolData const *, COFFSymbol *> symbol_map;
125 typedef DenseMap<MCSectionData const *, COFFSection *> section_map;
127 // Root level file contents.
134 // Maps used during object file creation.
135 section_map SectionMap;
136 symbol_map SymbolMap;
138 WinCOFFObjectWriter(raw_ostream &OS, bool is64Bit);
139 ~WinCOFFObjectWriter();
141 COFFSymbol *createSymbol(llvm::StringRef Name);
142 COFFSection *createSection(llvm::StringRef Name);
144 void InitCOFFEntity(COFFSymbol &Symbol);
145 void InitCOFFEntity(COFFSection &Section);
147 template <typename object_t, typename list_t>
148 object_t *createCOFFEntity(llvm::StringRef Name, list_t &List);
150 void DefineSection(MCSectionData const &SectionData);
151 void DefineSymbol(MCSymbolData const &SymbolData, MCAssembler &Assembler);
153 bool ExportSection(COFFSection *S);
154 bool ExportSymbol(MCSymbolData const &SymbolData, MCAssembler &Asm);
156 // Entity writing methods.
158 void WriteFileHeader(const COFF::header &Header);
159 void WriteSymbol(const COFFSymbol *S);
160 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
161 void WriteSectionHeader(const COFF::section &S);
162 void WriteRelocation(const COFF::relocation &R);
164 // MCObjectWriter interface implementation.
166 void ExecutePostLayoutBinding(MCAssembler &Asm);
168 void RecordRelocation(const MCAssembler &Asm,
169 const MCAsmLayout &Layout,
170 const MCFragment *Fragment,
171 const MCFixup &Fixup,
173 uint64_t &FixedValue);
175 void WriteObject(const MCAssembler &Asm, const MCAsmLayout &Layout);
179 static inline void write_uint32_le(void *Data, uint32_t const &Value) {
180 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data);
181 Ptr[0] = (Value & 0x000000FF) >> 0;
182 Ptr[1] = (Value & 0x0000FF00) >> 8;
183 Ptr[2] = (Value & 0x00FF0000) >> 16;
184 Ptr[3] = (Value & 0xFF000000) >> 24;
187 static inline void write_uint16_le(void *Data, uint16_t const &Value) {
188 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data);
189 Ptr[0] = (Value & 0x00FF) >> 0;
190 Ptr[1] = (Value & 0xFF00) >> 8;
193 static inline void write_uint8_le(void *Data, uint8_t const &Value) {
194 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data);
195 Ptr[0] = (Value & 0xFF) >> 0;
198 //------------------------------------------------------------------------------
199 // Symbol class implementation
201 COFFSymbol::COFFSymbol(llvm::StringRef name, size_t index)
202 : Name(name.begin(), name.end()), Index(-1)
203 , Other(NULL), MCData(NULL) {
204 memset(&Data, 0, sizeof(Data));
207 size_t COFFSymbol::size() const {
208 return COFF::SymbolSize + (Data.NumberOfAuxSymbols * COFF::SymbolSize);
211 // In the case that the name does not fit within 8 bytes, the offset
212 // into the string table is stored in the last 4 bytes instead, leaving
213 // the first 4 bytes as 0.
214 void COFFSymbol::set_name_offset(uint32_t Offset) {
215 write_uint32_le(Data.Name + 0, 0);
216 write_uint32_le(Data.Name + 4, Offset);
219 //------------------------------------------------------------------------------
220 // Section class implementation
222 COFFSection::COFFSection(llvm::StringRef name, size_t Index)
223 : Name(name), Number(Index + 1)
224 , MCData(NULL), Symb(NULL) {
225 memset(&Header, 0, sizeof(Header));
228 size_t COFFSection::size() {
229 return COFF::SectionSize;
232 //------------------------------------------------------------------------------
233 // StringTable class implementation
235 /// Write the length of the string table into Data.
236 /// The length of the string table includes uint32 length header.
237 void StringTable::update_length() {
238 write_uint32_le(&Data.front(), Data.size());
241 StringTable::StringTable() {
242 // The string table data begins with the length of the entire string table
243 // including the length header. Allocate space for this header.
247 size_t StringTable::size() const {
251 /// Add String to the table iff it is not already there.
252 /// @returns the index into the string table where the string is now located.
253 size_t StringTable::insert(llvm::StringRef String) {
254 map::iterator i = Map.find(String);
259 size_t Offset = Data.size();
261 // Insert string data into string table.
262 Data.insert(Data.end(), String.begin(), String.end());
263 Data.push_back('\0');
265 // Put a reference to it in the map.
266 Map[String] = Offset;
268 // Update the internal length field.
274 //------------------------------------------------------------------------------
275 // WinCOFFObjectWriter class implementation
277 WinCOFFObjectWriter::WinCOFFObjectWriter(raw_ostream &OS, bool is64Bit)
278 : MCObjectWriter(OS, true)
280 memset(&Header, 0, sizeof(Header));
282 Is64Bit ? Header.Machine = COFF::IMAGE_FILE_MACHINE_AMD64
283 : Header.Machine = COFF::IMAGE_FILE_MACHINE_I386;
286 WinCOFFObjectWriter::~WinCOFFObjectWriter() {
287 for (symbols::iterator I = Symbols.begin(), E = Symbols.end(); I != E; ++I)
289 for (sections::iterator I = Sections.begin(), E = Sections.end(); I != E; ++I)
293 COFFSymbol *WinCOFFObjectWriter::createSymbol(llvm::StringRef Name) {
294 return createCOFFEntity<COFFSymbol>(Name, Symbols);
297 COFFSection *WinCOFFObjectWriter::createSection(llvm::StringRef Name) {
298 return createCOFFEntity<COFFSection>(Name, Sections);
301 /// This function initializes a symbol by entering its name into the string
302 /// table if it is too long to fit in the symbol table header.
303 void WinCOFFObjectWriter::InitCOFFEntity(COFFSymbol &S) {
304 if (S.Name.size() > COFF::NameSize) {
305 size_t StringTableEntry = Strings.insert(S.Name.c_str());
307 S.set_name_offset(StringTableEntry);
309 memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
312 /// This function initializes a section by entering its name into the string
313 /// table if it is too long to fit in the section table header.
314 void WinCOFFObjectWriter::InitCOFFEntity(COFFSection &S) {
315 if (S.Name.size() > COFF::NameSize) {
316 size_t StringTableEntry = Strings.insert(S.Name.c_str());
318 // FIXME: Why is this number 999999? This number is never mentioned in the
319 // spec. I'm assuming this is due to the printed value needing to fit into
320 // the S.Header.Name field. In which case why not 9999999 (7 9's instead of
321 // 6)? The spec does not state if this entry should be null terminated in
322 // this case, and thus this seems to be the best way to do it. I think I
323 // just solved my own FIXME...
324 if (StringTableEntry > 999999)
325 report_fatal_error("COFF string table is greater than 999999 bytes.");
327 sprintf(S.Header.Name, "/%d", (unsigned)StringTableEntry);
329 memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
332 /// A template used to lookup or create a symbol/section, and initialize it if
334 template <typename object_t, typename list_t>
335 object_t *WinCOFFObjectWriter::createCOFFEntity(llvm::StringRef Name,
337 object_t *Object = new object_t(Name, List.size());
339 InitCOFFEntity(*Object);
341 List.push_back(Object);
346 /// This function takes a section data object from the assembler
347 /// and creates the associated COFF section staging object.
348 void WinCOFFObjectWriter::DefineSection(MCSectionData const &SectionData) {
349 // FIXME: Not sure how to verify this (at least in a debug build).
350 MCSectionCOFF const &Sec =
351 static_cast<MCSectionCOFF const &>(SectionData.getSection());
353 COFFSection *coff_section = createSection(Sec.getSectionName());
354 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
356 coff_section->Symb = coff_symbol;
357 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
358 coff_symbol->Data.SectionNumber = coff_section->Number;
360 // In this case the auxiliary symbol is a Section Definition.
361 coff_symbol->Aux.resize(1);
362 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
363 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
364 coff_symbol->Aux[0].Aux.SectionDefinition.Number = coff_section->Number;
365 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
367 coff_section->Header.Characteristics = Sec.getCharacteristics();
369 uint32_t &Characteristics = coff_section->Header.Characteristics;
370 switch (SectionData.getAlignment()) {
371 case 1: Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES; break;
372 case 2: Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES; break;
373 case 4: Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES; break;
374 case 8: Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES; break;
375 case 16: Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES; break;
376 case 32: Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES; break;
377 case 64: Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES; break;
378 case 128: Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES; break;
379 case 256: Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES; break;
380 case 512: Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES; break;
381 case 1024: Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES; break;
382 case 2048: Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES; break;
383 case 4096: Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES; break;
384 case 8192: Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES; break;
386 llvm_unreachable("unsupported section alignment");
389 // Bind internal COFF section to MC section.
390 coff_section->MCData = &SectionData;
391 SectionMap[&SectionData] = coff_section;
394 /// This function takes a section data object from the assembler
395 /// and creates the associated COFF symbol staging object.
396 void WinCOFFObjectWriter::DefineSymbol(MCSymbolData const &SymbolData,
397 MCAssembler &Assembler) {
398 COFFSymbol *coff_symbol = createSymbol(SymbolData.getSymbol().getName());
400 coff_symbol->Data.Type = (SymbolData.getFlags() & 0x0000FFFF) >> 0;
401 coff_symbol->Data.StorageClass = (SymbolData.getFlags() & 0x00FF0000) >> 16;
403 // If no storage class was specified in the streamer, define it here.
404 if (coff_symbol->Data.StorageClass == 0) {
405 bool external = SymbolData.isExternal() || (SymbolData.Fragment == NULL);
407 coff_symbol->Data.StorageClass =
408 external ? COFF::IMAGE_SYM_CLASS_EXTERNAL : COFF::IMAGE_SYM_CLASS_STATIC;
411 if (SymbolData.getFlags() & COFF::SF_WeakReference) {
412 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
414 const MCExpr *Value = SymbolData.getSymbol().getVariableValue();
416 // FIXME: This assert message isn't very good.
417 assert(Value->getKind() == MCExpr::SymbolRef &&
418 "Value must be a SymbolRef!");
420 const MCSymbolRefExpr *SymbolRef =
421 static_cast<const MCSymbolRefExpr *>(Value);
423 const MCSymbolData &OtherSymbolData =
424 Assembler.getSymbolData(SymbolRef->getSymbol());
426 // FIXME: This assert message isn't very good.
427 assert(SymbolMap.find(&OtherSymbolData) != SymbolMap.end() &&
428 "OtherSymbolData must be in the symbol map!");
430 coff_symbol->Other = SymbolMap[&OtherSymbolData];
432 // Setup the Weak External auxiliary symbol.
433 coff_symbol->Aux.resize(1);
434 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
435 coff_symbol->Aux[0].AuxType = ATWeakExternal;
436 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
437 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
438 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
441 // Bind internal COFF symbol to MC symbol.
442 coff_symbol->MCData = &SymbolData;
443 SymbolMap[&SymbolData] = coff_symbol;
446 bool WinCOFFObjectWriter::ExportSection(COFFSection *S) {
447 return (S->Header.Characteristics
448 & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0;
451 bool WinCOFFObjectWriter::ExportSymbol(MCSymbolData const &SymbolData,
453 // This doesn't seem to be right. Strings referred to from the .data section
454 // need symbols so they can be linked to code in the .text section right?
456 // return Asm.isSymbolLinkerVisible (&SymbolData);
458 // For now, all symbols are exported, the linker will sort it out for us.
462 //------------------------------------------------------------------------------
463 // entity writing methods
465 void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
466 WriteLE16(Header.Machine);
467 WriteLE16(Header.NumberOfSections);
468 WriteLE32(Header.TimeDateStamp);
469 WriteLE32(Header.PointerToSymbolTable);
470 WriteLE32(Header.NumberOfSymbols);
471 WriteLE16(Header.SizeOfOptionalHeader);
472 WriteLE16(Header.Characteristics);
475 void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol *S) {
476 WriteBytes(StringRef(S->Data.Name, COFF::NameSize));
477 WriteLE32(S->Data.Value);
478 WriteLE16(S->Data.SectionNumber);
479 WriteLE16(S->Data.Type);
480 Write8(S->Data.StorageClass);
481 Write8(S->Data.NumberOfAuxSymbols);
482 WriteAuxiliarySymbols(S->Aux);
485 void WinCOFFObjectWriter::WriteAuxiliarySymbols(
486 const COFFSymbol::AuxiliarySymbols &S) {
487 for(COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
490 case ATFunctionDefinition:
491 WriteLE32(i->Aux.FunctionDefinition.TagIndex);
492 WriteLE32(i->Aux.FunctionDefinition.TotalSize);
493 WriteLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
494 WriteLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
495 WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
497 case ATbfAndefSymbol:
498 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
499 WriteLE16(i->Aux.bfAndefSymbol.Linenumber);
500 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
501 WriteLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
502 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
505 WriteLE32(i->Aux.WeakExternal.TagIndex);
506 WriteLE32(i->Aux.WeakExternal.Characteristics);
507 WriteZeros(sizeof(i->Aux.WeakExternal.unused));
510 WriteBytes(StringRef(reinterpret_cast<const char *>(i->Aux.File.FileName),
511 sizeof(i->Aux.File.FileName)));
513 case ATSectionDefinition:
514 WriteLE32(i->Aux.SectionDefinition.Length);
515 WriteLE16(i->Aux.SectionDefinition.NumberOfRelocations);
516 WriteLE16(i->Aux.SectionDefinition.NumberOfLinenumbers);
517 WriteLE32(i->Aux.SectionDefinition.CheckSum);
518 WriteLE16(i->Aux.SectionDefinition.Number);
519 Write8(i->Aux.SectionDefinition.Selection);
520 WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
526 void WinCOFFObjectWriter::WriteSectionHeader(const COFF::section &S) {
527 WriteBytes(StringRef(S.Name, COFF::NameSize));
529 WriteLE32(S.VirtualSize);
530 WriteLE32(S.VirtualAddress);
531 WriteLE32(S.SizeOfRawData);
532 WriteLE32(S.PointerToRawData);
533 WriteLE32(S.PointerToRelocations);
534 WriteLE32(S.PointerToLineNumbers);
535 WriteLE16(S.NumberOfRelocations);
536 WriteLE16(S.NumberOfLineNumbers);
537 WriteLE32(S.Characteristics);
540 void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
541 WriteLE32(R.VirtualAddress);
542 WriteLE32(R.SymbolTableIndex);
546 ////////////////////////////////////////////////////////////////////////////////
547 // MCObjectWriter interface implementations
549 void WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
550 // "Define" each section & symbol. This creates section & symbol
551 // entries in the staging area and gives them their final indexes.
553 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e; i++)
556 for (MCAssembler::const_symbol_iterator i = Asm.symbol_begin(),
557 e = Asm.symbol_end(); i != e; i++) {
558 if (ExportSymbol(*i, Asm))
559 DefineSymbol(*i, Asm);
563 void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm,
564 const MCAsmLayout &Layout,
565 const MCFragment *Fragment,
566 const MCFixup &Fixup,
568 uint64_t &FixedValue) {
569 assert(Target.getSymA() != NULL && "Relocation must reference a symbol!");
571 const MCSymbol *A = &Target.getSymA()->getSymbol();
572 MCSymbolData &A_SD = Asm.getSymbolData(*A);
574 MCSectionData const *SectionData = Fragment->getParent();
576 // Mark this symbol as requiring an entry in the symbol table.
577 assert(SectionMap.find(SectionData) != SectionMap.end() &&
578 "Section must already have been defined in ExecutePostLayoutBinding!");
579 assert(SymbolMap.find(&A_SD) != SymbolMap.end() &&
580 "Symbol must already have been defined in ExecutePostLayoutBinding!");
582 COFFSection *coff_section = SectionMap[SectionData];
583 COFFSymbol *coff_symbol = SymbolMap[&A_SD];
585 if (Target.getSymB()) {
586 const MCSymbol *B = &Target.getSymB()->getSymbol();
587 MCSymbolData &B_SD = Asm.getSymbolData(*B);
589 FixedValue = Layout.getSymbolAddress(&A_SD) - Layout.getSymbolAddress(&B_SD);
591 // In the case where we have SymbA and SymB, we just need to store the delta
592 // between the two symbols. Update FixedValue to account for the delta, and
593 // skip recording the relocation.
596 FixedValue = Target.getConstant();
599 COFFRelocation Reloc;
601 Reloc.Data.SymbolTableIndex = 0;
602 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
603 Reloc.Symb = coff_symbol;
605 Reloc.Data.VirtualAddress += Fixup.getOffset();
607 switch (Fixup.getKind()) {
608 case X86::reloc_pcrel_4byte:
609 case X86::reloc_riprel_4byte:
610 case X86::reloc_riprel_4byte_movq_load:
611 Reloc.Data.Type = Is64Bit ? COFF::IMAGE_REL_AMD64_REL32
612 : COFF::IMAGE_REL_I386_REL32;
613 // FIXME: Can anyone explain what this does other than adjust for the size
618 Reloc.Data.Type = Is64Bit ? COFF::IMAGE_REL_AMD64_ADDR32
619 : COFF::IMAGE_REL_I386_DIR32;
623 Reloc.Data.Type = COFF::IMAGE_REL_AMD64_ADDR64;
625 llvm_unreachable("unsupported relocation type");
628 llvm_unreachable("unsupported relocation type");
631 coff_section->Relocations.push_back(Reloc);
634 void WinCOFFObjectWriter::WriteObject(const MCAssembler &Asm,
635 const MCAsmLayout &Layout) {
636 // Assign symbol and section indexes and offsets.
638 Header.NumberOfSymbols = 0;
640 for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++) {
641 COFFSymbol *coff_symbol = *i;
642 MCSymbolData const *SymbolData = coff_symbol->MCData;
644 coff_symbol->Index = Header.NumberOfSymbols++;
646 // Update section number & offset for symbols that have them.
647 if ((SymbolData != NULL) && (SymbolData->Fragment != NULL)) {
648 COFFSection *coff_section = SectionMap[SymbolData->Fragment->getParent()];
650 coff_symbol->Data.SectionNumber = coff_section->Number;
651 coff_symbol->Data.Value = Layout.getFragmentOffset(SymbolData->Fragment)
652 + SymbolData->Offset;
655 // Update auxiliary symbol info.
656 coff_symbol->Data.NumberOfAuxSymbols = coff_symbol->Aux.size();
657 Header.NumberOfSymbols += coff_symbol->Data.NumberOfAuxSymbols;
660 // Fixup weak external references.
661 for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++) {
662 COFFSymbol *symb = *i;
664 if (symb->Other != NULL) {
665 assert(symb->Aux.size() == 1 &&
666 "Symbol must contain one aux symbol!");
667 assert(symb->Aux[0].AuxType == ATWeakExternal &&
668 "Symbol's aux symbol must be a Weak External!");
669 symb->Aux[0].Aux.WeakExternal.TagIndex = symb->Other->Index;
673 // Assign file offsets to COFF object file structures.
677 offset += COFF::HeaderSize;
678 offset += COFF::SectionSize * Asm.size();
680 Header.NumberOfSections = Sections.size();
682 for (MCAssembler::const_iterator i = Asm.begin(),
685 COFFSection *Sec = SectionMap[i];
687 Sec->Header.SizeOfRawData = Layout.getSectionFileSize(i);
689 if (ExportSection(Sec)) {
690 Sec->Header.PointerToRawData = offset;
692 offset += Sec->Header.SizeOfRawData;
695 if (Sec->Relocations.size() > 0) {
696 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
697 Sec->Header.PointerToRelocations = offset;
699 offset += COFF::RelocationSize * Sec->Relocations.size();
701 for (relocations::iterator cr = Sec->Relocations.begin(),
702 er = Sec->Relocations.end();
704 (*cr).Data.SymbolTableIndex = (*cr).Symb->Index;
708 assert(Sec->Symb->Aux.size() == 1 && "Section's symbol must have one aux!");
709 AuxSymbol &Aux = Sec->Symb->Aux[0];
710 assert(Aux.AuxType == ATSectionDefinition &&
711 "Section's symbol's aux symbol must be a Section Definition!");
712 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
713 Aux.Aux.SectionDefinition.NumberOfRelocations =
714 Sec->Header.NumberOfRelocations;
715 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
716 Sec->Header.NumberOfLineNumbers;
719 Header.PointerToSymbolTable = offset;
721 Header.TimeDateStamp = sys::TimeValue::now().toEpochTime();
723 // Write it all to disk...
724 WriteFileHeader(Header);
727 sections::iterator i, ie;
728 MCAssembler::const_iterator j, je;
730 for (i = Sections.begin(), ie = Sections.end(); i != ie; i++)
731 WriteSectionHeader((*i)->Header);
733 for (i = Sections.begin(), ie = Sections.end(),
734 j = Asm.begin(), je = Asm.end();
735 (i != ie) && (j != je); i++, j++) {
736 if ((*i)->Header.PointerToRawData != 0) {
737 assert(OS.tell() == (*i)->Header.PointerToRawData &&
738 "Section::PointerToRawData is insane!");
740 Asm.WriteSectionData(j, Layout, this);
743 if ((*i)->Relocations.size() > 0) {
744 assert(OS.tell() == (*i)->Header.PointerToRelocations &&
745 "Section::PointerToRelocations is insane!");
747 for (relocations::const_iterator k = (*i)->Relocations.begin(),
748 ke = (*i)->Relocations.end();
750 WriteRelocation(k->Data);
753 assert((*i)->Header.PointerToRelocations == 0 &&
754 "Section::PointerToRelocations is insane!");
758 assert(OS.tell() == Header.PointerToSymbolTable &&
759 "Header::PointerToSymbolTable is insane!");
761 for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++)
764 OS.write((char const *)&Strings.Data.front(), Strings.Data.size());
767 //------------------------------------------------------------------------------
768 // WinCOFFObjectWriter factory function
771 MCObjectWriter *createWinCOFFObjectWriter(raw_ostream &OS, bool is64Bit) {
772 return new WinCOFFObjectWriter(OS, is64Bit);