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.
10//
11//===----------------------------------------------------------------------===//
12
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/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/CRC.h"
36#include "llvm/Support/Casting.h"
37#include "llvm/Support/EndianStream.h"
38#include "llvm/Support/ErrorHandling.h"
39#include "llvm/Support/LEB128.h"
40#include "llvm/Support/MathExtras.h"
41#include "llvm/Support/raw_ostream.h"
42#include <algorithm>
43#include <cassert>
44#include <cstdint>
45#include <cstring>
46#include <ctime>
47#include <memory>
48#include <string>
49#include <vector>
50
51using namespace llvm;
52using llvm::support::endian::write32le;
53
54#define DEBUG_TYPE "WinCOFFObjectWriter"
55
56namespace {
57
58constexpr int OffsetLabelIntervalBits = 20;
59
60using name = SmallString<COFF::NameSize>;
61
62enum AuxiliaryType { ATWeakExternal, ATFile, ATSectionDefinition };
63
64struct AuxSymbol {
65 AuxiliaryType AuxType;
66 COFF::Auxiliary Aux;
67};
68
69class COFFSection;
70
71class COFFSymbol {
72public:
73 COFF::symbol Data = {};
74
75 using AuxiliarySymbols = SmallVector<AuxSymbol, 1>;
76
77 name Name;
78 int Index = 0;
79 AuxiliarySymbols Aux;
80 COFFSymbol *Other = nullptr;
81 COFFSection *Section = nullptr;
82 int Relocations = 0;
83 const MCSymbol *MC = nullptr;
84
85 COFFSymbol(StringRef Name) : Name(Name) {}
86
87 void set_name_offset(uint32_t Offset);
88
89 int64_t getIndex() const { return Index; }
90 void setIndex(int Value) {
91 Index = Value;
92 if (MC)
93 MC->setIndex(static_cast<uint32_t>(Value));
94 }
95};
96
97// This class contains staging data for a COFF relocation entry.
98struct COFFRelocation {
99 COFF::relocation Data;
100 COFFSymbol *Symb = nullptr;
101
102 COFFRelocation() = default;
103
104 static size_t size() { return COFF::RelocationSize; }
105};
106
107using relocations = std::vector<COFFRelocation>;
108
109class COFFSection {
110public:
111 COFF::section Header = {};
112
113 std::string Name;
114 int Number = 0;
115 MCSectionCOFF const *MCSection = nullptr;
116 COFFSymbol *Symbol = nullptr;
117 relocations Relocations;
118
119 COFFSection(StringRef Name) : Name(std::string(Name)) {}
120
121 SmallVector<COFFSymbol *, 1> OffsetSymbols;
122};
123
124class WinCOFFObjectWriter;
125
126class WinCOFFWriter {
127 WinCOFFObjectWriter &OWriter;
128 support::endian::Writer W;
129
130 using symbols = std::vector<std::unique_ptr<COFFSymbol>>;
131 using sections = std::vector<std::unique_ptr<COFFSection>>;
132
133 using symbol_map = DenseMap<MCSymbol const *, COFFSymbol *>;
134 using section_map = DenseMap<MCSection const *, COFFSection *>;
135
136 using symbol_list = DenseSet<COFFSymbol *>;
137
138 // Root level file contents.
139 COFF::header Header = {};
140 sections Sections;
141 symbols Symbols;
142 StringTableBuilder Strings{StringTableBuilder::WinCOFF};
143
144 // Maps used during object file creation.
145 section_map SectionMap;
146 symbol_map SymbolMap;
147
148 symbol_list WeakDefaults;
149
150 bool UseBigObj;
151 bool UseOffsetLabels = false;
152
153public:
154 MCSectionCOFF *AddrsigSection = nullptr;
155 MCSectionCOFF *CGProfileSection = nullptr;
156
157 enum DwoMode {
158 AllSections,
159 NonDwoOnly,
160 DwoOnly,
161 } Mode;
162
163 WinCOFFWriter(WinCOFFObjectWriter &OWriter, raw_pwrite_stream &OS,
164 DwoMode Mode);
165
166 void reset();
167 void executePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout);
168 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
169 const MCFragment *Fragment, const MCFixup &Fixup,
170 MCValue Target, uint64_t &FixedValue);
171 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout);
172
173private:
174 COFFSymbol *createSymbol(StringRef Name);
175 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
176 COFFSection *createSection(StringRef Name);
177
178 void defineSection(MCSectionCOFF const &Sec, const MCAsmLayout &Layout);
179
180 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
181 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
182 const MCAsmLayout &Layout);
183
184 void SetSymbolName(COFFSymbol &S);
185 void SetSectionName(COFFSection &S);
186
187 bool IsPhysicalSection(COFFSection *S);
188
189 // Entity writing methods.
190 void WriteFileHeader(const COFF::header &Header);
191 void WriteSymbol(const COFFSymbol &S);
192 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
193 void writeSectionHeaders();
194 void WriteRelocation(const COFF::relocation &R);
195 uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout,
196 const MCSection &MCSec);
197 void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
198 const COFFSection &Sec);
199
200 void createFileSymbols(MCAssembler &Asm);
201 void setWeakDefaultNames();
202 void assignSectionNumbers();
203 void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout);
204};
205
206class WinCOFFObjectWriter : public MCObjectWriter {
207 friend class WinCOFFWriter;
208
209 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
210 std::unique_ptr<WinCOFFWriter> ObjWriter, DwoWriter;
211
212public:
213 WinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
214 raw_pwrite_stream &OS)
215 : TargetObjectWriter(std::move(MOTW)),
216 ObjWriter(std::make_unique<WinCOFFWriter>(args&: *this, args&: OS,
217 args: WinCOFFWriter::AllSections)) {
218 }
219 WinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
220 raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS)
221 : TargetObjectWriter(std::move(MOTW)),
222 ObjWriter(std::make_unique<WinCOFFWriter>(args&: *this, args&: OS,
223 args: WinCOFFWriter::NonDwoOnly)),
224 DwoWriter(std::make_unique<WinCOFFWriter>(args&: *this, args&: DwoOS,
225 args: WinCOFFWriter::DwoOnly)) {}
226
227 // MCObjectWriter interface implementation.
228 void reset() override;
229 void executePostLayoutBinding(MCAssembler &Asm,
230 const MCAsmLayout &Layout) override;
231 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
232 const MCSymbol &SymA,
233 const MCFragment &FB, bool InSet,
234 bool IsPCRel) const override;
235 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
236 const MCFragment *Fragment, const MCFixup &Fixup,
237 MCValue Target, uint64_t &FixedValue) override;
238 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
239};
240
241} // end anonymous namespace
242
243static bool isDwoSection(const MCSection &Sec) {
244 return Sec.getName().ends_with(Suffix: ".dwo");
245}
246
247//------------------------------------------------------------------------------
248// Symbol class implementation
249
250// In the case that the name does not fit within 8 bytes, the offset
251// into the string table is stored in the last 4 bytes instead, leaving
252// the first 4 bytes as 0.
253void COFFSymbol::set_name_offset(uint32_t Offset) {
254 write32le(P: Data.Name + 0, V: 0);
255 write32le(P: Data.Name + 4, V: Offset);
256}
257
258//------------------------------------------------------------------------------
259// WinCOFFWriter class implementation
260
261WinCOFFWriter::WinCOFFWriter(WinCOFFObjectWriter &OWriter,
262 raw_pwrite_stream &OS, DwoMode Mode)
263 : OWriter(OWriter), W(OS, llvm::endianness::little), Mode(Mode) {
264 Header.Machine = OWriter.TargetObjectWriter->getMachine();
265 // Some relocations on ARM64 (the 21 bit ADRP relocations) have a slightly
266 // limited range for the immediate offset (+/- 1 MB); create extra offset
267 // label symbols with regular intervals to allow referencing a
268 // non-temporary symbol that is close enough.
269 UseOffsetLabels = Header.Machine == COFF::IMAGE_FILE_MACHINE_ARM64;
270}
271
272COFFSymbol *WinCOFFWriter::createSymbol(StringRef Name) {
273 Symbols.push_back(x: std::make_unique<COFFSymbol>(args&: Name));
274 return Symbols.back().get();
275}
276
277COFFSymbol *WinCOFFWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
278 COFFSymbol *&Ret = SymbolMap[Symbol];
279 if (!Ret)
280 Ret = createSymbol(Name: Symbol->getName());
281 return Ret;
282}
283
284COFFSection *WinCOFFWriter::createSection(StringRef Name) {
285 Sections.emplace_back(args: std::make_unique<COFFSection>(args&: Name));
286 return Sections.back().get();
287}
288
289static uint32_t getAlignment(const MCSectionCOFF &Sec) {
290 switch (Sec.getAlign().value()) {
291 case 1:
292 return COFF::IMAGE_SCN_ALIGN_1BYTES;
293 case 2:
294 return COFF::IMAGE_SCN_ALIGN_2BYTES;
295 case 4:
296 return COFF::IMAGE_SCN_ALIGN_4BYTES;
297 case 8:
298 return COFF::IMAGE_SCN_ALIGN_8BYTES;
299 case 16:
300 return COFF::IMAGE_SCN_ALIGN_16BYTES;
301 case 32:
302 return COFF::IMAGE_SCN_ALIGN_32BYTES;
303 case 64:
304 return COFF::IMAGE_SCN_ALIGN_64BYTES;
305 case 128:
306 return COFF::IMAGE_SCN_ALIGN_128BYTES;
307 case 256:
308 return COFF::IMAGE_SCN_ALIGN_256BYTES;
309 case 512:
310 return COFF::IMAGE_SCN_ALIGN_512BYTES;
311 case 1024:
312 return COFF::IMAGE_SCN_ALIGN_1024BYTES;
313 case 2048:
314 return COFF::IMAGE_SCN_ALIGN_2048BYTES;
315 case 4096:
316 return COFF::IMAGE_SCN_ALIGN_4096BYTES;
317 case 8192:
318 return COFF::IMAGE_SCN_ALIGN_8192BYTES;
319 }
320 llvm_unreachable("unsupported section alignment");
321}
322
323/// This function takes a section data object from the assembler
324/// and creates the associated COFF section staging object.
325void WinCOFFWriter::defineSection(const MCSectionCOFF &MCSec,
326 const MCAsmLayout &Layout) {
327 COFFSection *Section = createSection(Name: MCSec.getName());
328 COFFSymbol *Symbol = createSymbol(Name: MCSec.getName());
329 Section->Symbol = Symbol;
330 Symbol->Section = Section;
331 Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
332
333 // Create a COMDAT symbol if needed.
334 if (MCSec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
335 if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
336 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(Symbol: S);
337 if (COMDATSymbol->Section)
338 report_fatal_error(reason: "two sections have the same comdat");
339 COMDATSymbol->Section = Section;
340 }
341 }
342
343 // In this case the auxiliary symbol is a Section Definition.
344 Symbol->Aux.resize(N: 1);
345 Symbol->Aux[0] = {};
346 Symbol->Aux[0].AuxType = ATSectionDefinition;
347 Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection();
348
349 // Set section alignment.
350 Section->Header.Characteristics = MCSec.getCharacteristics();
351 Section->Header.Characteristics |= getAlignment(Sec: MCSec);
352
353 // Bind internal COFF section to MC section.
354 Section->MCSection = &MCSec;
355 SectionMap[&MCSec] = Section;
356
357 if (UseOffsetLabels && !MCSec.getFragmentList().empty()) {
358 const uint32_t Interval = 1 << OffsetLabelIntervalBits;
359 uint32_t N = 1;
360 for (uint32_t Off = Interval, E = Layout.getSectionAddressSize(Sec: &MCSec);
361 Off < E; Off += Interval) {
362 auto Name = ("$L" + MCSec.getName() + "_" + Twine(N++)).str();
363 COFFSymbol *Label = createSymbol(Name);
364 Label->Section = Section;
365 Label->Data.StorageClass = COFF::IMAGE_SYM_CLASS_LABEL;
366 Label->Data.Value = Off;
367 Section->OffsetSymbols.push_back(Elt: Label);
368 }
369 }
370}
371
372static uint64_t getSymbolValue(const MCSymbol &Symbol,
373 const MCAsmLayout &Layout) {
374 if (Symbol.isCommon() && Symbol.isExternal())
375 return Symbol.getCommonSize();
376
377 uint64_t Res;
378 if (!Layout.getSymbolOffset(S: Symbol, Val&: Res))
379 return 0;
380
381 return Res;
382}
383
384COFFSymbol *WinCOFFWriter::getLinkedSymbol(const MCSymbol &Symbol) {
385 if (!Symbol.isVariable())
386 return nullptr;
387
388 const MCSymbolRefExpr *SymRef =
389 dyn_cast<MCSymbolRefExpr>(Val: Symbol.getVariableValue());
390 if (!SymRef)
391 return nullptr;
392
393 const MCSymbol &Aliasee = SymRef->getSymbol();
394 if (Aliasee.isUndefined() || Aliasee.isExternal())
395 return GetOrCreateCOFFSymbol(Symbol: &Aliasee);
396 else
397 return nullptr;
398}
399
400/// This function takes a symbol data object from the assembler
401/// and creates the associated COFF symbol staging object.
402void WinCOFFWriter::DefineSymbol(const MCSymbol &MCSym, MCAssembler &Assembler,
403 const MCAsmLayout &Layout) {
404 COFFSymbol *Sym = GetOrCreateCOFFSymbol(Symbol: &MCSym);
405 const MCSymbol *Base = Layout.getBaseSymbol(Symbol: MCSym);
406 COFFSection *Sec = nullptr;
407 if (Base && Base->getFragment()) {
408 Sec = SectionMap[Base->getFragment()->getParent()];
409 if (Sym->Section && Sym->Section != Sec)
410 report_fatal_error(reason: "conflicting sections for symbol");
411 }
412
413 COFFSymbol *Local = nullptr;
414 if (cast<MCSymbolCOFF>(Val: MCSym).getWeakExternalCharacteristics()) {
415 Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
416 Sym->Section = nullptr;
417
418 COFFSymbol *WeakDefault = getLinkedSymbol(Symbol: MCSym);
419 if (!WeakDefault) {
420 std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
421 WeakDefault = createSymbol(Name: WeakName);
422 if (!Sec)
423 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
424 else
425 WeakDefault->Section = Sec;
426 WeakDefaults.insert(V: WeakDefault);
427 Local = WeakDefault;
428 }
429
430 Sym->Other = WeakDefault;
431
432 // Setup the Weak External auxiliary symbol.
433 Sym->Aux.resize(N: 1);
434 memset(s: &Sym->Aux[0], c: 0, n: sizeof(Sym->Aux[0]));
435 Sym->Aux[0].AuxType = ATWeakExternal;
436 Sym->Aux[0].Aux.WeakExternal.TagIndex = 0; // Filled in later
437 Sym->Aux[0].Aux.WeakExternal.Characteristics =
438 cast<MCSymbolCOFF>(Val: MCSym).getWeakExternalCharacteristics();
439 } else {
440 if (!Base)
441 Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
442 else
443 Sym->Section = Sec;
444 Local = Sym;
445 }
446
447 if (Local) {
448 Local->Data.Value = getSymbolValue(Symbol: MCSym, Layout);
449
450 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(Val: MCSym);
451 Local->Data.Type = SymbolCOFF.getType();
452 Local->Data.StorageClass = SymbolCOFF.getClass();
453
454 // If no storage class was specified in the streamer, define it here.
455 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
456 bool IsExternal =
457 MCSym.isExternal() || (!MCSym.getFragment() && !MCSym.isVariable());
458
459 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
460 : COFF::IMAGE_SYM_CLASS_STATIC;
461 }
462 }
463
464 Sym->MC = &MCSym;
465}
466
467void WinCOFFWriter::SetSectionName(COFFSection &S) {
468 if (S.Name.size() <= COFF::NameSize) {
469 std::memcpy(dest: S.Header.Name, src: S.Name.c_str(), n: S.Name.size());
470 return;
471 }
472
473 uint64_t StringTableEntry = Strings.getOffset(S: S.Name);
474 if (!COFF::encodeSectionName(Out: S.Header.Name, Offset: StringTableEntry))
475 report_fatal_error(reason: "COFF string table is greater than 64 GB.");
476}
477
478void WinCOFFWriter::SetSymbolName(COFFSymbol &S) {
479 if (S.Name.size() > COFF::NameSize)
480 S.set_name_offset(Strings.getOffset(S: S.Name));
481 else
482 std::memcpy(dest: S.Data.Name, src: S.Name.c_str(), n: S.Name.size());
483}
484
485bool WinCOFFWriter::IsPhysicalSection(COFFSection *S) {
486 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
487 0;
488}
489
490//------------------------------------------------------------------------------
491// entity writing methods
492
493void WinCOFFWriter::WriteFileHeader(const COFF::header &Header) {
494 if (UseBigObj) {
495 W.write<uint16_t>(Val: COFF::IMAGE_FILE_MACHINE_UNKNOWN);
496 W.write<uint16_t>(Val: 0xFFFF);
497 W.write<uint16_t>(Val: COFF::BigObjHeader::MinBigObjectVersion);
498 W.write<uint16_t>(Val: Header.Machine);
499 W.write<uint32_t>(Val: Header.TimeDateStamp);
500 W.OS.write(Ptr: COFF::BigObjMagic, Size: sizeof(COFF::BigObjMagic));
501 W.write<uint32_t>(Val: 0);
502 W.write<uint32_t>(Val: 0);
503 W.write<uint32_t>(Val: 0);
504 W.write<uint32_t>(Val: 0);
505 W.write<uint32_t>(Val: Header.NumberOfSections);
506 W.write<uint32_t>(Val: Header.PointerToSymbolTable);
507 W.write<uint32_t>(Val: Header.NumberOfSymbols);
508 } else {
509 W.write<uint16_t>(Val: Header.Machine);
510 W.write<uint16_t>(Val: static_cast<int16_t>(Header.NumberOfSections));
511 W.write<uint32_t>(Val: Header.TimeDateStamp);
512 W.write<uint32_t>(Val: Header.PointerToSymbolTable);
513 W.write<uint32_t>(Val: Header.NumberOfSymbols);
514 W.write<uint16_t>(Val: Header.SizeOfOptionalHeader);
515 W.write<uint16_t>(Val: Header.Characteristics);
516 }
517}
518
519void WinCOFFWriter::WriteSymbol(const COFFSymbol &S) {
520 W.OS.write(Ptr: S.Data.Name, Size: COFF::NameSize);
521 W.write<uint32_t>(Val: S.Data.Value);
522 if (UseBigObj)
523 W.write<uint32_t>(Val: S.Data.SectionNumber);
524 else
525 W.write<uint16_t>(Val: static_cast<int16_t>(S.Data.SectionNumber));
526 W.write<uint16_t>(Val: S.Data.Type);
527 W.OS << char(S.Data.StorageClass);
528 W.OS << char(S.Data.NumberOfAuxSymbols);
529 WriteAuxiliarySymbols(S: S.Aux);
530}
531
532void WinCOFFWriter::WriteAuxiliarySymbols(
533 const COFFSymbol::AuxiliarySymbols &S) {
534 for (const AuxSymbol &i : S) {
535 switch (i.AuxType) {
536 case ATWeakExternal:
537 W.write<uint32_t>(Val: i.Aux.WeakExternal.TagIndex);
538 W.write<uint32_t>(Val: i.Aux.WeakExternal.Characteristics);
539 W.OS.write_zeros(NumZeros: sizeof(i.Aux.WeakExternal.unused));
540 if (UseBigObj)
541 W.OS.write_zeros(NumZeros: COFF::Symbol32Size - COFF::Symbol16Size);
542 break;
543 case ATFile:
544 W.OS.write(Ptr: reinterpret_cast<const char *>(&i.Aux),
545 Size: UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size);
546 break;
547 case ATSectionDefinition:
548 W.write<uint32_t>(Val: i.Aux.SectionDefinition.Length);
549 W.write<uint16_t>(Val: i.Aux.SectionDefinition.NumberOfRelocations);
550 W.write<uint16_t>(Val: i.Aux.SectionDefinition.NumberOfLinenumbers);
551 W.write<uint32_t>(Val: i.Aux.SectionDefinition.CheckSum);
552 W.write<uint16_t>(Val: static_cast<int16_t>(i.Aux.SectionDefinition.Number));
553 W.OS << char(i.Aux.SectionDefinition.Selection);
554 W.OS.write_zeros(NumZeros: sizeof(i.Aux.SectionDefinition.unused));
555 W.write<uint16_t>(
556 Val: static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
557 if (UseBigObj)
558 W.OS.write_zeros(NumZeros: COFF::Symbol32Size - COFF::Symbol16Size);
559 break;
560 }
561 }
562}
563
564// Write the section header.
565void WinCOFFWriter::writeSectionHeaders() {
566 // Section numbers must be monotonically increasing in the section
567 // header, but our Sections array is not sorted by section number,
568 // so make a copy of Sections and sort it.
569 std::vector<COFFSection *> Arr;
570 for (auto &Section : Sections)
571 Arr.push_back(x: Section.get());
572 llvm::sort(C&: Arr, Comp: [](const COFFSection *A, const COFFSection *B) {
573 return A->Number < B->Number;
574 });
575
576 for (auto &Section : Arr) {
577 if (Section->Number == -1)
578 continue;
579
580 COFF::section &S = Section->Header;
581 if (Section->Relocations.size() >= 0xffff)
582 S.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
583 W.OS.write(Ptr: S.Name, Size: COFF::NameSize);
584 W.write<uint32_t>(Val: S.VirtualSize);
585 W.write<uint32_t>(Val: S.VirtualAddress);
586 W.write<uint32_t>(Val: S.SizeOfRawData);
587 W.write<uint32_t>(Val: S.PointerToRawData);
588 W.write<uint32_t>(Val: S.PointerToRelocations);
589 W.write<uint32_t>(Val: S.PointerToLineNumbers);
590 W.write<uint16_t>(Val: S.NumberOfRelocations);
591 W.write<uint16_t>(Val: S.NumberOfLineNumbers);
592 W.write<uint32_t>(Val: S.Characteristics);
593 }
594}
595
596void WinCOFFWriter::WriteRelocation(const COFF::relocation &R) {
597 W.write<uint32_t>(Val: R.VirtualAddress);
598 W.write<uint32_t>(Val: R.SymbolTableIndex);
599 W.write<uint16_t>(Val: R.Type);
600}
601
602// Write MCSec's contents. What this function does is essentially
603// "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
604// because it needs to compute a CRC.
605uint32_t WinCOFFWriter::writeSectionContents(MCAssembler &Asm,
606 const MCAsmLayout &Layout,
607 const MCSection &MCSec) {
608 // Save the contents of the section to a temporary buffer, we need this
609 // to CRC the data before we dump it into the object file.
610 SmallVector<char, 128> Buf;
611 raw_svector_ostream VecOS(Buf);
612 Asm.writeSectionData(OS&: VecOS, Section: &MCSec, Layout);
613
614 // Write the section contents to the object file.
615 W.OS << Buf;
616
617 // Calculate our CRC with an initial value of '0', this is not how
618 // JamCRC is specified but it aligns with the expected output.
619 JamCRC JC(/*Init=*/0);
620 JC.update(Data: ArrayRef(reinterpret_cast<uint8_t *>(Buf.data()), Buf.size()));
621 return JC.getCRC();
622}
623
624void WinCOFFWriter::writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
625 const COFFSection &Sec) {
626 if (Sec.Number == -1)
627 return;
628
629 // Write the section contents.
630 if (Sec.Header.PointerToRawData != 0) {
631 assert(W.OS.tell() == Sec.Header.PointerToRawData &&
632 "Section::PointerToRawData is insane!");
633
634 uint32_t CRC = writeSectionContents(Asm, Layout, MCSec: *Sec.MCSection);
635
636 // Update the section definition auxiliary symbol to record the CRC.
637 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec.Symbol->Aux;
638 assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
639 AuxSymbol &SecDef = AuxSyms[0];
640 SecDef.Aux.SectionDefinition.CheckSum = CRC;
641 }
642
643 // Write relocations for this section.
644 if (Sec.Relocations.empty()) {
645 assert(Sec.Header.PointerToRelocations == 0 &&
646 "Section::PointerToRelocations is insane!");
647 return;
648 }
649
650 assert(W.OS.tell() == Sec.Header.PointerToRelocations &&
651 "Section::PointerToRelocations is insane!");
652
653 if (Sec.Relocations.size() >= 0xffff) {
654 // In case of overflow, write actual relocation count as first
655 // relocation. Including the synthetic reloc itself (+ 1).
656 COFF::relocation R;
657 R.VirtualAddress = Sec.Relocations.size() + 1;
658 R.SymbolTableIndex = 0;
659 R.Type = 0;
660 WriteRelocation(R);
661 }
662
663 for (const auto &Relocation : Sec.Relocations)
664 WriteRelocation(R: Relocation.Data);
665}
666
667// Create .file symbols.
668void WinCOFFWriter::createFileSymbols(MCAssembler &Asm) {
669 for (const std::pair<std::string, size_t> &It : Asm.getFileNames()) {
670 // round up to calculate the number of auxiliary symbols required
671 const std::string &Name = It.first;
672 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
673 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
674
675 COFFSymbol *File = createSymbol(Name: ".file");
676 File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
677 File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
678 File->Aux.resize(N: Count);
679
680 unsigned Offset = 0;
681 unsigned Length = Name.size();
682 for (auto &Aux : File->Aux) {
683 Aux.AuxType = ATFile;
684
685 if (Length > SymbolSize) {
686 memcpy(dest: &Aux.Aux, src: Name.c_str() + Offset, n: SymbolSize);
687 Length = Length - SymbolSize;
688 } else {
689 memcpy(dest: &Aux.Aux, src: Name.c_str() + Offset, n: Length);
690 memset(s: (char *)&Aux.Aux + Length, c: 0, n: SymbolSize - Length);
691 break;
692 }
693
694 Offset += SymbolSize;
695 }
696 }
697}
698
699void WinCOFFWriter::setWeakDefaultNames() {
700 if (WeakDefaults.empty())
701 return;
702
703 // If multiple object files use a weak symbol (either with a regular
704 // defined default, or an absolute zero symbol as default), the defaults
705 // cause duplicate definitions unless their names are made unique. Look
706 // for a defined extern symbol, that isn't comdat - that should be unique
707 // unless there are other duplicate definitions. And if none is found,
708 // allow picking a comdat symbol, as that's still better than nothing.
709
710 COFFSymbol *Unique = nullptr;
711 for (bool AllowComdat : {false, true}) {
712 for (auto &Sym : Symbols) {
713 // Don't include the names of the defaults themselves
714 if (WeakDefaults.count(V: Sym.get()))
715 continue;
716 // Only consider external symbols
717 if (Sym->Data.StorageClass != COFF::IMAGE_SYM_CLASS_EXTERNAL)
718 continue;
719 // Only consider symbols defined in a section or that are absolute
720 if (!Sym->Section && Sym->Data.SectionNumber != COFF::IMAGE_SYM_ABSOLUTE)
721 continue;
722 if (!AllowComdat && Sym->Section &&
723 Sym->Section->Header.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
724 continue;
725 Unique = Sym.get();
726 break;
727 }
728 if (Unique)
729 break;
730 }
731 // If we didn't find any unique symbol to use for the names, just skip this.
732 if (!Unique)
733 return;
734 for (auto *Sym : WeakDefaults) {
735 Sym->Name.append(RHS: ".");
736 Sym->Name.append(RHS: Unique->Name);
737 }
738}
739
740static bool isAssociative(const COFFSection &Section) {
741 return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
742 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
743}
744
745void WinCOFFWriter::assignSectionNumbers() {
746 size_t I = 1;
747 auto Assign = [&](COFFSection &Section) {
748 Section.Number = I;
749 Section.Symbol->Data.SectionNumber = I;
750 Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I;
751 ++I;
752 };
753
754 // Although it is not explicitly requested by the Microsoft COFF spec,
755 // we should avoid emitting forward associative section references,
756 // because MSVC link.exe as of 2017 cannot handle that.
757 for (const std::unique_ptr<COFFSection> &Section : Sections)
758 if (!isAssociative(Section: *Section))
759 Assign(*Section);
760 for (const std::unique_ptr<COFFSection> &Section : Sections)
761 if (isAssociative(Section: *Section))
762 Assign(*Section);
763}
764
765// Assign file offsets to COFF object file structures.
766void WinCOFFWriter::assignFileOffsets(MCAssembler &Asm,
767 const MCAsmLayout &Layout) {
768 unsigned Offset = W.OS.tell();
769
770 Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
771 Offset += COFF::SectionSize * Header.NumberOfSections;
772
773 for (const auto &Section : Asm) {
774 COFFSection *Sec = SectionMap[&Section];
775
776 if (!Sec || Sec->Number == -1)
777 continue;
778
779 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(Sec: &Section);
780
781 if (IsPhysicalSection(S: Sec)) {
782 Sec->Header.PointerToRawData = Offset;
783 Offset += Sec->Header.SizeOfRawData;
784 }
785
786 if (!Sec->Relocations.empty()) {
787 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
788
789 if (RelocationsOverflow) {
790 // Signal overflow by setting NumberOfRelocations to max value. Actual
791 // size is found in reloc #0. Microsoft tools understand this.
792 Sec->Header.NumberOfRelocations = 0xffff;
793 } else {
794 Sec->Header.NumberOfRelocations = Sec->Relocations.size();
795 }
796 Sec->Header.PointerToRelocations = Offset;
797
798 if (RelocationsOverflow) {
799 // Reloc #0 will contain actual count, so make room for it.
800 Offset += COFF::RelocationSize;
801 }
802
803 Offset += COFF::RelocationSize * Sec->Relocations.size();
804
805 for (auto &Relocation : Sec->Relocations) {
806 assert(Relocation.Symb->getIndex() != -1);
807 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
808 }
809 }
810
811 assert(Sec->Symbol->Aux.size() == 1 &&
812 "Section's symbol must have one aux!");
813 AuxSymbol &Aux = Sec->Symbol->Aux[0];
814 assert(Aux.AuxType == ATSectionDefinition &&
815 "Section's symbol's aux symbol must be a Section Definition!");
816 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
817 Aux.Aux.SectionDefinition.NumberOfRelocations =
818 Sec->Header.NumberOfRelocations;
819 Aux.Aux.SectionDefinition.NumberOfLinenumbers =
820 Sec->Header.NumberOfLineNumbers;
821 }
822
823 Header.PointerToSymbolTable = Offset;
824}
825
826void WinCOFFWriter::reset() {
827 memset(s: &Header, c: 0, n: sizeof(Header));
828 Header.Machine = OWriter.TargetObjectWriter->getMachine();
829 Sections.clear();
830 Symbols.clear();
831 Strings.clear();
832 SectionMap.clear();
833 SymbolMap.clear();
834 WeakDefaults.clear();
835}
836
837void WinCOFFWriter::executePostLayoutBinding(MCAssembler &Asm,
838 const MCAsmLayout &Layout) {
839 // "Define" each section & symbol. This creates section & symbol
840 // entries in the staging area.
841 for (const auto &Section : Asm) {
842 if ((Mode == NonDwoOnly && isDwoSection(Sec: Section)) ||
843 (Mode == DwoOnly && !isDwoSection(Sec: Section)))
844 continue;
845 defineSection(MCSec: static_cast<const MCSectionCOFF &>(Section), Layout);
846 }
847
848 if (Mode != DwoOnly)
849 for (const MCSymbol &Symbol : Asm.symbols())
850 // Define non-temporary or temporary static (private-linkage) symbols
851 if (!Symbol.isTemporary() ||
852 cast<MCSymbolCOFF>(Val: Symbol).getClass() == COFF::IMAGE_SYM_CLASS_STATIC)
853 DefineSymbol(MCSym: Symbol, Assembler&: Asm, Layout);
854}
855
856void WinCOFFWriter::recordRelocation(MCAssembler &Asm,
857 const MCAsmLayout &Layout,
858 const MCFragment *Fragment,
859 const MCFixup &Fixup, MCValue Target,
860 uint64_t &FixedValue) {
861 assert(Target.getSymA() && "Relocation must reference a symbol!");
862
863 const MCSymbol &A = Target.getSymA()->getSymbol();
864 if (!A.isRegistered()) {
865 Asm.getContext().reportError(L: Fixup.getLoc(), Msg: Twine("symbol '") +
866 A.getName() +
867 "' can not be undefined");
868 return;
869 }
870 if (A.isTemporary() && A.isUndefined()) {
871 Asm.getContext().reportError(L: Fixup.getLoc(), Msg: Twine("assembler label '") +
872 A.getName() +
873 "' can not be undefined");
874 return;
875 }
876
877 MCSection *MCSec = Fragment->getParent();
878
879 // Mark this symbol as requiring an entry in the symbol table.
880 assert(SectionMap.contains(MCSec) &&
881 "Section must already have been defined in executePostLayoutBinding!");
882
883 COFFSection *Sec = SectionMap[MCSec];
884 const MCSymbolRefExpr *SymB = Target.getSymB();
885
886 if (SymB) {
887 const MCSymbol *B = &SymB->getSymbol();
888 if (!B->getFragment()) {
889 Asm.getContext().reportError(
890 L: Fixup.getLoc(),
891 Msg: Twine("symbol '") + B->getName() +
892 "' can not be undefined in a subtraction expression");
893 return;
894 }
895
896 // Offset of the symbol in the section
897 int64_t OffsetOfB = Layout.getSymbolOffset(S: *B);
898
899 // Offset of the relocation in the section
900 int64_t OffsetOfRelocation =
901 Layout.getFragmentOffset(F: Fragment) + Fixup.getOffset();
902
903 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
904 } else {
905 FixedValue = Target.getConstant();
906 }
907
908 COFFRelocation Reloc;
909
910 Reloc.Data.SymbolTableIndex = 0;
911 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(F: Fragment);
912
913 // Turn relocations for temporary symbols into section relocations.
914 if (A.isTemporary() && !SymbolMap[&A]) {
915 MCSection *TargetSection = &A.getSection();
916 assert(
917 SectionMap.contains(TargetSection) &&
918 "Section must already have been defined in executePostLayoutBinding!");
919 COFFSection *Section = SectionMap[TargetSection];
920 Reloc.Symb = Section->Symbol;
921 FixedValue += Layout.getSymbolOffset(S: A);
922 // Technically, we should do the final adjustments of FixedValue (below)
923 // before picking an offset symbol, otherwise we might choose one which
924 // is slightly too far away. The relocations where it really matters
925 // (arm64 adrp relocations) don't get any offset though.
926 if (UseOffsetLabels && !Section->OffsetSymbols.empty()) {
927 uint64_t LabelIndex = FixedValue >> OffsetLabelIntervalBits;
928 if (LabelIndex > 0) {
929 if (LabelIndex <= Section->OffsetSymbols.size())
930 Reloc.Symb = Section->OffsetSymbols[LabelIndex - 1];
931 else
932 Reloc.Symb = Section->OffsetSymbols.back();
933 FixedValue -= Reloc.Symb->Data.Value;
934 }
935 }
936 } else {
937 assert(
938 SymbolMap.contains(&A) &&
939 "Symbol must already have been defined in executePostLayoutBinding!");
940 Reloc.Symb = SymbolMap[&A];
941 }
942
943 ++Reloc.Symb->Relocations;
944
945 Reloc.Data.VirtualAddress += Fixup.getOffset();
946 Reloc.Data.Type = OWriter.TargetObjectWriter->getRelocType(
947 Ctx&: Asm.getContext(), Target, Fixup, IsCrossSection: SymB, MAB: Asm.getBackend());
948
949 // The *_REL32 relocations are relative to the end of the relocation,
950 // not to the start.
951 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
952 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
953 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
954 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32) ||
955 (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT &&
956 Reloc.Data.Type == COFF::IMAGE_REL_ARM_REL32) ||
957 (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARM64 &&
958 Reloc.Data.Type == COFF::IMAGE_REL_ARM64_REL32))
959 FixedValue += 4;
960
961 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
962 switch (Reloc.Data.Type) {
963 case COFF::IMAGE_REL_ARM_ABSOLUTE:
964 case COFF::IMAGE_REL_ARM_ADDR32:
965 case COFF::IMAGE_REL_ARM_ADDR32NB:
966 case COFF::IMAGE_REL_ARM_TOKEN:
967 case COFF::IMAGE_REL_ARM_SECTION:
968 case COFF::IMAGE_REL_ARM_SECREL:
969 break;
970 case COFF::IMAGE_REL_ARM_BRANCH11:
971 case COFF::IMAGE_REL_ARM_BLX11:
972 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
973 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
974 // for Windows CE).
975 case COFF::IMAGE_REL_ARM_BRANCH24:
976 case COFF::IMAGE_REL_ARM_BLX24:
977 case COFF::IMAGE_REL_ARM_MOV32A:
978 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
979 // only used for ARM mode code, which is documented as being unsupported
980 // by Windows on ARM. Empirical proof indicates that masm is able to
981 // generate the relocations however the rest of the MSVC toolchain is
982 // unable to handle it.
983 llvm_unreachable("unsupported relocation");
984 break;
985 case COFF::IMAGE_REL_ARM_MOV32T:
986 break;
987 case COFF::IMAGE_REL_ARM_BRANCH20T:
988 case COFF::IMAGE_REL_ARM_BRANCH24T:
989 case COFF::IMAGE_REL_ARM_BLX23T:
990 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
991 // perform a 4 byte adjustment to the relocation. Relative branches are
992 // offset by 4 on ARM, however, because there is no RELA relocations, all
993 // branches are offset by 4.
994 FixedValue = FixedValue + 4;
995 break;
996 }
997 }
998
999 // The fixed value never makes sense for section indices, ignore it.
1000 if (Fixup.getKind() == FK_SecRel_2)
1001 FixedValue = 0;
1002
1003 if (OWriter.TargetObjectWriter->recordRelocation(Fixup))
1004 Sec->Relocations.push_back(x: Reloc);
1005}
1006
1007static std::time_t getTime() {
1008 std::time_t Now = time(timer: nullptr);
1009 if (Now < 0 || !isUInt<32>(x: Now))
1010 return UINT32_MAX;
1011 return Now;
1012}
1013
1014uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm,
1015 const MCAsmLayout &Layout) {
1016 uint64_t StartOffset = W.OS.tell();
1017
1018 if (Sections.size() > INT32_MAX)
1019 report_fatal_error(
1020 reason: "PE COFF object files can't have more than 2147483647 sections");
1021
1022 UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
1023 Header.NumberOfSections = Sections.size();
1024 Header.NumberOfSymbols = 0;
1025
1026 setWeakDefaultNames();
1027 assignSectionNumbers();
1028 if (Mode != DwoOnly)
1029 createFileSymbols(Asm);
1030
1031 for (auto &Symbol : Symbols) {
1032 // Update section number & offset for symbols that have them.
1033 if (Symbol->Section)
1034 Symbol->Data.SectionNumber = Symbol->Section->Number;
1035 Symbol->setIndex(Header.NumberOfSymbols++);
1036 // Update auxiliary symbol info.
1037 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
1038 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
1039 }
1040
1041 // Build string table.
1042 for (const auto &S : Sections)
1043 if (S->Name.size() > COFF::NameSize)
1044 Strings.add(S: S->Name);
1045 for (const auto &S : Symbols)
1046 if (S->Name.size() > COFF::NameSize)
1047 Strings.add(S: S->Name);
1048 Strings.finalize();
1049
1050 // Set names.
1051 for (const auto &S : Sections)
1052 SetSectionName(*S);
1053 for (auto &S : Symbols)
1054 SetSymbolName(*S);
1055
1056 // Fixup weak external references.
1057 for (auto &Symbol : Symbols) {
1058 if (Symbol->Other) {
1059 assert(Symbol->getIndex() != -1);
1060 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
1061 assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
1062 "Symbol's aux symbol must be a Weak External!");
1063 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
1064 }
1065 }
1066
1067 // Fixup associative COMDAT sections.
1068 for (auto &Section : Sections) {
1069 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
1070 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1071 continue;
1072
1073 const MCSectionCOFF &MCSec = *Section->MCSection;
1074 const MCSymbol *AssocMCSym = MCSec.getCOMDATSymbol();
1075 assert(AssocMCSym);
1076
1077 // It's an error to try to associate with an undefined symbol or a symbol
1078 // without a section.
1079 if (!AssocMCSym->isInSection()) {
1080 Asm.getContext().reportError(
1081 L: SMLoc(), Msg: Twine("cannot make section ") + MCSec.getName() +
1082 Twine(" associative with sectionless symbol ") +
1083 AssocMCSym->getName());
1084 continue;
1085 }
1086
1087 const auto *AssocMCSec = cast<MCSectionCOFF>(Val: &AssocMCSym->getSection());
1088 assert(SectionMap.count(AssocMCSec));
1089 COFFSection *AssocSec = SectionMap[AssocMCSec];
1090
1091 // Skip this section if the associated section is unused.
1092 if (AssocSec->Number == -1)
1093 continue;
1094
1095 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = AssocSec->Number;
1096 }
1097
1098 // Create the contents of the .llvm_addrsig section.
1099 if (Mode != DwoOnly && OWriter.EmitAddrsigSection) {
1100 auto Frag = new MCDataFragment(AddrsigSection);
1101 Frag->setLayoutOrder(0);
1102 raw_svector_ostream OS(Frag->getContents());
1103 for (const MCSymbol *S : OWriter.AddrsigSyms) {
1104 if (!S->isRegistered())
1105 continue;
1106 if (!S->isTemporary()) {
1107 encodeULEB128(Value: S->getIndex(), OS);
1108 continue;
1109 }
1110
1111 MCSection *TargetSection = &S->getSection();
1112 assert(SectionMap.contains(TargetSection) &&
1113 "Section must already have been defined in "
1114 "executePostLayoutBinding!");
1115 encodeULEB128(Value: SectionMap[TargetSection]->Symbol->getIndex(), OS);
1116 }
1117 }
1118
1119 // Create the contents of the .llvm.call-graph-profile section.
1120 if (Mode != DwoOnly && CGProfileSection) {
1121 auto *Frag = new MCDataFragment(CGProfileSection);
1122 Frag->setLayoutOrder(0);
1123 raw_svector_ostream OS(Frag->getContents());
1124 for (const MCAssembler::CGProfileEntry &CGPE : Asm.CGProfile) {
1125 uint32_t FromIndex = CGPE.From->getSymbol().getIndex();
1126 uint32_t ToIndex = CGPE.To->getSymbol().getIndex();
1127 support::endian::write(os&: OS, value: FromIndex, endian: W.Endian);
1128 support::endian::write(os&: OS, value: ToIndex, endian: W.Endian);
1129 support::endian::write(os&: OS, value: CGPE.Count, endian: W.Endian);
1130 }
1131 }
1132
1133 assignFileOffsets(Asm, Layout);
1134
1135 // MS LINK expects to be able to use this timestamp to implement their
1136 // /INCREMENTAL feature.
1137 if (Asm.isIncrementalLinkerCompatible()) {
1138 Header.TimeDateStamp = getTime();
1139 } else {
1140 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
1141 Header.TimeDateStamp = 0;
1142 }
1143
1144 // Write it all to disk...
1145 WriteFileHeader(Header);
1146 writeSectionHeaders();
1147
1148#ifndef NDEBUG
1149 sections::iterator I = Sections.begin();
1150 sections::iterator IE = Sections.end();
1151 MCAssembler::iterator J = Asm.begin();
1152 MCAssembler::iterator JE = Asm.end();
1153 for (; I != IE && J != JE; ++I, ++J) {
1154 while (J != JE && ((Mode == NonDwoOnly && isDwoSection(Sec: *J)) ||
1155 (Mode == DwoOnly && !isDwoSection(Sec: *J))))
1156 ++J;
1157 assert(J != JE && (**I).MCSection == &*J && "Wrong bound MCSection");
1158 }
1159#endif
1160
1161 // Write section contents.
1162 for (std::unique_ptr<COFFSection> &Sec : Sections)
1163 writeSection(Asm, Layout, Sec: *Sec);
1164
1165 assert(W.OS.tell() == Header.PointerToSymbolTable &&
1166 "Header::PointerToSymbolTable is insane!");
1167
1168 // Write a symbol table.
1169 for (auto &Symbol : Symbols)
1170 if (Symbol->getIndex() != -1)
1171 WriteSymbol(S: *Symbol);
1172
1173 // Write a string table, which completes the entire COFF file.
1174 Strings.write(OS&: W.OS);
1175
1176 return W.OS.tell() - StartOffset;
1177}
1178
1179//------------------------------------------------------------------------------
1180// WinCOFFObjectWriter class implementation
1181
1182////////////////////////////////////////////////////////////////////////////////
1183// MCObjectWriter interface implementations
1184
1185void WinCOFFObjectWriter::reset() {
1186 ObjWriter->reset();
1187 if (DwoWriter)
1188 DwoWriter->reset();
1189 MCObjectWriter::reset();
1190}
1191
1192bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1193 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
1194 bool InSet, bool IsPCRel) const {
1195 // Don't drop relocations between functions, even if they are in the same text
1196 // section. Multiple Visual C++ linker features depend on having the
1197 // relocations present. The /INCREMENTAL flag will cause these relocations to
1198 // point to thunks, and the /GUARD:CF flag assumes that it can use relocations
1199 // to approximate the set of all address taken functions. LLD's implementation
1200 // of /GUARD:CF also relies on the existance of these relocations.
1201 uint16_t Type = cast<MCSymbolCOFF>(Val: SymA).getType();
1202 if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
1203 return false;
1204 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
1205 InSet, IsPCRel);
1206}
1207
1208void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
1209 const MCAsmLayout &Layout) {
1210 if (EmitAddrsigSection) {
1211 ObjWriter->AddrsigSection = Asm.getContext().getCOFFSection(
1212 Section: ".llvm_addrsig", Characteristics: COFF::IMAGE_SCN_LNK_REMOVE,
1213 Kind: SectionKind::getMetadata());
1214 Asm.registerSection(Section&: *ObjWriter->AddrsigSection);
1215 }
1216
1217 if (!Asm.CGProfile.empty()) {
1218 ObjWriter->CGProfileSection = Asm.getContext().getCOFFSection(
1219 Section: ".llvm.call-graph-profile", Characteristics: COFF::IMAGE_SCN_LNK_REMOVE,
1220 Kind: SectionKind::getMetadata());
1221 Asm.registerSection(Section&: *ObjWriter->CGProfileSection);
1222 }
1223
1224 ObjWriter->executePostLayoutBinding(Asm, Layout);
1225 if (DwoWriter)
1226 DwoWriter->executePostLayoutBinding(Asm, Layout);
1227}
1228
1229void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
1230 const MCAsmLayout &Layout,
1231 const MCFragment *Fragment,
1232 const MCFixup &Fixup, MCValue Target,
1233 uint64_t &FixedValue) {
1234 assert(!isDwoSection(*Fragment->getParent()) &&
1235 "No relocation in Dwo sections");
1236 ObjWriter->recordRelocation(Asm, Layout, Fragment, Fixup, Target, FixedValue);
1237}
1238
1239uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
1240 const MCAsmLayout &Layout) {
1241 uint64_t TotalSize = ObjWriter->writeObject(Asm, Layout);
1242 if (DwoWriter)
1243 TotalSize += DwoWriter->writeObject(Asm, Layout);
1244 return TotalSize;
1245}
1246
1247MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1248 : Machine(Machine_) {}
1249
1250// Pin the vtable to this file.
1251void MCWinCOFFObjectTargetWriter::anchor() {}
1252
1253//------------------------------------------------------------------------------
1254// WinCOFFObjectWriter factory function
1255
1256std::unique_ptr<MCObjectWriter> llvm::createWinCOFFObjectWriter(
1257 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) {
1258 return std::make_unique<WinCOFFObjectWriter>(args: std::move(MOTW), args&: OS);
1259}
1260
1261std::unique_ptr<MCObjectWriter> llvm::createWinCOFFDwoObjectWriter(
1262 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS,
1263 raw_pwrite_stream &DwoOS) {
1264 return std::make_unique<WinCOFFObjectWriter>(args: std::move(MOTW), args&: OS, args&: DwoOS);
1265}
1266

source code of llvm/lib/MC/WinCOFFObjectWriter.cpp