LLVM 17.0.0git
COFFEmitter.cpp
Go to the documentation of this file.
1//===- yaml2coff - Convert YAML to a COFF object file ---------------------===//
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/// \file
10/// The COFF component of yaml2obj.
11///
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringMap.h"
19#include "llvm/Object/COFF.h"
23#include "llvm/Support/Endian.h"
28#include <optional>
29#include <vector>
30
31using namespace llvm;
32
33namespace {
34
35/// This parses a yaml stream that represents a COFF object file.
36/// See docs/yaml2obj for the yaml scheema.
37struct COFFParser {
38 COFFParser(COFFYAML::Object &Obj, yaml::ErrorHandler EH)
39 : Obj(Obj), SectionTableStart(0), SectionTableSize(0), ErrHandler(EH) {
40 // A COFF string table always starts with a 4 byte size field. Offsets into
41 // it include this size, so allocate it now.
42 StringTable.append(4, char(0));
43 }
44
45 bool useBigObj() const {
46 return static_cast<int32_t>(Obj.Sections.size()) >
48 }
49
50 bool isPE() const { return Obj.OptionalHeader.has_value(); }
51 bool is64Bit() const {
54 }
55
56 uint32_t getFileAlignment() const {
57 return Obj.OptionalHeader->Header.FileAlignment;
58 }
59
60 unsigned getHeaderSize() const {
61 return useBigObj() ? COFF::Header32Size : COFF::Header16Size;
62 }
63
64 unsigned getSymbolSize() const {
65 return useBigObj() ? COFF::Symbol32Size : COFF::Symbol16Size;
66 }
67
68 bool parseSections() {
69 for (COFFYAML::Section &Sec : Obj.Sections) {
70 // If the name is less than 8 bytes, store it in place, otherwise
71 // store it in the string table.
72 StringRef Name = Sec.Name;
73
74 if (Name.size() <= COFF::NameSize) {
75 std::copy(Name.begin(), Name.end(), Sec.Header.Name);
76 } else {
77 // Add string to the string table and format the index for output.
78 unsigned Index = getStringIndex(Name);
79 std::string str = utostr(Index);
80 if (str.size() > 7) {
81 ErrHandler("string table got too large");
82 return false;
83 }
84 Sec.Header.Name[0] = '/';
85 std::copy(str.begin(), str.end(), Sec.Header.Name + 1);
86 }
87
88 if (Sec.Alignment) {
89 if (Sec.Alignment > 8192) {
90 ErrHandler("section alignment is too large");
91 return false;
92 }
93 if (!isPowerOf2_32(Sec.Alignment)) {
94 ErrHandler("section alignment is not a power of 2");
95 return false;
96 }
97 Sec.Header.Characteristics |= (Log2_32(Sec.Alignment) + 1) << 20;
98 }
99 }
100 return true;
101 }
102
103 bool parseSymbols() {
104 for (COFFYAML::Symbol &Sym : Obj.Symbols) {
105 // If the name is less than 8 bytes, store it in place, otherwise
106 // store it in the string table.
107 StringRef Name = Sym.Name;
108 if (Name.size() <= COFF::NameSize) {
109 std::copy(Name.begin(), Name.end(), Sym.Header.Name);
110 } else {
111 // Add string to the string table and format the index for output.
112 unsigned Index = getStringIndex(Name);
113 *reinterpret_cast<support::aligned_ulittle32_t *>(Sym.Header.Name + 4) =
114 Index;
115 }
116
117 Sym.Header.Type = Sym.SimpleType;
118 Sym.Header.Type |= Sym.ComplexType << COFF::SCT_COMPLEX_TYPE_SHIFT;
119 }
120 return true;
121 }
122
123 bool parse() {
124 if (!parseSections())
125 return false;
126 if (!parseSymbols())
127 return false;
128 return true;
129 }
130
131 unsigned getStringIndex(StringRef Str) {
132 StringMap<unsigned>::iterator i = StringTableMap.find(Str);
133 if (i == StringTableMap.end()) {
134 unsigned Index = StringTable.size();
135 StringTable.append(Str.begin(), Str.end());
136 StringTable.push_back(0);
137 StringTableMap[Str] = Index;
138 return Index;
139 }
140 return i->second;
141 }
142
143 COFFYAML::Object &Obj;
144
145 codeview::StringsAndChecksums StringsAndChecksums;
147 StringMap<unsigned> StringTableMap;
148 std::string StringTable;
149 uint32_t SectionTableStart;
150 uint32_t SectionTableSize;
151
152 yaml::ErrorHandler ErrHandler;
153};
154
155enum { DOSStubSize = 128 };
156
157} // end anonymous namespace
158
159// Take a CP and assign addresses and sizes to everything. Returns false if the
160// layout is not valid to do.
161static bool layoutOptionalHeader(COFFParser &CP) {
162 if (!CP.isPE())
163 return true;
164 unsigned PEHeaderSize = CP.is64Bit() ? sizeof(object::pe32plus_header)
165 : sizeof(object::pe32_header);
166 CP.Obj.Header.SizeOfOptionalHeader =
167 PEHeaderSize + sizeof(object::data_directory) *
168 CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize;
169 return true;
170}
171
172static yaml::BinaryRef
174 const codeview::StringsAndChecksums &SC, BumpPtrAllocator &Allocator) {
175 using namespace codeview;
176 ExitOnError Err("Error occurred writing .debug$S section");
177 auto CVSS =
179
180 std::vector<DebugSubsectionRecordBuilder> Builders;
181 uint32_t Size = sizeof(uint32_t);
182 for (auto &SS : CVSS) {
183 DebugSubsectionRecordBuilder B(SS);
184 Size += B.calculateSerializedLength();
185 Builders.push_back(std::move(B));
186 }
187 uint8_t *Buffer = Allocator.Allocate<uint8_t>(Size);
188 MutableArrayRef<uint8_t> Output(Buffer, Size);
189 BinaryStreamWriter Writer(Output, support::little);
190
192 for (const auto &B : Builders) {
193 Err(B.commit(Writer, CodeViewContainer::ObjectFile));
194 }
195 return {Output};
196}
197
198// Take a CP and assign addresses and sizes to everything. Returns false if the
199// layout is not valid to do.
200static bool layoutCOFF(COFFParser &CP) {
201 // The section table starts immediately after the header, including the
202 // optional header.
203 CP.SectionTableStart =
204 CP.getHeaderSize() + CP.Obj.Header.SizeOfOptionalHeader;
205 if (CP.isPE())
206 CP.SectionTableStart += DOSStubSize + sizeof(COFF::PEMagic);
207 CP.SectionTableSize = COFF::SectionSize * CP.Obj.Sections.size();
208
209 uint32_t CurrentSectionDataOffset =
210 CP.SectionTableStart + CP.SectionTableSize;
211
212 for (COFFYAML::Section &S : CP.Obj.Sections) {
213 // We support specifying exactly one of SectionData or Subsections. So if
214 // there is already some SectionData, then we don't need to do any of this.
215 if (S.Name == ".debug$S" && S.SectionData.binary_size() == 0) {
217 CP.StringsAndChecksums);
218 if (CP.StringsAndChecksums.hasChecksums() &&
219 CP.StringsAndChecksums.hasStrings())
220 break;
221 }
222 }
223
224 // Assign each section data address consecutively.
225 for (COFFYAML::Section &S : CP.Obj.Sections) {
226 if (S.Name == ".debug$S") {
227 if (S.SectionData.binary_size() == 0) {
228 assert(CP.StringsAndChecksums.hasStrings() &&
229 "Object file does not have debug string table!");
230
231 S.SectionData =
232 toDebugS(S.DebugS, CP.StringsAndChecksums, CP.Allocator);
233 }
234 } else if (S.Name == ".debug$T") {
235 if (S.SectionData.binary_size() == 0)
236 S.SectionData = CodeViewYAML::toDebugT(S.DebugT, CP.Allocator, S.Name);
237 } else if (S.Name == ".debug$P") {
238 if (S.SectionData.binary_size() == 0)
239 S.SectionData = CodeViewYAML::toDebugT(S.DebugP, CP.Allocator, S.Name);
240 } else if (S.Name == ".debug$H") {
241 if (S.DebugH && S.SectionData.binary_size() == 0)
242 S.SectionData = CodeViewYAML::toDebugH(*S.DebugH, CP.Allocator);
243 }
244
245 if (S.SectionData.binary_size() > 0) {
246 CurrentSectionDataOffset = alignTo(CurrentSectionDataOffset,
247 CP.isPE() ? CP.getFileAlignment() : 4);
249 if (CP.isPE())
251 alignTo(S.Header.SizeOfRawData, CP.getFileAlignment());
252 S.Header.PointerToRawData = CurrentSectionDataOffset;
253 CurrentSectionDataOffset += S.Header.SizeOfRawData;
254 if (!S.Relocations.empty()) {
255 S.Header.PointerToRelocations = CurrentSectionDataOffset;
257 S.Header.NumberOfRelocations = 0xffff;
258 CurrentSectionDataOffset += COFF::RelocationSize;
259 } else
261 CurrentSectionDataOffset += S.Relocations.size() * COFF::RelocationSize;
262 }
263 } else {
264 // Leave SizeOfRawData unaltered. For .bss sections in object files, it
265 // carries the section size.
267 }
268 }
269
270 uint32_t SymbolTableStart = CurrentSectionDataOffset;
271
272 // Calculate number of symbols.
273 uint32_t NumberOfSymbols = 0;
274 for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(),
275 e = CP.Obj.Symbols.end();
276 i != e; ++i) {
277 uint32_t NumberOfAuxSymbols = 0;
278 if (i->FunctionDefinition)
279 NumberOfAuxSymbols += 1;
280 if (i->bfAndefSymbol)
281 NumberOfAuxSymbols += 1;
282 if (i->WeakExternal)
283 NumberOfAuxSymbols += 1;
284 if (!i->File.empty())
285 NumberOfAuxSymbols +=
286 (i->File.size() + CP.getSymbolSize() - 1) / CP.getSymbolSize();
287 if (i->SectionDefinition)
288 NumberOfAuxSymbols += 1;
289 if (i->CLRToken)
290 NumberOfAuxSymbols += 1;
291 i->Header.NumberOfAuxSymbols = NumberOfAuxSymbols;
292 NumberOfSymbols += 1 + NumberOfAuxSymbols;
293 }
294
295 // Store all the allocated start addresses in the header.
296 CP.Obj.Header.NumberOfSections = CP.Obj.Sections.size();
297 CP.Obj.Header.NumberOfSymbols = NumberOfSymbols;
298 if (NumberOfSymbols > 0 || CP.StringTable.size() > 4)
299 CP.Obj.Header.PointerToSymbolTable = SymbolTableStart;
300 else
301 CP.Obj.Header.PointerToSymbolTable = 0;
302
303 *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0]) =
304 CP.StringTable.size();
305
306 return true;
307}
308
309template <typename value_type> struct binary_le_impl {
310 value_type Value;
311 binary_le_impl(value_type V) : Value(V) {}
312};
313
314template <typename value_type>
316 const binary_le_impl<value_type> &BLE) {
317 char Buffer[sizeof(BLE.Value)];
318 support::endian::write<value_type, support::little, support::unaligned>(
319 Buffer, BLE.Value);
320 OS.write(Buffer, sizeof(BLE.Value));
321 return OS;
322}
323
324template <typename value_type>
327}
328
329template <size_t NumBytes> struct zeros_impl {};
330
331template <size_t NumBytes>
333 char Buffer[NumBytes];
334 memset(Buffer, 0, sizeof(Buffer));
335 OS.write(Buffer, sizeof(Buffer));
336 return OS;
337}
338
339template <typename T> zeros_impl<sizeof(T)> zeros(const T &) {
340 return zeros_impl<sizeof(T)>();
341}
342
343template <typename T>
344static uint32_t initializeOptionalHeader(COFFParser &CP, uint16_t Magic,
345 T Header) {
346 memset(Header, 0, sizeof(*Header));
347 Header->Magic = Magic;
348 Header->SectionAlignment = CP.Obj.OptionalHeader->Header.SectionAlignment;
349 Header->FileAlignment = CP.Obj.OptionalHeader->Header.FileAlignment;
350 uint32_t SizeOfCode = 0, SizeOfInitializedData = 0,
351 SizeOfUninitializedData = 0;
352 uint32_t SizeOfHeaders = alignTo(CP.SectionTableStart + CP.SectionTableSize,
353 Header->FileAlignment);
354 uint32_t SizeOfImage = alignTo(SizeOfHeaders, Header->SectionAlignment);
355 uint32_t BaseOfData = 0;
356 for (const COFFYAML::Section &S : CP.Obj.Sections) {
358 SizeOfCode += S.Header.SizeOfRawData;
360 SizeOfInitializedData += S.Header.SizeOfRawData;
362 SizeOfUninitializedData += S.Header.SizeOfRawData;
363 if (S.Name.equals(".text"))
364 Header->BaseOfCode = S.Header.VirtualAddress; // RVA
365 else if (S.Name.equals(".data"))
366 BaseOfData = S.Header.VirtualAddress; // RVA
368 SizeOfImage += alignTo(S.Header.VirtualSize, Header->SectionAlignment);
369 }
370 Header->SizeOfCode = SizeOfCode;
371 Header->SizeOfInitializedData = SizeOfInitializedData;
372 Header->SizeOfUninitializedData = SizeOfUninitializedData;
373 Header->AddressOfEntryPoint =
374 CP.Obj.OptionalHeader->Header.AddressOfEntryPoint; // RVA
375 Header->ImageBase = CP.Obj.OptionalHeader->Header.ImageBase;
376 Header->MajorOperatingSystemVersion =
377 CP.Obj.OptionalHeader->Header.MajorOperatingSystemVersion;
378 Header->MinorOperatingSystemVersion =
379 CP.Obj.OptionalHeader->Header.MinorOperatingSystemVersion;
380 Header->MajorImageVersion = CP.Obj.OptionalHeader->Header.MajorImageVersion;
381 Header->MinorImageVersion = CP.Obj.OptionalHeader->Header.MinorImageVersion;
382 Header->MajorSubsystemVersion =
383 CP.Obj.OptionalHeader->Header.MajorSubsystemVersion;
384 Header->MinorSubsystemVersion =
385 CP.Obj.OptionalHeader->Header.MinorSubsystemVersion;
386 Header->SizeOfImage = SizeOfImage;
387 Header->SizeOfHeaders = SizeOfHeaders;
388 Header->Subsystem = CP.Obj.OptionalHeader->Header.Subsystem;
389 Header->DLLCharacteristics = CP.Obj.OptionalHeader->Header.DLLCharacteristics;
390 Header->SizeOfStackReserve = CP.Obj.OptionalHeader->Header.SizeOfStackReserve;
391 Header->SizeOfStackCommit = CP.Obj.OptionalHeader->Header.SizeOfStackCommit;
392 Header->SizeOfHeapReserve = CP.Obj.OptionalHeader->Header.SizeOfHeapReserve;
393 Header->SizeOfHeapCommit = CP.Obj.OptionalHeader->Header.SizeOfHeapCommit;
394 Header->NumberOfRvaAndSize = CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize;
395 return BaseOfData;
396}
397
398static bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
399 if (CP.isPE()) {
400 // PE files start with a DOS stub.
402 memset(&DH, 0, sizeof(DH));
403
404 // DOS EXEs start with "MZ" magic.
405 DH.Magic[0] = 'M';
406 DH.Magic[1] = 'Z';
407 // Initializing the AddressOfRelocationTable is strictly optional but
408 // mollifies certain tools which expect it to have a value greater than
409 // 0x40.
410 DH.AddressOfRelocationTable = sizeof(DH);
411 // This is the address of the PE signature.
412 DH.AddressOfNewExeHeader = DOSStubSize;
413
414 // Write out our DOS stub.
415 OS.write(reinterpret_cast<char *>(&DH), sizeof(DH));
416 // Write padding until we reach the position of where our PE signature
417 // should live.
418 OS.write_zeros(DOSStubSize - sizeof(DH));
419 // Write out the PE signature.
421 }
422 if (CP.useBigObj()) {
423 OS << binary_le(static_cast<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN))
424 << binary_le(static_cast<uint16_t>(0xffff))
425 << binary_le(
427 << binary_le(CP.Obj.Header.Machine)
428 << binary_le(CP.Obj.Header.TimeDateStamp);
430 OS << zeros(uint32_t(0)) << zeros(uint32_t(0)) << zeros(uint32_t(0))
431 << zeros(uint32_t(0)) << binary_le(CP.Obj.Header.NumberOfSections)
432 << binary_le(CP.Obj.Header.PointerToSymbolTable)
433 << binary_le(CP.Obj.Header.NumberOfSymbols);
434 } else {
435 OS << binary_le(CP.Obj.Header.Machine)
436 << binary_le(static_cast<int16_t>(CP.Obj.Header.NumberOfSections))
437 << binary_le(CP.Obj.Header.TimeDateStamp)
438 << binary_le(CP.Obj.Header.PointerToSymbolTable)
439 << binary_le(CP.Obj.Header.NumberOfSymbols)
440 << binary_le(CP.Obj.Header.SizeOfOptionalHeader)
441 << binary_le(CP.Obj.Header.Characteristics);
442 }
443 if (CP.isPE()) {
444 if (CP.is64Bit()) {
447 OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH));
448 } else {
450 uint32_t BaseOfData =
452 PEH.BaseOfData = BaseOfData;
453 OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH));
454 }
455 for (uint32_t I = 0; I < CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize;
456 ++I) {
457 const std::optional<COFF::DataDirectory> *DataDirectories =
458 CP.Obj.OptionalHeader->DataDirectories;
459 uint32_t NumDataDir = std::size(CP.Obj.OptionalHeader->DataDirectories);
460 if (I >= NumDataDir || !DataDirectories[I]) {
461 OS << zeros(uint32_t(0));
462 OS << zeros(uint32_t(0));
463 } else {
464 OS << binary_le(DataDirectories[I]->RelativeVirtualAddress);
465 OS << binary_le(DataDirectories[I]->Size);
466 }
467 }
468 }
469
470 assert(OS.tell() == CP.SectionTableStart);
471 // Output section table.
472 for (const COFFYAML::Section &S : CP.Obj.Sections) {
483 }
484 assert(OS.tell() == CP.SectionTableStart + CP.SectionTableSize);
485
486 unsigned CurSymbol = 0;
487 StringMap<unsigned> SymbolTableIndexMap;
488 for (const COFFYAML::Symbol &Sym : CP.Obj.Symbols) {
489 SymbolTableIndexMap[Sym.Name] = CurSymbol;
490 CurSymbol += 1 + Sym.Header.NumberOfAuxSymbols;
491 }
492
493 // Output section data.
494 for (const COFFYAML::Section &S : CP.Obj.Sections) {
495 if (S.Header.SizeOfRawData == 0 || S.Header.PointerToRawData == 0)
496 continue;
503 OS << binary_le<uint32_t>(/*VirtualAddress=*/ S.Relocations.size() + 1)
504 << binary_le<uint32_t>(/*SymbolTableIndex=*/ 0)
505 << binary_le<uint16_t>(/*Type=*/ 0);
506 for (const COFFYAML::Relocation &R : S.Relocations) {
507 uint32_t SymbolTableIndex;
508 if (R.SymbolTableIndex) {
509 if (!R.SymbolName.empty())
511 << "Both SymbolName and SymbolTableIndex specified\n";
512 SymbolTableIndex = *R.SymbolTableIndex;
513 } else {
514 SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];
515 }
516 OS << binary_le(R.VirtualAddress) << binary_le(SymbolTableIndex)
517 << binary_le(R.Type);
518 }
519 }
520
521 // Output symbol table.
522
523 for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.Symbols.begin(),
524 e = CP.Obj.Symbols.end();
525 i != e; ++i) {
526 OS.write(i->Header.Name, COFF::NameSize);
527 OS << binary_le(i->Header.Value);
528 if (CP.useBigObj())
529 OS << binary_le(i->Header.SectionNumber);
530 else
531 OS << binary_le(static_cast<int16_t>(i->Header.SectionNumber));
532 OS << binary_le(i->Header.Type) << binary_le(i->Header.StorageClass)
533 << binary_le(i->Header.NumberOfAuxSymbols);
534
535 if (i->FunctionDefinition) {
536 OS << binary_le(i->FunctionDefinition->TagIndex)
537 << binary_le(i->FunctionDefinition->TotalSize)
538 << binary_le(i->FunctionDefinition->PointerToLinenumber)
539 << binary_le(i->FunctionDefinition->PointerToNextFunction)
540 << zeros(i->FunctionDefinition->unused);
541 OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
542 }
543 if (i->bfAndefSymbol) {
544 OS << zeros(i->bfAndefSymbol->unused1)
545 << binary_le(i->bfAndefSymbol->Linenumber)
546 << zeros(i->bfAndefSymbol->unused2)
547 << binary_le(i->bfAndefSymbol->PointerToNextFunction)
548 << zeros(i->bfAndefSymbol->unused3);
549 OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
550 }
551 if (i->WeakExternal) {
552 OS << binary_le(i->WeakExternal->TagIndex)
553 << binary_le(i->WeakExternal->Characteristics)
554 << zeros(i->WeakExternal->unused);
555 OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
556 }
557 if (!i->File.empty()) {
558 unsigned SymbolSize = CP.getSymbolSize();
559 uint32_t NumberOfAuxRecords =
560 (i->File.size() + SymbolSize - 1) / SymbolSize;
561 uint32_t NumberOfAuxBytes = NumberOfAuxRecords * SymbolSize;
562 uint32_t NumZeros = NumberOfAuxBytes - i->File.size();
563 OS.write(i->File.data(), i->File.size());
564 OS.write_zeros(NumZeros);
565 }
566 if (i->SectionDefinition) {
567 OS << binary_le(i->SectionDefinition->Length)
568 << binary_le(i->SectionDefinition->NumberOfRelocations)
569 << binary_le(i->SectionDefinition->NumberOfLinenumbers)
570 << binary_le(i->SectionDefinition->CheckSum)
571 << binary_le(static_cast<int16_t>(i->SectionDefinition->Number))
572 << binary_le(i->SectionDefinition->Selection)
573 << zeros(i->SectionDefinition->unused)
574 << binary_le(static_cast<int16_t>(i->SectionDefinition->Number >> 16));
575 OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
576 }
577 if (i->CLRToken) {
578 OS << binary_le(i->CLRToken->AuxType) << zeros(i->CLRToken->unused1)
579 << binary_le(i->CLRToken->SymbolTableIndex)
580 << zeros(i->CLRToken->unused2);
581 OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
582 }
583 }
584
585 // Output string table.
586 if (CP.Obj.Header.PointerToSymbolTable)
587 OS.write(&CP.StringTable[0], CP.StringTable.size());
588 return true;
589}
590
591namespace llvm {
592namespace yaml {
593
595 ErrorHandler ErrHandler) {
596 COFFParser CP(Doc, ErrHandler);
597 if (!CP.parse()) {
598 ErrHandler("failed to parse YAML file");
599 return false;
600 }
601
602 if (!layoutOptionalHeader(CP)) {
603 ErrHandler("failed to layout optional header for COFF file");
604 return false;
605 }
606
607 if (!layoutCOFF(CP)) {
608 ErrHandler("failed to layout COFF file");
609 return false;
610 }
611 if (!writeCOFF(CP, Out)) {
612 ErrHandler("failed to write COFF file");
613 return false;
614 }
615 return true;
616}
617
618} // namespace yaml
619} // namespace llvm
This file defines the StringMap class.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool layoutCOFF(COFFParser &CP)
binary_le_impl< value_type > binary_le(value_type V)
static yaml::BinaryRef toDebugS(ArrayRef< CodeViewYAML::YAMLDebugSubsection > Subsections, const codeview::StringsAndChecksums &SC, BumpPtrAllocator &Allocator)
zeros_impl< sizeof(T)> zeros(const T &)
static uint32_t initializeOptionalHeader(COFFParser &CP, uint16_t Magic, T Header)
raw_ostream & operator<<(raw_ostream &OS, const binary_le_impl< value_type > &BLE)
static bool writeCOFF(COFFParser &CP, raw_ostream &OS)
static bool layoutOptionalHeader(COFFParser &CP)
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:463
static size_t getStringIndex(StringRef Name)
Definition: LVElement.cpp:78
#define I(x, y, z)
Definition: MD5.cpp:58
Basic Register Allocator
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
This file contains some functions that are useful when dealing with strings.
static bool is64Bit(const char *name)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Provides write only access to a subclass of WritableBinaryStream.
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
Helper for check-and-exit error handling.
Definition: Error.h:1355
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:305
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:111
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:164
LLVM Value Representation.
Definition: Value.h:74
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:83
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:134
raw_ostream & write(unsigned char C)
Specialized YAMLIO scalar type for representing a binary blob.
Definition: YAML.h:63
ArrayRef< uint8_t >::size_type binary_size() const
The number of bytes that are represented by this BinaryRef.
Definition: YAML.h:80
void writeAsBinary(raw_ostream &OS, uint64_t N=UINT64_MAX) const
Write the contents (regardless of whether it is binary or a hex string) as binary to the given raw_os...
Definition: YAML.cpp:39
@ IMAGE_FILE_MACHINE_UNKNOWN
Definition: COFF.h:95
@ IMAGE_FILE_MACHINE_AMD64
Definition: COFF.h:97
@ IMAGE_SCN_CNT_CODE
Definition: COFF.h:298
@ IMAGE_SCN_CNT_UNINITIALIZED_DATA
Definition: COFF.h:300
@ IMAGE_SCN_CNT_INITIALIZED_DATA
Definition: COFF.h:299
@ IMAGE_SCN_LNK_NRELOC_OVFL
Definition: COFF.h:325
bool isAnyArm64(T Machine)
Definition: COFF.h:129
@ 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
@ DEBUG_SECTION_MAGIC
Definition: COFF.h:781
const int32_t MaxNumberOfSections16
Definition: COFF.h:32
static const char BigObjMagic[]
Definition: COFF.h:37
static const char PEMagic[]
Definition: COFF.h:35
@ SCT_COMPLEX_TYPE_SHIFT
Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT))
Definition: COFF.h:275
void initializeStringsAndChecksums(ArrayRef< YAMLDebugSubsection > Sections, codeview::StringsAndChecksums &SC)
Expected< std::vector< std::shared_ptr< codeview::DebugSubsection > > > toCodeViewSubsectionList(BumpPtrAllocator &Allocator, ArrayRef< YAMLDebugSubsection > Subsections, const codeview::StringsAndChecksums &SC)
ArrayRef< uint8_t > toDebugH(const DebugHSection &DebugH, BumpPtrAllocator &Alloc)
ArrayRef< uint8_t > toDebugT(ArrayRef< LeafRecord >, BumpPtrAllocator &Alloc, StringRef SectionName)
bool yaml2coff(COFFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:382
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:292
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
binary_le_impl(value_type V)
value_type Value
std::optional< PEHeader > OptionalHeader
Definition: COFFYAML.h:105
std::vector< Section > Sections
Definition: COFFYAML.h:107
std::vector< Symbol > Symbols
Definition: COFFYAML.h:108
COFF::header Header
Definition: COFFYAML.h:106
std::vector< CodeViewYAML::YAMLDebugSubsection > DebugS
Definition: COFFYAML.h:73
std::vector< CodeViewYAML::LeafRecord > DebugT
Definition: COFFYAML.h:74
yaml::BinaryRef SectionData
Definition: COFFYAML.h:72
std::optional< CodeViewYAML::DebugHSection > DebugH
Definition: COFFYAML.h:76
std::vector< CodeViewYAML::LeafRecord > DebugP
Definition: COFFYAML.h:75
COFF::section Header
Definition: COFFYAML.h:70
std::vector< Relocation > Relocations
Definition: COFFYAML.h:77
uint16_t Machine
Definition: COFF.h:65
uint32_t VirtualSize
Definition: COFF.h:282
uint32_t PointerToRelocations
Definition: COFF.h:286
uint16_t NumberOfLineNumbers
Definition: COFF.h:289
uint32_t PointerToRawData
Definition: COFF.h:285
uint32_t SizeOfRawData
Definition: COFF.h:284
uint32_t Characteristics
Definition: COFF.h:290
uint16_t NumberOfRelocations
Definition: COFF.h:288
char Name[NameSize]
Definition: COFF.h:281
uint32_t VirtualAddress
Definition: COFF.h:283
uint32_t PointerToLineNumbers
Definition: COFF.h:287
The DOS compatible header at the front of all PE/COFF executables.
Definition: COFF.h:53
support::ulittle16_t AddressOfRelocationTable
Definition: COFF.h:66
support::ulittle32_t AddressOfNewExeHeader
Definition: COFF.h:72
The 32-bit PE header that follows the COFF header.
Definition: COFF.h:104
support::ulittle32_t BaseOfData
Definition: COFF.h:113
The 64-bit PE header that follows the COFF header.
Definition: COFF.h:140
Definition: regcomp.c:192
Common declarations for yaml2obj.