1 //===- yaml2obj - Convert YAML to a binary object file --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program takes a YAML description of an object file and outputs the
13 // This is used for writing tests that require binary files.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/Support/COFF.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Endian.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/PrettyStackTrace.h"
28 #include "llvm/Support/Signals.h"
29 #include "llvm/Support/SourceMgr.h"
30 #include "llvm/Support/YAMLTraits.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Support/system_error.h"
37 static cl::opt<std::string>
38 Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
41 typename llvm::enable_if_c<std::numeric_limits<T>::is_integer, bool>::type
42 getAs(const llvm::yaml::ScalarNode *SN, T &Result) {
43 SmallString<4> Storage;
44 StringRef Value = SN->getValue(Storage);
45 if (Value.getAsInteger(0, Result))
50 // Given a container with begin and end with ::value_type of a character type.
51 // Iterate through pairs of characters in the the set of [a-fA-F0-9] ignoring
52 // all other characters.
53 struct hex_pair_iterator {
54 StringRef::const_iterator Current, End;
55 typedef SmallVector<char, 2> value_type;
59 hex_pair_iterator(StringRef C)
60 : Current(C.begin()), End(C.end()), IsDone(false) {
66 hex_pair_iterator() : Current(), End(), IsDone(true) {}
68 value_type operator *() const {
72 hex_pair_iterator operator ++() {
73 // We're at the end of the input.
79 for (; Current != End && Pair.size() != 2; ++Current) {
80 // Is a valid hex digit.
81 if ((*Current >= '0' && *Current <= '9') ||
82 (*Current >= 'a' && *Current <= 'f') ||
83 (*Current >= 'A' && *Current <= 'F'))
84 Pair.push_back(*Current);
86 // Hit the end without getting 2 hex digits. Pair is invalid.
92 bool operator ==(const hex_pair_iterator Other) {
93 return (IsDone == Other.IsDone) ||
94 (Current == Other.Current && End == Other.End);
97 bool operator !=(const hex_pair_iterator Other) {
98 return !(*this == Other);
102 template <class ContainerOut>
103 static bool hexStringToByteArray(StringRef Str, ContainerOut &Out) {
104 for (hex_pair_iterator I(Str), E; I != E; ++I) {
105 typename hex_pair_iterator::value_type Pair = *I;
106 typename ContainerOut::value_type Byte;
107 if (StringRef(Pair.data(), 2).getAsInteger(16, Byte))
114 // The structure of the yaml files is not an exact 1:1 match to COFF. In order
115 // to use yaml::IO, we use these structures which are closer to the source.
118 COFF::section Header;
119 StringRef SectionData;
120 std::vector<COFF::relocation> Relocations;
123 memset(&Header, 0, sizeof(COFF::section));
129 COFF::SymbolBaseType SimpleType;
130 COFF::SymbolComplexType ComplexType;
131 StringRef AuxiliaryData;
134 memset(&Header, 0, sizeof(COFF::symbol));
140 std::vector<Section> Sections;
141 std::vector<Symbol> Symbols;
143 memset(&Header, 0, sizeof(COFF::header));
148 /// This parses a yaml stream that represents a COFF object file.
149 /// See docs/yaml2obj for the yaml scheema.
151 COFFParser(COFFYAML::Object &Obj) : Obj(Obj) {
152 // A COFF string table always starts with a 4 byte size field. Offsets into
153 // it include this size, so allocate it now.
154 StringTable.append(4, 0);
157 bool parseSections() {
158 for (std::vector<COFFYAML::Section>::iterator i = Obj.Sections.begin(),
159 e = Obj.Sections.end(); i != e; ++i) {
160 COFFYAML::Section &Sec = *i;
162 // If the name is less than 8 bytes, store it in place, otherwise
163 // store it in the string table.
164 StringRef Name = Sec.Name;
166 if (Name.size() <= COFF::NameSize) {
167 std::copy(Name.begin(), Name.end(), Sec.Header.Name);
169 // Add string to the string table and format the index for output.
170 unsigned Index = getStringIndex(Name);
171 std::string str = utostr(Index);
172 if (str.size() > 7) {
173 errs() << "String table got too large";
176 Sec.Header.Name[0] = '/';
177 std::copy(str.begin(), str.end(), Sec.Header.Name + 1);
183 bool parseSymbols() {
184 for (std::vector<COFFYAML::Symbol>::iterator i = Obj.Symbols.begin(),
185 e = Obj.Symbols.end(); i != e; ++i) {
186 COFFYAML::Symbol &Sym = *i;
188 // If the name is less than 8 bytes, store it in place, otherwise
189 // store it in the string table.
190 StringRef Name = Sym.Name;
191 if (Name.size() <= COFF::NameSize) {
192 std::copy(Name.begin(), Name.end(), Sym.Header.Name);
194 // Add string to the string table and format the index for output.
195 unsigned Index = getStringIndex(Name);
196 *reinterpret_cast<support::aligned_ulittle32_t*>(
197 Sym.Header.Name + 4) = Index;
200 Sym.Header.Type = Sym.SimpleType;
201 Sym.Header.Type |= Sym.ComplexType << COFF::SCT_COMPLEX_TYPE_SHIFT;
207 if (!parseSections())
214 unsigned getStringIndex(StringRef Str) {
215 StringMap<unsigned>::iterator i = StringTableMap.find(Str);
216 if (i == StringTableMap.end()) {
217 unsigned Index = StringTable.size();
218 StringTable.append(Str.begin(), Str.end());
219 StringTable.push_back(0);
220 StringTableMap[Str] = Index;
226 COFFYAML::Object &Obj;
228 StringMap<unsigned> StringTableMap;
229 std::string StringTable;
232 // Take a CP and assign addresses and sizes to everything. Returns false if the
233 // layout is not valid to do.
234 static bool layoutCOFF(COFFParser &CP) {
235 uint32_t SectionTableStart = 0;
236 uint32_t SectionTableSize = 0;
238 // The section table starts immediately after the header, including the
240 SectionTableStart = sizeof(COFF::header) + CP.Obj.Header.SizeOfOptionalHeader;
241 SectionTableSize = sizeof(COFF::section) * CP.Obj.Sections.size();
243 uint32_t CurrentSectionDataOffset = SectionTableStart + SectionTableSize;
245 // Assign each section data address consecutively.
246 for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
247 e = CP.Obj.Sections.end();
249 if (!i->SectionData.empty()) {
250 i->Header.SizeOfRawData = i->SectionData.size()/2;
251 i->Header.PointerToRawData = CurrentSectionDataOffset;
252 CurrentSectionDataOffset += i->Header.SizeOfRawData;
253 if (!i->Relocations.empty()) {
254 i->Header.PointerToRelocations = CurrentSectionDataOffset;
255 i->Header.NumberOfRelocations = i->Relocations.size();
256 CurrentSectionDataOffset += i->Header.NumberOfRelocations *
257 COFF::RelocationSize;
259 // TODO: Handle alignment.
261 i->Header.SizeOfRawData = 0;
262 i->Header.PointerToRawData = 0;
266 uint32_t SymbolTableStart = CurrentSectionDataOffset;
268 // Calculate number of symbols.
269 uint32_t NumberOfSymbols = 0;
270 for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(),
271 e = CP.Obj.Symbols.end();
273 unsigned AuxBytes = i->AuxiliaryData.size() / 2;
274 if (AuxBytes % COFF::SymbolSize != 0) {
275 errs() << "AuxiliaryData size not a multiple of symbol size!\n";
278 i->Header.NumberOfAuxSymbols = AuxBytes / COFF::SymbolSize;
279 NumberOfSymbols += 1 + i->Header.NumberOfAuxSymbols;
282 // Store all the allocated start addresses in the header.
283 CP.Obj.Header.NumberOfSections = CP.Obj.Sections.size();
284 CP.Obj.Header.NumberOfSymbols = NumberOfSymbols;
285 CP.Obj.Header.PointerToSymbolTable = SymbolTableStart;
287 *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0])
288 = CP.StringTable.size();
293 template <typename value_type>
294 struct binary_le_impl {
296 binary_le_impl(value_type V) : Value(V) {}
299 template <typename value_type>
300 raw_ostream &operator <<( raw_ostream &OS
301 , const binary_le_impl<value_type> &BLE) {
302 char Buffer[sizeof(BLE.Value)];
303 support::endian::write<value_type, support::little, support::unaligned>(
305 OS.write(Buffer, sizeof(BLE.Value));
309 template <typename value_type>
310 binary_le_impl<value_type> binary_le(value_type V) {
311 return binary_le_impl<value_type>(V);
314 bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
315 OS << binary_le(CP.Obj.Header.Machine)
316 << binary_le(CP.Obj.Header.NumberOfSections)
317 << binary_le(CP.Obj.Header.TimeDateStamp)
318 << binary_le(CP.Obj.Header.PointerToSymbolTable)
319 << binary_le(CP.Obj.Header.NumberOfSymbols)
320 << binary_le(CP.Obj.Header.SizeOfOptionalHeader)
321 << binary_le(CP.Obj.Header.Characteristics);
323 // Output section table.
324 for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
325 e = CP.Obj.Sections.end();
327 OS.write(i->Header.Name, COFF::NameSize);
328 OS << binary_le(i->Header.VirtualSize)
329 << binary_le(i->Header.VirtualAddress)
330 << binary_le(i->Header.SizeOfRawData)
331 << binary_le(i->Header.PointerToRawData)
332 << binary_le(i->Header.PointerToRelocations)
333 << binary_le(i->Header.PointerToLineNumbers)
334 << binary_le(i->Header.NumberOfRelocations)
335 << binary_le(i->Header.NumberOfLineNumbers)
336 << binary_le(i->Header.Characteristics);
339 // Output section data.
340 for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
341 e = CP.Obj.Sections.end();
343 if (!i->SectionData.empty()) {
344 std::vector<uint8_t> Data;
345 if (!hexStringToByteArray(i->SectionData, Data)) {
346 errs() << "SectionData must be a collection of pairs of hex bytes";
350 OS.write(reinterpret_cast<const char*>(&Data[0]), Data.size());
352 for (unsigned I2 = 0, E2 = i->Relocations.size(); I2 != E2; ++I2) {
353 const COFF::relocation &R = i->Relocations[I2];
354 OS << binary_le(R.VirtualAddress)
355 << binary_le(R.SymbolTableIndex)
356 << binary_le(R.Type);
360 // Output symbol table.
362 for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.Symbols.begin(),
363 e = CP.Obj.Symbols.end();
365 OS.write(i->Header.Name, COFF::NameSize);
366 OS << binary_le(i->Header.Value)
367 << binary_le(i->Header.SectionNumber)
368 << binary_le(i->Header.Type)
369 << binary_le(i->Header.StorageClass)
370 << binary_le(i->Header.NumberOfAuxSymbols);
371 if (!i->AuxiliaryData.empty()) {
372 std::vector<uint8_t> AuxSymbols;
373 if (!hexStringToByteArray(i->AuxiliaryData, AuxSymbols)) {
374 errs() << "AuxiliaryData must be a collection of pairs of hex bytes";
378 OS.write(reinterpret_cast<const char*>(&AuxSymbols[0]),
383 // Output string table.
384 OS.write(&CP.StringTable[0], CP.StringTable.size());
388 LLVM_YAML_IS_SEQUENCE_VECTOR(COFF::relocation)
389 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section)
390 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)
395 Characteristics operator|(Characteristics a, Characteristics b) {
396 uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
397 return static_cast<Characteristics>(Ret);
400 SectionCharacteristics
401 operator|(SectionCharacteristics a, SectionCharacteristics b) {
402 uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
403 return static_cast<SectionCharacteristics>(Ret);
409 #define BCase(X) IO.bitSetCase(Value, #X, COFF::X);
412 struct ScalarBitSetTraits<COFF::SectionCharacteristics> {
413 static void bitset(IO &IO, COFF::SectionCharacteristics &Value) {
414 BCase(IMAGE_SCN_TYPE_NO_PAD);
415 BCase(IMAGE_SCN_CNT_CODE);
416 BCase(IMAGE_SCN_CNT_INITIALIZED_DATA);
417 BCase(IMAGE_SCN_CNT_UNINITIALIZED_DATA);
418 BCase(IMAGE_SCN_LNK_OTHER);
419 BCase(IMAGE_SCN_LNK_INFO);
420 BCase(IMAGE_SCN_LNK_REMOVE);
421 BCase(IMAGE_SCN_LNK_COMDAT);
422 BCase(IMAGE_SCN_GPREL);
423 BCase(IMAGE_SCN_MEM_PURGEABLE);
424 BCase(IMAGE_SCN_MEM_16BIT);
425 BCase(IMAGE_SCN_MEM_LOCKED);
426 BCase(IMAGE_SCN_MEM_PRELOAD);
427 BCase(IMAGE_SCN_ALIGN_1BYTES);
428 BCase(IMAGE_SCN_ALIGN_2BYTES);
429 BCase(IMAGE_SCN_ALIGN_4BYTES);
430 BCase(IMAGE_SCN_ALIGN_8BYTES);
431 BCase(IMAGE_SCN_ALIGN_16BYTES);
432 BCase(IMAGE_SCN_ALIGN_32BYTES);
433 BCase(IMAGE_SCN_ALIGN_64BYTES);
434 BCase(IMAGE_SCN_ALIGN_128BYTES);
435 BCase(IMAGE_SCN_ALIGN_256BYTES);
436 BCase(IMAGE_SCN_ALIGN_512BYTES);
437 BCase(IMAGE_SCN_ALIGN_1024BYTES);
438 BCase(IMAGE_SCN_ALIGN_2048BYTES);
439 BCase(IMAGE_SCN_ALIGN_4096BYTES);
440 BCase(IMAGE_SCN_ALIGN_8192BYTES);
441 BCase(IMAGE_SCN_LNK_NRELOC_OVFL);
442 BCase(IMAGE_SCN_MEM_DISCARDABLE);
443 BCase(IMAGE_SCN_MEM_NOT_CACHED);
444 BCase(IMAGE_SCN_MEM_NOT_PAGED);
445 BCase(IMAGE_SCN_MEM_SHARED);
446 BCase(IMAGE_SCN_MEM_EXECUTE);
447 BCase(IMAGE_SCN_MEM_READ);
448 BCase(IMAGE_SCN_MEM_WRITE);
453 struct ScalarBitSetTraits<COFF::Characteristics> {
454 static void bitset(IO &IO, COFF::Characteristics &Value) {
455 BCase(IMAGE_FILE_RELOCS_STRIPPED);
456 BCase(IMAGE_FILE_EXECUTABLE_IMAGE);
457 BCase(IMAGE_FILE_LINE_NUMS_STRIPPED);
458 BCase(IMAGE_FILE_LOCAL_SYMS_STRIPPED);
459 BCase(IMAGE_FILE_AGGRESSIVE_WS_TRIM);
460 BCase(IMAGE_FILE_LARGE_ADDRESS_AWARE);
461 BCase(IMAGE_FILE_BYTES_REVERSED_LO);
462 BCase(IMAGE_FILE_32BIT_MACHINE);
463 BCase(IMAGE_FILE_DEBUG_STRIPPED);
464 BCase(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP);
465 BCase(IMAGE_FILE_NET_RUN_FROM_SWAP);
466 BCase(IMAGE_FILE_SYSTEM);
467 BCase(IMAGE_FILE_DLL);
468 BCase(IMAGE_FILE_UP_SYSTEM_ONLY);
469 BCase(IMAGE_FILE_BYTES_REVERSED_HI);
474 #define ECase(X) IO.enumCase(Value, #X, COFF::X);
477 struct ScalarEnumerationTraits<COFF::SymbolComplexType> {
478 static void enumeration(IO &IO, COFF::SymbolComplexType &Value) {
479 ECase(IMAGE_SYM_DTYPE_NULL);
480 ECase(IMAGE_SYM_DTYPE_POINTER);
481 ECase(IMAGE_SYM_DTYPE_FUNCTION);
482 ECase(IMAGE_SYM_DTYPE_ARRAY);
487 struct ScalarEnumerationTraits<COFF::SymbolStorageClass> {
488 static void enumeration(IO &IO, COFF::SymbolStorageClass &Value) {
489 ECase(IMAGE_SYM_CLASS_END_OF_FUNCTION);
490 ECase(IMAGE_SYM_CLASS_NULL);
491 ECase(IMAGE_SYM_CLASS_AUTOMATIC);
492 ECase(IMAGE_SYM_CLASS_EXTERNAL);
493 ECase(IMAGE_SYM_CLASS_STATIC);
494 ECase(IMAGE_SYM_CLASS_REGISTER);
495 ECase(IMAGE_SYM_CLASS_EXTERNAL_DEF);
496 ECase(IMAGE_SYM_CLASS_LABEL);
497 ECase(IMAGE_SYM_CLASS_UNDEFINED_LABEL);
498 ECase(IMAGE_SYM_CLASS_MEMBER_OF_STRUCT);
499 ECase(IMAGE_SYM_CLASS_ARGUMENT);
500 ECase(IMAGE_SYM_CLASS_STRUCT_TAG);
501 ECase(IMAGE_SYM_CLASS_MEMBER_OF_UNION);
502 ECase(IMAGE_SYM_CLASS_UNION_TAG);
503 ECase(IMAGE_SYM_CLASS_TYPE_DEFINITION);
504 ECase(IMAGE_SYM_CLASS_UNDEFINED_STATIC);
505 ECase(IMAGE_SYM_CLASS_ENUM_TAG);
506 ECase(IMAGE_SYM_CLASS_MEMBER_OF_ENUM);
507 ECase(IMAGE_SYM_CLASS_REGISTER_PARAM);
508 ECase(IMAGE_SYM_CLASS_BIT_FIELD);
509 ECase(IMAGE_SYM_CLASS_BLOCK);
510 ECase(IMAGE_SYM_CLASS_FUNCTION);
511 ECase(IMAGE_SYM_CLASS_END_OF_STRUCT);
512 ECase(IMAGE_SYM_CLASS_FILE);
513 ECase(IMAGE_SYM_CLASS_SECTION);
514 ECase(IMAGE_SYM_CLASS_WEAK_EXTERNAL);
515 ECase(IMAGE_SYM_CLASS_CLR_TOKEN);
520 struct ScalarEnumerationTraits<COFF::SymbolBaseType> {
521 static void enumeration(IO &IO, COFF::SymbolBaseType &Value) {
522 ECase(IMAGE_SYM_TYPE_NULL);
523 ECase(IMAGE_SYM_TYPE_VOID);
524 ECase(IMAGE_SYM_TYPE_CHAR);
525 ECase(IMAGE_SYM_TYPE_SHORT);
526 ECase(IMAGE_SYM_TYPE_INT);
527 ECase(IMAGE_SYM_TYPE_LONG);
528 ECase(IMAGE_SYM_TYPE_FLOAT);
529 ECase(IMAGE_SYM_TYPE_DOUBLE);
530 ECase(IMAGE_SYM_TYPE_STRUCT);
531 ECase(IMAGE_SYM_TYPE_UNION);
532 ECase(IMAGE_SYM_TYPE_ENUM);
533 ECase(IMAGE_SYM_TYPE_MOE);
534 ECase(IMAGE_SYM_TYPE_BYTE);
535 ECase(IMAGE_SYM_TYPE_WORD);
536 ECase(IMAGE_SYM_TYPE_UINT);
537 ECase(IMAGE_SYM_TYPE_DWORD);
542 struct ScalarEnumerationTraits<COFF::MachineTypes> {
543 static void enumeration(IO &IO, COFF::MachineTypes &Value) {
544 ECase(IMAGE_FILE_MACHINE_UNKNOWN);
545 ECase(IMAGE_FILE_MACHINE_AM33);
546 ECase(IMAGE_FILE_MACHINE_AMD64);
547 ECase(IMAGE_FILE_MACHINE_ARM);
548 ECase(IMAGE_FILE_MACHINE_ARMV7);
549 ECase(IMAGE_FILE_MACHINE_EBC);
550 ECase(IMAGE_FILE_MACHINE_I386);
551 ECase(IMAGE_FILE_MACHINE_IA64);
552 ECase(IMAGE_FILE_MACHINE_M32R);
553 ECase(IMAGE_FILE_MACHINE_MIPS16);
554 ECase(IMAGE_FILE_MACHINE_MIPSFPU);
555 ECase(IMAGE_FILE_MACHINE_MIPSFPU16);
556 ECase(IMAGE_FILE_MACHINE_POWERPC);
557 ECase(IMAGE_FILE_MACHINE_POWERPCFP);
558 ECase(IMAGE_FILE_MACHINE_R4000);
559 ECase(IMAGE_FILE_MACHINE_SH3);
560 ECase(IMAGE_FILE_MACHINE_SH3DSP);
561 ECase(IMAGE_FILE_MACHINE_SH4);
562 ECase(IMAGE_FILE_MACHINE_SH5);
563 ECase(IMAGE_FILE_MACHINE_THUMB);
564 ECase(IMAGE_FILE_MACHINE_WCEMIPSV2);
569 struct ScalarEnumerationTraits<COFF::RelocationTypeX86> {
570 static void enumeration(IO &IO, COFF::RelocationTypeX86 &Value) {
571 ECase(IMAGE_REL_I386_ABSOLUTE);
572 ECase(IMAGE_REL_I386_DIR16);
573 ECase(IMAGE_REL_I386_REL16);
574 ECase(IMAGE_REL_I386_DIR32);
575 ECase(IMAGE_REL_I386_DIR32NB);
576 ECase(IMAGE_REL_I386_SEG12);
577 ECase(IMAGE_REL_I386_SECTION);
578 ECase(IMAGE_REL_I386_SECREL);
579 ECase(IMAGE_REL_I386_TOKEN);
580 ECase(IMAGE_REL_I386_SECREL7);
581 ECase(IMAGE_REL_I386_REL32);
582 ECase(IMAGE_REL_AMD64_ABSOLUTE);
583 ECase(IMAGE_REL_AMD64_ADDR64);
584 ECase(IMAGE_REL_AMD64_ADDR32);
585 ECase(IMAGE_REL_AMD64_ADDR32NB);
586 ECase(IMAGE_REL_AMD64_REL32);
587 ECase(IMAGE_REL_AMD64_REL32_1);
588 ECase(IMAGE_REL_AMD64_REL32_2);
589 ECase(IMAGE_REL_AMD64_REL32_3);
590 ECase(IMAGE_REL_AMD64_REL32_4);
591 ECase(IMAGE_REL_AMD64_REL32_5);
592 ECase(IMAGE_REL_AMD64_SECTION);
593 ECase(IMAGE_REL_AMD64_SECREL);
594 ECase(IMAGE_REL_AMD64_SECREL7);
595 ECase(IMAGE_REL_AMD64_TOKEN);
596 ECase(IMAGE_REL_AMD64_SREL32);
597 ECase(IMAGE_REL_AMD64_PAIR);
598 ECase(IMAGE_REL_AMD64_SSPAN32);
605 struct MappingTraits<COFFYAML::Symbol> {
606 struct NStorageClass {
607 NStorageClass(IO&) : StorageClass(COFF::SymbolStorageClass(0)) {
609 NStorageClass(IO&, uint8_t S) : StorageClass(COFF::SymbolStorageClass(S)) {
611 uint8_t denormalize(IO &) {
615 COFF::SymbolStorageClass StorageClass;
618 static void mapping(IO &IO, COFFYAML::Symbol &S) {
619 MappingNormalization<NStorageClass, uint8_t> NS(IO, S.Header.StorageClass);
621 IO.mapRequired("SimpleType", S.SimpleType);
622 IO.mapOptional("NumberOfAuxSymbols", S.Header.NumberOfAuxSymbols);
623 IO.mapRequired("Name", S.Name);
624 IO.mapRequired("StorageClass", NS->StorageClass);
625 IO.mapOptional("AuxiliaryData", S.AuxiliaryData);
626 IO.mapRequired("ComplexType", S.ComplexType);
627 IO.mapRequired("Value", S.Header.Value);
628 IO.mapRequired("SectionNumber", S.Header.SectionNumber);
633 struct MappingTraits<COFF::header> {
635 NMachine(IO&) : Machine(COFF::MachineTypes(0)) {
637 NMachine(IO&, uint16_t M) : Machine(COFF::MachineTypes(M)) {
639 uint16_t denormalize(IO &) {
642 COFF::MachineTypes Machine;
645 struct NCharacteristics {
646 NCharacteristics(IO&) : Characteristics(COFF::Characteristics(0)) {
648 NCharacteristics(IO&, uint16_t C) :
649 Characteristics(COFF::Characteristics(C)) {
651 uint16_t denormalize(IO &) {
652 return Characteristics;
655 COFF::Characteristics Characteristics;
658 static void mapping(IO &IO, COFF::header &H) {
659 MappingNormalization<NMachine, uint16_t> NM(IO, H.Machine);
660 MappingNormalization<NCharacteristics, uint16_t> NC(IO, H.Characteristics);
662 IO.mapRequired("Machine", NM->Machine);
663 IO.mapOptional("Characteristics", NC->Characteristics);
668 struct MappingTraits<COFF::relocation> {
670 NType(IO &) : Type(COFF::RelocationTypeX86(0)) {
672 NType(IO &, uint16_t T) : Type(COFF::RelocationTypeX86(T)) {
674 uint16_t denormalize(IO &) {
677 COFF::RelocationTypeX86 Type;
680 static void mapping(IO &IO, COFF::relocation &Rel) {
681 MappingNormalization<NType, uint16_t> NT(IO, Rel.Type);
683 IO.mapRequired("Type", NT->Type);
684 IO.mapRequired("VirtualAddress", Rel.VirtualAddress);
685 IO.mapRequired("SymbolTableIndex", Rel.SymbolTableIndex);
690 struct MappingTraits<COFFYAML::Section> {
691 struct NCharacteristics {
692 NCharacteristics(IO &) : Characteristics(COFF::SectionCharacteristics(0)) {
694 NCharacteristics(IO &, uint32_t C) :
695 Characteristics(COFF::SectionCharacteristics(C)) {
697 uint32_t denormalize(IO &) {
698 return Characteristics;
700 COFF::SectionCharacteristics Characteristics;
703 static void mapping(IO &IO, COFFYAML::Section &Sec) {
704 MappingNormalization<NCharacteristics, uint32_t> NC(IO,
705 Sec.Header.Characteristics);
706 IO.mapOptional("Relocations", Sec.Relocations);
707 IO.mapRequired("SectionData", Sec.SectionData);
708 IO.mapRequired("Characteristics", NC->Characteristics);
709 IO.mapRequired("Name", Sec.Name);
714 struct MappingTraits<COFFYAML::Object> {
715 static void mapping(IO &IO, COFFYAML::Object &Obj) {
716 IO.mapRequired("sections", Obj.Sections);
717 IO.mapRequired("header", Obj.Header);
718 IO.mapRequired("symbols", Obj.Symbols);
721 } // end namespace yaml
722 } // end namespace llvm
724 int main(int argc, char **argv) {
725 cl::ParseCommandLineOptions(argc, argv);
726 sys::PrintStackTraceOnErrorSignal();
727 PrettyStackTraceProgram X(argc, argv);
728 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
730 OwningPtr<MemoryBuffer> Buf;
731 if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
734 yaml::Input YIn(Buf->getBuffer());
735 COFFYAML::Object Doc;
738 errs() << "yaml2obj: Failed to parse YAML file!\n";
744 errs() << "yaml2obj: Failed to parse YAML file!\n";
748 if (!layoutCOFF(CP)) {
749 errs() << "yaml2obj: Failed to layout COFF file!\n";
752 if (!writeCOFF(CP, outs())) {
753 errs() << "yaml2obj: Failed to write COFF file!\n";