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