1 //===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains an implementation of a Win32 COFF object file writer.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/BinaryFormat/COFF.h"
20 #include "llvm/MC/MCAsmLayout.h"
21 #include "llvm/MC/MCAssembler.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCFixup.h"
25 #include "llvm/MC/MCFragment.h"
26 #include "llvm/MC/MCObjectWriter.h"
27 #include "llvm/MC/MCSection.h"
28 #include "llvm/MC/MCSectionCOFF.h"
29 #include "llvm/MC/MCSymbol.h"
30 #include "llvm/MC/MCSymbolCOFF.h"
31 #include "llvm/MC/MCValue.h"
32 #include "llvm/MC/MCWinCOFFObjectWriter.h"
33 #include "llvm/MC/StringTableBuilder.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/Endian.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/JamCRC.h"
38 #include "llvm/Support/LEB128.h"
39 #include "llvm/Support/MathExtras.h"
40 #include "llvm/Support/raw_ostream.h"
52 using llvm::support::endian::write32le
;
54 #define DEBUG_TYPE "WinCOFFObjectWriter"
58 using name
= SmallString
<COFF::NameSize
>;
67 AuxiliaryType AuxType
;
75 COFF::symbol Data
= {};
77 using AuxiliarySymbols
= SmallVector
<AuxSymbol
, 1>;
82 COFFSymbol
*Other
= nullptr;
83 COFFSection
*Section
= nullptr;
85 const MCSymbol
*MC
= nullptr;
87 COFFSymbol(StringRef Name
) : Name(Name
) {}
89 void set_name_offset(uint32_t Offset
);
91 int64_t getIndex() const { return Index
; }
92 void setIndex(int Value
) {
95 MC
->setIndex(static_cast<uint32_t>(Value
));
99 // This class contains staging data for a COFF relocation entry.
100 struct COFFRelocation
{
101 COFF::relocation Data
;
102 COFFSymbol
*Symb
= nullptr;
104 COFFRelocation() = default;
106 static size_t size() { return COFF::RelocationSize
; }
109 using relocations
= std::vector
<COFFRelocation
>;
113 COFF::section Header
= {};
117 MCSectionCOFF
const *MCSection
= nullptr;
118 COFFSymbol
*Symbol
= nullptr;
119 relocations Relocations
;
121 COFFSection(StringRef Name
) : Name(Name
) {}
124 class WinCOFFObjectWriter
: public MCObjectWriter
{
126 support::endian::Writer W
;
128 using symbols
= std::vector
<std::unique_ptr
<COFFSymbol
>>;
129 using sections
= std::vector
<std::unique_ptr
<COFFSection
>>;
131 using symbol_map
= DenseMap
<MCSymbol
const *, COFFSymbol
*>;
132 using section_map
= DenseMap
<MCSection
const *, COFFSection
*>;
134 std::unique_ptr
<MCWinCOFFObjectTargetWriter
> TargetObjectWriter
;
136 // Root level file contents.
137 COFF::header Header
= {};
140 StringTableBuilder Strings
{StringTableBuilder::WinCOFF
};
142 // Maps used during object file creation.
143 section_map SectionMap
;
144 symbol_map SymbolMap
;
148 bool EmitAddrsigSection
= false;
149 MCSectionCOFF
*AddrsigSection
;
150 std::vector
<const MCSymbol
*> AddrsigSyms
;
152 WinCOFFObjectWriter(std::unique_ptr
<MCWinCOFFObjectTargetWriter
> MOTW
,
153 raw_pwrite_stream
&OS
);
155 void reset() override
{
156 memset(&Header
, 0, sizeof(Header
));
157 Header
.Machine
= TargetObjectWriter
->getMachine();
163 MCObjectWriter::reset();
166 COFFSymbol
*createSymbol(StringRef Name
);
167 COFFSymbol
*GetOrCreateCOFFSymbol(const MCSymbol
*Symbol
);
168 COFFSection
*createSection(StringRef Name
);
170 void defineSection(MCSectionCOFF
const &Sec
);
172 COFFSymbol
*getLinkedSymbol(const MCSymbol
&Symbol
);
173 void DefineSymbol(const MCSymbol
&Symbol
, MCAssembler
&Assembler
,
174 const MCAsmLayout
&Layout
);
176 void SetSymbolName(COFFSymbol
&S
);
177 void SetSectionName(COFFSection
&S
);
179 bool IsPhysicalSection(COFFSection
*S
);
181 // Entity writing methods.
183 void WriteFileHeader(const COFF::header
&Header
);
184 void WriteSymbol(const COFFSymbol
&S
);
185 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols
&S
);
186 void writeSectionHeaders();
187 void WriteRelocation(const COFF::relocation
&R
);
188 uint32_t writeSectionContents(MCAssembler
&Asm
, const MCAsmLayout
&Layout
,
189 const MCSection
&MCSec
);
190 void writeSection(MCAssembler
&Asm
, const MCAsmLayout
&Layout
,
191 const COFFSection
&Sec
, const MCSection
&MCSec
);
193 // MCObjectWriter interface implementation.
195 void executePostLayoutBinding(MCAssembler
&Asm
,
196 const MCAsmLayout
&Layout
) override
;
198 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler
&Asm
,
199 const MCSymbol
&SymA
,
200 const MCFragment
&FB
, bool InSet
,
201 bool IsPCRel
) const override
;
203 void recordRelocation(MCAssembler
&Asm
, const MCAsmLayout
&Layout
,
204 const MCFragment
*Fragment
, const MCFixup
&Fixup
,
205 MCValue Target
, uint64_t &FixedValue
) override
;
207 void createFileSymbols(MCAssembler
&Asm
);
208 void assignSectionNumbers();
209 void assignFileOffsets(MCAssembler
&Asm
, const MCAsmLayout
&Layout
);
211 void emitAddrsigSection() override
{ EmitAddrsigSection
= true; }
212 void addAddrsigSymbol(const MCSymbol
*Sym
) override
{
213 AddrsigSyms
.push_back(Sym
);
216 uint64_t writeObject(MCAssembler
&Asm
, const MCAsmLayout
&Layout
) override
;
219 } // end anonymous namespace
221 //------------------------------------------------------------------------------
222 // Symbol class implementation
224 // In the case that the name does not fit within 8 bytes, the offset
225 // into the string table is stored in the last 4 bytes instead, leaving
226 // the first 4 bytes as 0.
227 void COFFSymbol::set_name_offset(uint32_t Offset
) {
228 write32le(Data
.Name
+ 0, 0);
229 write32le(Data
.Name
+ 4, Offset
);
232 //------------------------------------------------------------------------------
233 // WinCOFFObjectWriter class implementation
235 WinCOFFObjectWriter::WinCOFFObjectWriter(
236 std::unique_ptr
<MCWinCOFFObjectTargetWriter
> MOTW
, raw_pwrite_stream
&OS
)
237 : W(OS
, support::little
), TargetObjectWriter(std::move(MOTW
)) {
238 Header
.Machine
= TargetObjectWriter
->getMachine();
241 COFFSymbol
*WinCOFFObjectWriter::createSymbol(StringRef Name
) {
242 Symbols
.push_back(std::make_unique
<COFFSymbol
>(Name
));
243 return Symbols
.back().get();
246 COFFSymbol
*WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol
*Symbol
) {
247 COFFSymbol
*&Ret
= SymbolMap
[Symbol
];
249 Ret
= createSymbol(Symbol
->getName());
253 COFFSection
*WinCOFFObjectWriter::createSection(StringRef Name
) {
254 Sections
.emplace_back(std::make_unique
<COFFSection
>(Name
));
255 return Sections
.back().get();
258 static uint32_t getAlignment(const MCSectionCOFF
&Sec
) {
259 switch (Sec
.getAlignment()) {
261 return COFF::IMAGE_SCN_ALIGN_1BYTES
;
263 return COFF::IMAGE_SCN_ALIGN_2BYTES
;
265 return COFF::IMAGE_SCN_ALIGN_4BYTES
;
267 return COFF::IMAGE_SCN_ALIGN_8BYTES
;
269 return COFF::IMAGE_SCN_ALIGN_16BYTES
;
271 return COFF::IMAGE_SCN_ALIGN_32BYTES
;
273 return COFF::IMAGE_SCN_ALIGN_64BYTES
;
275 return COFF::IMAGE_SCN_ALIGN_128BYTES
;
277 return COFF::IMAGE_SCN_ALIGN_256BYTES
;
279 return COFF::IMAGE_SCN_ALIGN_512BYTES
;
281 return COFF::IMAGE_SCN_ALIGN_1024BYTES
;
283 return COFF::IMAGE_SCN_ALIGN_2048BYTES
;
285 return COFF::IMAGE_SCN_ALIGN_4096BYTES
;
287 return COFF::IMAGE_SCN_ALIGN_8192BYTES
;
289 llvm_unreachable("unsupported section alignment");
292 /// This function takes a section data object from the assembler
293 /// and creates the associated COFF section staging object.
294 void WinCOFFObjectWriter::defineSection(const MCSectionCOFF
&MCSec
) {
295 COFFSection
*Section
= createSection(MCSec
.getSectionName());
296 COFFSymbol
*Symbol
= createSymbol(MCSec
.getSectionName());
297 Section
->Symbol
= Symbol
;
298 Symbol
->Section
= Section
;
299 Symbol
->Data
.StorageClass
= COFF::IMAGE_SYM_CLASS_STATIC
;
301 // Create a COMDAT symbol if needed.
302 if (MCSec
.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
) {
303 if (const MCSymbol
*S
= MCSec
.getCOMDATSymbol()) {
304 COFFSymbol
*COMDATSymbol
= GetOrCreateCOFFSymbol(S
);
305 if (COMDATSymbol
->Section
)
306 report_fatal_error("two sections have the same comdat");
307 COMDATSymbol
->Section
= Section
;
311 // In this case the auxiliary symbol is a Section Definition.
312 Symbol
->Aux
.resize(1);
314 Symbol
->Aux
[0].AuxType
= ATSectionDefinition
;
315 Symbol
->Aux
[0].Aux
.SectionDefinition
.Selection
= MCSec
.getSelection();
317 // Set section alignment.
318 Section
->Header
.Characteristics
= MCSec
.getCharacteristics();
319 Section
->Header
.Characteristics
|= getAlignment(MCSec
);
321 // Bind internal COFF section to MC section.
322 Section
->MCSection
= &MCSec
;
323 SectionMap
[&MCSec
] = Section
;
326 static uint64_t getSymbolValue(const MCSymbol
&Symbol
,
327 const MCAsmLayout
&Layout
) {
328 if (Symbol
.isCommon() && Symbol
.isExternal())
329 return Symbol
.getCommonSize();
332 if (!Layout
.getSymbolOffset(Symbol
, Res
))
338 COFFSymbol
*WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol
&Symbol
) {
339 if (!Symbol
.isVariable())
342 const MCSymbolRefExpr
*SymRef
=
343 dyn_cast
<MCSymbolRefExpr
>(Symbol
.getVariableValue());
347 const MCSymbol
&Aliasee
= SymRef
->getSymbol();
348 if (!Aliasee
.isUndefined())
350 return GetOrCreateCOFFSymbol(&Aliasee
);
353 /// This function takes a symbol data object from the assembler
354 /// and creates the associated COFF symbol staging object.
355 void WinCOFFObjectWriter::DefineSymbol(const MCSymbol
&MCSym
,
356 MCAssembler
&Assembler
,
357 const MCAsmLayout
&Layout
) {
358 COFFSymbol
*Sym
= GetOrCreateCOFFSymbol(&MCSym
);
359 const MCSymbol
*Base
= Layout
.getBaseSymbol(MCSym
);
360 COFFSection
*Sec
= nullptr;
361 if (Base
&& Base
->getFragment()) {
362 Sec
= SectionMap
[Base
->getFragment()->getParent()];
363 if (Sym
->Section
&& Sym
->Section
!= Sec
)
364 report_fatal_error("conflicting sections for symbol");
367 COFFSymbol
*Local
= nullptr;
368 if (cast
<MCSymbolCOFF
>(MCSym
).isWeakExternal()) {
369 Sym
->Data
.StorageClass
= COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL
;
371 COFFSymbol
*WeakDefault
= getLinkedSymbol(MCSym
);
373 std::string WeakName
= (".weak." + MCSym
.getName() + ".default").str();
374 WeakDefault
= createSymbol(WeakName
);
376 WeakDefault
->Data
.SectionNumber
= COFF::IMAGE_SYM_ABSOLUTE
;
378 WeakDefault
->Section
= Sec
;
382 Sym
->Other
= WeakDefault
;
384 // Setup the Weak External auxiliary symbol.
386 memset(&Sym
->Aux
[0], 0, sizeof(Sym
->Aux
[0]));
387 Sym
->Aux
[0].AuxType
= ATWeakExternal
;
388 Sym
->Aux
[0].Aux
.WeakExternal
.TagIndex
= 0;
389 Sym
->Aux
[0].Aux
.WeakExternal
.Characteristics
=
390 COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS
;
393 Sym
->Data
.SectionNumber
= COFF::IMAGE_SYM_ABSOLUTE
;
400 Local
->Data
.Value
= getSymbolValue(MCSym
, Layout
);
402 const MCSymbolCOFF
&SymbolCOFF
= cast
<MCSymbolCOFF
>(MCSym
);
403 Local
->Data
.Type
= SymbolCOFF
.getType();
404 Local
->Data
.StorageClass
= SymbolCOFF
.getClass();
406 // If no storage class was specified in the streamer, define it here.
407 if (Local
->Data
.StorageClass
== COFF::IMAGE_SYM_CLASS_NULL
) {
408 bool IsExternal
= MCSym
.isExternal() ||
409 (!MCSym
.getFragment() && !MCSym
.isVariable());
411 Local
->Data
.StorageClass
= IsExternal
? COFF::IMAGE_SYM_CLASS_EXTERNAL
412 : COFF::IMAGE_SYM_CLASS_STATIC
;
419 // Maximum offsets for different string table entry encodings.
420 enum : unsigned { Max7DecimalOffset
= 9999999U };
421 enum : uint64_t { MaxBase64Offset
= 0xFFFFFFFFFULL
}; // 64^6, including 0
423 // Encode a string table entry offset in base 64, padded to 6 chars, and
424 // prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
425 // Buffer must be at least 8 bytes large. No terminating null appended.
426 static void encodeBase64StringEntry(char *Buffer
, uint64_t Value
) {
427 assert(Value
> Max7DecimalOffset
&& Value
<= MaxBase64Offset
&&
428 "Illegal section name encoding for value");
430 static const char Alphabet
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
431 "abcdefghijklmnopqrstuvwxyz"
437 char *Ptr
= Buffer
+ 7;
438 for (unsigned i
= 0; i
< 6; ++i
) {
439 unsigned Rem
= Value
% 64;
441 *(Ptr
--) = Alphabet
[Rem
];
445 void WinCOFFObjectWriter::SetSectionName(COFFSection
&S
) {
446 if (S
.Name
.size() <= COFF::NameSize
) {
447 std::memcpy(S
.Header
.Name
, S
.Name
.c_str(), S
.Name
.size());
451 uint64_t StringTableEntry
= Strings
.getOffset(S
.Name
);
452 if (StringTableEntry
<= Max7DecimalOffset
) {
453 SmallVector
<char, COFF::NameSize
> Buffer
;
454 Twine('/').concat(Twine(StringTableEntry
)).toVector(Buffer
);
455 assert(Buffer
.size() <= COFF::NameSize
&& Buffer
.size() >= 2);
456 std::memcpy(S
.Header
.Name
, Buffer
.data(), Buffer
.size());
459 if (StringTableEntry
<= MaxBase64Offset
) {
460 // Starting with 10,000,000, offsets are encoded as base64.
461 encodeBase64StringEntry(S
.Header
.Name
, StringTableEntry
);
464 report_fatal_error("COFF string table is greater than 64 GB.");
467 void WinCOFFObjectWriter::SetSymbolName(COFFSymbol
&S
) {
468 if (S
.Name
.size() > COFF::NameSize
)
469 S
.set_name_offset(Strings
.getOffset(S
.Name
));
471 std::memcpy(S
.Data
.Name
, S
.Name
.c_str(), S
.Name
.size());
474 bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection
*S
) {
475 return (S
->Header
.Characteristics
& COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
) ==
479 //------------------------------------------------------------------------------
480 // entity writing methods
482 void WinCOFFObjectWriter::WriteFileHeader(const COFF::header
&Header
) {
484 W
.write
<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN
);
485 W
.write
<uint16_t>(0xFFFF);
486 W
.write
<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion
);
487 W
.write
<uint16_t>(Header
.Machine
);
488 W
.write
<uint32_t>(Header
.TimeDateStamp
);
489 W
.OS
.write(COFF::BigObjMagic
, sizeof(COFF::BigObjMagic
));
490 W
.write
<uint32_t>(0);
491 W
.write
<uint32_t>(0);
492 W
.write
<uint32_t>(0);
493 W
.write
<uint32_t>(0);
494 W
.write
<uint32_t>(Header
.NumberOfSections
);
495 W
.write
<uint32_t>(Header
.PointerToSymbolTable
);
496 W
.write
<uint32_t>(Header
.NumberOfSymbols
);
498 W
.write
<uint16_t>(Header
.Machine
);
499 W
.write
<uint16_t>(static_cast<int16_t>(Header
.NumberOfSections
));
500 W
.write
<uint32_t>(Header
.TimeDateStamp
);
501 W
.write
<uint32_t>(Header
.PointerToSymbolTable
);
502 W
.write
<uint32_t>(Header
.NumberOfSymbols
);
503 W
.write
<uint16_t>(Header
.SizeOfOptionalHeader
);
504 W
.write
<uint16_t>(Header
.Characteristics
);
508 void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol
&S
) {
509 W
.OS
.write(S
.Data
.Name
, COFF::NameSize
);
510 W
.write
<uint32_t>(S
.Data
.Value
);
512 W
.write
<uint32_t>(S
.Data
.SectionNumber
);
514 W
.write
<uint16_t>(static_cast<int16_t>(S
.Data
.SectionNumber
));
515 W
.write
<uint16_t>(S
.Data
.Type
);
516 W
.OS
<< char(S
.Data
.StorageClass
);
517 W
.OS
<< char(S
.Data
.NumberOfAuxSymbols
);
518 WriteAuxiliarySymbols(S
.Aux
);
521 void WinCOFFObjectWriter::WriteAuxiliarySymbols(
522 const COFFSymbol::AuxiliarySymbols
&S
) {
523 for (const AuxSymbol
&i
: S
) {
526 W
.write
<uint32_t>(i
.Aux
.WeakExternal
.TagIndex
);
527 W
.write
<uint32_t>(i
.Aux
.WeakExternal
.Characteristics
);
528 W
.OS
.write_zeros(sizeof(i
.Aux
.WeakExternal
.unused
));
530 W
.OS
.write_zeros(COFF::Symbol32Size
- COFF::Symbol16Size
);
533 W
.OS
.write(reinterpret_cast<const char *>(&i
.Aux
),
534 UseBigObj
? COFF::Symbol32Size
: COFF::Symbol16Size
);
536 case ATSectionDefinition
:
537 W
.write
<uint32_t>(i
.Aux
.SectionDefinition
.Length
);
538 W
.write
<uint16_t>(i
.Aux
.SectionDefinition
.NumberOfRelocations
);
539 W
.write
<uint16_t>(i
.Aux
.SectionDefinition
.NumberOfLinenumbers
);
540 W
.write
<uint32_t>(i
.Aux
.SectionDefinition
.CheckSum
);
541 W
.write
<uint16_t>(static_cast<int16_t>(i
.Aux
.SectionDefinition
.Number
));
542 W
.OS
<< char(i
.Aux
.SectionDefinition
.Selection
);
543 W
.OS
.write_zeros(sizeof(i
.Aux
.SectionDefinition
.unused
));
544 W
.write
<uint16_t>(static_cast<int16_t>(i
.Aux
.SectionDefinition
.Number
>> 16));
546 W
.OS
.write_zeros(COFF::Symbol32Size
- COFF::Symbol16Size
);
552 // Write the section header.
553 void WinCOFFObjectWriter::writeSectionHeaders() {
554 // Section numbers must be monotonically increasing in the section
555 // header, but our Sections array is not sorted by section number,
556 // so make a copy of Sections and sort it.
557 std::vector
<COFFSection
*> Arr
;
558 for (auto &Section
: Sections
)
559 Arr
.push_back(Section
.get());
560 llvm::sort(Arr
, [](const COFFSection
*A
, const COFFSection
*B
) {
561 return A
->Number
< B
->Number
;
564 for (auto &Section
: Arr
) {
565 if (Section
->Number
== -1)
568 COFF::section
&S
= Section
->Header
;
569 if (Section
->Relocations
.size() >= 0xffff)
570 S
.Characteristics
|= COFF::IMAGE_SCN_LNK_NRELOC_OVFL
;
571 W
.OS
.write(S
.Name
, COFF::NameSize
);
572 W
.write
<uint32_t>(S
.VirtualSize
);
573 W
.write
<uint32_t>(S
.VirtualAddress
);
574 W
.write
<uint32_t>(S
.SizeOfRawData
);
575 W
.write
<uint32_t>(S
.PointerToRawData
);
576 W
.write
<uint32_t>(S
.PointerToRelocations
);
577 W
.write
<uint32_t>(S
.PointerToLineNumbers
);
578 W
.write
<uint16_t>(S
.NumberOfRelocations
);
579 W
.write
<uint16_t>(S
.NumberOfLineNumbers
);
580 W
.write
<uint32_t>(S
.Characteristics
);
584 void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation
&R
) {
585 W
.write
<uint32_t>(R
.VirtualAddress
);
586 W
.write
<uint32_t>(R
.SymbolTableIndex
);
587 W
.write
<uint16_t>(R
.Type
);
590 // Write MCSec's contents. What this function does is essentially
591 // "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
592 // because it needs to compute a CRC.
593 uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler
&Asm
,
594 const MCAsmLayout
&Layout
,
595 const MCSection
&MCSec
) {
596 // Save the contents of the section to a temporary buffer, we need this
597 // to CRC the data before we dump it into the object file.
598 SmallVector
<char, 128> Buf
;
599 raw_svector_ostream
VecOS(Buf
);
600 Asm
.writeSectionData(VecOS
, &MCSec
, Layout
);
602 // Write the section contents to the object file.
605 // Calculate our CRC with an initial value of '0', this is not how
606 // JamCRC is specified but it aligns with the expected output.
607 JamCRC
JC(/*Init=*/0);
612 void WinCOFFObjectWriter::writeSection(MCAssembler
&Asm
,
613 const MCAsmLayout
&Layout
,
614 const COFFSection
&Sec
,
615 const MCSection
&MCSec
) {
616 if (Sec
.Number
== -1)
619 // Write the section contents.
620 if (Sec
.Header
.PointerToRawData
!= 0) {
621 assert(W
.OS
.tell() == Sec
.Header
.PointerToRawData
&&
622 "Section::PointerToRawData is insane!");
624 uint32_t CRC
= writeSectionContents(Asm
, Layout
, MCSec
);
626 // Update the section definition auxiliary symbol to record the CRC.
627 COFFSection
*Sec
= SectionMap
[&MCSec
];
628 COFFSymbol::AuxiliarySymbols
&AuxSyms
= Sec
->Symbol
->Aux
;
629 assert(AuxSyms
.size() == 1 && AuxSyms
[0].AuxType
== ATSectionDefinition
);
630 AuxSymbol
&SecDef
= AuxSyms
[0];
631 SecDef
.Aux
.SectionDefinition
.CheckSum
= CRC
;
634 // Write relocations for this section.
635 if (Sec
.Relocations
.empty()) {
636 assert(Sec
.Header
.PointerToRelocations
== 0 &&
637 "Section::PointerToRelocations is insane!");
641 assert(W
.OS
.tell() == Sec
.Header
.PointerToRelocations
&&
642 "Section::PointerToRelocations is insane!");
644 if (Sec
.Relocations
.size() >= 0xffff) {
645 // In case of overflow, write actual relocation count as first
646 // relocation. Including the synthetic reloc itself (+ 1).
648 R
.VirtualAddress
= Sec
.Relocations
.size() + 1;
649 R
.SymbolTableIndex
= 0;
654 for (const auto &Relocation
: Sec
.Relocations
)
655 WriteRelocation(Relocation
.Data
);
658 ////////////////////////////////////////////////////////////////////////////////
659 // MCObjectWriter interface implementations
661 void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler
&Asm
,
662 const MCAsmLayout
&Layout
) {
663 if (EmitAddrsigSection
) {
664 AddrsigSection
= Asm
.getContext().getCOFFSection(
665 ".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE
,
666 SectionKind::getMetadata());
667 Asm
.registerSection(*AddrsigSection
);
670 // "Define" each section & symbol. This creates section & symbol
671 // entries in the staging area.
672 for (const auto &Section
: Asm
)
673 defineSection(static_cast<const MCSectionCOFF
&>(Section
));
675 for (const MCSymbol
&Symbol
: Asm
.symbols())
676 if (!Symbol
.isTemporary())
677 DefineSymbol(Symbol
, Asm
, Layout
);
680 bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
681 const MCAssembler
&Asm
, const MCSymbol
&SymA
, const MCFragment
&FB
,
682 bool InSet
, bool IsPCRel
) const {
683 // Don't drop relocations between functions, even if they are in the same text
684 // section. Multiple Visual C++ linker features depend on having the
685 // relocations present. The /INCREMENTAL flag will cause these relocations to
686 // point to thunks, and the /GUARD:CF flag assumes that it can use relocations
687 // to approximate the set of all address taken functions. LLD's implementation
688 // of /GUARD:CF also relies on the existance of these relocations.
689 uint16_t Type
= cast
<MCSymbolCOFF
>(SymA
).getType();
690 if ((Type
>> COFF::SCT_COMPLEX_TYPE_SHIFT
) == COFF::IMAGE_SYM_DTYPE_FUNCTION
)
692 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm
, SymA
, FB
,
696 void WinCOFFObjectWriter::recordRelocation(MCAssembler
&Asm
,
697 const MCAsmLayout
&Layout
,
698 const MCFragment
*Fragment
,
699 const MCFixup
&Fixup
, MCValue Target
,
700 uint64_t &FixedValue
) {
701 assert(Target
.getSymA() && "Relocation must reference a symbol!");
703 const MCSymbol
&A
= Target
.getSymA()->getSymbol();
704 if (!A
.isRegistered()) {
705 Asm
.getContext().reportError(Fixup
.getLoc(),
706 Twine("symbol '") + A
.getName() +
707 "' can not be undefined");
710 if (A
.isTemporary() && A
.isUndefined()) {
711 Asm
.getContext().reportError(Fixup
.getLoc(),
712 Twine("assembler label '") + A
.getName() +
713 "' can not be undefined");
717 MCSection
*MCSec
= Fragment
->getParent();
719 // Mark this symbol as requiring an entry in the symbol table.
720 assert(SectionMap
.find(MCSec
) != SectionMap
.end() &&
721 "Section must already have been defined in executePostLayoutBinding!");
723 COFFSection
*Sec
= SectionMap
[MCSec
];
724 const MCSymbolRefExpr
*SymB
= Target
.getSymB();
727 const MCSymbol
*B
= &SymB
->getSymbol();
728 if (!B
->getFragment()) {
729 Asm
.getContext().reportError(
731 Twine("symbol '") + B
->getName() +
732 "' can not be undefined in a subtraction expression");
736 // Offset of the symbol in the section
737 int64_t OffsetOfB
= Layout
.getSymbolOffset(*B
);
739 // Offset of the relocation in the section
740 int64_t OffsetOfRelocation
=
741 Layout
.getFragmentOffset(Fragment
) + Fixup
.getOffset();
743 FixedValue
= (OffsetOfRelocation
- OffsetOfB
) + Target
.getConstant();
745 FixedValue
= Target
.getConstant();
748 COFFRelocation Reloc
;
750 Reloc
.Data
.SymbolTableIndex
= 0;
751 Reloc
.Data
.VirtualAddress
= Layout
.getFragmentOffset(Fragment
);
753 // Turn relocations for temporary symbols into section relocations.
754 if (A
.isTemporary()) {
755 MCSection
*TargetSection
= &A
.getSection();
757 SectionMap
.find(TargetSection
) != SectionMap
.end() &&
758 "Section must already have been defined in executePostLayoutBinding!");
759 Reloc
.Symb
= SectionMap
[TargetSection
]->Symbol
;
760 FixedValue
+= Layout
.getSymbolOffset(A
);
763 SymbolMap
.find(&A
) != SymbolMap
.end() &&
764 "Symbol must already have been defined in executePostLayoutBinding!");
765 Reloc
.Symb
= SymbolMap
[&A
];
768 ++Reloc
.Symb
->Relocations
;
770 Reloc
.Data
.VirtualAddress
+= Fixup
.getOffset();
771 Reloc
.Data
.Type
= TargetObjectWriter
->getRelocType(
772 Asm
.getContext(), Target
, Fixup
, SymB
, Asm
.getBackend());
774 // FIXME: Can anyone explain what this does other than adjust for the size
776 if ((Header
.Machine
== COFF::IMAGE_FILE_MACHINE_AMD64
&&
777 Reloc
.Data
.Type
== COFF::IMAGE_REL_AMD64_REL32
) ||
778 (Header
.Machine
== COFF::IMAGE_FILE_MACHINE_I386
&&
779 Reloc
.Data
.Type
== COFF::IMAGE_REL_I386_REL32
))
782 if (Header
.Machine
== COFF::IMAGE_FILE_MACHINE_ARMNT
) {
783 switch (Reloc
.Data
.Type
) {
784 case COFF::IMAGE_REL_ARM_ABSOLUTE
:
785 case COFF::IMAGE_REL_ARM_ADDR32
:
786 case COFF::IMAGE_REL_ARM_ADDR32NB
:
787 case COFF::IMAGE_REL_ARM_TOKEN
:
788 case COFF::IMAGE_REL_ARM_SECTION
:
789 case COFF::IMAGE_REL_ARM_SECREL
:
791 case COFF::IMAGE_REL_ARM_BRANCH11
:
792 case COFF::IMAGE_REL_ARM_BLX11
:
793 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
794 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
796 case COFF::IMAGE_REL_ARM_BRANCH24
:
797 case COFF::IMAGE_REL_ARM_BLX24
:
798 case COFF::IMAGE_REL_ARM_MOV32A
:
799 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
800 // only used for ARM mode code, which is documented as being unsupported
801 // by Windows on ARM. Empirical proof indicates that masm is able to
802 // generate the relocations however the rest of the MSVC toolchain is
803 // unable to handle it.
804 llvm_unreachable("unsupported relocation");
806 case COFF::IMAGE_REL_ARM_MOV32T
:
808 case COFF::IMAGE_REL_ARM_BRANCH20T
:
809 case COFF::IMAGE_REL_ARM_BRANCH24T
:
810 case COFF::IMAGE_REL_ARM_BLX23T
:
811 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
812 // perform a 4 byte adjustment to the relocation. Relative branches are
813 // offset by 4 on ARM, however, because there is no RELA relocations, all
814 // branches are offset by 4.
815 FixedValue
= FixedValue
+ 4;
820 // The fixed value never makes sense for section indices, ignore it.
821 if (Fixup
.getKind() == FK_SecRel_2
)
824 if (TargetObjectWriter
->recordRelocation(Fixup
))
825 Sec
->Relocations
.push_back(Reloc
);
828 static std::time_t getTime() {
829 std::time_t Now
= time(nullptr);
830 if (Now
< 0 || !isUInt
<32>(Now
))
835 // Create .file symbols.
836 void WinCOFFObjectWriter::createFileSymbols(MCAssembler
&Asm
) {
837 for (const std::string
&Name
: Asm
.getFileNames()) {
838 // round up to calculate the number of auxiliary symbols required
839 unsigned SymbolSize
= UseBigObj
? COFF::Symbol32Size
: COFF::Symbol16Size
;
840 unsigned Count
= (Name
.size() + SymbolSize
- 1) / SymbolSize
;
842 COFFSymbol
*File
= createSymbol(".file");
843 File
->Data
.SectionNumber
= COFF::IMAGE_SYM_DEBUG
;
844 File
->Data
.StorageClass
= COFF::IMAGE_SYM_CLASS_FILE
;
845 File
->Aux
.resize(Count
);
848 unsigned Length
= Name
.size();
849 for (auto &Aux
: File
->Aux
) {
850 Aux
.AuxType
= ATFile
;
852 if (Length
> SymbolSize
) {
853 memcpy(&Aux
.Aux
, Name
.c_str() + Offset
, SymbolSize
);
854 Length
= Length
- SymbolSize
;
856 memcpy(&Aux
.Aux
, Name
.c_str() + Offset
, Length
);
857 memset((char *)&Aux
.Aux
+ Length
, 0, SymbolSize
- Length
);
861 Offset
+= SymbolSize
;
866 static bool isAssociative(const COFFSection
&Section
) {
867 return Section
.Symbol
->Aux
[0].Aux
.SectionDefinition
.Selection
==
868 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
;
871 void WinCOFFObjectWriter::assignSectionNumbers() {
873 auto Assign
= [&](COFFSection
&Section
) {
875 Section
.Symbol
->Data
.SectionNumber
= I
;
876 Section
.Symbol
->Aux
[0].Aux
.SectionDefinition
.Number
= I
;
880 // Although it is not explicitly requested by the Microsoft COFF spec,
881 // we should avoid emitting forward associative section references,
882 // because MSVC link.exe as of 2017 cannot handle that.
883 for (const std::unique_ptr
<COFFSection
> &Section
: Sections
)
884 if (!isAssociative(*Section
))
886 for (const std::unique_ptr
<COFFSection
> &Section
: Sections
)
887 if (isAssociative(*Section
))
891 // Assign file offsets to COFF object file structures.
892 void WinCOFFObjectWriter::assignFileOffsets(MCAssembler
&Asm
,
893 const MCAsmLayout
&Layout
) {
894 unsigned Offset
= W
.OS
.tell();
896 Offset
+= UseBigObj
? COFF::Header32Size
: COFF::Header16Size
;
897 Offset
+= COFF::SectionSize
* Header
.NumberOfSections
;
899 for (const auto &Section
: Asm
) {
900 COFFSection
*Sec
= SectionMap
[&Section
];
902 if (Sec
->Number
== -1)
905 Sec
->Header
.SizeOfRawData
= Layout
.getSectionAddressSize(&Section
);
907 if (IsPhysicalSection(Sec
)) {
908 Sec
->Header
.PointerToRawData
= Offset
;
909 Offset
+= Sec
->Header
.SizeOfRawData
;
912 if (!Sec
->Relocations
.empty()) {
913 bool RelocationsOverflow
= Sec
->Relocations
.size() >= 0xffff;
915 if (RelocationsOverflow
) {
916 // Signal overflow by setting NumberOfRelocations to max value. Actual
917 // size is found in reloc #0. Microsoft tools understand this.
918 Sec
->Header
.NumberOfRelocations
= 0xffff;
920 Sec
->Header
.NumberOfRelocations
= Sec
->Relocations
.size();
922 Sec
->Header
.PointerToRelocations
= Offset
;
924 if (RelocationsOverflow
) {
925 // Reloc #0 will contain actual count, so make room for it.
926 Offset
+= COFF::RelocationSize
;
929 Offset
+= COFF::RelocationSize
* Sec
->Relocations
.size();
931 for (auto &Relocation
: Sec
->Relocations
) {
932 assert(Relocation
.Symb
->getIndex() != -1);
933 Relocation
.Data
.SymbolTableIndex
= Relocation
.Symb
->getIndex();
937 assert(Sec
->Symbol
->Aux
.size() == 1 &&
938 "Section's symbol must have one aux!");
939 AuxSymbol
&Aux
= Sec
->Symbol
->Aux
[0];
940 assert(Aux
.AuxType
== ATSectionDefinition
&&
941 "Section's symbol's aux symbol must be a Section Definition!");
942 Aux
.Aux
.SectionDefinition
.Length
= Sec
->Header
.SizeOfRawData
;
943 Aux
.Aux
.SectionDefinition
.NumberOfRelocations
=
944 Sec
->Header
.NumberOfRelocations
;
945 Aux
.Aux
.SectionDefinition
.NumberOfLinenumbers
=
946 Sec
->Header
.NumberOfLineNumbers
;
949 Header
.PointerToSymbolTable
= Offset
;
952 uint64_t WinCOFFObjectWriter::writeObject(MCAssembler
&Asm
,
953 const MCAsmLayout
&Layout
) {
954 uint64_t StartOffset
= W
.OS
.tell();
956 if (Sections
.size() > INT32_MAX
)
958 "PE COFF object files can't have more than 2147483647 sections");
960 UseBigObj
= Sections
.size() > COFF::MaxNumberOfSections16
;
961 Header
.NumberOfSections
= Sections
.size();
962 Header
.NumberOfSymbols
= 0;
964 assignSectionNumbers();
965 createFileSymbols(Asm
);
967 for (auto &Symbol
: Symbols
) {
968 // Update section number & offset for symbols that have them.
970 Symbol
->Data
.SectionNumber
= Symbol
->Section
->Number
;
971 Symbol
->setIndex(Header
.NumberOfSymbols
++);
972 // Update auxiliary symbol info.
973 Symbol
->Data
.NumberOfAuxSymbols
= Symbol
->Aux
.size();
974 Header
.NumberOfSymbols
+= Symbol
->Data
.NumberOfAuxSymbols
;
977 // Build string table.
978 for (const auto &S
: Sections
)
979 if (S
->Name
.size() > COFF::NameSize
)
980 Strings
.add(S
->Name
);
981 for (const auto &S
: Symbols
)
982 if (S
->Name
.size() > COFF::NameSize
)
983 Strings
.add(S
->Name
);
987 for (const auto &S
: Sections
)
989 for (auto &S
: Symbols
)
992 // Fixup weak external references.
993 for (auto &Symbol
: Symbols
) {
995 assert(Symbol
->getIndex() != -1);
996 assert(Symbol
->Aux
.size() == 1 && "Symbol must contain one aux symbol!");
997 assert(Symbol
->Aux
[0].AuxType
== ATWeakExternal
&&
998 "Symbol's aux symbol must be a Weak External!");
999 Symbol
->Aux
[0].Aux
.WeakExternal
.TagIndex
= Symbol
->Other
->getIndex();
1003 // Fixup associative COMDAT sections.
1004 for (auto &Section
: Sections
) {
1005 if (Section
->Symbol
->Aux
[0].Aux
.SectionDefinition
.Selection
!=
1006 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
)
1009 const MCSectionCOFF
&MCSec
= *Section
->MCSection
;
1010 const MCSymbol
*AssocMCSym
= MCSec
.getCOMDATSymbol();
1013 // It's an error to try to associate with an undefined symbol or a symbol
1014 // without a section.
1015 if (!AssocMCSym
->isInSection()) {
1016 Asm
.getContext().reportError(
1017 SMLoc(), Twine("cannot make section ") + MCSec
.getSectionName() +
1018 Twine(" associative with sectionless symbol ") +
1019 AssocMCSym
->getName());
1023 const auto *AssocMCSec
= cast
<MCSectionCOFF
>(&AssocMCSym
->getSection());
1024 assert(SectionMap
.count(AssocMCSec
));
1025 COFFSection
*AssocSec
= SectionMap
[AssocMCSec
];
1027 // Skip this section if the associated section is unused.
1028 if (AssocSec
->Number
== -1)
1031 Section
->Symbol
->Aux
[0].Aux
.SectionDefinition
.Number
= AssocSec
->Number
;
1034 // Create the contents of the .llvm_addrsig section.
1035 if (EmitAddrsigSection
) {
1036 auto Frag
= new MCDataFragment(AddrsigSection
);
1037 Frag
->setLayoutOrder(0);
1038 raw_svector_ostream
OS(Frag
->getContents());
1039 for (const MCSymbol
*S
: AddrsigSyms
) {
1040 if (!S
->isTemporary()) {
1041 encodeULEB128(S
->getIndex(), OS
);
1045 MCSection
*TargetSection
= &S
->getSection();
1046 assert(SectionMap
.find(TargetSection
) != SectionMap
.end() &&
1047 "Section must already have been defined in "
1048 "executePostLayoutBinding!");
1049 encodeULEB128(SectionMap
[TargetSection
]->Symbol
->getIndex(), OS
);
1053 assignFileOffsets(Asm
, Layout
);
1055 // MS LINK expects to be able to use this timestamp to implement their
1056 // /INCREMENTAL feature.
1057 if (Asm
.isIncrementalLinkerCompatible()) {
1058 Header
.TimeDateStamp
= getTime();
1060 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
1061 Header
.TimeDateStamp
= 0;
1064 // Write it all to disk...
1065 WriteFileHeader(Header
);
1066 writeSectionHeaders();
1068 // Write section contents.
1069 sections::iterator I
= Sections
.begin();
1070 sections::iterator IE
= Sections
.end();
1071 MCAssembler::iterator J
= Asm
.begin();
1072 MCAssembler::iterator JE
= Asm
.end();
1073 for (; I
!= IE
&& J
!= JE
; ++I
, ++J
)
1074 writeSection(Asm
, Layout
, **I
, *J
);
1076 assert(W
.OS
.tell() == Header
.PointerToSymbolTable
&&
1077 "Header::PointerToSymbolTable is insane!");
1079 // Write a symbol table.
1080 for (auto &Symbol
: Symbols
)
1081 if (Symbol
->getIndex() != -1)
1082 WriteSymbol(*Symbol
);
1084 // Write a string table, which completes the entire COFF file.
1085 Strings
.write(W
.OS
);
1087 return W
.OS
.tell() - StartOffset
;
1090 MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_
)
1091 : Machine(Machine_
) {}
1093 // Pin the vtable to this file.
1094 void MCWinCOFFObjectTargetWriter::anchor() {}
1096 //------------------------------------------------------------------------------
1097 // WinCOFFObjectWriter factory function
1099 std::unique_ptr
<MCObjectWriter
> llvm::createWinCOFFObjectWriter(
1100 std::unique_ptr
<MCWinCOFFObjectTargetWriter
> MOTW
, raw_pwrite_stream
&OS
) {
1101 return std::make_unique
<WinCOFFObjectWriter
>(std::move(MOTW
), OS
);