1 //===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
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 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/BinaryFormat/COFF.h"
21 #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/MCFixup.h"
26 #include "llvm/MC/MCFragment.h"
27 #include "llvm/MC/MCObjectWriter.h"
28 #include "llvm/MC/MCSection.h"
29 #include "llvm/MC/MCSectionCOFF.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MCSymbolCOFF.h"
32 #include "llvm/MC/MCValue.h"
33 #include "llvm/MC/MCWinCOFFObjectWriter.h"
34 #include "llvm/MC/StringTableBuilder.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/Endian.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/JamCRC.h"
39 #include "llvm/Support/MathExtras.h"
40 #include "llvm/Support/raw_ostream.h"
52 using llvm::support::endian::write32le;
54 #define DEBUG_TYPE "WinCOFFObjectWriter"
58 using name = SmallString<COFF::NameSize>;
69 AuxiliaryType AuxType;
77 COFF::symbol Data = {};
79 using AuxiliarySymbols = SmallVector<AuxSymbol, 1>;
84 COFFSymbol *Other = nullptr;
85 COFFSection *Section = nullptr;
87 const MCSymbol *MC = nullptr;
89 COFFSymbol(StringRef Name) : Name(Name) {}
91 void set_name_offset(uint32_t Offset);
93 int64_t getIndex() const { return Index; }
94 void setIndex(int Value) {
97 MC->setIndex(static_cast<uint32_t>(Value));
101 // This class contains staging data for a COFF relocation entry.
102 struct COFFRelocation {
103 COFF::relocation Data;
104 COFFSymbol *Symb = nullptr;
106 COFFRelocation() = default;
108 static size_t size() { return COFF::RelocationSize; }
111 using relocations = std::vector<COFFRelocation>;
115 COFF::section Header = {};
119 MCSectionCOFF const *MCSection = nullptr;
120 COFFSymbol *Symbol = nullptr;
121 relocations Relocations;
123 COFFSection(StringRef Name) : Name(Name) {}
126 class WinCOFFObjectWriter : public MCObjectWriter {
128 using symbols = std::vector<std::unique_ptr<COFFSymbol>>;
129 using sections = std::vector<std::unique_ptr<COFFSection>>;
131 using symbol_map = DenseMap<MCSymbol const *, COFFSymbol *>;
132 using section_map = DenseMap<MCSection const *, COFFSection *>;
134 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
136 // Root level file contents.
137 COFF::header Header = {};
140 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
142 // Maps used during object file creation.
143 section_map SectionMap;
144 symbol_map SymbolMap;
148 WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
150 void reset() override {
151 memset(&Header, 0, sizeof(Header));
152 Header.Machine = TargetObjectWriter->getMachine();
158 MCObjectWriter::reset();
161 COFFSymbol *createSymbol(StringRef Name);
162 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
163 COFFSection *createSection(StringRef Name);
165 void defineSection(MCSectionCOFF const &Sec);
167 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
168 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
169 const MCAsmLayout &Layout);
171 void SetSymbolName(COFFSymbol &S);
172 void SetSectionName(COFFSection &S);
174 bool IsPhysicalSection(COFFSection *S);
176 // Entity writing methods.
178 void WriteFileHeader(const COFF::header &Header);
179 void WriteSymbol(const COFFSymbol &S);
180 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
181 void writeSectionHeaders();
182 void WriteRelocation(const COFF::relocation &R);
183 uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout,
184 const MCSection &MCSec);
185 void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
186 const COFFSection &Sec, const MCSection &MCSec);
188 // MCObjectWriter interface implementation.
190 void executePostLayoutBinding(MCAssembler &Asm,
191 const MCAsmLayout &Layout) override;
193 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
194 const MCSymbol &SymA,
195 const MCFragment &FB, bool InSet,
196 bool IsPCRel) const override;
198 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
199 const MCFragment *Fragment, const MCFixup &Fixup,
200 MCValue Target, uint64_t &FixedValue) override;
202 void createFileSymbols(MCAssembler &Asm);
203 void assignSectionNumbers();
204 void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout);
206 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
209 } // end anonymous namespace
211 //------------------------------------------------------------------------------
212 // Symbol class implementation
214 // In the case that the name does not fit within 8 bytes, the offset
215 // into the string table is stored in the last 4 bytes instead, leaving
216 // the first 4 bytes as 0.
217 void COFFSymbol::set_name_offset(uint32_t Offset) {
218 write32le(Data.Name + 0, 0);
219 write32le(Data.Name + 4, Offset);
222 //------------------------------------------------------------------------------
223 // WinCOFFObjectWriter class implementation
225 WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
226 raw_pwrite_stream &OS)
227 : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
228 Header.Machine = TargetObjectWriter->getMachine();
231 COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
232 Symbols.push_back(make_unique<COFFSymbol>(Name));
233 return Symbols.back().get();
236 COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
237 COFFSymbol *&Ret = SymbolMap[Symbol];
239 Ret = createSymbol(Symbol->getName());
243 COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
244 Sections.emplace_back(make_unique<COFFSection>(Name));
245 return Sections.back().get();
248 static uint32_t getAlignment(const MCSectionCOFF &Sec) {
249 switch (Sec.getAlignment()) {
251 return COFF::IMAGE_SCN_ALIGN_1BYTES;
253 return COFF::IMAGE_SCN_ALIGN_2BYTES;
255 return COFF::IMAGE_SCN_ALIGN_4BYTES;
257 return COFF::IMAGE_SCN_ALIGN_8BYTES;
259 return COFF::IMAGE_SCN_ALIGN_16BYTES;
261 return COFF::IMAGE_SCN_ALIGN_32BYTES;
263 return COFF::IMAGE_SCN_ALIGN_64BYTES;
265 return COFF::IMAGE_SCN_ALIGN_128BYTES;
267 return COFF::IMAGE_SCN_ALIGN_256BYTES;
269 return COFF::IMAGE_SCN_ALIGN_512BYTES;
271 return COFF::IMAGE_SCN_ALIGN_1024BYTES;
273 return COFF::IMAGE_SCN_ALIGN_2048BYTES;
275 return COFF::IMAGE_SCN_ALIGN_4096BYTES;
277 return COFF::IMAGE_SCN_ALIGN_8192BYTES;
279 llvm_unreachable("unsupported section alignment");
282 /// This function takes a section data object from the assembler
283 /// and creates the associated COFF section staging object.
284 void WinCOFFObjectWriter::defineSection(const MCSectionCOFF &MCSec) {
285 COFFSection *Section = createSection(MCSec.getSectionName());
286 COFFSymbol *Symbol = createSymbol(MCSec.getSectionName());
287 Section->Symbol = Symbol;
288 Symbol->Section = Section;
289 Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
291 // Create a COMDAT symbol if needed.
292 if (MCSec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
293 if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
294 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
295 if (COMDATSymbol->Section)
296 report_fatal_error("two sections have the same comdat");
297 COMDATSymbol->Section = Section;
301 // In this case the auxiliary symbol is a Section Definition.
302 Symbol->Aux.resize(1);
304 Symbol->Aux[0].AuxType = ATSectionDefinition;
305 Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection();
307 // Set section alignment.
308 Section->Header.Characteristics = MCSec.getCharacteristics();
309 Section->Header.Characteristics |= getAlignment(MCSec);
311 // Bind internal COFF section to MC section.
312 Section->MCSection = &MCSec;
313 SectionMap[&MCSec] = Section;
316 static uint64_t getSymbolValue(const MCSymbol &Symbol,
317 const MCAsmLayout &Layout) {
318 if (Symbol.isCommon() && Symbol.isExternal())
319 return Symbol.getCommonSize();
322 if (!Layout.getSymbolOffset(Symbol, Res))
328 COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
329 if (!Symbol.isVariable())
332 const MCSymbolRefExpr *SymRef =
333 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
337 const MCSymbol &Aliasee = SymRef->getSymbol();
338 if (!Aliasee.isUndefined())
340 return GetOrCreateCOFFSymbol(&Aliasee);
343 /// This function takes a symbol data object from the assembler
344 /// and creates the associated COFF symbol staging object.
345 void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
346 MCAssembler &Assembler,
347 const MCAsmLayout &Layout) {
348 COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
349 const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
350 COFFSection *Sec = nullptr;
351 if (Base && Base->getFragment()) {
352 Sec = SectionMap[Base->getFragment()->getParent()];
353 if (Sym->Section && Sym->Section != Sec)
354 report_fatal_error("conflicting sections for symbol");
357 COFFSymbol *Local = nullptr;
358 if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
359 Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
361 COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
363 std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
364 WeakDefault = createSymbol(WeakName);
366 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
368 WeakDefault->Section = Sec;
372 Sym->Other = WeakDefault;
374 // Setup the Weak External auxiliary symbol.
376 memset(&Sym->Aux[0], 0, sizeof(Sym->Aux[0]));
377 Sym->Aux[0].AuxType = ATWeakExternal;
378 Sym->Aux[0].Aux.WeakExternal.TagIndex = 0;
379 Sym->Aux[0].Aux.WeakExternal.Characteristics =
380 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
383 Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
390 Local->Data.Value = getSymbolValue(MCSym, Layout);
392 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
393 Local->Data.Type = SymbolCOFF.getType();
394 Local->Data.StorageClass = SymbolCOFF.getClass();
396 // If no storage class was specified in the streamer, define it here.
397 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
398 bool IsExternal = MCSym.isExternal() ||
399 (!MCSym.getFragment() && !MCSym.isVariable());
401 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
402 : COFF::IMAGE_SYM_CLASS_STATIC;
409 // Maximum offsets for different string table entry encodings.
410 enum : unsigned { Max7DecimalOffset = 9999999U };
411 enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
413 // Encode a string table entry offset in base 64, padded to 6 chars, and
414 // prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
415 // Buffer must be at least 8 bytes large. No terminating null appended.
416 static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
417 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
418 "Illegal section name encoding for value");
420 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
421 "abcdefghijklmnopqrstuvwxyz"
427 char *Ptr = Buffer + 7;
428 for (unsigned i = 0; i < 6; ++i) {
429 unsigned Rem = Value % 64;
431 *(Ptr--) = Alphabet[Rem];
435 void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
436 if (S.Name.size() <= COFF::NameSize) {
437 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
441 uint64_t StringTableEntry = Strings.getOffset(S.Name);
442 if (StringTableEntry <= Max7DecimalOffset) {
443 SmallVector<char, COFF::NameSize> Buffer;
444 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
445 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
446 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
449 if (StringTableEntry <= MaxBase64Offset) {
450 // Starting with 10,000,000, offsets are encoded as base64.
451 encodeBase64StringEntry(S.Header.Name, StringTableEntry);
454 report_fatal_error("COFF string table is greater than 64 GB.");
457 void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
458 if (S.Name.size() > COFF::NameSize)
459 S.set_name_offset(Strings.getOffset(S.Name));
461 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
464 bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
465 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
469 //------------------------------------------------------------------------------
470 // entity writing methods
472 void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
474 writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
476 writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
477 writeLE16(Header.Machine);
478 writeLE32(Header.TimeDateStamp);
479 writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
484 writeLE32(Header.NumberOfSections);
485 writeLE32(Header.PointerToSymbolTable);
486 writeLE32(Header.NumberOfSymbols);
488 writeLE16(Header.Machine);
489 writeLE16(static_cast<int16_t>(Header.NumberOfSections));
490 writeLE32(Header.TimeDateStamp);
491 writeLE32(Header.PointerToSymbolTable);
492 writeLE32(Header.NumberOfSymbols);
493 writeLE16(Header.SizeOfOptionalHeader);
494 writeLE16(Header.Characteristics);
498 void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
499 writeBytes(StringRef(S.Data.Name, COFF::NameSize));
500 writeLE32(S.Data.Value);
502 writeLE32(S.Data.SectionNumber);
504 writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
505 writeLE16(S.Data.Type);
506 write8(S.Data.StorageClass);
507 write8(S.Data.NumberOfAuxSymbols);
508 WriteAuxiliarySymbols(S.Aux);
511 void WinCOFFObjectWriter::WriteAuxiliarySymbols(
512 const COFFSymbol::AuxiliarySymbols &S) {
513 for (const AuxSymbol &i : S) {
515 case ATFunctionDefinition:
516 writeLE32(i.Aux.FunctionDefinition.TagIndex);
517 writeLE32(i.Aux.FunctionDefinition.TotalSize);
518 writeLE32(i.Aux.FunctionDefinition.PointerToLinenumber);
519 writeLE32(i.Aux.FunctionDefinition.PointerToNextFunction);
520 WriteZeros(sizeof(i.Aux.FunctionDefinition.unused));
522 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
524 case ATbfAndefSymbol:
525 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused1));
526 writeLE16(i.Aux.bfAndefSymbol.Linenumber);
527 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused2));
528 writeLE32(i.Aux.bfAndefSymbol.PointerToNextFunction);
529 WriteZeros(sizeof(i.Aux.bfAndefSymbol.unused3));
531 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
534 writeLE32(i.Aux.WeakExternal.TagIndex);
535 writeLE32(i.Aux.WeakExternal.Characteristics);
536 WriteZeros(sizeof(i.Aux.WeakExternal.unused));
538 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
542 StringRef(reinterpret_cast<const char *>(&i.Aux),
543 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
545 case ATSectionDefinition:
546 writeLE32(i.Aux.SectionDefinition.Length);
547 writeLE16(i.Aux.SectionDefinition.NumberOfRelocations);
548 writeLE16(i.Aux.SectionDefinition.NumberOfLinenumbers);
549 writeLE32(i.Aux.SectionDefinition.CheckSum);
550 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
551 write8(i.Aux.SectionDefinition.Selection);
552 WriteZeros(sizeof(i.Aux.SectionDefinition.unused));
553 writeLE16(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
555 WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
561 // Write the section header.
562 void WinCOFFObjectWriter::writeSectionHeaders() {
563 // Section numbers must be monotonically increasing in the section
564 // header, but our Sections array is not sorted by section number,
565 // so make a copy of Sections and sort it.
566 std::vector<COFFSection *> Arr;
567 for (auto &Section : Sections)
568 Arr.push_back(Section.get());
569 std::sort(Arr.begin(), Arr.end(),
570 [](const COFFSection *A, const COFFSection *B) {
571 return A->Number < B->Number;
574 for (auto &Section : Arr) {
575 if (Section->Number == -1)
578 COFF::section &S = Section->Header;
579 if (Section->Relocations.size() >= 0xffff)
580 S.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
581 writeBytes(StringRef(S.Name, COFF::NameSize));
582 writeLE32(S.VirtualSize);
583 writeLE32(S.VirtualAddress);
584 writeLE32(S.SizeOfRawData);
585 writeLE32(S.PointerToRawData);
586 writeLE32(S.PointerToRelocations);
587 writeLE32(S.PointerToLineNumbers);
588 writeLE16(S.NumberOfRelocations);
589 writeLE16(S.NumberOfLineNumbers);
590 writeLE32(S.Characteristics);
594 void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
595 writeLE32(R.VirtualAddress);
596 writeLE32(R.SymbolTableIndex);
600 // Write MCSec's contents. What this function does is essentially
601 // "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
602 // because it needs to compute a CRC.
603 uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler &Asm,
604 const MCAsmLayout &Layout,
605 const MCSection &MCSec) {
606 // Save the contents of the section to a temporary buffer, we need this
607 // to CRC the data before we dump it into the object file.
608 SmallVector<char, 128> Buf;
609 raw_svector_ostream VecOS(Buf);
610 raw_pwrite_stream &OldStream = getStream();
612 // Redirect the output stream to our buffer and fill our buffer with
615 Asm.writeSectionData(&MCSec, Layout);
617 // Reset the stream back to what it was before.
618 setStream(OldStream);
620 // Write the section contents to the object file.
623 // Calculate our CRC with an initial value of '0', this is not how
624 // JamCRC is specified but it aligns with the expected output.
625 JamCRC JC(/*Init=*/0);
630 void WinCOFFObjectWriter::writeSection(MCAssembler &Asm,
631 const MCAsmLayout &Layout,
632 const COFFSection &Sec,
633 const MCSection &MCSec) {
634 if (Sec.Number == -1)
637 // Write the section contents.
638 if (Sec.Header.PointerToRawData != 0) {
639 assert(getStream().tell() <= Sec.Header.PointerToRawData &&
640 "Section::PointerToRawData is insane!");
642 unsigned PaddingSize = Sec.Header.PointerToRawData - getStream().tell();
643 assert(PaddingSize < 4 &&
644 "Should only need at most three bytes of padding!");
645 WriteZeros(PaddingSize);
647 uint32_t CRC = writeSectionContents(Asm, Layout, MCSec);
649 // Update the section definition auxiliary symbol to record the CRC.
650 COFFSection *Sec = SectionMap[&MCSec];
651 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
652 assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
653 AuxSymbol &SecDef = AuxSyms[0];
654 SecDef.Aux.SectionDefinition.CheckSum = CRC;
657 // Write relocations for this section.
658 if (Sec.Relocations.empty()) {
659 assert(Sec.Header.PointerToRelocations == 0 &&
660 "Section::PointerToRelocations is insane!");
664 assert(getStream().tell() == Sec.Header.PointerToRelocations &&
665 "Section::PointerToRelocations is insane!");
667 if (Sec.Relocations.size() >= 0xffff) {
668 // In case of overflow, write actual relocation count as first
669 // relocation. Including the synthetic reloc itself (+ 1).
671 R.VirtualAddress = Sec.Relocations.size() + 1;
672 R.SymbolTableIndex = 0;
677 for (const auto &Relocation : Sec.Relocations)
678 WriteRelocation(Relocation.Data);
681 ////////////////////////////////////////////////////////////////////////////////
682 // MCObjectWriter interface implementations
684 void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
685 const MCAsmLayout &Layout) {
686 // "Define" each section & symbol. This creates section & symbol
687 // entries in the staging area.
688 for (const auto &Section : Asm)
689 defineSection(static_cast<const MCSectionCOFF &>(Section));
691 for (const MCSymbol &Symbol : Asm.symbols())
692 if (!Symbol.isTemporary())
693 DefineSymbol(Symbol, Asm, Layout);
696 bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
697 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
698 bool InSet, bool IsPCRel) const {
699 // MS LINK expects to be able to replace all references to a function with a
700 // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
701 // away any relocations to functions.
702 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
703 if (Asm.isIncrementalLinkerCompatible() &&
704 (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
706 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
710 void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
711 const MCAsmLayout &Layout,
712 const MCFragment *Fragment,
713 const MCFixup &Fixup, MCValue Target,
714 uint64_t &FixedValue) {
715 assert(Target.getSymA() && "Relocation must reference a symbol!");
717 const MCSymbol &A = Target.getSymA()->getSymbol();
718 if (!A.isRegistered()) {
719 Asm.getContext().reportError(Fixup.getLoc(),
720 Twine("symbol '") + A.getName() +
721 "' can not be undefined");
724 if (A.isTemporary() && A.isUndefined()) {
725 Asm.getContext().reportError(Fixup.getLoc(),
726 Twine("assembler label '") + A.getName() +
727 "' can not be undefined");
731 MCSection *MCSec = Fragment->getParent();
733 // Mark this symbol as requiring an entry in the symbol table.
734 assert(SectionMap.find(MCSec) != SectionMap.end() &&
735 "Section must already have been defined in executePostLayoutBinding!");
737 COFFSection *Sec = SectionMap[MCSec];
738 const MCSymbolRefExpr *SymB = Target.getSymB();
741 const MCSymbol *B = &SymB->getSymbol();
742 if (!B->getFragment()) {
743 Asm.getContext().reportError(
745 Twine("symbol '") + B->getName() +
746 "' can not be undefined in a subtraction expression");
750 // Offset of the symbol in the section
751 int64_t OffsetOfB = Layout.getSymbolOffset(*B);
753 // Offset of the relocation in the section
754 int64_t OffsetOfRelocation =
755 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
757 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
759 FixedValue = Target.getConstant();
762 COFFRelocation Reloc;
764 Reloc.Data.SymbolTableIndex = 0;
765 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
767 // Turn relocations for temporary symbols into section relocations.
768 if (A.isTemporary()) {
769 MCSection *TargetSection = &A.getSection();
771 SectionMap.find(TargetSection) != SectionMap.end() &&
772 "Section must already have been defined in executePostLayoutBinding!");
773 Reloc.Symb = SectionMap[TargetSection]->Symbol;
774 FixedValue += Layout.getSymbolOffset(A);
777 SymbolMap.find(&A) != SymbolMap.end() &&
778 "Symbol must already have been defined in executePostLayoutBinding!");
779 Reloc.Symb = SymbolMap[&A];
782 ++Reloc.Symb->Relocations;
784 Reloc.Data.VirtualAddress += Fixup.getOffset();
785 Reloc.Data.Type = TargetObjectWriter->getRelocType(
786 Asm.getContext(), Target, Fixup, SymB, Asm.getBackend());
788 // FIXME: Can anyone explain what this does other than adjust for the size
790 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
791 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
792 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
793 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
796 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
797 switch (Reloc.Data.Type) {
798 case COFF::IMAGE_REL_ARM_ABSOLUTE:
799 case COFF::IMAGE_REL_ARM_ADDR32:
800 case COFF::IMAGE_REL_ARM_ADDR32NB:
801 case COFF::IMAGE_REL_ARM_TOKEN:
802 case COFF::IMAGE_REL_ARM_SECTION:
803 case COFF::IMAGE_REL_ARM_SECREL:
805 case COFF::IMAGE_REL_ARM_BRANCH11:
806 case COFF::IMAGE_REL_ARM_BLX11:
807 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
808 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
810 case COFF::IMAGE_REL_ARM_BRANCH24:
811 case COFF::IMAGE_REL_ARM_BLX24:
812 case COFF::IMAGE_REL_ARM_MOV32A:
813 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
814 // only used for ARM mode code, which is documented as being unsupported
815 // by Windows on ARM. Empirical proof indicates that masm is able to
816 // generate the relocations however the rest of the MSVC toolchain is
817 // unable to handle it.
818 llvm_unreachable("unsupported relocation");
820 case COFF::IMAGE_REL_ARM_MOV32T:
822 case COFF::IMAGE_REL_ARM_BRANCH20T:
823 case COFF::IMAGE_REL_ARM_BRANCH24T:
824 case COFF::IMAGE_REL_ARM_BLX23T:
825 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
826 // perform a 4 byte adjustment to the relocation. Relative branches are
827 // offset by 4 on ARM, however, because there is no RELA relocations, all
828 // branches are offset by 4.
829 FixedValue = FixedValue + 4;
834 // The fixed value never makes sense for section indices, ignore it.
835 if (Fixup.getKind() == FK_SecRel_2)
838 if (TargetObjectWriter->recordRelocation(Fixup))
839 Sec->Relocations.push_back(Reloc);
842 static std::time_t getTime() {
843 std::time_t Now = time(nullptr);
844 if (Now < 0 || !isUInt<32>(Now))
849 // Create .file symbols.
850 void WinCOFFObjectWriter::createFileSymbols(MCAssembler &Asm) {
851 for (const std::string &Name : Asm.getFileNames()) {
852 // round up to calculate the number of auxiliary symbols required
853 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
854 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
856 COFFSymbol *File = createSymbol(".file");
857 File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
858 File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
859 File->Aux.resize(Count);
862 unsigned Length = Name.size();
863 for (auto &Aux : File->Aux) {
864 Aux.AuxType = ATFile;
866 if (Length > SymbolSize) {
867 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
868 Length = Length - SymbolSize;
870 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
871 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
875 Offset += SymbolSize;
880 static bool isAssociative(const COFFSection &Section) {
881 return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
882 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
885 void WinCOFFObjectWriter::assignSectionNumbers() {
887 auto Assign = [&](COFFSection &Section) {
889 Section.Symbol->Data.SectionNumber = I;
890 Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I;
894 // Although it is not explicitly requested by the Microsoft COFF spec,
895 // we should avoid emitting forward associative section references,
896 // because MSVC link.exe as of 2017 cannot handle that.
897 for (const std::unique_ptr<COFFSection> &Section : Sections)
898 if (!isAssociative(*Section))
900 for (const std::unique_ptr<COFFSection> &Section : Sections)
901 if (isAssociative(*Section))
905 // Assign file offsets to COFF object file structures.
906 void WinCOFFObjectWriter::assignFileOffsets(MCAssembler &Asm,
907 const MCAsmLayout &Layout) {
908 unsigned Offset = getInitialOffset();
910 Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
911 Offset += COFF::SectionSize * Header.NumberOfSections;
913 for (const auto &Section : Asm) {
914 COFFSection *Sec = SectionMap[&Section];
916 if (Sec->Number == -1)
919 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
921 if (IsPhysicalSection(Sec)) {
922 // Align the section data to a four byte boundary.
923 Offset = alignTo(Offset, 4);
924 Sec->Header.PointerToRawData = Offset;
926 Offset += Sec->Header.SizeOfRawData;
929 if (!Sec->Relocations.empty()) {
930 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
932 if (RelocationsOverflow) {
933 // Signal overflow by setting NumberOfRelocations to max value. Actual
934 // size is found in reloc #0. Microsoft tools understand this.
935 Sec->Header.NumberOfRelocations = 0xffff;
937 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
939 Sec->Header.PointerToRelocations = Offset;
941 if (RelocationsOverflow) {
942 // Reloc #0 will contain actual count, so make room for it.
943 Offset += COFF::RelocationSize;
946 Offset += COFF::RelocationSize * Sec->Relocations.size();
948 for (auto &Relocation : Sec->Relocations) {
949 assert(Relocation.Symb->getIndex() != -1);
950 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
954 assert(Sec->Symbol->Aux.size() == 1 &&
955 "Section's symbol must have one aux!");
956 AuxSymbol &Aux = Sec->Symbol->Aux[0];
957 assert(Aux.AuxType == ATSectionDefinition &&
958 "Section's symbol's aux symbol must be a Section Definition!");
959 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
960 Aux.Aux.SectionDefinition.NumberOfRelocations =
961 Sec->Header.NumberOfRelocations;
962 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
963 Sec->Header.NumberOfLineNumbers;
966 Header.PointerToSymbolTable = Offset;
969 void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
970 const MCAsmLayout &Layout) {
971 if (Sections.size() > INT32_MAX)
973 "PE COFF object files can't have more than 2147483647 sections");
975 UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
976 Header.NumberOfSections = Sections.size();
977 Header.NumberOfSymbols = 0;
979 assignSectionNumbers();
980 createFileSymbols(Asm);
982 for (auto &Symbol : Symbols) {
983 // Update section number & offset for symbols that have them.
985 Symbol->Data.SectionNumber = Symbol->Section->Number;
986 Symbol->setIndex(Header.NumberOfSymbols++);
987 // Update auxiliary symbol info.
988 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
989 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
992 // Build string table.
993 for (const auto &S : Sections)
994 if (S->Name.size() > COFF::NameSize)
995 Strings.add(S->Name);
996 for (const auto &S : Symbols)
997 if (S->Name.size() > COFF::NameSize)
998 Strings.add(S->Name);
1002 for (const auto &S : Sections)
1004 for (auto &S : Symbols)
1007 // Fixup weak external references.
1008 for (auto &Symbol : Symbols) {
1009 if (Symbol->Other) {
1010 assert(Symbol->getIndex() != -1);
1011 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
1012 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
1013 "Symbol's aux symbol must be a Weak External!");
1014 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
1018 // Fixup associative COMDAT sections.
1019 for (auto &Section : Sections) {
1020 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
1021 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1024 const MCSectionCOFF &MCSec = *Section->MCSection;
1026 const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
1028 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
1029 assert(COMDATSymbol);
1030 COFFSection *Assoc = COMDATSymbol->Section;
1033 Twine("Missing associated COMDAT section for section ") +
1034 MCSec.getSectionName());
1036 // Skip this section if the associated section is unused.
1037 if (Assoc->Number == -1)
1040 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
1043 assignFileOffsets(Asm, Layout);
1045 // MS LINK expects to be able to use this timestamp to implement their
1046 // /INCREMENTAL feature.
1047 if (Asm.isIncrementalLinkerCompatible()) {
1048 Header.TimeDateStamp = getTime();
1050 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
1051 Header.TimeDateStamp = 0;
1054 // Write it all to disk...
1055 WriteFileHeader(Header);
1056 writeSectionHeaders();
1058 // Write section contents.
1059 sections::iterator I = Sections.begin();
1060 sections::iterator IE = Sections.end();
1061 MCAssembler::iterator J = Asm.begin();
1062 MCAssembler::iterator JE = Asm.end();
1063 for (; I != IE && J != JE; ++I, ++J)
1064 writeSection(Asm, Layout, **I, *J);
1066 assert(getStream().tell() == Header.PointerToSymbolTable &&
1067 "Header::PointerToSymbolTable is insane!");
1069 // Write a symbol table.
1070 for (auto &Symbol : Symbols)
1071 if (Symbol->getIndex() != -1)
1072 WriteSymbol(*Symbol);
1074 // Write a string table, which completes the entire COFF file.
1075 Strings.write(getStream());
1078 MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1079 : Machine(Machine_) {}
1081 // Pin the vtable to this file.
1082 void MCWinCOFFObjectTargetWriter::anchor() {}
1084 //------------------------------------------------------------------------------
1085 // WinCOFFObjectWriter factory function
1088 llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
1089 raw_pwrite_stream &OS) {
1090 return new WinCOFFObjectWriter(MOTW, OS);