[AMDGPU] prevent shrinking udiv/urem if either operand is in (SignedMax,UnsignedMax...
[llvm-project.git] / llvm / lib / MC / WinCOFFObjectWriter.cpp
blob2d883e974a584a031976782d258dc4cd97459992
1 //===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains an implementation of a Win32 COFF object file writer.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/DenseSet.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/MCAssembler.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCFixup.h"
25 #include "llvm/MC/MCFragment.h"
26 #include "llvm/MC/MCObjectWriter.h"
27 #include "llvm/MC/MCSection.h"
28 #include "llvm/MC/MCSectionCOFF.h"
29 #include "llvm/MC/MCSymbol.h"
30 #include "llvm/MC/MCSymbolCOFF.h"
31 #include "llvm/MC/MCValue.h"
32 #include "llvm/MC/MCWinCOFFObjectWriter.h"
33 #include "llvm/MC/StringTableBuilder.h"
34 #include "llvm/Support/CRC.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/EndianStream.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/LEB128.h"
39 #include "llvm/Support/MathExtras.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include <cassert>
42 #include <cstdint>
43 #include <cstring>
44 #include <ctime>
45 #include <memory>
46 #include <string>
47 #include <vector>
49 using namespace llvm;
50 using llvm::support::endian::write32le;
52 #define DEBUG_TYPE "WinCOFFObjectWriter"
54 namespace {
56 constexpr int OffsetLabelIntervalBits = 20;
58 using name = SmallString<COFF::NameSize>;
60 enum AuxiliaryType { ATWeakExternal, ATFile, ATSectionDefinition };
62 struct AuxSymbol {
63 AuxiliaryType AuxType;
64 COFF::Auxiliary Aux;
67 class COFFSection;
69 class COFFSymbol {
70 public:
71 COFF::symbol Data = {};
73 using AuxiliarySymbols = SmallVector<AuxSymbol, 1>;
75 name Name;
76 int Index = 0;
77 AuxiliarySymbols Aux;
78 COFFSymbol *Other = nullptr;
79 COFFSection *Section = nullptr;
80 int Relocations = 0;
81 const MCSymbol *MC = nullptr;
83 COFFSymbol(StringRef Name) : Name(Name) {}
85 void set_name_offset(uint32_t Offset);
87 int64_t getIndex() const { return Index; }
88 void setIndex(int Value) {
89 Index = Value;
90 if (MC)
91 MC->setIndex(static_cast<uint32_t>(Value));
95 // This class contains staging data for a COFF relocation entry.
96 struct COFFRelocation {
97 COFF::relocation Data;
98 COFFSymbol *Symb = nullptr;
100 COFFRelocation() = default;
102 static size_t size() { return COFF::RelocationSize; }
105 using relocations = std::vector<COFFRelocation>;
107 class COFFSection {
108 public:
109 COFF::section Header = {};
111 std::string Name;
112 int Number = 0;
113 MCSectionCOFF const *MCSection = nullptr;
114 COFFSymbol *Symbol = nullptr;
115 relocations Relocations;
117 COFFSection(StringRef Name) : Name(std::string(Name)) {}
119 SmallVector<COFFSymbol *, 1> OffsetSymbols;
121 } // namespace
123 class llvm::WinCOFFWriter {
124 WinCOFFObjectWriter &OWriter;
125 support::endian::Writer W;
127 using symbols = std::vector<std::unique_ptr<COFFSymbol>>;
128 using sections = std::vector<std::unique_ptr<COFFSection>>;
130 using symbol_map = DenseMap<MCSymbol const *, COFFSymbol *>;
131 using section_map = DenseMap<MCSection const *, COFFSection *>;
133 using symbol_list = DenseSet<COFFSymbol *>;
135 // Root level file contents.
136 COFF::header Header = {};
137 sections Sections;
138 symbols Symbols;
139 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
141 // Maps used during object file creation.
142 section_map SectionMap;
143 symbol_map SymbolMap;
145 symbol_list WeakDefaults;
147 bool UseBigObj;
148 bool UseOffsetLabels = false;
150 public:
151 enum DwoMode {
152 AllSections,
153 NonDwoOnly,
154 DwoOnly,
155 } Mode;
157 WinCOFFWriter(WinCOFFObjectWriter &OWriter, raw_pwrite_stream &OS,
158 DwoMode Mode);
160 void reset();
161 void executePostLayoutBinding(MCAssembler &Asm);
162 void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
163 const MCFixup &Fixup, MCValue Target,
164 uint64_t &FixedValue);
165 uint64_t writeObject(MCAssembler &Asm);
167 private:
168 COFFSymbol *createSymbol(StringRef Name);
169 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
170 COFFSection *createSection(StringRef Name);
172 void defineSection(const MCAssembler &Asm, MCSectionCOFF const &Sec);
174 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
175 void defineSymbol(const MCAssembler &Asm, const MCSymbol &Symbol);
177 void SetSymbolName(COFFSymbol &S);
178 void SetSectionName(COFFSection &S);
180 bool IsPhysicalSection(COFFSection *S);
182 // Entity writing methods.
183 void WriteFileHeader(const COFF::header &Header);
184 void WriteSymbol(const COFFSymbol &S);
185 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
186 void writeSectionHeaders();
187 void WriteRelocation(const COFF::relocation &R);
188 uint32_t writeSectionContents(MCAssembler &Asm, const MCSection &MCSec);
189 void writeSection(MCAssembler &Asm, const COFFSection &Sec);
191 void createFileSymbols(MCAssembler &Asm);
192 void setWeakDefaultNames();
193 void assignSectionNumbers();
194 void assignFileOffsets(MCAssembler &Asm);
197 WinCOFFObjectWriter::WinCOFFObjectWriter(
198 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS)
199 : TargetObjectWriter(std::move(MOTW)),
200 ObjWriter(std::make_unique<WinCOFFWriter>(*this, OS,
201 WinCOFFWriter::AllSections)) {}
202 WinCOFFObjectWriter::WinCOFFObjectWriter(
203 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS,
204 raw_pwrite_stream &DwoOS)
205 : TargetObjectWriter(std::move(MOTW)),
206 ObjWriter(std::make_unique<WinCOFFWriter>(*this, OS,
207 WinCOFFWriter::NonDwoOnly)),
208 DwoWriter(std::make_unique<WinCOFFWriter>(*this, DwoOS,
209 WinCOFFWriter::DwoOnly)) {}
211 static bool isDwoSection(const MCSection &Sec) {
212 return Sec.getName().ends_with(".dwo");
215 //------------------------------------------------------------------------------
216 // Symbol class implementation
218 // In the case that the name does not fit within 8 bytes, the offset
219 // into the string table is stored in the last 4 bytes instead, leaving
220 // the first 4 bytes as 0.
221 void COFFSymbol::set_name_offset(uint32_t Offset) {
222 write32le(Data.Name + 0, 0);
223 write32le(Data.Name + 4, Offset);
226 //------------------------------------------------------------------------------
227 // WinCOFFWriter class implementation
229 WinCOFFWriter::WinCOFFWriter(WinCOFFObjectWriter &OWriter,
230 raw_pwrite_stream &OS, DwoMode Mode)
231 : OWriter(OWriter), W(OS, llvm::endianness::little), Mode(Mode) {
232 Header.Machine = OWriter.TargetObjectWriter->getMachine();
233 // Some relocations on ARM64 (the 21 bit ADRP relocations) have a slightly
234 // limited range for the immediate offset (+/- 1 MB); create extra offset
235 // label symbols with regular intervals to allow referencing a
236 // non-temporary symbol that is close enough.
237 UseOffsetLabels = COFF::isAnyArm64(Header.Machine);
240 COFFSymbol *WinCOFFWriter::createSymbol(StringRef Name) {
241 Symbols.push_back(std::make_unique<COFFSymbol>(Name));
242 return Symbols.back().get();
245 COFFSymbol *WinCOFFWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
246 COFFSymbol *&Ret = SymbolMap[Symbol];
247 if (!Ret)
248 Ret = createSymbol(Symbol->getName());
249 return Ret;
252 COFFSection *WinCOFFWriter::createSection(StringRef Name) {
253 Sections.emplace_back(std::make_unique<COFFSection>(Name));
254 return Sections.back().get();
257 static uint32_t getAlignment(const MCSectionCOFF &Sec) {
258 switch (Sec.getAlign().value()) {
259 case 1:
260 return COFF::IMAGE_SCN_ALIGN_1BYTES;
261 case 2:
262 return COFF::IMAGE_SCN_ALIGN_2BYTES;
263 case 4:
264 return COFF::IMAGE_SCN_ALIGN_4BYTES;
265 case 8:
266 return COFF::IMAGE_SCN_ALIGN_8BYTES;
267 case 16:
268 return COFF::IMAGE_SCN_ALIGN_16BYTES;
269 case 32:
270 return COFF::IMAGE_SCN_ALIGN_32BYTES;
271 case 64:
272 return COFF::IMAGE_SCN_ALIGN_64BYTES;
273 case 128:
274 return COFF::IMAGE_SCN_ALIGN_128BYTES;
275 case 256:
276 return COFF::IMAGE_SCN_ALIGN_256BYTES;
277 case 512:
278 return COFF::IMAGE_SCN_ALIGN_512BYTES;
279 case 1024:
280 return COFF::IMAGE_SCN_ALIGN_1024BYTES;
281 case 2048:
282 return COFF::IMAGE_SCN_ALIGN_2048BYTES;
283 case 4096:
284 return COFF::IMAGE_SCN_ALIGN_4096BYTES;
285 case 8192:
286 return COFF::IMAGE_SCN_ALIGN_8192BYTES;
288 llvm_unreachable("unsupported section alignment");
291 /// This function takes a section data object from the assembler
292 /// and creates the associated COFF section staging object.
293 void WinCOFFWriter::defineSection(const MCAssembler &Asm,
294 const MCSectionCOFF &MCSec) {
295 COFFSection *Section = createSection(MCSec.getName());
296 COFFSymbol *Symbol = createSymbol(MCSec.getName());
297 Section->Symbol = Symbol;
298 SymbolMap[MCSec.getBeginSymbol()] = Symbol;
299 Symbol->Section = Section;
300 Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
302 // Create a COMDAT symbol if needed.
303 if (MCSec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
304 if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
305 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
306 if (COMDATSymbol->Section)
307 report_fatal_error("two sections have the same comdat");
308 COMDATSymbol->Section = Section;
312 // In this case the auxiliary symbol is a Section Definition.
313 Symbol->Aux.resize(1);
314 Symbol->Aux[0] = {};
315 Symbol->Aux[0].AuxType = ATSectionDefinition;
316 Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection();
318 // Set section alignment.
319 Section->Header.Characteristics = MCSec.getCharacteristics();
320 Section->Header.Characteristics |= getAlignment(MCSec);
322 // Bind internal COFF section to MC section.
323 Section->MCSection = &MCSec;
324 SectionMap[&MCSec] = Section;
326 if (UseOffsetLabels && !MCSec.empty()) {
327 const uint32_t Interval = 1 << OffsetLabelIntervalBits;
328 uint32_t N = 1;
329 for (uint32_t Off = Interval, E = Asm.getSectionAddressSize(MCSec); Off < E;
330 Off += Interval) {
331 auto Name = ("$L" + MCSec.getName() + "_" + Twine(N++)).str();
332 COFFSymbol *Label = createSymbol(Name);
333 Label->Section = Section;
334 Label->Data.StorageClass = COFF::IMAGE_SYM_CLASS_LABEL;
335 Label->Data.Value = Off;
336 Section->OffsetSymbols.push_back(Label);
341 static uint64_t getSymbolValue(const MCSymbol &Symbol, const MCAssembler &Asm) {
342 if (Symbol.isCommon() && Symbol.isExternal())
343 return Symbol.getCommonSize();
345 uint64_t Res;
346 if (!Asm.getSymbolOffset(Symbol, Res))
347 return 0;
349 return Res;
352 COFFSymbol *WinCOFFWriter::getLinkedSymbol(const MCSymbol &Symbol) {
353 if (!Symbol.isVariable())
354 return nullptr;
356 const MCSymbolRefExpr *SymRef =
357 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
358 if (!SymRef)
359 return nullptr;
361 const MCSymbol &Aliasee = SymRef->getSymbol();
362 if (Aliasee.isUndefined() || Aliasee.isExternal())
363 return GetOrCreateCOFFSymbol(&Aliasee);
364 else
365 return nullptr;
368 /// This function takes a symbol data object from the assembler
369 /// and creates the associated COFF symbol staging object.
370 void WinCOFFWriter::defineSymbol(const MCAssembler &Asm,
371 const MCSymbol &MCSym) {
372 const MCSymbol *Base = Asm.getBaseSymbol(MCSym);
373 COFFSection *Sec = nullptr;
374 MCSectionCOFF *MCSec = nullptr;
375 if (Base && Base->getFragment()) {
376 MCSec = cast<MCSectionCOFF>(Base->getFragment()->getParent());
377 Sec = SectionMap[MCSec];
380 if (Mode == NonDwoOnly && MCSec && isDwoSection(*MCSec))
381 return;
383 COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
384 COFFSymbol *Local = nullptr;
385 if (cast<MCSymbolCOFF>(MCSym).getWeakExternalCharacteristics()) {
386 Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
387 Sym->Section = nullptr;
389 COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
390 if (!WeakDefault) {
391 std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
392 WeakDefault = createSymbol(WeakName);
393 if (!Sec)
394 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
395 else
396 WeakDefault->Section = Sec;
397 WeakDefaults.insert(WeakDefault);
398 Local = WeakDefault;
401 Sym->Other = WeakDefault;
403 // Setup the Weak External auxiliary symbol.
404 Sym->Aux.resize(1);
405 memset(&Sym->Aux[0], 0, sizeof(Sym->Aux[0]));
406 Sym->Aux[0].AuxType = ATWeakExternal;
407 Sym->Aux[0].Aux.WeakExternal.TagIndex = 0; // Filled in later
408 Sym->Aux[0].Aux.WeakExternal.Characteristics =
409 cast<MCSymbolCOFF>(MCSym).getWeakExternalCharacteristics();
410 } else {
411 if (!Base)
412 Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
413 else
414 Sym->Section = Sec;
415 Local = Sym;
418 if (Local) {
419 Local->Data.Value = getSymbolValue(MCSym, Asm);
421 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
422 Local->Data.Type = SymbolCOFF.getType();
423 Local->Data.StorageClass = SymbolCOFF.getClass();
425 // If no storage class was specified in the streamer, define it here.
426 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
427 bool IsExternal =
428 MCSym.isExternal() || (!MCSym.getFragment() && !MCSym.isVariable());
430 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
431 : COFF::IMAGE_SYM_CLASS_STATIC;
435 Sym->MC = &MCSym;
438 void WinCOFFWriter::SetSectionName(COFFSection &S) {
439 if (S.Name.size() <= COFF::NameSize) {
440 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
441 return;
444 uint64_t StringTableEntry = Strings.getOffset(S.Name);
445 if (!COFF::encodeSectionName(S.Header.Name, StringTableEntry))
446 report_fatal_error("COFF string table is greater than 64 GB.");
449 void WinCOFFWriter::SetSymbolName(COFFSymbol &S) {
450 if (S.Name.size() > COFF::NameSize)
451 S.set_name_offset(Strings.getOffset(S.Name));
452 else
453 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
456 bool WinCOFFWriter::IsPhysicalSection(COFFSection *S) {
457 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
461 //------------------------------------------------------------------------------
462 // entity writing methods
464 void WinCOFFWriter::WriteFileHeader(const COFF::header &Header) {
465 if (UseBigObj) {
466 W.write<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
467 W.write<uint16_t>(0xFFFF);
468 W.write<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion);
469 W.write<uint16_t>(Header.Machine);
470 W.write<uint32_t>(Header.TimeDateStamp);
471 W.OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic));
472 W.write<uint32_t>(0);
473 W.write<uint32_t>(0);
474 W.write<uint32_t>(0);
475 W.write<uint32_t>(0);
476 W.write<uint32_t>(Header.NumberOfSections);
477 W.write<uint32_t>(Header.PointerToSymbolTable);
478 W.write<uint32_t>(Header.NumberOfSymbols);
479 } else {
480 W.write<uint16_t>(Header.Machine);
481 W.write<uint16_t>(static_cast<int16_t>(Header.NumberOfSections));
482 W.write<uint32_t>(Header.TimeDateStamp);
483 W.write<uint32_t>(Header.PointerToSymbolTable);
484 W.write<uint32_t>(Header.NumberOfSymbols);
485 W.write<uint16_t>(Header.SizeOfOptionalHeader);
486 W.write<uint16_t>(Header.Characteristics);
490 void WinCOFFWriter::WriteSymbol(const COFFSymbol &S) {
491 W.OS.write(S.Data.Name, COFF::NameSize);
492 W.write<uint32_t>(S.Data.Value);
493 if (UseBigObj)
494 W.write<uint32_t>(S.Data.SectionNumber);
495 else
496 W.write<uint16_t>(static_cast<int16_t>(S.Data.SectionNumber));
497 W.write<uint16_t>(S.Data.Type);
498 W.OS << char(S.Data.StorageClass);
499 W.OS << char(S.Data.NumberOfAuxSymbols);
500 WriteAuxiliarySymbols(S.Aux);
503 void WinCOFFWriter::WriteAuxiliarySymbols(
504 const COFFSymbol::AuxiliarySymbols &S) {
505 for (const AuxSymbol &i : S) {
506 switch (i.AuxType) {
507 case ATWeakExternal:
508 W.write<uint32_t>(i.Aux.WeakExternal.TagIndex);
509 W.write<uint32_t>(i.Aux.WeakExternal.Characteristics);
510 W.OS.write_zeros(sizeof(i.Aux.WeakExternal.unused));
511 if (UseBigObj)
512 W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
513 break;
514 case ATFile:
515 W.OS.write(reinterpret_cast<const char *>(&i.Aux),
516 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size);
517 break;
518 case ATSectionDefinition:
519 W.write<uint32_t>(i.Aux.SectionDefinition.Length);
520 W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfRelocations);
521 W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfLinenumbers);
522 W.write<uint32_t>(i.Aux.SectionDefinition.CheckSum);
523 W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
524 W.OS << char(i.Aux.SectionDefinition.Selection);
525 W.OS.write_zeros(sizeof(i.Aux.SectionDefinition.unused));
526 W.write<uint16_t>(
527 static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
528 if (UseBigObj)
529 W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
530 break;
535 // Write the section header.
536 void WinCOFFWriter::writeSectionHeaders() {
537 // Section numbers must be monotonically increasing in the section
538 // header, but our Sections array is not sorted by section number,
539 // so make a copy of Sections and sort it.
540 std::vector<COFFSection *> Arr;
541 for (auto &Section : Sections)
542 Arr.push_back(Section.get());
543 llvm::sort(Arr, [](const COFFSection *A, const COFFSection *B) {
544 return A->Number < B->Number;
547 for (auto &Section : Arr) {
548 if (Section->Number == -1)
549 continue;
551 COFF::section &S = Section->Header;
552 if (Section->Relocations.size() >= 0xffff)
553 S.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
554 W.OS.write(S.Name, COFF::NameSize);
555 W.write<uint32_t>(S.VirtualSize);
556 W.write<uint32_t>(S.VirtualAddress);
557 W.write<uint32_t>(S.SizeOfRawData);
558 W.write<uint32_t>(S.PointerToRawData);
559 W.write<uint32_t>(S.PointerToRelocations);
560 W.write<uint32_t>(S.PointerToLineNumbers);
561 W.write<uint16_t>(S.NumberOfRelocations);
562 W.write<uint16_t>(S.NumberOfLineNumbers);
563 W.write<uint32_t>(S.Characteristics);
567 void WinCOFFWriter::WriteRelocation(const COFF::relocation &R) {
568 W.write<uint32_t>(R.VirtualAddress);
569 W.write<uint32_t>(R.SymbolTableIndex);
570 W.write<uint16_t>(R.Type);
573 // Write MCSec's contents. What this function does is essentially
574 // "Asm.writeSectionData(&MCSec)", but it's a bit complicated
575 // because it needs to compute a CRC.
576 uint32_t WinCOFFWriter::writeSectionContents(MCAssembler &Asm,
577 const MCSection &MCSec) {
578 // Save the contents of the section to a temporary buffer, we need this
579 // to CRC the data before we dump it into the object file.
580 SmallVector<char, 128> Buf;
581 raw_svector_ostream VecOS(Buf);
582 Asm.writeSectionData(VecOS, &MCSec);
584 // Write the section contents to the object file.
585 W.OS << Buf;
587 // Calculate our CRC with an initial value of '0', this is not how
588 // JamCRC is specified but it aligns with the expected output.
589 JamCRC JC(/*Init=*/0);
590 JC.update(ArrayRef(reinterpret_cast<uint8_t *>(Buf.data()), Buf.size()));
591 return JC.getCRC();
594 void WinCOFFWriter::writeSection(MCAssembler &Asm, const COFFSection &Sec) {
595 if (Sec.Number == -1)
596 return;
598 // Write the section contents.
599 if (Sec.Header.PointerToRawData != 0) {
600 assert(W.OS.tell() == Sec.Header.PointerToRawData &&
601 "Section::PointerToRawData is insane!");
603 uint32_t CRC = writeSectionContents(Asm, *Sec.MCSection);
605 // Update the section definition auxiliary symbol to record the CRC.
606 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec.Symbol->Aux;
607 assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
608 AuxSymbol &SecDef = AuxSyms[0];
609 SecDef.Aux.SectionDefinition.CheckSum = CRC;
612 // Write relocations for this section.
613 if (Sec.Relocations.empty()) {
614 assert(Sec.Header.PointerToRelocations == 0 &&
615 "Section::PointerToRelocations is insane!");
616 return;
619 assert(W.OS.tell() == Sec.Header.PointerToRelocations &&
620 "Section::PointerToRelocations is insane!");
622 if (Sec.Relocations.size() >= 0xffff) {
623 // In case of overflow, write actual relocation count as first
624 // relocation. Including the synthetic reloc itself (+ 1).
625 COFF::relocation R;
626 R.VirtualAddress = Sec.Relocations.size() + 1;
627 R.SymbolTableIndex = 0;
628 R.Type = 0;
629 WriteRelocation(R);
632 for (const auto &Relocation : Sec.Relocations)
633 WriteRelocation(Relocation.Data);
636 // Create .file symbols.
637 void WinCOFFWriter::createFileSymbols(MCAssembler &Asm) {
638 for (const std::pair<std::string, size_t> &It : OWriter.getFileNames()) {
639 // round up to calculate the number of auxiliary symbols required
640 const std::string &Name = It.first;
641 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
642 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
644 COFFSymbol *File = createSymbol(".file");
645 File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
646 File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
647 File->Aux.resize(Count);
649 unsigned Offset = 0;
650 unsigned Length = Name.size();
651 for (auto &Aux : File->Aux) {
652 Aux.AuxType = ATFile;
654 if (Length > SymbolSize) {
655 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
656 Length = Length - SymbolSize;
657 } else {
658 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
659 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
660 break;
663 Offset += SymbolSize;
668 void WinCOFFWriter::setWeakDefaultNames() {
669 if (WeakDefaults.empty())
670 return;
672 // If multiple object files use a weak symbol (either with a regular
673 // defined default, or an absolute zero symbol as default), the defaults
674 // cause duplicate definitions unless their names are made unique. Look
675 // for a defined extern symbol, that isn't comdat - that should be unique
676 // unless there are other duplicate definitions. And if none is found,
677 // allow picking a comdat symbol, as that's still better than nothing.
679 COFFSymbol *Unique = nullptr;
680 for (bool AllowComdat : {false, true}) {
681 for (auto &Sym : Symbols) {
682 // Don't include the names of the defaults themselves
683 if (WeakDefaults.count(Sym.get()))
684 continue;
685 // Only consider external symbols
686 if (Sym->Data.StorageClass != COFF::IMAGE_SYM_CLASS_EXTERNAL)
687 continue;
688 // Only consider symbols defined in a section or that are absolute
689 if (!Sym->Section && Sym->Data.SectionNumber != COFF::IMAGE_SYM_ABSOLUTE)
690 continue;
691 if (!AllowComdat && Sym->Section &&
692 Sym->Section->Header.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
693 continue;
694 Unique = Sym.get();
695 break;
697 if (Unique)
698 break;
700 // If we didn't find any unique symbol to use for the names, just skip this.
701 if (!Unique)
702 return;
703 for (auto *Sym : WeakDefaults) {
704 Sym->Name.append(".");
705 Sym->Name.append(Unique->Name);
709 static bool isAssociative(const COFFSection &Section) {
710 return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
711 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
714 void WinCOFFWriter::assignSectionNumbers() {
715 size_t I = 1;
716 auto Assign = [&](COFFSection &Section) {
717 Section.Number = I;
718 Section.Symbol->Data.SectionNumber = I;
719 Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I;
720 ++I;
723 // Although it is not explicitly requested by the Microsoft COFF spec,
724 // we should avoid emitting forward associative section references,
725 // because MSVC link.exe as of 2017 cannot handle that.
726 for (const std::unique_ptr<COFFSection> &Section : Sections)
727 if (!isAssociative(*Section))
728 Assign(*Section);
729 for (const std::unique_ptr<COFFSection> &Section : Sections)
730 if (isAssociative(*Section))
731 Assign(*Section);
734 // Assign file offsets to COFF object file structures.
735 void WinCOFFWriter::assignFileOffsets(MCAssembler &Asm) {
736 unsigned Offset = W.OS.tell();
738 Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
739 Offset += COFF::SectionSize * Header.NumberOfSections;
741 for (const auto &Section : Asm) {
742 COFFSection *Sec = SectionMap[&Section];
744 if (!Sec || Sec->Number == -1)
745 continue;
747 Sec->Header.SizeOfRawData = Asm.getSectionAddressSize(Section);
749 if (IsPhysicalSection(Sec)) {
750 Sec->Header.PointerToRawData = Offset;
751 Offset += Sec->Header.SizeOfRawData;
754 if (!Sec->Relocations.empty()) {
755 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
757 if (RelocationsOverflow) {
758 // Signal overflow by setting NumberOfRelocations to max value. Actual
759 // size is found in reloc #0. Microsoft tools understand this.
760 Sec->Header.NumberOfRelocations = 0xffff;
761 } else {
762 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
764 Sec->Header.PointerToRelocations = Offset;
766 if (RelocationsOverflow) {
767 // Reloc #0 will contain actual count, so make room for it.
768 Offset += COFF::RelocationSize;
771 Offset += COFF::RelocationSize * Sec->Relocations.size();
773 for (auto &Relocation : Sec->Relocations) {
774 assert(Relocation.Symb->getIndex() != -1);
775 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
779 assert(Sec->Symbol->Aux.size() == 1 &&
780 "Section's symbol must have one aux!");
781 AuxSymbol &Aux = Sec->Symbol->Aux[0];
782 assert(Aux.AuxType == ATSectionDefinition &&
783 "Section's symbol's aux symbol must be a Section Definition!");
784 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
785 Aux.Aux.SectionDefinition.NumberOfRelocations =
786 Sec->Header.NumberOfRelocations;
787 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
788 Sec->Header.NumberOfLineNumbers;
791 Header.PointerToSymbolTable = Offset;
794 void WinCOFFWriter::reset() {
795 memset(&Header, 0, sizeof(Header));
796 Header.Machine = OWriter.TargetObjectWriter->getMachine();
797 Sections.clear();
798 Symbols.clear();
799 Strings.clear();
800 SectionMap.clear();
801 SymbolMap.clear();
802 WeakDefaults.clear();
805 void WinCOFFWriter::executePostLayoutBinding(MCAssembler &Asm) {
806 // "Define" each section & symbol. This creates section & symbol
807 // entries in the staging area.
808 for (const auto &Section : Asm) {
809 if ((Mode == NonDwoOnly && isDwoSection(Section)) ||
810 (Mode == DwoOnly && !isDwoSection(Section)))
811 continue;
812 defineSection(Asm, static_cast<const MCSectionCOFF &>(Section));
815 if (Mode != DwoOnly)
816 for (const MCSymbol &Symbol : Asm.symbols())
817 // Define non-temporary or temporary static (private-linkage) symbols
818 if (!Symbol.isTemporary() ||
819 cast<MCSymbolCOFF>(Symbol).getClass() == COFF::IMAGE_SYM_CLASS_STATIC)
820 defineSymbol(Asm, Symbol);
823 void WinCOFFWriter::recordRelocation(MCAssembler &Asm,
824 const MCFragment *Fragment,
825 const MCFixup &Fixup, MCValue Target,
826 uint64_t &FixedValue) {
827 assert(Target.getSymA() && "Relocation must reference a symbol!");
829 const MCSymbol &A = Target.getSymA()->getSymbol();
830 if (!A.isRegistered()) {
831 Asm.getContext().reportError(Fixup.getLoc(), Twine("symbol '") +
832 A.getName() +
833 "' can not be undefined");
834 return;
836 if (A.isTemporary() && A.isUndefined()) {
837 Asm.getContext().reportError(Fixup.getLoc(), Twine("assembler label '") +
838 A.getName() +
839 "' can not be undefined");
840 return;
843 MCSection *MCSec = Fragment->getParent();
845 // Mark this symbol as requiring an entry in the symbol table.
846 assert(SectionMap.contains(MCSec) &&
847 "Section must already have been defined in executePostLayoutBinding!");
849 COFFSection *Sec = SectionMap[MCSec];
850 const MCSymbolRefExpr *SymB = Target.getSymB();
852 if (SymB) {
853 const MCSymbol *B = &SymB->getSymbol();
854 if (!B->getFragment()) {
855 Asm.getContext().reportError(
856 Fixup.getLoc(),
857 Twine("symbol '") + B->getName() +
858 "' can not be undefined in a subtraction expression");
859 return;
862 // Offset of the symbol in the section
863 int64_t OffsetOfB = Asm.getSymbolOffset(*B);
865 // Offset of the relocation in the section
866 int64_t OffsetOfRelocation =
867 Asm.getFragmentOffset(*Fragment) + Fixup.getOffset();
869 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
870 } else {
871 FixedValue = Target.getConstant();
874 COFFRelocation Reloc;
876 Reloc.Data.SymbolTableIndex = 0;
877 Reloc.Data.VirtualAddress = Asm.getFragmentOffset(*Fragment);
879 // Turn relocations for temporary symbols into section relocations.
880 if (A.isTemporary() && !SymbolMap[&A]) {
881 MCSection *TargetSection = &A.getSection();
882 assert(
883 SectionMap.contains(TargetSection) &&
884 "Section must already have been defined in executePostLayoutBinding!");
885 COFFSection *Section = SectionMap[TargetSection];
886 Reloc.Symb = Section->Symbol;
887 FixedValue += Asm.getSymbolOffset(A);
888 // Technically, we should do the final adjustments of FixedValue (below)
889 // before picking an offset symbol, otherwise we might choose one which
890 // is slightly too far away. The relocations where it really matters
891 // (arm64 adrp relocations) don't get any offset though.
892 if (UseOffsetLabels && !Section->OffsetSymbols.empty()) {
893 uint64_t LabelIndex = FixedValue >> OffsetLabelIntervalBits;
894 if (LabelIndex > 0) {
895 if (LabelIndex <= Section->OffsetSymbols.size())
896 Reloc.Symb = Section->OffsetSymbols[LabelIndex - 1];
897 else
898 Reloc.Symb = Section->OffsetSymbols.back();
899 FixedValue -= Reloc.Symb->Data.Value;
902 } else {
903 assert(
904 SymbolMap.contains(&A) &&
905 "Symbol must already have been defined in executePostLayoutBinding!");
906 Reloc.Symb = SymbolMap[&A];
909 ++Reloc.Symb->Relocations;
911 Reloc.Data.VirtualAddress += Fixup.getOffset();
912 Reloc.Data.Type = OWriter.TargetObjectWriter->getRelocType(
913 Asm.getContext(), Target, Fixup, SymB, Asm.getBackend());
915 // The *_REL32 relocations are relative to the end of the relocation,
916 // not to the start.
917 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
918 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
919 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
920 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32) ||
921 (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT &&
922 Reloc.Data.Type == COFF::IMAGE_REL_ARM_REL32) ||
923 (COFF::isAnyArm64(Header.Machine) &&
924 Reloc.Data.Type == COFF::IMAGE_REL_ARM64_REL32))
925 FixedValue += 4;
927 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
928 switch (Reloc.Data.Type) {
929 case COFF::IMAGE_REL_ARM_ABSOLUTE:
930 case COFF::IMAGE_REL_ARM_ADDR32:
931 case COFF::IMAGE_REL_ARM_ADDR32NB:
932 case COFF::IMAGE_REL_ARM_TOKEN:
933 case COFF::IMAGE_REL_ARM_SECTION:
934 case COFF::IMAGE_REL_ARM_SECREL:
935 break;
936 case COFF::IMAGE_REL_ARM_BRANCH11:
937 case COFF::IMAGE_REL_ARM_BLX11:
938 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
939 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
940 // for Windows CE).
941 case COFF::IMAGE_REL_ARM_BRANCH24:
942 case COFF::IMAGE_REL_ARM_BLX24:
943 case COFF::IMAGE_REL_ARM_MOV32A:
944 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
945 // only used for ARM mode code, which is documented as being unsupported
946 // by Windows on ARM. Empirical proof indicates that masm is able to
947 // generate the relocations however the rest of the MSVC toolchain is
948 // unable to handle it.
949 llvm_unreachable("unsupported relocation");
950 break;
951 case COFF::IMAGE_REL_ARM_MOV32T:
952 break;
953 case COFF::IMAGE_REL_ARM_BRANCH20T:
954 case COFF::IMAGE_REL_ARM_BRANCH24T:
955 case COFF::IMAGE_REL_ARM_BLX23T:
956 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
957 // perform a 4 byte adjustment to the relocation. Relative branches are
958 // offset by 4 on ARM, however, because there is no RELA relocations, all
959 // branches are offset by 4.
960 FixedValue = FixedValue + 4;
961 break;
965 // The fixed value never makes sense for section indices, ignore it.
966 if (Fixup.getKind() == FK_SecRel_2)
967 FixedValue = 0;
969 if (OWriter.TargetObjectWriter->recordRelocation(Fixup))
970 Sec->Relocations.push_back(Reloc);
973 static std::time_t getTime() {
974 std::time_t Now = time(nullptr);
975 if (Now < 0 || !isUInt<32>(Now))
976 return UINT32_MAX;
977 return Now;
980 uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
981 uint64_t StartOffset = W.OS.tell();
983 if (Sections.size() > INT32_MAX)
984 report_fatal_error(
985 "PE COFF object files can't have more than 2147483647 sections");
987 UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
988 Header.NumberOfSections = Sections.size();
989 Header.NumberOfSymbols = 0;
991 setWeakDefaultNames();
992 assignSectionNumbers();
993 if (Mode != DwoOnly)
994 createFileSymbols(Asm);
996 for (auto &Symbol : Symbols) {
997 // Update section number & offset for symbols that have them.
998 if (Symbol->Section)
999 Symbol->Data.SectionNumber = Symbol->Section->Number;
1000 Symbol->setIndex(Header.NumberOfSymbols++);
1001 // Update auxiliary symbol info.
1002 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
1003 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
1006 // Build string table.
1007 for (const auto &S : Sections)
1008 if (S->Name.size() > COFF::NameSize)
1009 Strings.add(S->Name);
1010 for (const auto &S : Symbols)
1011 if (S->Name.size() > COFF::NameSize)
1012 Strings.add(S->Name);
1013 Strings.finalize();
1015 // Set names.
1016 for (const auto &S : Sections)
1017 SetSectionName(*S);
1018 for (auto &S : Symbols)
1019 SetSymbolName(*S);
1021 // Fixup weak external references.
1022 for (auto &Symbol : Symbols) {
1023 if (Symbol->Other) {
1024 assert(Symbol->getIndex() != -1);
1025 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
1026 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
1027 "Symbol's aux symbol must be a Weak External!");
1028 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
1032 // Fixup associative COMDAT sections.
1033 for (auto &Section : Sections) {
1034 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
1035 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1036 continue;
1038 const MCSectionCOFF &MCSec = *Section->MCSection;
1039 const MCSymbol *AssocMCSym = MCSec.getCOMDATSymbol();
1040 assert(AssocMCSym);
1042 // It's an error to try to associate with an undefined symbol or a symbol
1043 // without a section.
1044 if (!AssocMCSym->isInSection()) {
1045 Asm.getContext().reportError(
1046 SMLoc(), Twine("cannot make section ") + MCSec.getName() +
1047 Twine(" associative with sectionless symbol ") +
1048 AssocMCSym->getName());
1049 continue;
1052 const auto *AssocMCSec = cast<MCSectionCOFF>(&AssocMCSym->getSection());
1053 assert(SectionMap.count(AssocMCSec));
1054 COFFSection *AssocSec = SectionMap[AssocMCSec];
1056 // Skip this section if the associated section is unused.
1057 if (AssocSec->Number == -1)
1058 continue;
1060 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = AssocSec->Number;
1063 // Create the contents of the .llvm_addrsig section.
1064 if (Mode != DwoOnly && OWriter.getEmitAddrsigSection()) {
1065 auto *Sec = Asm.getContext().getCOFFSection(
1066 ".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE);
1067 auto *Frag = cast<MCDataFragment>(Sec->curFragList()->Head);
1068 raw_svector_ostream OS(Frag->getContents());
1069 for (const MCSymbol *S : OWriter.AddrsigSyms) {
1070 if (!S->isRegistered())
1071 continue;
1072 if (!S->isTemporary()) {
1073 encodeULEB128(S->getIndex(), OS);
1074 continue;
1077 MCSection *TargetSection = &S->getSection();
1078 assert(SectionMap.contains(TargetSection) &&
1079 "Section must already have been defined in "
1080 "executePostLayoutBinding!");
1081 encodeULEB128(SectionMap[TargetSection]->Symbol->getIndex(), OS);
1085 // Create the contents of the .llvm.call-graph-profile section.
1086 if (Mode != DwoOnly && !OWriter.getCGProfile().empty()) {
1087 auto *Sec = Asm.getContext().getCOFFSection(
1088 ".llvm.call-graph-profile", COFF::IMAGE_SCN_LNK_REMOVE);
1089 auto *Frag = cast<MCDataFragment>(Sec->curFragList()->Head);
1090 raw_svector_ostream OS(Frag->getContents());
1091 for (const auto &CGPE : OWriter.getCGProfile()) {
1092 uint32_t FromIndex = CGPE.From->getSymbol().getIndex();
1093 uint32_t ToIndex = CGPE.To->getSymbol().getIndex();
1094 support::endian::write(OS, FromIndex, W.Endian);
1095 support::endian::write(OS, ToIndex, W.Endian);
1096 support::endian::write(OS, CGPE.Count, W.Endian);
1100 assignFileOffsets(Asm);
1102 // MS LINK expects to be able to use this timestamp to implement their
1103 // /INCREMENTAL feature.
1104 if (OWriter.IncrementalLinkerCompatible) {
1105 Header.TimeDateStamp = getTime();
1106 } else {
1107 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
1108 Header.TimeDateStamp = 0;
1111 // Write it all to disk...
1112 WriteFileHeader(Header);
1113 writeSectionHeaders();
1115 #ifndef NDEBUG
1116 sections::iterator I = Sections.begin();
1117 sections::iterator IE = Sections.end();
1118 auto J = Asm.begin();
1119 auto JE = Asm.end();
1120 for (; I != IE && J != JE; ++I, ++J) {
1121 while (J != JE && ((Mode == NonDwoOnly && isDwoSection(*J)) ||
1122 (Mode == DwoOnly && !isDwoSection(*J))))
1123 ++J;
1124 assert(J != JE && (**I).MCSection == &*J && "Wrong bound MCSection");
1126 #endif
1128 // Write section contents.
1129 for (std::unique_ptr<COFFSection> &Sec : Sections)
1130 writeSection(Asm, *Sec);
1132 assert(W.OS.tell() == Header.PointerToSymbolTable &&
1133 "Header::PointerToSymbolTable is insane!");
1135 // Write a symbol table.
1136 for (auto &Symbol : Symbols)
1137 if (Symbol->getIndex() != -1)
1138 WriteSymbol(*Symbol);
1140 // Write a string table, which completes the entire COFF file.
1141 Strings.write(W.OS);
1143 return W.OS.tell() - StartOffset;
1146 //------------------------------------------------------------------------------
1147 // WinCOFFObjectWriter class implementation
1149 ////////////////////////////////////////////////////////////////////////////////
1150 // MCObjectWriter interface implementations
1152 void WinCOFFObjectWriter::reset() {
1153 IncrementalLinkerCompatible = false;
1154 ObjWriter->reset();
1155 if (DwoWriter)
1156 DwoWriter->reset();
1157 MCObjectWriter::reset();
1160 bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1161 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
1162 bool InSet, bool IsPCRel) const {
1163 // Don't drop relocations between functions, even if they are in the same text
1164 // section. Multiple Visual C++ linker features depend on having the
1165 // relocations present. The /INCREMENTAL flag will cause these relocations to
1166 // point to thunks, and the /GUARD:CF flag assumes that it can use relocations
1167 // to approximate the set of all address taken functions. LLD's implementation
1168 // of /GUARD:CF also relies on the existance of these relocations.
1169 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
1170 if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
1171 return false;
1172 return &SymA.getSection() == FB.getParent();
1175 void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
1176 ObjWriter->executePostLayoutBinding(Asm);
1177 if (DwoWriter)
1178 DwoWriter->executePostLayoutBinding(Asm);
1181 void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
1182 const MCFragment *Fragment,
1183 const MCFixup &Fixup, MCValue Target,
1184 uint64_t &FixedValue) {
1185 assert(!isDwoSection(*Fragment->getParent()) &&
1186 "No relocation in Dwo sections");
1187 ObjWriter->recordRelocation(Asm, Fragment, Fixup, Target, FixedValue);
1190 uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm) {
1191 uint64_t TotalSize = ObjWriter->writeObject(Asm);
1192 if (DwoWriter)
1193 TotalSize += DwoWriter->writeObject(Asm);
1194 return TotalSize;
1197 MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1198 : Machine(Machine_) {}
1200 // Pin the vtable to this file.
1201 void MCWinCOFFObjectTargetWriter::anchor() {}
1203 //------------------------------------------------------------------------------
1204 // WinCOFFObjectWriter factory function
1206 std::unique_ptr<MCObjectWriter> llvm::createWinCOFFObjectWriter(
1207 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) {
1208 return std::make_unique<WinCOFFObjectWriter>(std::move(MOTW), OS);
1211 std::unique_ptr<MCObjectWriter> llvm::createWinCOFFDwoObjectWriter(
1212 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS,
1213 raw_pwrite_stream &DwoOS) {
1214 return std::make_unique<WinCOFFObjectWriter>(std::move(MOTW), OS, DwoOS);