LLVM 19.0.0git
WinCOFFObjectWriter.cpp
Go to the documentation of this file.
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"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.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"
28#include "llvm/MC/MCSection.h"
30#include "llvm/MC/MCSymbol.h"
32#include "llvm/MC/MCValue.h"
35#include "llvm/Support/CRC.h"
39#include "llvm/Support/LEB128.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;
53
54#define DEBUG_TYPE "WinCOFFObjectWriter"
55
56namespace {
57
58constexpr int OffsetLabelIntervalBits = 20;
59
61
62enum AuxiliaryType { ATWeakExternal, ATFile, ATSectionDefinition };
63
64struct AuxSymbol {
65 AuxiliaryType AuxType;
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 {
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;
129
130 using symbols = std::vector<std::unique_ptr<COFFSymbol>>;
131 using sections = std::vector<std::unique_ptr<COFFSection>>;
132
135
136 using symbol_list = DenseSet<COFFSymbol *>;
137
138 // Root level file contents.
139 COFF::header Header = {};
140 sections Sections;
141 symbols Symbols;
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,
215 : TargetObjectWriter(std::move(MOTW)),
216 ObjWriter(std::make_unique<WinCOFFWriter>(*this, OS,
217 WinCOFFWriter::AllSections)) {
218 }
219 WinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
221 : TargetObjectWriter(std::move(MOTW)),
222 ObjWriter(std::make_unique<WinCOFFWriter>(*this, OS,
223 WinCOFFWriter::NonDwoOnly)),
224 DwoWriter(std::make_unique<WinCOFFWriter>(*this, DwoOS,
225 WinCOFFWriter::DwoOnly)) {}
226
227 // MCObjectWriter interface implementation.
228 void reset() override;
230 const MCAsmLayout &Layout) override;
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(".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(Data.Name + 0, 0);
255 write32le(Data.Name + 4, 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(std::make_unique<COFFSymbol>(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(Symbol->getName());
281 return Ret;
282}
283
284COFFSection *WinCOFFWriter::createSection(StringRef Name) {
285 Sections.emplace_back(std::make_unique<COFFSection>(Name));
286 return Sections.back().get();
287}
288
290 switch (Sec.getAlign().value()) {
291 case 1:
293 case 2:
295 case 4:
297 case 8:
299 case 16:
301 case 32:
303 case 64:
305 case 128:
307 case 256:
309 case 512:
311 case 1024:
313 case 2048:
315 case 4096:
317 case 8192:
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(MCSec.getName());
328 COFFSymbol *Symbol = createSymbol(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.
335 if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
336 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
337 if (COMDATSymbol->Section)
338 report_fatal_error("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(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(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(&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(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(Symbol, 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>(Symbol.getVariableValue());
390 if (!SymRef)
391 return nullptr;
392
393 const MCSymbol &Aliasee = SymRef->getSymbol();
394 if (Aliasee.isUndefined() || Aliasee.isExternal())
395 return GetOrCreateCOFFSymbol(&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(&MCSym);
405 const MCSymbol *Base = Layout.getBaseSymbol(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("conflicting sections for symbol");
411 }
412
413 COFFSymbol *Local = nullptr;
414 if (cast<MCSymbolCOFF>(MCSym).getWeakExternalCharacteristics()) {
415 Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
416 Sym->Section = nullptr;
417
418 COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
419 if (!WeakDefault) {
420 std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
421 WeakDefault = createSymbol(WeakName);
422 if (!Sec)
423 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
424 else
425 WeakDefault->Section = Sec;
426 WeakDefaults.insert(WeakDefault);
427 Local = WeakDefault;
428 }
429
430 Sym->Other = WeakDefault;
431
432 // Setup the Weak External auxiliary symbol.
433 Sym->Aux.resize(1);
434 memset(&Sym->Aux[0], 0, 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>(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(MCSym, Layout);
449
450 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(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
461 }
462 }
463
464 Sym->MC = &MCSym;
465}
466
467void WinCOFFWriter::SetSectionName(COFFSection &S) {
468 if (S.Name.size() <= COFF::NameSize) {
469 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
470 return;
471 }
472
473 uint64_t StringTableEntry = Strings.getOffset(S.Name);
474 if (!COFF::encodeSectionName(S.Header.Name, StringTableEntry))
475 report_fatal_error("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.Name));
481 else
482 std::memcpy(S.Data.Name, S.Name.c_str(), 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) {
496 W.write<uint16_t>(0xFFFF);
498 W.write<uint16_t>(Header.Machine);
499 W.write<uint32_t>(Header.TimeDateStamp);
500 W.OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic));
501 W.write<uint32_t>(0);
502 W.write<uint32_t>(0);
503 W.write<uint32_t>(0);
504 W.write<uint32_t>(0);
505 W.write<uint32_t>(Header.NumberOfSections);
506 W.write<uint32_t>(Header.PointerToSymbolTable);
507 W.write<uint32_t>(Header.NumberOfSymbols);
508 } else {
509 W.write<uint16_t>(Header.Machine);
510 W.write<uint16_t>(static_cast<int16_t>(Header.NumberOfSections));
511 W.write<uint32_t>(Header.TimeDateStamp);
512 W.write<uint32_t>(Header.PointerToSymbolTable);
513 W.write<uint32_t>(Header.NumberOfSymbols);
514 W.write<uint16_t>(Header.SizeOfOptionalHeader);
515 W.write<uint16_t>(Header.Characteristics);
516 }
517}
518
519void WinCOFFWriter::WriteSymbol(const COFFSymbol &S) {
520 W.OS.write(S.Data.Name, COFF::NameSize);
521 W.write<uint32_t>(S.Data.Value);
522 if (UseBigObj)
523 W.write<uint32_t>(S.Data.SectionNumber);
524 else
525 W.write<uint16_t>(static_cast<int16_t>(S.Data.SectionNumber));
526 W.write<uint16_t>(S.Data.Type);
527 W.OS << char(S.Data.StorageClass);
528 W.OS << char(S.Data.NumberOfAuxSymbols);
529 WriteAuxiliarySymbols(S.Aux);
530}
531
532void WinCOFFWriter::WriteAuxiliarySymbols(
534 for (const AuxSymbol &i : S) {
535 switch (i.AuxType) {
536 case ATWeakExternal:
537 W.write<uint32_t>(i.Aux.WeakExternal.TagIndex);
538 W.write<uint32_t>(i.Aux.WeakExternal.Characteristics);
539 W.OS.write_zeros(sizeof(i.Aux.WeakExternal.unused));
540 if (UseBigObj)
541 W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
542 break;
543 case ATFile:
544 W.OS.write(reinterpret_cast<const char *>(&i.Aux),
546 break;
547 case ATSectionDefinition:
548 W.write<uint32_t>(i.Aux.SectionDefinition.Length);
549 W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfRelocations);
550 W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfLinenumbers);
551 W.write<uint32_t>(i.Aux.SectionDefinition.CheckSum);
552 W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
553 W.OS << char(i.Aux.SectionDefinition.Selection);
554 W.OS.write_zeros(sizeof(i.Aux.SectionDefinition.unused));
555 W.write<uint16_t>(
556 static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
557 if (UseBigObj)
558 W.OS.write_zeros(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(Section.get());
572 llvm::sort(Arr, [](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)
583 W.OS.write(S.Name, COFF::NameSize);
584 W.write<uint32_t>(S.VirtualSize);
585 W.write<uint32_t>(S.VirtualAddress);
586 W.write<uint32_t>(S.SizeOfRawData);
587 W.write<uint32_t>(S.PointerToRawData);
592 W.write<uint32_t>(S.Characteristics);
593 }
594}
595
596void WinCOFFWriter::WriteRelocation(const COFF::relocation &R) {
597 W.write<uint32_t>(R.VirtualAddress);
598 W.write<uint32_t>(R.SymbolTableIndex);
599 W.write<uint16_t>(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.
611 raw_svector_ostream VecOS(Buf);
612 Asm.writeSectionData(VecOS, &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(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, *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).
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(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(".file");
676 File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
677 File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
678 File->Aux.resize(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(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
687 Length = Length - SymbolSize;
688 } else {
689 memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
690 memset((char *)&Aux.Aux + Length, 0, 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(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(".");
736 Sym->Name.append(Unique->Name);
737 }
738}
739
740static bool isAssociative(const COFFSection &Section) {
741 return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
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))
759 Assign(*Section);
760 for (const std::unique_ptr<COFFSection> &Section : Sections)
761 if (isAssociative(*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
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(&Section);
780
781 if (IsPhysicalSection(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.
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(&Header, 0, sizeof(Header));
828 Header.Machine = OWriter.TargetObjectWriter->getMachine();
829 Sections.clear();
830 Symbols.clear();
831 Strings.clear();
832 SectionMap.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(Section)) ||
843 (Mode == DwoOnly && !isDwoSection(Section)))
844 continue;
845 defineSection(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>(Symbol).getClass() == COFF::IMAGE_SYM_CLASS_STATIC)
853 DefineSymbol(Symbol, 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(Fixup.getLoc(), Twine("symbol '") +
866 A.getName() +
867 "' can not be undefined");
868 return;
869 }
870 if (A.isTemporary() && A.isUndefined()) {
871 Asm.getContext().reportError(Fixup.getLoc(), 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 Fixup.getLoc(),
891 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(*B);
898
899 // Offset of the relocation in the section
900 int64_t OffsetOfRelocation =
901 Layout.getFragmentOffset(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(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(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 Asm.getContext(), Target, Fixup, SymB, 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) {
969 break;
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).
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;
986 break;
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(Reloc);
1005}
1006
1007static std::time_t getTime() {
1008 std::time_t Now = time(nullptr);
1009 if (Now < 0 || !isUInt<32>(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)
1020 "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->Name);
1045 for (const auto &S : Symbols)
1046 if (S->Name.size() > COFF::NameSize)
1047 Strings.add(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 !=
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 SMLoc(), 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>(&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(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(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, FromIndex, W.Endian);
1128 support::endian::write(OS, ToIndex, W.Endian);
1129 support::endian::write(OS, CGPE.Count, 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(*J)) ||
1155 (Mode == DwoOnly && !isDwoSection(*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);
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(*Symbol);
1172
1173 // Write a string table, which completes the entire COFF file.
1174 Strings.write(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();
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>(SymA).getType();
1203 return false;
1205 InSet, IsPCRel);
1206}
1207
1208void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
1209 const MCAsmLayout &Layout) {
1210 if (EmitAddrsigSection) {
1211 ObjWriter->AddrsigSection = Asm.getContext().getCOFFSection(
1212 ".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE,
1214 Asm.registerSection(*ObjWriter->AddrsigSection);
1215 }
1216
1217 if (!Asm.CGProfile.empty()) {
1218 ObjWriter->CGProfileSection = Asm.getContext().getCOFFSection(
1219 ".llvm.call-graph-profile", COFF::IMAGE_SCN_LNK_REMOVE,
1221 Asm.registerSection(*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
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>(std::move(MOTW), 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>(std::move(MOTW), OS, DwoOS);
1265}
bbsections Prepares for basic block sections
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
COFFYAML::AuxSymbolType AuxType
Definition: COFFYAML.cpp:353
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:371
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
std::string Name
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1289
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define I(x, y, z)
Definition: MD5.cpp:58
PowerPC TLS Dynamic Call Fixup
uint32_t Number
Definition: Profile.cpp:47
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char * name
Definition: SMEABIPass.cpp:49
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
This file defines the SmallString class.
This file defines the SmallVector class.
static uint32_t getAlignment(const MCSectionCOFF &Sec)
static bool isAssociative(const COFFSection &Section)
static uint64_t getSymbolValue(const MCSymbol &Symbol, const MCAsmLayout &Layout)
static bool isDwoSection(const MCSection &Sec)
static std::time_t getTime()
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition: DenseMap.h:145
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Interval Class - An Interval is a set of nodes defined such that every node in the interval has all o...
Definition: Interval.h:36
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
const MCSymbol * getBaseSymbol(const MCSymbol &Symbol) const
If this symbol is equivalent to A + Constant, return A.
Definition: MCFragment.cpp:162
uint64_t getSectionAddressSize(const MCSection *Sec) const
Get the address space size of the given section, as it effects layout.
Definition: MCFragment.cpp:198
bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
Get the offset of the given symbol, as computed in the current layout.
Definition: MCFragment.cpp:152
uint64_t getFragmentOffset(const MCFragment *F) const
Get the offset of the given fragment inside its containing section.
Definition: MCFragment.cpp:96
Fragment for data and encoded instructions.
Definition: MCFragment.h:242
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
MCSection * getParent() const
Definition: MCFragment.h:96
Defines the object file and target independent interfaces used by the assembler backend to write nati...
virtual uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout)=0
Write the object file and returns the number of bytes written.
virtual void executePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout)=0
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
virtual void reset()
lifetime management
virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, const MCSymbol &A, const MCSymbol &B, bool InSet) const
virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue)=0
Record a relocation entry.
This represents a section on Windows.
Definition: MCSectionCOFF.h:26
MCSymbol * getCOMDATSymbol() const
Definition: MCSectionCOFF.h:67
unsigned getCharacteristics() const
Definition: MCSectionCOFF.h:66
int getSelection() const
Definition: MCSectionCOFF.h:68
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
MCSection::FragmentListType & getFragmentList()
Definition: MCSection.h:172
Align getAlign() const
Definition: MCSection.h:140
StringRef getName() const
Definition: MCSection.h:124
uint16_t getType() const
Definition: MCSymbolCOFF.h:35
uint16_t getClass() const
Definition: MCSymbolCOFF.h:42
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
const MCSymbol & getSymbol() const
Definition: MCExpr.h:402
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
Definition: MCSymbol.h:254
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition: MCSymbol.h:300
bool isRegistered() const
Definition: MCSymbol.h:212
void setIndex(uint32_t Value) const
Set the (implementation defined) index.
Definition: MCSymbol.h:321
bool isUndefined(bool SetUsed=true) const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
Definition: MCSymbol.h:259
uint32_t getIndex() const
Get the (implementation defined) index.
Definition: MCSymbol.h:316
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:269
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition: MCSymbol.h:222
bool isExternal() const
Definition: MCSymbol.h:406
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:397
This represents an "assembler immediate".
Definition: MCValue.h:36
Represents a location in source code.
Definition: SMLoc.h:23
static SectionKind getMetadata()
Definition: SectionKind.h:188
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
size_t size() const
Definition: SmallVector.h:91
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:299
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:271
Utility for building string tables with deduplicated suffixes.
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:444
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:690
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ IMAGE_FILE_MACHINE_ARM64
Definition: COFF.h:100
@ IMAGE_FILE_MACHINE_UNKNOWN
Definition: COFF.h:95
@ IMAGE_FILE_MACHINE_AMD64
Definition: COFF.h:97
@ IMAGE_FILE_MACHINE_I386
Definition: COFF.h:104
@ IMAGE_FILE_MACHINE_ARMNT
Definition: COFF.h:99
@ IMAGE_SCN_ALIGN_64BYTES
Definition: COFF.h:320
@ IMAGE_SCN_ALIGN_128BYTES
Definition: COFF.h:321
@ IMAGE_SCN_ALIGN_256BYTES
Definition: COFF.h:322
@ IMAGE_SCN_ALIGN_1024BYTES
Definition: COFF.h:324
@ IMAGE_SCN_ALIGN_1BYTES
Definition: COFF.h:314
@ IMAGE_SCN_LNK_REMOVE
Definition: COFF.h:307
@ IMAGE_SCN_ALIGN_512BYTES
Definition: COFF.h:323
@ IMAGE_SCN_CNT_UNINITIALIZED_DATA
Definition: COFF.h:304
@ IMAGE_SCN_ALIGN_4096BYTES
Definition: COFF.h:326
@ IMAGE_SCN_ALIGN_8192BYTES
Definition: COFF.h:327
@ IMAGE_SCN_LNK_NRELOC_OVFL
Definition: COFF.h:329
@ IMAGE_SCN_ALIGN_16BYTES
Definition: COFF.h:318
@ IMAGE_SCN_LNK_COMDAT
Definition: COFF.h:308
@ IMAGE_SCN_ALIGN_8BYTES
Definition: COFF.h:317
@ IMAGE_SCN_ALIGN_4BYTES
Definition: COFF.h:316
@ IMAGE_SCN_ALIGN_32BYTES
Definition: COFF.h:319
@ IMAGE_SCN_ALIGN_2BYTES
Definition: COFF.h:315
@ IMAGE_SCN_ALIGN_2048BYTES
Definition: COFF.h:325
@ NameSize
Definition: COFF.h:57
@ Header16Size
Definition: COFF.h:55
@ Symbol16Size
Definition: COFF.h:58
@ Header32Size
Definition: COFF.h:56
@ SectionSize
Definition: COFF.h:60
@ Symbol32Size
Definition: COFF.h:59
@ RelocationSize
Definition: COFF.h:61
@ IMAGE_REL_ARM64_REL32
Definition: COFF.h:417
@ IMAGE_REL_AMD64_REL32
Definition: COFF.h:364
@ IMAGE_SYM_CLASS_EXTERNAL
External symbol.
Definition: COFF.h:223
@ IMAGE_SYM_CLASS_LABEL
Label.
Definition: COFF.h:227
@ IMAGE_SYM_CLASS_FILE
File name.
Definition: COFF.h:245
@ IMAGE_SYM_CLASS_NULL
No symbol.
Definition: COFF.h:221
@ IMAGE_SYM_CLASS_WEAK_EXTERNAL
Duplicate tag.
Definition: COFF.h:248
@ IMAGE_SYM_CLASS_STATIC
Static.
Definition: COFF.h:224
bool encodeSectionName(char *Out, uint64_t Offset)
Encode section name based on string table offset.
Definition: COFF.cpp:39
@ IMAGE_COMDAT_SELECT_ASSOCIATIVE
Definition: COFF.h:425
@ IMAGE_REL_ARM_MOV32A
Definition: COFF.h:391
@ IMAGE_REL_ARM_BRANCH20T
Definition: COFF.h:393
@ IMAGE_REL_ARM_BRANCH24
Definition: COFF.h:383
@ IMAGE_REL_ARM_ADDR32NB
Definition: COFF.h:382
@ IMAGE_REL_ARM_BRANCH11
Definition: COFF.h:384
@ IMAGE_REL_ARM_BLX24
Definition: COFF.h:386
@ IMAGE_REL_ARM_ADDR32
Definition: COFF.h:381
@ IMAGE_REL_ARM_MOV32T
Definition: COFF.h:392
@ IMAGE_REL_ARM_BRANCH24T
Definition: COFF.h:394
@ IMAGE_REL_ARM_ABSOLUTE
Definition: COFF.h:380
@ IMAGE_REL_ARM_REL32
Definition: COFF.h:388
@ IMAGE_REL_ARM_BLX23T
Definition: COFF.h:395
@ IMAGE_REL_ARM_SECREL
Definition: COFF.h:390
@ IMAGE_REL_ARM_SECTION
Definition: COFF.h:389
@ IMAGE_REL_ARM_BLX11
Definition: COFF.h:387
@ IMAGE_REL_ARM_TOKEN
Definition: COFF.h:385
const int32_t MaxNumberOfSections16
Definition: COFF.h:32
@ IMAGE_REL_I386_REL32
Definition: COFF.h:356
static const char BigObjMagic[]
Definition: COFF.h:37
@ IMAGE_SYM_DEBUG
Definition: COFF.h:211
@ IMAGE_SYM_ABSOLUTE
Definition: COFF.h:212
@ IMAGE_SYM_DTYPE_FUNCTION
A function that returns a base type.
Definition: COFF.h:275
@ SCT_COMPLEX_TYPE_SHIFT
Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT))
Definition: COFF.h:279
DenseMap< SymbolStringPtr, ExecutorSymbolDef > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
Definition: Core.h:121
void write32le(void *P, uint32_t V)
Definition: Endian.h:452
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition: Endian.h:91
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
@ Length
Definition: DWP.cpp:456
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1689
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1656
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
std::unique_ptr< MCObjectWriter > createWinCOFFDwoObjectWriter(std::unique_ptr< MCWinCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS)
@ FK_SecRel_2
A two-byte section relative fixup.
Definition: MCFixup.h:41
std::unique_ptr< MCObjectWriter > createWinCOFFObjectWriter(std::unique_ptr< MCWinCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Win COFF writer instance.
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80
endianness
Definition: bit.h:70
#define N
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
uint32_t VirtualSize
Definition: COFF.h:286
uint32_t PointerToRelocations
Definition: COFF.h:290
uint16_t NumberOfLineNumbers
Definition: COFF.h:293
uint32_t PointerToRawData
Definition: COFF.h:289
uint32_t SizeOfRawData
Definition: COFF.h:288
uint32_t Characteristics
Definition: COFF.h:294
uint16_t NumberOfRelocations
Definition: COFF.h:292
char Name[NameSize]
Definition: COFF.h:285
uint32_t VirtualAddress
Definition: COFF.h:287
uint32_t PointerToLineNumbers
Definition: COFF.h:291
const MCSymbolRefExpr * From
Definition: MCAssembler.h:471
const MCSymbolRefExpr * To
Definition: MCAssembler.h:472
An iterator type that allows iterating over the pointees via some other iterator.
Definition: iterator.h:324
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:67