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/BinaryStreamWriter.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> 29 30 using namespace llvm; 31 32 namespace { 33 34 /// This parses a yaml stream that represents a COFF object file. 35 /// See docs/yaml2obj for the yaml scheema. 36 struct COFFParser { 37 COFFParser(COFFYAML::Object &Obj, yaml::ErrorHandler EH) 38 : Obj(Obj), SectionTableStart(0), SectionTableSize(0), ErrHandler(EH) { 39 // A COFF string table always starts with a 4 byte size field. Offsets into 40 // it include this size, so allocate it now. 41 StringTable.append(4, char(0)); 42 } 43 44 bool useBigObj() const { 45 return static_cast<int32_t>(Obj.Sections.size()) > 46 COFF::MaxNumberOfSections16; 47 } 48 49 bool isPE() const { return Obj.OptionalHeader.has_value(); } 50 bool is64Bit() const { 51 return Obj.Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 || 52 Obj.Header.Machine == COFF::IMAGE_FILE_MACHINE_ARM64; 53 } 54 55 uint32_t getFileAlignment() const { 56 return Obj.OptionalHeader->Header.FileAlignment; 57 } 58 59 unsigned getHeaderSize() const { 60 return useBigObj() ? COFF::Header32Size : COFF::Header16Size; 61 } 62 63 unsigned getSymbolSize() const { 64 return useBigObj() ? COFF::Symbol32Size : COFF::Symbol16Size; 65 } 66 67 bool parseSections() { 68 for (COFFYAML::Section &Sec : Obj.Sections) { 69 // If the name is less than 8 bytes, store it in place, otherwise 70 // store it in the string table. 71 StringRef Name = Sec.Name; 72 73 if (Name.size() <= COFF::NameSize) { 74 std::copy(Name.begin(), Name.end(), Sec.Header.Name); 75 } else { 76 // Add string to the string table and format the index for output. 77 unsigned Index = getStringIndex(Name); 78 std::string str = utostr(Index); 79 if (str.size() > 7) { 80 ErrHandler("string table got too large"); 81 return false; 82 } 83 Sec.Header.Name[0] = '/'; 84 std::copy(str.begin(), str.end(), Sec.Header.Name + 1); 85 } 86 87 if (Sec.Alignment) { 88 if (Sec.Alignment > 8192) { 89 ErrHandler("section alignment is too large"); 90 return false; 91 } 92 if (!isPowerOf2_32(Sec.Alignment)) { 93 ErrHandler("section alignment is not a power of 2"); 94 return false; 95 } 96 Sec.Header.Characteristics |= (Log2_32(Sec.Alignment) + 1) << 20; 97 } 98 } 99 return true; 100 } 101 102 bool parseSymbols() { 103 for (COFFYAML::Symbol &Sym : Obj.Symbols) { 104 // If the name is less than 8 bytes, store it in place, otherwise 105 // store it in the string table. 106 StringRef Name = Sym.Name; 107 if (Name.size() <= COFF::NameSize) { 108 std::copy(Name.begin(), Name.end(), Sym.Header.Name); 109 } else { 110 // Add string to the string table and format the index for output. 111 unsigned Index = getStringIndex(Name); 112 *reinterpret_cast<support::aligned_ulittle32_t *>(Sym.Header.Name + 4) = 113 Index; 114 } 115 116 Sym.Header.Type = Sym.SimpleType; 117 Sym.Header.Type |= Sym.ComplexType << COFF::SCT_COMPLEX_TYPE_SHIFT; 118 } 119 return true; 120 } 121 122 bool parse() { 123 if (!parseSections()) 124 return false; 125 if (!parseSymbols()) 126 return false; 127 return true; 128 } 129 130 unsigned getStringIndex(StringRef Str) { 131 StringMap<unsigned>::iterator i = StringTableMap.find(Str); 132 if (i == StringTableMap.end()) { 133 unsigned Index = StringTable.size(); 134 StringTable.append(Str.begin(), Str.end()); 135 StringTable.push_back(0); 136 StringTableMap[Str] = Index; 137 return Index; 138 } 139 return i->second; 140 } 141 142 COFFYAML::Object &Obj; 143 144 codeview::StringsAndChecksums StringsAndChecksums; 145 BumpPtrAllocator Allocator; 146 StringMap<unsigned> StringTableMap; 147 std::string StringTable; 148 uint32_t SectionTableStart; 149 uint32_t SectionTableSize; 150 151 yaml::ErrorHandler ErrHandler; 152 }; 153 154 enum { DOSStubSize = 128 }; 155 156 } // end anonymous namespace 157 158 // Take a CP and assign addresses and sizes to everything. Returns false if the 159 // layout is not valid to do. 160 static bool layoutOptionalHeader(COFFParser &CP) { 161 if (!CP.isPE()) 162 return true; 163 unsigned PEHeaderSize = CP.is64Bit() ? sizeof(object::pe32plus_header) 164 : sizeof(object::pe32_header); 165 CP.Obj.Header.SizeOfOptionalHeader = 166 PEHeaderSize + sizeof(object::data_directory) * 167 CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize; 168 return true; 169 } 170 171 static yaml::BinaryRef 172 toDebugS(ArrayRef<CodeViewYAML::YAMLDebugSubsection> Subsections, 173 const codeview::StringsAndChecksums &SC, BumpPtrAllocator &Allocator) { 174 using namespace codeview; 175 ExitOnError Err("Error occurred writing .debug$S section"); 176 auto CVSS = 177 Err(CodeViewYAML::toCodeViewSubsectionList(Allocator, Subsections, SC)); 178 179 std::vector<DebugSubsectionRecordBuilder> Builders; 180 uint32_t Size = sizeof(uint32_t); 181 for (auto &SS : CVSS) { 182 DebugSubsectionRecordBuilder B(SS); 183 Size += B.calculateSerializedLength(); 184 Builders.push_back(std::move(B)); 185 } 186 uint8_t *Buffer = Allocator.Allocate<uint8_t>(Size); 187 MutableArrayRef<uint8_t> Output(Buffer, Size); 188 BinaryStreamWriter Writer(Output, support::little); 189 190 Err(Writer.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC)); 191 for (const auto &B : Builders) { 192 Err(B.commit(Writer, CodeViewContainer::ObjectFile)); 193 } 194 return {Output}; 195 } 196 197 // Take a CP and assign addresses and sizes to everything. Returns false if the 198 // layout is not valid to do. 199 static bool layoutCOFF(COFFParser &CP) { 200 // The section table starts immediately after the header, including the 201 // optional header. 202 CP.SectionTableStart = 203 CP.getHeaderSize() + CP.Obj.Header.SizeOfOptionalHeader; 204 if (CP.isPE()) 205 CP.SectionTableStart += DOSStubSize + sizeof(COFF::PEMagic); 206 CP.SectionTableSize = COFF::SectionSize * CP.Obj.Sections.size(); 207 208 uint32_t CurrentSectionDataOffset = 209 CP.SectionTableStart + CP.SectionTableSize; 210 211 for (COFFYAML::Section &S : CP.Obj.Sections) { 212 // We support specifying exactly one of SectionData or Subsections. So if 213 // there is already some SectionData, then we don't need to do any of this. 214 if (S.Name == ".debug$S" && S.SectionData.binary_size() == 0) { 215 CodeViewYAML::initializeStringsAndChecksums(S.DebugS, 216 CP.StringsAndChecksums); 217 if (CP.StringsAndChecksums.hasChecksums() && 218 CP.StringsAndChecksums.hasStrings()) 219 break; 220 } 221 } 222 223 // Assign each section data address consecutively. 224 for (COFFYAML::Section &S : CP.Obj.Sections) { 225 if (S.Name == ".debug$S") { 226 if (S.SectionData.binary_size() == 0) { 227 assert(CP.StringsAndChecksums.hasStrings() && 228 "Object file does not have debug string table!"); 229 230 S.SectionData = 231 toDebugS(S.DebugS, CP.StringsAndChecksums, CP.Allocator); 232 } 233 } else if (S.Name == ".debug$T") { 234 if (S.SectionData.binary_size() == 0) 235 S.SectionData = CodeViewYAML::toDebugT(S.DebugT, CP.Allocator, S.Name); 236 } else if (S.Name == ".debug$P") { 237 if (S.SectionData.binary_size() == 0) 238 S.SectionData = CodeViewYAML::toDebugT(S.DebugP, CP.Allocator, S.Name); 239 } else if (S.Name == ".debug$H") { 240 if (S.DebugH && S.SectionData.binary_size() == 0) 241 S.SectionData = CodeViewYAML::toDebugH(*S.DebugH, CP.Allocator); 242 } 243 244 if (S.SectionData.binary_size() > 0) { 245 CurrentSectionDataOffset = alignTo(CurrentSectionDataOffset, 246 CP.isPE() ? CP.getFileAlignment() : 4); 247 S.Header.SizeOfRawData = S.SectionData.binary_size(); 248 if (CP.isPE()) 249 S.Header.SizeOfRawData = 250 alignTo(S.Header.SizeOfRawData, CP.getFileAlignment()); 251 S.Header.PointerToRawData = CurrentSectionDataOffset; 252 CurrentSectionDataOffset += S.Header.SizeOfRawData; 253 if (!S.Relocations.empty()) { 254 S.Header.PointerToRelocations = CurrentSectionDataOffset; 255 if (S.Header.Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL) { 256 S.Header.NumberOfRelocations = 0xffff; 257 CurrentSectionDataOffset += COFF::RelocationSize; 258 } else 259 S.Header.NumberOfRelocations = S.Relocations.size(); 260 CurrentSectionDataOffset += S.Relocations.size() * COFF::RelocationSize; 261 } 262 } else { 263 // Leave SizeOfRawData unaltered. For .bss sections in object files, it 264 // carries the section size. 265 S.Header.PointerToRawData = 0; 266 } 267 } 268 269 uint32_t SymbolTableStart = CurrentSectionDataOffset; 270 271 // Calculate number of symbols. 272 uint32_t NumberOfSymbols = 0; 273 for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(), 274 e = CP.Obj.Symbols.end(); 275 i != e; ++i) { 276 uint32_t NumberOfAuxSymbols = 0; 277 if (i->FunctionDefinition) 278 NumberOfAuxSymbols += 1; 279 if (i->bfAndefSymbol) 280 NumberOfAuxSymbols += 1; 281 if (i->WeakExternal) 282 NumberOfAuxSymbols += 1; 283 if (!i->File.empty()) 284 NumberOfAuxSymbols += 285 (i->File.size() + CP.getSymbolSize() - 1) / CP.getSymbolSize(); 286 if (i->SectionDefinition) 287 NumberOfAuxSymbols += 1; 288 if (i->CLRToken) 289 NumberOfAuxSymbols += 1; 290 i->Header.NumberOfAuxSymbols = NumberOfAuxSymbols; 291 NumberOfSymbols += 1 + NumberOfAuxSymbols; 292 } 293 294 // Store all the allocated start addresses in the header. 295 CP.Obj.Header.NumberOfSections = CP.Obj.Sections.size(); 296 CP.Obj.Header.NumberOfSymbols = NumberOfSymbols; 297 if (NumberOfSymbols > 0 || CP.StringTable.size() > 4) 298 CP.Obj.Header.PointerToSymbolTable = SymbolTableStart; 299 else 300 CP.Obj.Header.PointerToSymbolTable = 0; 301 302 *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0]) = 303 CP.StringTable.size(); 304 305 return true; 306 } 307 308 template <typename value_type> struct binary_le_impl { 309 value_type Value; 310 binary_le_impl(value_type V) : Value(V) {} 311 }; 312 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; 321 } 322 323 template <typename value_type> 324 binary_le_impl<value_type> binary_le(value_type V) { 325 return binary_le_impl<value_type>(V); 326 } 327 328 template <size_t NumBytes> struct zeros_impl {}; 329 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; 336 } 337 338 template <typename T> zeros_impl<sizeof(T)> zeros(const T &) { 339 return zeros_impl<sizeof(T)>(); 340 } 341 342 template <typename T> 343 static uint32_t initializeOptionalHeader(COFFParser &CP, uint16_t Magic, 344 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); 368 } 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 = CP.Obj.OptionalHeader->Header.MajorImageVersion; 380 Header->MinorImageVersion = CP.Obj.OptionalHeader->Header.MinorImageVersion; 381 Header->MajorSubsystemVersion = 382 CP.Obj.OptionalHeader->Header.MajorSubsystemVersion; 383 Header->MinorSubsystemVersion = 384 CP.Obj.OptionalHeader->Header.MinorSubsystemVersion; 385 Header->SizeOfImage = SizeOfImage; 386 Header->SizeOfHeaders = SizeOfHeaders; 387 Header->Subsystem = CP.Obj.OptionalHeader->Header.Subsystem; 388 Header->DLLCharacteristics = CP.Obj.OptionalHeader->Header.DLLCharacteristics; 389 Header->SizeOfStackReserve = CP.Obj.OptionalHeader->Header.SizeOfStackReserve; 390 Header->SizeOfStackCommit = CP.Obj.OptionalHeader->Header.SizeOfStackCommit; 391 Header->SizeOfHeapReserve = CP.Obj.OptionalHeader->Header.SizeOfHeapReserve; 392 Header->SizeOfHeapCommit = CP.Obj.OptionalHeader->Header.SizeOfHeapCommit; 393 Header->NumberOfRvaAndSize = CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize; 394 return BaseOfData; 395 } 396 397 static bool writeCOFF(COFFParser &CP, raw_ostream &OS) { 398 if (CP.isPE()) { 399 // PE files start with a DOS stub. 400 object::dos_header DH; 401 memset(&DH, 0, sizeof(DH)); 402 403 // DOS EXEs start with "MZ" magic. 404 DH.Magic[0] = 'M'; 405 DH.Magic[1] = 'Z'; 406 // Initializing the AddressOfRelocationTable is strictly optional but 407 // mollifies certain tools which expect it to have a value greater than 408 // 0x40. 409 DH.AddressOfRelocationTable = sizeof(DH); 410 // This is the address of the PE signature. 411 DH.AddressOfNewExeHeader = DOSStubSize; 412 413 // Write out our DOS stub. 414 OS.write(reinterpret_cast<char *>(&DH), sizeof(DH)); 415 // Write padding until we reach the position of where our PE signature 416 // should live. 417 OS.write_zeros(DOSStubSize - sizeof(DH)); 418 // Write out the PE signature. 419 OS.write(COFF::PEMagic, sizeof(COFF::PEMagic)); 420 } 421 if (CP.useBigObj()) { 422 OS << binary_le(static_cast<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN)) 423 << binary_le(static_cast<uint16_t>(0xffff)) 424 << binary_le( 425 static_cast<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion)) 426 << binary_le(CP.Obj.Header.Machine) 427 << binary_le(CP.Obj.Header.TimeDateStamp); 428 OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)); 429 OS << zeros(uint32_t(0)) << zeros(uint32_t(0)) << zeros(uint32_t(0)) 430 << zeros(uint32_t(0)) << binary_le(CP.Obj.Header.NumberOfSections) 431 << binary_le(CP.Obj.Header.PointerToSymbolTable) 432 << binary_le(CP.Obj.Header.NumberOfSymbols); 433 } else { 434 OS << binary_le(CP.Obj.Header.Machine) 435 << binary_le(static_cast<int16_t>(CP.Obj.Header.NumberOfSections)) 436 << binary_le(CP.Obj.Header.TimeDateStamp) 437 << binary_le(CP.Obj.Header.PointerToSymbolTable) 438 << binary_le(CP.Obj.Header.NumberOfSymbols) 439 << binary_le(CP.Obj.Header.SizeOfOptionalHeader) 440 << binary_le(CP.Obj.Header.Characteristics); 441 } 442 if (CP.isPE()) { 443 if (CP.is64Bit()) { 444 object::pe32plus_header PEH; 445 initializeOptionalHeader(CP, COFF::PE32Header::PE32_PLUS, &PEH); 446 OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH)); 447 } else { 448 object::pe32_header PEH; 449 uint32_t BaseOfData = 450 initializeOptionalHeader(CP, COFF::PE32Header::PE32, &PEH); 451 PEH.BaseOfData = BaseOfData; 452 OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH)); 453 } 454 for (uint32_t I = 0; I < CP.Obj.OptionalHeader->Header.NumberOfRvaAndSize; 455 ++I) { 456 const Optional<COFF::DataDirectory> *DataDirectories = 457 CP.Obj.OptionalHeader->DataDirectories; 458 uint32_t NumDataDir = sizeof(CP.Obj.OptionalHeader->DataDirectories) / 459 sizeof(Optional<COFF::DataDirectory>); 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) { 473 OS.write(S.Header.Name, COFF::NameSize); 474 OS << binary_le(S.Header.VirtualSize) 475 << binary_le(S.Header.VirtualAddress) 476 << binary_le(S.Header.SizeOfRawData) 477 << binary_le(S.Header.PointerToRawData) 478 << binary_le(S.Header.PointerToRelocations) 479 << binary_le(S.Header.PointerToLineNumbers) 480 << binary_le(S.Header.NumberOfRelocations) 481 << binary_le(S.Header.NumberOfLineNumbers) 482 << binary_le(S.Header.Characteristics); 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; 497 assert(S.Header.PointerToRawData >= OS.tell()); 498 OS.write_zeros(S.Header.PointerToRawData - OS.tell()); 499 S.SectionData.writeAsBinary(OS); 500 assert(S.Header.SizeOfRawData >= S.SectionData.binary_size()); 501 OS.write_zeros(S.Header.SizeOfRawData - S.SectionData.binary_size()); 502 if (S.Header.Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL) 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()) 510 WithColor::error() 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 591 namespace llvm { 592 namespace yaml { 593 594 bool yaml2coff(llvm::COFFYAML::Object &Doc, raw_ostream &Out, 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 620