[ARM] Basic And/Or/Xor handling for MVE predicates
[llvm-complete.git] / tools / yaml2obj / yaml2coff.cpp
blob3afbd5848af7423b19ce7c15d83ba9447207dd6a
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 //===----------------------------------------------------------------------===//
14 #include "yaml2obj.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
20 #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
21 #include "llvm/Object/COFF.h"
22 #include "llvm/ObjectYAML/ObjectYAML.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/Support/WithColor.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <vector>
30 using namespace llvm;
32 /// This parses a yaml stream that represents a COFF object file.
33 /// See docs/yaml2obj for the yaml scheema.
34 struct COFFParser {
35 COFFParser(COFFYAML::Object &Obj)
36 : Obj(Obj), SectionTableStart(0), SectionTableSize(0) {
37 // A COFF string table always starts with a 4 byte size field. Offsets into
38 // it include this size, so allocate it now.
39 StringTable.append(4, char(0));
42 bool useBigObj() const {
43 return static_cast<int32_t>(Obj.Sections.size()) >
44 COFF::MaxNumberOfSections16;
47 bool isPE() const { return Obj.OptionalHeader.hasValue(); }
48 bool is64Bit() const {
49 return Obj.Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 ||
50 Obj.Header.Machine == COFF::IMAGE_FILE_MACHINE_ARM64;
53 uint32_t getFileAlignment() const {
54 return Obj.OptionalHeader->Header.FileAlignment;
57 unsigned getHeaderSize() const {
58 return useBigObj() ? COFF::Header32Size : COFF::Header16Size;
61 unsigned getSymbolSize() const {
62 return useBigObj() ? COFF::Symbol32Size : COFF::Symbol16Size;
65 bool parseSections() {
66 for (std::vector<COFFYAML::Section>::iterator i = Obj.Sections.begin(),
67 e = Obj.Sections.end(); i != e; ++i) {
68 COFFYAML::Section &Sec = *i;
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;
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 errs() << "String table got too large\n";
82 return false;
84 Sec.Header.Name[0] = '/';
85 std::copy(str.begin(), str.end(), Sec.Header.Name + 1);
88 if (Sec.Alignment) {
89 if (Sec.Alignment > 8192) {
90 errs() << "Section alignment is too large\n";
91 return false;
93 if (!isPowerOf2_32(Sec.Alignment)) {
94 errs() << "Section alignment is not a power of 2\n";
95 return false;
97 Sec.Header.Characteristics |= (Log2_32(Sec.Alignment) + 1) << 20;
100 return true;
103 bool parseSymbols() {
104 for (std::vector<COFFYAML::Symbol>::iterator i = Obj.Symbols.begin(),
105 e = Obj.Symbols.end(); i != e; ++i) {
106 COFFYAML::Symbol &Sym = *i;
108 // If the name is less than 8 bytes, store it in place, otherwise
109 // store it in the string table.
110 StringRef Name = Sym.Name;
111 if (Name.size() <= COFF::NameSize) {
112 std::copy(Name.begin(), Name.end(), Sym.Header.Name);
113 } else {
114 // Add string to the string table and format the index for output.
115 unsigned Index = getStringIndex(Name);
116 *reinterpret_cast<support::aligned_ulittle32_t*>(
117 Sym.Header.Name + 4) = Index;
120 Sym.Header.Type = Sym.SimpleType;
121 Sym.Header.Type |= Sym.ComplexType << COFF::SCT_COMPLEX_TYPE_SHIFT;
123 return true;
126 bool parse() {
127 if (!parseSections())
128 return false;
129 if (!parseSymbols())
130 return false;
131 return true;
134 unsigned getStringIndex(StringRef Str) {
135 StringMap<unsigned>::iterator i = StringTableMap.find(Str);
136 if (i == StringTableMap.end()) {
137 unsigned Index = StringTable.size();
138 StringTable.append(Str.begin(), Str.end());
139 StringTable.push_back(0);
140 StringTableMap[Str] = Index;
141 return Index;
143 return i->second;
146 COFFYAML::Object &Obj;
148 codeview::StringsAndChecksums StringsAndChecksums;
149 BumpPtrAllocator Allocator;
150 StringMap<unsigned> StringTableMap;
151 std::string StringTable;
152 uint32_t SectionTableStart;
153 uint32_t SectionTableSize;
156 // Take a CP and assign addresses and sizes to everything. Returns false if the
157 // layout is not valid to do.
158 static bool layoutOptionalHeader(COFFParser &CP) {
159 if (!CP.isPE())
160 return true;
161 unsigned PEHeaderSize = CP.is64Bit() ? sizeof(object::pe32plus_header)
162 : sizeof(object::pe32_header);
163 CP.Obj.Header.SizeOfOptionalHeader =
164 PEHeaderSize +
165 sizeof(object::data_directory) * (COFF::NUM_DATA_DIRECTORIES + 1);
166 return true;
169 namespace {
170 enum { DOSStubSize = 128 };
173 static yaml::BinaryRef
174 toDebugS(ArrayRef<CodeViewYAML::YAMLDebugSubsection> Subsections,
175 const codeview::StringsAndChecksums &SC, BumpPtrAllocator &Allocator) {
176 using namespace codeview;
177 ExitOnError Err("Error occurred writing .debug$S section");
178 auto CVSS =
179 Err(CodeViewYAML::toCodeViewSubsectionList(Allocator, Subsections, SC));
181 std::vector<DebugSubsectionRecordBuilder> Builders;
182 uint32_t Size = sizeof(uint32_t);
183 for (auto &SS : CVSS) {
184 DebugSubsectionRecordBuilder B(SS, CodeViewContainer::ObjectFile);
185 Size += B.calculateSerializedLength();
186 Builders.push_back(std::move(B));
188 uint8_t *Buffer = Allocator.Allocate<uint8_t>(Size);
189 MutableArrayRef<uint8_t> Output(Buffer, Size);
190 BinaryStreamWriter Writer(Output, support::little);
192 Err(Writer.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC));
193 for (const auto &B : Builders) {
194 Err(B.commit(Writer));
196 return {Output};
199 // Take a CP and assign addresses and sizes to everything. Returns false if the
200 // layout is not valid to do.
201 static bool layoutCOFF(COFFParser &CP) {
202 // The section table starts immediately after the header, including the
203 // optional header.
204 CP.SectionTableStart =
205 CP.getHeaderSize() + CP.Obj.Header.SizeOfOptionalHeader;
206 if (CP.isPE())
207 CP.SectionTableStart += DOSStubSize + sizeof(COFF::PEMagic);
208 CP.SectionTableSize = COFF::SectionSize * CP.Obj.Sections.size();
210 uint32_t CurrentSectionDataOffset =
211 CP.SectionTableStart + CP.SectionTableSize;
213 for (COFFYAML::Section &S : CP.Obj.Sections) {
214 // We support specifying exactly one of SectionData or Subsections. So if
215 // there is already some SectionData, then we don't need to do any of this.
216 if (S.Name == ".debug$S" && S.SectionData.binary_size() == 0) {
217 CodeViewYAML::initializeStringsAndChecksums(S.DebugS,
218 CP.StringsAndChecksums);
219 if (CP.StringsAndChecksums.hasChecksums() &&
220 CP.StringsAndChecksums.hasStrings())
221 break;
225 // Assign each section data address consecutively.
226 for (COFFYAML::Section &S : CP.Obj.Sections) {
227 if (S.Name == ".debug$S") {
228 if (S.SectionData.binary_size() == 0) {
229 assert(CP.StringsAndChecksums.hasStrings() &&
230 "Object file does not have debug string table!");
232 S.SectionData =
233 toDebugS(S.DebugS, CP.StringsAndChecksums, CP.Allocator);
235 } else if (S.Name == ".debug$T") {
236 if (S.SectionData.binary_size() == 0)
237 S.SectionData = CodeViewYAML::toDebugT(S.DebugT, CP.Allocator, S.Name);
238 } else if (S.Name == ".debug$P") {
239 if (S.SectionData.binary_size() == 0)
240 S.SectionData = CodeViewYAML::toDebugT(S.DebugP, CP.Allocator, S.Name);
241 } else if (S.Name == ".debug$H") {
242 if (S.DebugH.hasValue() && S.SectionData.binary_size() == 0)
243 S.SectionData = CodeViewYAML::toDebugH(*S.DebugH, CP.Allocator);
246 if (S.SectionData.binary_size() > 0) {
247 CurrentSectionDataOffset = alignTo(CurrentSectionDataOffset,
248 CP.isPE() ? CP.getFileAlignment() : 4);
249 S.Header.SizeOfRawData = S.SectionData.binary_size();
250 if (CP.isPE())
251 S.Header.SizeOfRawData =
252 alignTo(S.Header.SizeOfRawData, CP.getFileAlignment());
253 S.Header.PointerToRawData = CurrentSectionDataOffset;
254 CurrentSectionDataOffset += S.Header.SizeOfRawData;
255 if (!S.Relocations.empty()) {
256 S.Header.PointerToRelocations = CurrentSectionDataOffset;
257 S.Header.NumberOfRelocations = S.Relocations.size();
258 CurrentSectionDataOffset +=
259 S.Header.NumberOfRelocations * COFF::RelocationSize;
261 } else {
262 // Leave SizeOfRawData unaltered. For .bss sections in object files, it
263 // carries the section size.
264 S.Header.PointerToRawData = 0;
268 uint32_t SymbolTableStart = CurrentSectionDataOffset;
270 // Calculate number of symbols.
271 uint32_t NumberOfSymbols = 0;
272 for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(),
273 e = CP.Obj.Symbols.end();
274 i != e; ++i) {
275 uint32_t NumberOfAuxSymbols = 0;
276 if (i->FunctionDefinition)
277 NumberOfAuxSymbols += 1;
278 if (i->bfAndefSymbol)
279 NumberOfAuxSymbols += 1;
280 if (i->WeakExternal)
281 NumberOfAuxSymbols += 1;
282 if (!i->File.empty())
283 NumberOfAuxSymbols +=
284 (i->File.size() + CP.getSymbolSize() - 1) / CP.getSymbolSize();
285 if (i->SectionDefinition)
286 NumberOfAuxSymbols += 1;
287 if (i->CLRToken)
288 NumberOfAuxSymbols += 1;
289 i->Header.NumberOfAuxSymbols = NumberOfAuxSymbols;
290 NumberOfSymbols += 1 + NumberOfAuxSymbols;
293 // Store all the allocated start addresses in the header.
294 CP.Obj.Header.NumberOfSections = CP.Obj.Sections.size();
295 CP.Obj.Header.NumberOfSymbols = NumberOfSymbols;
296 if (NumberOfSymbols > 0 || CP.StringTable.size() > 4)
297 CP.Obj.Header.PointerToSymbolTable = SymbolTableStart;
298 else
299 CP.Obj.Header.PointerToSymbolTable = 0;
301 *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0])
302 = CP.StringTable.size();
304 return true;
307 template <typename value_type>
308 struct binary_le_impl {
309 value_type Value;
310 binary_le_impl(value_type V) : Value(V) {}
313 template <typename value_type>
314 raw_ostream &operator <<( raw_ostream &OS
315 , const binary_le_impl<value_type> &BLE) {
316 char Buffer[sizeof(BLE.Value)];
317 support::endian::write<value_type, support::little, support::unaligned>(
318 Buffer, BLE.Value);
319 OS.write(Buffer, sizeof(BLE.Value));
320 return OS;
323 template <typename value_type>
324 binary_le_impl<value_type> binary_le(value_type V) {
325 return binary_le_impl<value_type>(V);
328 template <size_t NumBytes> struct zeros_impl {};
330 template <size_t NumBytes>
331 raw_ostream &operator<<(raw_ostream &OS, const zeros_impl<NumBytes> &) {
332 char Buffer[NumBytes];
333 memset(Buffer, 0, sizeof(Buffer));
334 OS.write(Buffer, sizeof(Buffer));
335 return OS;
338 template <typename T>
339 zeros_impl<sizeof(T)> zeros(const T &) {
340 return zeros_impl<sizeof(T)>();
343 template <typename T>
344 static uint32_t initializeOptionalHeader(COFFParser &CP, uint16_t Magic, T Header) {
345 memset(Header, 0, sizeof(*Header));
346 Header->Magic = Magic;
347 Header->SectionAlignment = CP.Obj.OptionalHeader->Header.SectionAlignment;
348 Header->FileAlignment = CP.Obj.OptionalHeader->Header.FileAlignment;
349 uint32_t SizeOfCode = 0, SizeOfInitializedData = 0,
350 SizeOfUninitializedData = 0;
351 uint32_t SizeOfHeaders = alignTo(CP.SectionTableStart + CP.SectionTableSize,
352 Header->FileAlignment);
353 uint32_t SizeOfImage = alignTo(SizeOfHeaders, Header->SectionAlignment);
354 uint32_t BaseOfData = 0;
355 for (const COFFYAML::Section &S : CP.Obj.Sections) {
356 if (S.Header.Characteristics & COFF::IMAGE_SCN_CNT_CODE)
357 SizeOfCode += S.Header.SizeOfRawData;
358 if (S.Header.Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
359 SizeOfInitializedData += S.Header.SizeOfRawData;
360 if (S.Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
361 SizeOfUninitializedData += S.Header.SizeOfRawData;
362 if (S.Name.equals(".text"))
363 Header->BaseOfCode = S.Header.VirtualAddress; // RVA
364 else if (S.Name.equals(".data"))
365 BaseOfData = S.Header.VirtualAddress; // RVA
366 if (S.Header.VirtualAddress)
367 SizeOfImage += alignTo(S.Header.VirtualSize, Header->SectionAlignment);
369 Header->SizeOfCode = SizeOfCode;
370 Header->SizeOfInitializedData = SizeOfInitializedData;
371 Header->SizeOfUninitializedData = SizeOfUninitializedData;
372 Header->AddressOfEntryPoint =
373 CP.Obj.OptionalHeader->Header.AddressOfEntryPoint; // RVA
374 Header->ImageBase = CP.Obj.OptionalHeader->Header.ImageBase;
375 Header->MajorOperatingSystemVersion =
376 CP.Obj.OptionalHeader->Header.MajorOperatingSystemVersion;
377 Header->MinorOperatingSystemVersion =
378 CP.Obj.OptionalHeader->Header.MinorOperatingSystemVersion;
379 Header->MajorImageVersion =
380 CP.Obj.OptionalHeader->Header.MajorImageVersion;
381 Header->MinorImageVersion =
382 CP.Obj.OptionalHeader->Header.MinorImageVersion;
383 Header->MajorSubsystemVersion =
384 CP.Obj.OptionalHeader->Header.MajorSubsystemVersion;
385 Header->MinorSubsystemVersion =
386 CP.Obj.OptionalHeader->Header.MinorSubsystemVersion;
387 Header->SizeOfImage = SizeOfImage;
388 Header->SizeOfHeaders = SizeOfHeaders;
389 Header->Subsystem = CP.Obj.OptionalHeader->Header.Subsystem;
390 Header->DLLCharacteristics = CP.Obj.OptionalHeader->Header.DLLCharacteristics;
391 Header->SizeOfStackReserve = CP.Obj.OptionalHeader->Header.SizeOfStackReserve;
392 Header->SizeOfStackCommit = CP.Obj.OptionalHeader->Header.SizeOfStackCommit;
393 Header->SizeOfHeapReserve = CP.Obj.OptionalHeader->Header.SizeOfHeapReserve;
394 Header->SizeOfHeapCommit = CP.Obj.OptionalHeader->Header.SizeOfHeapCommit;
395 Header->NumberOfRvaAndSize = COFF::NUM_DATA_DIRECTORIES + 1;
396 return BaseOfData;
399 static bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
400 if (CP.isPE()) {
401 // PE files start with a DOS stub.
402 object::dos_header DH;
403 memset(&DH, 0, sizeof(DH));
405 // DOS EXEs start with "MZ" magic.
406 DH.Magic[0] = 'M';
407 DH.Magic[1] = 'Z';
408 // Initializing the AddressOfRelocationTable is strictly optional but
409 // mollifies certain tools which expect it to have a value greater than
410 // 0x40.
411 DH.AddressOfRelocationTable = sizeof(DH);
412 // This is the address of the PE signature.
413 DH.AddressOfNewExeHeader = DOSStubSize;
415 // Write out our DOS stub.
416 OS.write(reinterpret_cast<char *>(&DH), sizeof(DH));
417 // Write padding until we reach the position of where our PE signature
418 // should live.
419 OS.write_zeros(DOSStubSize - sizeof(DH));
420 // Write out the PE signature.
421 OS.write(COFF::PEMagic, sizeof(COFF::PEMagic));
423 if (CP.useBigObj()) {
424 OS << binary_le(static_cast<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN))
425 << binary_le(static_cast<uint16_t>(0xffff))
426 << binary_le(static_cast<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion))
427 << binary_le(CP.Obj.Header.Machine)
428 << binary_le(CP.Obj.Header.TimeDateStamp);
429 OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic));
430 OS << zeros(uint32_t(0))
431 << zeros(uint32_t(0))
432 << zeros(uint32_t(0))
433 << zeros(uint32_t(0))
434 << binary_le(CP.Obj.Header.NumberOfSections)
435 << binary_le(CP.Obj.Header.PointerToSymbolTable)
436 << binary_le(CP.Obj.Header.NumberOfSymbols);
437 } else {
438 OS << binary_le(CP.Obj.Header.Machine)
439 << binary_le(static_cast<int16_t>(CP.Obj.Header.NumberOfSections))
440 << binary_le(CP.Obj.Header.TimeDateStamp)
441 << binary_le(CP.Obj.Header.PointerToSymbolTable)
442 << binary_le(CP.Obj.Header.NumberOfSymbols)
443 << binary_le(CP.Obj.Header.SizeOfOptionalHeader)
444 << binary_le(CP.Obj.Header.Characteristics);
446 if (CP.isPE()) {
447 if (CP.is64Bit()) {
448 object::pe32plus_header PEH;
449 initializeOptionalHeader(CP, COFF::PE32Header::PE32_PLUS, &PEH);
450 OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH));
451 } else {
452 object::pe32_header PEH;
453 uint32_t BaseOfData = initializeOptionalHeader(CP, COFF::PE32Header::PE32, &PEH);
454 PEH.BaseOfData = BaseOfData;
455 OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH));
457 for (const Optional<COFF::DataDirectory> &DD :
458 CP.Obj.OptionalHeader->DataDirectories) {
459 if (!DD.hasValue()) {
460 OS << zeros(uint32_t(0));
461 OS << zeros(uint32_t(0));
462 } else {
463 OS << binary_le(DD->RelativeVirtualAddress);
464 OS << binary_le(DD->Size);
467 OS << zeros(uint32_t(0));
468 OS << zeros(uint32_t(0));
471 assert(OS.tell() == CP.SectionTableStart);
472 // Output section table.
473 for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
474 e = CP.Obj.Sections.end();
475 i != e; ++i) {
476 OS.write(i->Header.Name, COFF::NameSize);
477 OS << binary_le(i->Header.VirtualSize)
478 << binary_le(i->Header.VirtualAddress)
479 << binary_le(i->Header.SizeOfRawData)
480 << binary_le(i->Header.PointerToRawData)
481 << binary_le(i->Header.PointerToRelocations)
482 << binary_le(i->Header.PointerToLineNumbers)
483 << binary_le(i->Header.NumberOfRelocations)
484 << binary_le(i->Header.NumberOfLineNumbers)
485 << binary_le(i->Header.Characteristics);
487 assert(OS.tell() == CP.SectionTableStart + CP.SectionTableSize);
489 unsigned CurSymbol = 0;
490 StringMap<unsigned> SymbolTableIndexMap;
491 for (std::vector<COFFYAML::Symbol>::iterator I = CP.Obj.Symbols.begin(),
492 E = CP.Obj.Symbols.end();
493 I != E; ++I) {
494 SymbolTableIndexMap[I->Name] = CurSymbol;
495 CurSymbol += 1 + I->Header.NumberOfAuxSymbols;
498 // Output section data.
499 for (const COFFYAML::Section &S : CP.Obj.Sections) {
500 if (S.Header.SizeOfRawData == 0 || S.Header.PointerToRawData == 0)
501 continue;
502 assert(S.Header.PointerToRawData >= OS.tell());
503 OS.write_zeros(S.Header.PointerToRawData - OS.tell());
504 S.SectionData.writeAsBinary(OS);
505 assert(S.Header.SizeOfRawData >= S.SectionData.binary_size());
506 OS.write_zeros(S.Header.SizeOfRawData - S.SectionData.binary_size());
507 for (const COFFYAML::Relocation &R : S.Relocations) {
508 uint32_t SymbolTableIndex;
509 if (R.SymbolTableIndex) {
510 if (!R.SymbolName.empty())
511 WithColor::error()
512 << "Both SymbolName and SymbolTableIndex specified\n";
513 SymbolTableIndex = *R.SymbolTableIndex;
514 } else {
515 SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];
517 OS << binary_le(R.VirtualAddress)
518 << binary_le(SymbolTableIndex)
519 << binary_le(R.Type);
523 // Output symbol table.
525 for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.Symbols.begin(),
526 e = CP.Obj.Symbols.end();
527 i != e; ++i) {
528 OS.write(i->Header.Name, COFF::NameSize);
529 OS << binary_le(i->Header.Value);
530 if (CP.useBigObj())
531 OS << binary_le(i->Header.SectionNumber);
532 else
533 OS << binary_le(static_cast<int16_t>(i->Header.SectionNumber));
534 OS << binary_le(i->Header.Type)
535 << binary_le(i->Header.StorageClass)
536 << binary_le(i->Header.NumberOfAuxSymbols);
538 if (i->FunctionDefinition) {
539 OS << binary_le(i->FunctionDefinition->TagIndex)
540 << binary_le(i->FunctionDefinition->TotalSize)
541 << binary_le(i->FunctionDefinition->PointerToLinenumber)
542 << binary_le(i->FunctionDefinition->PointerToNextFunction)
543 << zeros(i->FunctionDefinition->unused);
544 OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
546 if (i->bfAndefSymbol) {
547 OS << zeros(i->bfAndefSymbol->unused1)
548 << binary_le(i->bfAndefSymbol->Linenumber)
549 << zeros(i->bfAndefSymbol->unused2)
550 << binary_le(i->bfAndefSymbol->PointerToNextFunction)
551 << zeros(i->bfAndefSymbol->unused3);
552 OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
554 if (i->WeakExternal) {
555 OS << binary_le(i->WeakExternal->TagIndex)
556 << binary_le(i->WeakExternal->Characteristics)
557 << zeros(i->WeakExternal->unused);
558 OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
560 if (!i->File.empty()) {
561 unsigned SymbolSize = CP.getSymbolSize();
562 uint32_t NumberOfAuxRecords =
563 (i->File.size() + SymbolSize - 1) / SymbolSize;
564 uint32_t NumberOfAuxBytes = NumberOfAuxRecords * SymbolSize;
565 uint32_t NumZeros = NumberOfAuxBytes - i->File.size();
566 OS.write(i->File.data(), i->File.size());
567 OS.write_zeros(NumZeros);
569 if (i->SectionDefinition) {
570 OS << binary_le(i->SectionDefinition->Length)
571 << binary_le(i->SectionDefinition->NumberOfRelocations)
572 << binary_le(i->SectionDefinition->NumberOfLinenumbers)
573 << binary_le(i->SectionDefinition->CheckSum)
574 << binary_le(static_cast<int16_t>(i->SectionDefinition->Number))
575 << binary_le(i->SectionDefinition->Selection)
576 << zeros(i->SectionDefinition->unused)
577 << binary_le(static_cast<int16_t>(i->SectionDefinition->Number >> 16));
578 OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
580 if (i->CLRToken) {
581 OS << binary_le(i->CLRToken->AuxType)
582 << zeros(i->CLRToken->unused1)
583 << binary_le(i->CLRToken->SymbolTableIndex)
584 << zeros(i->CLRToken->unused2);
585 OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
589 // Output string table.
590 if (CP.Obj.Header.PointerToSymbolTable)
591 OS.write(&CP.StringTable[0], CP.StringTable.size());
592 return true;
595 int yaml2coff(llvm::COFFYAML::Object &Doc, raw_ostream &Out) {
596 COFFParser CP(Doc);
597 if (!CP.parse()) {
598 errs() << "yaml2obj: Failed to parse YAML file!\n";
599 return 1;
602 if (!layoutOptionalHeader(CP)) {
603 errs() << "yaml2obj: Failed to layout optional header for COFF file!\n";
604 return 1;
607 if (!layoutCOFF(CP)) {
608 errs() << "yaml2obj: Failed to layout COFF file!\n";
609 return 1;
611 if (!writeCOFF(CP, Out)) {
612 errs() << "yaml2obj: Failed to write COFF file!\n";
613 return 1;
615 return 0;