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"
39 typedef llvm::SmallString<COFF::NameSize> name;
50 AuxiliaryType AuxType;
58 typedef llvm::SmallVector<AuxSymbol, 1> AuxiliarySymbols;
65 MCSymbolData const *MCData;
67 COFFSymbol(llvm::StringRef name, size_t index);
69 void set_name_offset(uint32_t Offset);
72 // This class contains staging data for a COFF relocation entry.
73 struct COFFRelocation {
74 COFF::relocation Data;
77 COFFRelocation() : Symb(NULL) {}
78 static size_t size() { return COFF::RelocationSize; }
81 typedef std::vector<COFFRelocation> relocations;
89 MCSectionData const *MCData;
91 relocations Relocations;
93 COFFSection(llvm::StringRef name, size_t Index);
97 // This class holds the COFF string table.
99 typedef llvm::StringMap<size_t> map;
102 void update_length();
104 std::vector<char> Data;
108 size_t insert(llvm::StringRef String);
111 class WinCOFFObjectWriter : public MCObjectWriter {
114 typedef std::vector<COFFSymbol*> symbols;
115 typedef std::vector<COFFSection*> sections;
117 typedef StringMap<COFFSymbol *> name_symbol_map;
118 typedef StringMap<COFFSection *> name_section_map;
120 typedef DenseMap<MCSymbolData const *, COFFSymbol *> symbol_map;
121 typedef DenseMap<MCSectionData const *, COFFSection *> section_map;
123 // Root level file contents.
129 // Maps used during object file creation.
130 section_map SectionMap;
131 symbol_map SymbolMap;
133 WinCOFFObjectWriter(raw_ostream &OS);
134 ~WinCOFFObjectWriter();
136 COFFSymbol *createSymbol(llvm::StringRef Name);
137 COFFSection *createSection(llvm::StringRef Name);
139 void InitCOFFEntity(COFFSymbol &Symbol);
140 void InitCOFFEntity(COFFSection &Section);
142 template <typename object_t, typename list_t>
143 object_t *createCOFFEntity(llvm::StringRef Name, list_t &List);
145 void DefineSection(MCSectionData const &SectionData);
146 void DefineSymbol(MCSymbolData const &SymbolData, MCAssembler &Assembler);
148 bool ExportSection(COFFSection *S);
149 bool ExportSymbol(MCSymbolData const &SymbolData, MCAssembler &Asm);
151 // Entity writing methods.
153 void WriteFileHeader(const COFF::header &Header);
154 void WriteSymbol(const COFFSymbol *S);
155 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
156 void WriteSectionHeader(const COFF::section &S);
157 void WriteRelocation(const COFF::relocation &R);
159 // MCObjectWriter interface implementation.
161 void ExecutePostLayoutBinding(MCAssembler &Asm);
163 void RecordRelocation(const MCAssembler &Asm,
164 const MCAsmLayout &Layout,
165 const MCFragment *Fragment,
166 const MCFixup &Fixup,
168 uint64_t &FixedValue);
170 void WriteObject(const MCAssembler &Asm, const MCAsmLayout &Layout);
174 static inline void write_uint32_le(void *Data, uint32_t const &Value) {
175 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data);
176 Ptr[0] = (Value & 0x000000FF) >> 0;
177 Ptr[1] = (Value & 0x0000FF00) >> 8;
178 Ptr[2] = (Value & 0x00FF0000) >> 16;
179 Ptr[3] = (Value & 0xFF000000) >> 24;
182 static inline void write_uint16_le(void *Data, uint16_t const &Value) {
183 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data);
184 Ptr[0] = (Value & 0x00FF) >> 0;
185 Ptr[1] = (Value & 0xFF00) >> 8;
188 static inline void write_uint8_le(void *Data, uint8_t const &Value) {
189 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data);
190 Ptr[0] = (Value & 0xFF) >> 0;
193 //------------------------------------------------------------------------------
194 // Symbol class implementation
196 COFFSymbol::COFFSymbol(llvm::StringRef name, size_t index)
197 : Name(name.begin(), name.end()), Index(-1)
198 , Other(NULL), MCData(NULL) {
199 memset(&Data, 0, sizeof(Data));
202 size_t COFFSymbol::size() const {
203 return COFF::SymbolSize + (Data.NumberOfAuxSymbols * COFF::SymbolSize);
206 // In the case that the name does not fit within 8 bytes, the offset
207 // into the string table is stored in the last 4 bytes instead, leaving
208 // the first 4 bytes as 0.
209 void COFFSymbol::set_name_offset(uint32_t Offset) {
210 write_uint32_le(Data.Name + 0, 0);
211 write_uint32_le(Data.Name + 4, Offset);
214 //------------------------------------------------------------------------------
215 // Section class implementation
217 COFFSection::COFFSection(llvm::StringRef name, size_t Index)
218 : Name(name), Number(Index + 1)
219 , MCData(NULL), Symb(NULL) {
220 memset(&Header, 0, sizeof(Header));
223 size_t COFFSection::size() {
224 return COFF::SectionSize;
227 //------------------------------------------------------------------------------
228 // StringTable class implementation
230 /// Write the length of the string table into Data.
231 /// The length of the string table includes uint32 length header.
232 void StringTable::update_length() {
233 write_uint32_le(&Data.front(), Data.size());
236 StringTable::StringTable() {
237 // The string table data begins with the length of the entire string table
238 // including the length header. Allocate space for this header.
242 size_t StringTable::size() const {
246 /// Add String to the table iff it is not already there.
247 /// @returns the index into the string table where the string is now located.
248 size_t StringTable::insert(llvm::StringRef String) {
249 map::iterator i = Map.find(String);
254 size_t Offset = Data.size();
256 // Insert string data into string table.
257 Data.insert(Data.end(), String.begin(), String.end());
258 Data.push_back('\0');
260 // Put a reference to it in the map.
261 Map[String] = Offset;
263 // Update the internal length field.
269 //------------------------------------------------------------------------------
270 // WinCOFFObjectWriter class implementation
272 WinCOFFObjectWriter::WinCOFFObjectWriter(raw_ostream &OS)
273 : MCObjectWriter(OS, true) {
274 memset(&Header, 0, sizeof(Header));
275 // TODO: Move magic constant out to COFF.h
276 Header.Machine = 0x14C; // x86
279 WinCOFFObjectWriter::~WinCOFFObjectWriter() {
280 for (symbols::iterator I = Symbols.begin(), E = Symbols.end(); I != E; ++I)
282 for (sections::iterator I = Sections.begin(), E = Sections.end(); I != E; ++I)
286 COFFSymbol *WinCOFFObjectWriter::createSymbol(llvm::StringRef Name) {
287 return createCOFFEntity<COFFSymbol>(Name, Symbols);
290 COFFSection *WinCOFFObjectWriter::createSection(llvm::StringRef Name) {
291 return createCOFFEntity<COFFSection>(Name, Sections);
294 /// This function initializes a symbol by entering its name into the string
295 /// table if it is too long to fit in the symbol table header.
296 void WinCOFFObjectWriter::InitCOFFEntity(COFFSymbol &S) {
297 if (S.Name.size() > COFF::NameSize) {
298 size_t StringTableEntry = Strings.insert(S.Name.c_str());
300 S.set_name_offset(StringTableEntry);
302 memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
305 /// This function initializes a section by entering its name into the string
306 /// table if it is too long to fit in the section table header.
307 void WinCOFFObjectWriter::InitCOFFEntity(COFFSection &S) {
308 if (S.Name.size() > COFF::NameSize) {
309 size_t StringTableEntry = Strings.insert(S.Name.c_str());
311 // FIXME: Why is this number 999999? This number is never mentioned in the
312 // spec. I'm assuming this is due to the printed value needing to fit into
313 // the S.Header.Name field. In which case why not 9999999 (7 9's instead of
314 // 6)? The spec does not state if this entry should be null terminated in
315 // this case, and thus this seems to be the best way to do it. I think I
316 // just solved my own FIXME...
317 if (StringTableEntry > 999999)
318 report_fatal_error("COFF string table is greater than 999999 bytes.");
320 sprintf(S.Header.Name, "/%d", (unsigned)StringTableEntry);
322 memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
325 /// A template used to lookup or create a symbol/section, and initialize it if
327 template <typename object_t, typename list_t>
328 object_t *WinCOFFObjectWriter::createCOFFEntity(llvm::StringRef Name,
330 object_t *Object = new object_t(Name, List.size());
332 InitCOFFEntity(*Object);
334 List.push_back(Object);
339 /// This function takes a section data object from the assembler
340 /// and creates the associated COFF section staging object.
341 void WinCOFFObjectWriter::DefineSection(MCSectionData const &SectionData) {
342 // FIXME: Not sure how to verify this (at least in a debug build).
343 MCSectionCOFF const &Sec =
344 static_cast<MCSectionCOFF const &>(SectionData.getSection());
346 COFFSection *coff_section = createSection(Sec.getSectionName());
347 COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
349 coff_section->Symb = coff_symbol;
350 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
351 coff_symbol->Data.SectionNumber = coff_section->Number;
353 // In this case the auxiliary symbol is a Section Definition.
354 coff_symbol->Aux.resize(1);
355 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
356 coff_symbol->Aux[0].AuxType = ATSectionDefinition;
357 coff_symbol->Aux[0].Aux.SectionDefinition.Number = coff_section->Number;
358 coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
360 coff_section->Header.Characteristics = Sec.getCharacteristics();
362 uint32_t &Characteristics = coff_section->Header.Characteristics;
363 switch (SectionData.getAlignment()) {
364 case 1: Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES; break;
365 case 2: Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES; break;
366 case 4: Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES; break;
367 case 8: Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES; break;
368 case 16: Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES; break;
369 case 32: Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES; break;
370 case 64: Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES; break;
371 case 128: Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES; break;
372 case 256: Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES; break;
373 case 512: Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES; break;
374 case 1024: Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES; break;
375 case 2048: Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES; break;
376 case 4096: Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES; break;
377 case 8192: Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES; break;
379 llvm_unreachable("unsupported section alignment");
382 // Bind internal COFF section to MC section.
383 coff_section->MCData = &SectionData;
384 SectionMap[&SectionData] = coff_section;
387 /// This function takes a section data object from the assembler
388 /// and creates the associated COFF symbol staging object.
389 void WinCOFFObjectWriter::DefineSymbol(MCSymbolData const &SymbolData,
390 MCAssembler &Assembler) {
391 COFFSymbol *coff_symbol = createSymbol(SymbolData.getSymbol().getName());
393 coff_symbol->Data.Type = (SymbolData.getFlags() & 0x0000FFFF) >> 0;
394 coff_symbol->Data.StorageClass = (SymbolData.getFlags() & 0x00FF0000) >> 16;
396 // If no storage class was specified in the streamer, define it here.
397 if (coff_symbol->Data.StorageClass == 0) {
398 bool external = SymbolData.isExternal() || (SymbolData.Fragment == NULL);
400 coff_symbol->Data.StorageClass =
401 external ? COFF::IMAGE_SYM_CLASS_EXTERNAL : COFF::IMAGE_SYM_CLASS_STATIC;
404 if (SymbolData.getFlags() & COFF::SF_WeakReference) {
405 coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
407 const MCExpr *Value = SymbolData.getSymbol().getVariableValue();
409 // FIXME: This assert message isn't very good.
410 assert(Value->getKind() == MCExpr::SymbolRef &&
411 "Value must be a SymbolRef!");
413 const MCSymbolRefExpr *SymbolRef =
414 static_cast<const MCSymbolRefExpr *>(Value);
416 const MCSymbolData &OtherSymbolData =
417 Assembler.getSymbolData(SymbolRef->getSymbol());
419 // FIXME: This assert message isn't very good.
420 assert(SymbolMap.find(&OtherSymbolData) != SymbolMap.end() &&
421 "OtherSymbolData must be in the symbol map!");
423 coff_symbol->Other = SymbolMap[&OtherSymbolData];
425 // Setup the Weak External auxiliary symbol.
426 coff_symbol->Aux.resize(1);
427 memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
428 coff_symbol->Aux[0].AuxType = ATWeakExternal;
429 coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
430 coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
431 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
434 // Bind internal COFF symbol to MC symbol.
435 coff_symbol->MCData = &SymbolData;
436 SymbolMap[&SymbolData] = coff_symbol;
439 bool WinCOFFObjectWriter::ExportSection(COFFSection *S) {
440 return (S->Header.Characteristics
441 & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0;
444 bool WinCOFFObjectWriter::ExportSymbol(MCSymbolData const &SymbolData,
446 // This doesn't seem to be right. Strings referred to from the .data section
447 // need symbols so they can be linked to code in the .text section right?
449 // return Asm.isSymbolLinkerVisible (&SymbolData);
451 // For now, all symbols are exported, the linker will sort it out for us.
455 //------------------------------------------------------------------------------
456 // entity writing methods
458 void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
459 WriteLE16(Header.Machine);
460 WriteLE16(Header.NumberOfSections);
461 WriteLE32(Header.TimeDateStamp);
462 WriteLE32(Header.PointerToSymbolTable);
463 WriteLE32(Header.NumberOfSymbols);
464 WriteLE16(Header.SizeOfOptionalHeader);
465 WriteLE16(Header.Characteristics);
468 void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol *S) {
469 WriteBytes(StringRef(S->Data.Name, COFF::NameSize));
470 WriteLE32(S->Data.Value);
471 WriteLE16(S->Data.SectionNumber);
472 WriteLE16(S->Data.Type);
473 Write8(S->Data.StorageClass);
474 Write8(S->Data.NumberOfAuxSymbols);
475 WriteAuxiliarySymbols(S->Aux);
478 void WinCOFFObjectWriter::WriteAuxiliarySymbols(
479 const COFFSymbol::AuxiliarySymbols &S) {
480 for(COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
483 case ATFunctionDefinition:
484 WriteLE32(i->Aux.FunctionDefinition.TagIndex);
485 WriteLE32(i->Aux.FunctionDefinition.TotalSize);
486 WriteLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
487 WriteLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
488 WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
490 case ATbfAndefSymbol:
491 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
492 WriteLE16(i->Aux.bfAndefSymbol.Linenumber);
493 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
494 WriteLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
495 WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
498 WriteLE32(i->Aux.WeakExternal.TagIndex);
499 WriteLE32(i->Aux.WeakExternal.Characteristics);
500 WriteZeros(sizeof(i->Aux.WeakExternal.unused));
503 WriteBytes(StringRef(reinterpret_cast<const char *>(i->Aux.File.FileName),
504 sizeof(i->Aux.File.FileName)));
506 case ATSectionDefinition:
507 WriteLE32(i->Aux.SectionDefinition.Length);
508 WriteLE16(i->Aux.SectionDefinition.NumberOfRelocations);
509 WriteLE16(i->Aux.SectionDefinition.NumberOfLinenumbers);
510 WriteLE32(i->Aux.SectionDefinition.CheckSum);
511 WriteLE16(i->Aux.SectionDefinition.Number);
512 Write8(i->Aux.SectionDefinition.Selection);
513 WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
519 void WinCOFFObjectWriter::WriteSectionHeader(const COFF::section &S) {
520 WriteBytes(StringRef(S.Name, COFF::NameSize));
522 WriteLE32(S.VirtualSize);
523 WriteLE32(S.VirtualAddress);
524 WriteLE32(S.SizeOfRawData);
525 WriteLE32(S.PointerToRawData);
526 WriteLE32(S.PointerToRelocations);
527 WriteLE32(S.PointerToLineNumbers);
528 WriteLE16(S.NumberOfRelocations);
529 WriteLE16(S.NumberOfLineNumbers);
530 WriteLE32(S.Characteristics);
533 void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
534 WriteLE32(R.VirtualAddress);
535 WriteLE32(R.SymbolTableIndex);
539 ////////////////////////////////////////////////////////////////////////////////
540 // MCObjectWriter interface implementations
542 void WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
543 // "Define" each section & symbol. This creates section & symbol
544 // entries in the staging area and gives them their final indexes.
546 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e; i++)
549 for (MCAssembler::const_symbol_iterator i = Asm.symbol_begin(),
550 e = Asm.symbol_end(); i != e; i++) {
551 if (ExportSymbol(*i, Asm))
552 DefineSymbol(*i, Asm);
556 void WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm,
557 const MCAsmLayout &Layout,
558 const MCFragment *Fragment,
559 const MCFixup &Fixup,
561 uint64_t &FixedValue) {
562 assert(Target.getSymA() != NULL && "Relocation must reference a symbol!");
563 assert(Target.getSymB() == NULL &&
564 "Relocation must reference only one symbol!");
566 MCSectionData const *SectionData = Fragment->getParent();
567 MCSymbolData const *SymbolData =
568 &Asm.getSymbolData(Target.getSymA()->getSymbol());
570 assert(SectionMap.find(SectionData) != SectionMap.end() &&
571 "Section must already have been defined in ExecutePostLayoutBinding!");
572 assert(SymbolMap.find(SymbolData) != SymbolMap.end() &&
573 "Symbol must already have been defined in ExecutePostLayoutBinding!");
575 COFFSection *coff_section = SectionMap[SectionData];
576 COFFSymbol *coff_symbol = SymbolMap[SymbolData];
578 FixedValue = Target.getConstant();
580 COFFRelocation Reloc;
582 Reloc.Data.SymbolTableIndex = 0;
583 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
584 Reloc.Symb = coff_symbol;
586 Reloc.Data.VirtualAddress += Fixup.getOffset();
588 switch (Fixup.getKind()) {
589 case FirstTargetFixupKind: // reloc_pcrel_4byte
590 Reloc.Data.Type = COFF::IMAGE_REL_I386_REL32;
594 Reloc.Data.Type = COFF::IMAGE_REL_I386_DIR32;
597 llvm_unreachable("unsupported relocation type");
600 coff_section->Relocations.push_back(Reloc);
603 void WinCOFFObjectWriter::WriteObject(const MCAssembler &Asm,
604 const MCAsmLayout &Layout) {
605 // Assign symbol and section indexes and offsets.
607 Header.NumberOfSymbols = 0;
609 for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++) {
610 COFFSymbol *coff_symbol = *i;
611 MCSymbolData const *SymbolData = coff_symbol->MCData;
613 coff_symbol->Index = Header.NumberOfSymbols++;
615 // Update section number & offset for symbols that have them.
616 if ((SymbolData != NULL) && (SymbolData->Fragment != NULL)) {
617 COFFSection *coff_section = SectionMap[SymbolData->Fragment->getParent()];
619 coff_symbol->Data.SectionNumber = coff_section->Number;
620 coff_symbol->Data.Value = Layout.getFragmentOffset(SymbolData->Fragment);
623 // Update auxiliary symbol info.
624 coff_symbol->Data.NumberOfAuxSymbols = coff_symbol->Aux.size();
625 Header.NumberOfSymbols += coff_symbol->Data.NumberOfAuxSymbols;
628 // Fixup weak external references.
629 for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++) {
630 COFFSymbol *symb = *i;
632 if (symb->Other != NULL) {
633 assert(symb->Aux.size() == 1 &&
634 "Symbol must contain one aux symbol!");
635 assert(symb->Aux[0].AuxType == ATWeakExternal &&
636 "Symbol's aux symbol must be a Weak External!");
637 symb->Aux[0].Aux.WeakExternal.TagIndex = symb->Other->Index;
641 // Assign file offsets to COFF object file structures.
645 offset += COFF::HeaderSize;
646 offset += COFF::SectionSize * Asm.size();
648 Header.NumberOfSections = Sections.size();
650 for (MCAssembler::const_iterator i = Asm.begin(),
653 COFFSection *Sec = SectionMap[i];
655 Sec->Header.SizeOfRawData = Layout.getSectionFileSize(i);
657 if (ExportSection(Sec)) {
658 Sec->Header.PointerToRawData = offset;
660 offset += Sec->Header.SizeOfRawData;
663 if (Sec->Relocations.size() > 0) {
664 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
665 Sec->Header.PointerToRelocations = offset;
667 offset += COFF::RelocationSize * Sec->Relocations.size();
669 for (relocations::iterator cr = Sec->Relocations.begin(),
670 er = Sec->Relocations.end();
672 (*cr).Data.SymbolTableIndex = (*cr).Symb->Index;
676 assert(Sec->Symb->Aux.size() == 1 && "Section's symbol must have one aux!");
677 AuxSymbol &Aux = Sec->Symb->Aux[0];
678 assert(Aux.AuxType == ATSectionDefinition &&
679 "Section's symbol's aux symbol must be a Section Definition!");
680 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
681 Aux.Aux.SectionDefinition.NumberOfRelocations =
682 Sec->Header.NumberOfRelocations;
683 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
684 Sec->Header.NumberOfLineNumbers;
687 Header.PointerToSymbolTable = offset;
689 // Write it all to disk...
690 WriteFileHeader(Header);
693 sections::iterator i, ie;
694 MCAssembler::const_iterator j, je;
696 for (i = Sections.begin(), ie = Sections.end(); i != ie; i++)
697 WriteSectionHeader((*i)->Header);
699 for (i = Sections.begin(), ie = Sections.end(),
700 j = Asm.begin(), je = Asm.end();
701 (i != ie) && (j != je); i++, j++) {
702 if ((*i)->Header.PointerToRawData != 0) {
703 assert(OS.tell() == (*i)->Header.PointerToRawData &&
704 "Section::PointerToRawData is insane!");
706 Asm.WriteSectionData(j, Layout, this);
709 if ((*i)->Relocations.size() > 0) {
710 assert(OS.tell() == (*i)->Header.PointerToRelocations &&
711 "Section::PointerToRelocations is insane!");
713 for (relocations::const_iterator k = (*i)->Relocations.begin(),
714 ke = (*i)->Relocations.end();
716 WriteRelocation(k->Data);
719 assert((*i)->Header.PointerToRelocations == 0 &&
720 "Section::PointerToRelocations is insane!");
724 assert(OS.tell() == Header.PointerToSymbolTable &&
725 "Header::PointerToSymbolTable is insane!");
727 for (symbols::iterator i = Symbols.begin(), e = Symbols.end(); i != e; i++)
730 OS.write((char const *)&Strings.Data.front(), Strings.Data.size());
733 //------------------------------------------------------------------------------
734 // WinCOFFObjectWriter factory function
737 MCObjectWriter *createWinCOFFObjectWriter(raw_ostream &OS) {
738 return new WinCOFFObjectWriter(OS);