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::SectionCharacteristics Characteristics;
119 StringRef SectionData;
120 std::vector<COFF::relocation> Relocations;
125 COFF::MachineTypes Machine;
126 COFF::Characteristics Characteristics;
130 COFF::SymbolBaseType SimpleType;
131 uint8_t NumberOfAuxSymbols;
133 COFF::SymbolStorageClass StorageClass;
134 StringRef AuxillaryData;
135 COFF::SymbolComplexType ComplexType;
137 uint16_t SectionNumber;
142 std::vector<Section> Sections;
143 std::vector<Symbol> Symbols;
147 /// This parses a yaml stream that represents a COFF object file.
148 /// See docs/yaml2obj for the yaml scheema.
150 COFFParser(COFFYAML::Object &Obj) : Obj(Obj) {
151 std::memset(&Header, 0, sizeof(Header));
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);
158 Header.Machine = Obj.HeaderData.Machine;
159 Header.Characteristics = Obj.HeaderData.Characteristics;
162 bool parseSections() {
163 for (std::vector<COFFYAML::Section>::iterator i = Obj.Sections.begin(),
164 e = Obj.Sections.end(); i != e; ++i) {
165 const COFFYAML::Section &YamlSection = *i;
167 std::memset(&Sec.Header, 0, sizeof(Sec.Header));
169 // If the name is less than 8 bytes, store it in place, otherwise
170 // store it in the string table.
171 StringRef Name = YamlSection.Name;
172 std::fill_n(Sec.Header.Name, unsigned(COFF::NameSize), 0);
173 if (Name.size() <= COFF::NameSize) {
174 std::copy(Name.begin(), Name.end(), Sec.Header.Name);
176 // Add string to the string table and format the index for output.
177 unsigned Index = getStringIndex(Name);
178 std::string str = utostr(Index);
179 if (str.size() > 7) {
180 errs() << "String table got too large";
183 Sec.Header.Name[0] = '/';
184 std::copy(str.begin(), str.end(), Sec.Header.Name + 1);
187 Sec.Header.Characteristics = YamlSection.Characteristics;
189 StringRef Data = YamlSection.SectionData;
190 if (!hexStringToByteArray(Data, Sec.Data)) {
191 errs() << "SectionData must be a collection of pairs of hex bytes";
194 Sections.push_back(Sec);
199 bool parseSymbols() {
200 for (std::vector<COFFYAML::Symbol>::iterator i = Obj.Symbols.begin(),
201 e = Obj.Symbols.end(); i != e; ++i) {
202 COFFYAML::Symbol YamlSymbol = *i;
204 std::memset(&Sym.Header, 0, sizeof(Sym.Header));
206 // If the name is less than 8 bytes, store it in place, otherwise
207 // store it in the string table.
208 StringRef Name = YamlSymbol.Name;
209 std::fill_n(Sym.Header.Name, unsigned(COFF::NameSize), 0);
210 if (Name.size() <= COFF::NameSize) {
211 std::copy(Name.begin(), Name.end(), Sym.Header.Name);
213 // Add string to the string table and format the index for output.
214 unsigned Index = getStringIndex(Name);
215 *reinterpret_cast<support::aligned_ulittle32_t*>(
216 Sym.Header.Name + 4) = Index;
219 Sym.Header.Value = YamlSymbol.Value;
220 Sym.Header.Type |= YamlSymbol.SimpleType;
221 Sym.Header.Type |= YamlSymbol.ComplexType << COFF::SCT_COMPLEX_TYPE_SHIFT;
222 Sym.Header.StorageClass = YamlSymbol.StorageClass;
223 Sym.Header.SectionNumber = YamlSymbol.SectionNumber;
225 StringRef Data = YamlSymbol.AuxillaryData;
226 if (!hexStringToByteArray(Data, Sym.AuxSymbols)) {
227 errs() << "AuxillaryData must be a collection of pairs of hex bytes";
230 Symbols.push_back(Sym);
237 if (!parseSections())
244 unsigned getStringIndex(StringRef Str) {
245 StringMap<unsigned>::iterator i = StringTableMap.find(Str);
246 if (i == StringTableMap.end()) {
247 unsigned Index = StringTable.size();
248 StringTable.append(Str.begin(), Str.end());
249 StringTable.push_back(0);
250 StringTableMap[Str] = Index;
256 COFFYAML::Object &Obj;
260 COFF::section Header;
261 std::vector<uint8_t> Data;
262 std::vector<COFF::relocation> Relocations;
267 std::vector<uint8_t> AuxSymbols;
270 std::vector<Section> Sections;
271 std::vector<Symbol> Symbols;
272 StringMap<unsigned> StringTableMap;
273 std::string StringTable;
276 // Take a CP and assign addresses and sizes to everything. Returns false if the
277 // layout is not valid to do.
278 static bool layoutCOFF(COFFParser &CP) {
279 uint32_t SectionTableStart = 0;
280 uint32_t SectionTableSize = 0;
282 // The section table starts immediately after the header, including the
284 SectionTableStart = sizeof(COFF::header) + CP.Header.SizeOfOptionalHeader;
285 SectionTableSize = sizeof(COFF::section) * CP.Sections.size();
287 uint32_t CurrentSectionDataOffset = SectionTableStart + SectionTableSize;
289 // Assign each section data address consecutively.
290 for (std::vector<COFFParser::Section>::iterator i = CP.Sections.begin(),
291 e = CP.Sections.end();
293 if (!i->Data.empty()) {
294 i->Header.SizeOfRawData = i->Data.size();
295 i->Header.PointerToRawData = CurrentSectionDataOffset;
296 CurrentSectionDataOffset += i->Header.SizeOfRawData;
297 // TODO: Handle alignment.
299 i->Header.SizeOfRawData = 0;
300 i->Header.PointerToRawData = 0;
304 uint32_t SymbolTableStart = CurrentSectionDataOffset;
306 // Calculate number of symbols.
307 uint32_t NumberOfSymbols = 0;
308 for (std::vector<COFFParser::Symbol>::iterator i = CP.Symbols.begin(),
309 e = CP.Symbols.end();
311 if (i->AuxSymbols.size() % COFF::SymbolSize != 0) {
312 errs() << "AuxillaryData size not a multiple of symbol size!\n";
315 i->Header.NumberOfAuxSymbols = i->AuxSymbols.size() / COFF::SymbolSize;
316 NumberOfSymbols += 1 + i->Header.NumberOfAuxSymbols;
319 // Store all the allocated start addresses in the header.
320 CP.Header.NumberOfSections = CP.Sections.size();
321 CP.Header.NumberOfSymbols = NumberOfSymbols;
322 CP.Header.PointerToSymbolTable = SymbolTableStart;
324 *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0])
325 = CP.StringTable.size();
330 template <typename value_type>
331 struct binary_le_impl {
333 binary_le_impl(value_type V) : Value(V) {}
336 template <typename value_type>
337 raw_ostream &operator <<( raw_ostream &OS
338 , const binary_le_impl<value_type> &BLE) {
339 char Buffer[sizeof(BLE.Value)];
340 support::endian::write<value_type, support::little, support::unaligned>(
342 OS.write(Buffer, sizeof(BLE.Value));
346 template <typename value_type>
347 binary_le_impl<value_type> binary_le(value_type V) {
348 return binary_le_impl<value_type>(V);
351 void writeCOFF(COFFParser &CP, raw_ostream &OS) {
352 OS << binary_le(CP.Header.Machine)
353 << binary_le(CP.Header.NumberOfSections)
354 << binary_le(CP.Header.TimeDateStamp)
355 << binary_le(CP.Header.PointerToSymbolTable)
356 << binary_le(CP.Header.NumberOfSymbols)
357 << binary_le(CP.Header.SizeOfOptionalHeader)
358 << binary_le(CP.Header.Characteristics);
360 // Output section table.
361 for (std::vector<COFFParser::Section>::const_iterator i = CP.Sections.begin(),
362 e = CP.Sections.end();
364 OS.write(i->Header.Name, COFF::NameSize);
365 OS << binary_le(i->Header.VirtualSize)
366 << binary_le(i->Header.VirtualAddress)
367 << binary_le(i->Header.SizeOfRawData)
368 << binary_le(i->Header.PointerToRawData)
369 << binary_le(i->Header.PointerToRelocations)
370 << binary_le(i->Header.PointerToLineNumbers)
371 << binary_le(i->Header.NumberOfRelocations)
372 << binary_le(i->Header.NumberOfLineNumbers)
373 << binary_le(i->Header.Characteristics);
376 // Output section data.
377 for (std::vector<COFFParser::Section>::const_iterator i = CP.Sections.begin(),
378 e = CP.Sections.end();
380 if (!i->Data.empty())
381 OS.write(reinterpret_cast<const char*>(&i->Data[0]), i->Data.size());
384 // Output symbol table.
386 for (std::vector<COFFParser::Symbol>::const_iterator i = CP.Symbols.begin(),
387 e = CP.Symbols.end();
389 OS.write(i->Header.Name, COFF::NameSize);
390 OS << binary_le(i->Header.Value)
391 << binary_le(i->Header.SectionNumber)
392 << binary_le(i->Header.Type)
393 << binary_le(i->Header.StorageClass)
394 << binary_le(i->Header.NumberOfAuxSymbols);
395 if (!i->AuxSymbols.empty())
396 OS.write( reinterpret_cast<const char*>(&i->AuxSymbols[0])
397 , i->AuxSymbols.size());
400 // Output string table.
401 OS.write(&CP.StringTable[0], CP.StringTable.size());
404 LLVM_YAML_IS_SEQUENCE_VECTOR(COFF::relocation)
405 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section)
406 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)
411 Characteristics operator|(Characteristics a, Characteristics b) {
412 uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
413 return static_cast<Characteristics>(Ret);
416 SectionCharacteristics
417 operator|(SectionCharacteristics a, SectionCharacteristics b) {
418 uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
419 return static_cast<SectionCharacteristics>(Ret);
425 #define BCase(X) IO.bitSetCase(Value, #X, COFF::X);
428 struct ScalarBitSetTraits<COFF::SectionCharacteristics> {
429 static void bitset(IO &IO, COFF::SectionCharacteristics &Value) {
430 BCase(IMAGE_SCN_TYPE_NO_PAD);
431 BCase(IMAGE_SCN_CNT_CODE);
432 BCase(IMAGE_SCN_CNT_INITIALIZED_DATA);
433 BCase(IMAGE_SCN_CNT_UNINITIALIZED_DATA);
434 BCase(IMAGE_SCN_LNK_OTHER);
435 BCase(IMAGE_SCN_LNK_INFO);
436 BCase(IMAGE_SCN_LNK_REMOVE);
437 BCase(IMAGE_SCN_LNK_COMDAT);
438 BCase(IMAGE_SCN_GPREL);
439 BCase(IMAGE_SCN_MEM_PURGEABLE);
440 BCase(IMAGE_SCN_MEM_16BIT);
441 BCase(IMAGE_SCN_MEM_LOCKED);
442 BCase(IMAGE_SCN_MEM_PRELOAD);
443 BCase(IMAGE_SCN_ALIGN_1BYTES);
444 BCase(IMAGE_SCN_ALIGN_2BYTES);
445 BCase(IMAGE_SCN_ALIGN_4BYTES);
446 BCase(IMAGE_SCN_ALIGN_8BYTES);
447 BCase(IMAGE_SCN_ALIGN_16BYTES);
448 BCase(IMAGE_SCN_ALIGN_32BYTES);
449 BCase(IMAGE_SCN_ALIGN_64BYTES);
450 BCase(IMAGE_SCN_ALIGN_128BYTES);
451 BCase(IMAGE_SCN_ALIGN_256BYTES);
452 BCase(IMAGE_SCN_ALIGN_512BYTES);
453 BCase(IMAGE_SCN_ALIGN_1024BYTES);
454 BCase(IMAGE_SCN_ALIGN_2048BYTES);
455 BCase(IMAGE_SCN_ALIGN_4096BYTES);
456 BCase(IMAGE_SCN_ALIGN_8192BYTES);
457 BCase(IMAGE_SCN_LNK_NRELOC_OVFL);
458 BCase(IMAGE_SCN_MEM_DISCARDABLE);
459 BCase(IMAGE_SCN_MEM_NOT_CACHED);
460 BCase(IMAGE_SCN_MEM_NOT_PAGED);
461 BCase(IMAGE_SCN_MEM_SHARED);
462 BCase(IMAGE_SCN_MEM_EXECUTE);
463 BCase(IMAGE_SCN_MEM_READ);
464 BCase(IMAGE_SCN_MEM_WRITE);
469 struct ScalarBitSetTraits<COFF::Characteristics> {
470 static void bitset(IO &IO, COFF::Characteristics &Value) {
471 BCase(IMAGE_FILE_RELOCS_STRIPPED);
472 BCase(IMAGE_FILE_EXECUTABLE_IMAGE);
473 BCase(IMAGE_FILE_LINE_NUMS_STRIPPED);
474 BCase(IMAGE_FILE_LOCAL_SYMS_STRIPPED);
475 BCase(IMAGE_FILE_AGGRESSIVE_WS_TRIM);
476 BCase(IMAGE_FILE_LARGE_ADDRESS_AWARE);
477 BCase(IMAGE_FILE_BYTES_REVERSED_LO);
478 BCase(IMAGE_FILE_32BIT_MACHINE);
479 BCase(IMAGE_FILE_DEBUG_STRIPPED);
480 BCase(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP);
481 BCase(IMAGE_FILE_NET_RUN_FROM_SWAP);
482 BCase(IMAGE_FILE_SYSTEM);
483 BCase(IMAGE_FILE_DLL);
484 BCase(IMAGE_FILE_UP_SYSTEM_ONLY);
485 BCase(IMAGE_FILE_BYTES_REVERSED_HI);
490 #define ECase(X) IO.enumCase(Value, #X, COFF::X);
493 struct ScalarEnumerationTraits<COFF::SymbolComplexType> {
494 static void enumeration(IO &IO, COFF::SymbolComplexType &Value) {
495 ECase(IMAGE_SYM_DTYPE_NULL);
496 ECase(IMAGE_SYM_DTYPE_POINTER);
497 ECase(IMAGE_SYM_DTYPE_FUNCTION);
498 ECase(IMAGE_SYM_DTYPE_ARRAY);
503 struct ScalarEnumerationTraits<COFF::SymbolStorageClass> {
504 static void enumeration(IO &IO, COFF::SymbolStorageClass &Value) {
505 ECase(IMAGE_SYM_CLASS_END_OF_FUNCTION);
506 ECase(IMAGE_SYM_CLASS_NULL);
507 ECase(IMAGE_SYM_CLASS_AUTOMATIC);
508 ECase(IMAGE_SYM_CLASS_EXTERNAL);
509 ECase(IMAGE_SYM_CLASS_STATIC);
510 ECase(IMAGE_SYM_CLASS_REGISTER);
511 ECase(IMAGE_SYM_CLASS_EXTERNAL_DEF);
512 ECase(IMAGE_SYM_CLASS_LABEL);
513 ECase(IMAGE_SYM_CLASS_UNDEFINED_LABEL);
514 ECase(IMAGE_SYM_CLASS_MEMBER_OF_STRUCT);
515 ECase(IMAGE_SYM_CLASS_ARGUMENT);
516 ECase(IMAGE_SYM_CLASS_STRUCT_TAG);
517 ECase(IMAGE_SYM_CLASS_MEMBER_OF_UNION);
518 ECase(IMAGE_SYM_CLASS_UNION_TAG);
519 ECase(IMAGE_SYM_CLASS_TYPE_DEFINITION);
520 ECase(IMAGE_SYM_CLASS_UNDEFINED_STATIC);
521 ECase(IMAGE_SYM_CLASS_ENUM_TAG);
522 ECase(IMAGE_SYM_CLASS_MEMBER_OF_ENUM);
523 ECase(IMAGE_SYM_CLASS_REGISTER_PARAM);
524 ECase(IMAGE_SYM_CLASS_BIT_FIELD);
525 ECase(IMAGE_SYM_CLASS_BLOCK);
526 ECase(IMAGE_SYM_CLASS_FUNCTION);
527 ECase(IMAGE_SYM_CLASS_END_OF_STRUCT);
528 ECase(IMAGE_SYM_CLASS_FILE);
529 ECase(IMAGE_SYM_CLASS_SECTION);
530 ECase(IMAGE_SYM_CLASS_WEAK_EXTERNAL);
531 ECase(IMAGE_SYM_CLASS_CLR_TOKEN);
536 struct ScalarEnumerationTraits<COFF::SymbolBaseType> {
537 static void enumeration(IO &IO, COFF::SymbolBaseType &Value) {
538 ECase(IMAGE_SYM_TYPE_NULL);
539 ECase(IMAGE_SYM_TYPE_VOID);
540 ECase(IMAGE_SYM_TYPE_CHAR);
541 ECase(IMAGE_SYM_TYPE_SHORT);
542 ECase(IMAGE_SYM_TYPE_INT);
543 ECase(IMAGE_SYM_TYPE_LONG);
544 ECase(IMAGE_SYM_TYPE_FLOAT);
545 ECase(IMAGE_SYM_TYPE_DOUBLE);
546 ECase(IMAGE_SYM_TYPE_STRUCT);
547 ECase(IMAGE_SYM_TYPE_UNION);
548 ECase(IMAGE_SYM_TYPE_ENUM);
549 ECase(IMAGE_SYM_TYPE_MOE);
550 ECase(IMAGE_SYM_TYPE_BYTE);
551 ECase(IMAGE_SYM_TYPE_WORD);
552 ECase(IMAGE_SYM_TYPE_UINT);
553 ECase(IMAGE_SYM_TYPE_DWORD);
558 struct ScalarEnumerationTraits<COFF::MachineTypes> {
559 static void enumeration(IO &IO, COFF::MachineTypes &Value) {
560 ECase(IMAGE_FILE_MACHINE_UNKNOWN);
561 ECase(IMAGE_FILE_MACHINE_AM33);
562 ECase(IMAGE_FILE_MACHINE_AMD64);
563 ECase(IMAGE_FILE_MACHINE_ARM);
564 ECase(IMAGE_FILE_MACHINE_ARMV7);
565 ECase(IMAGE_FILE_MACHINE_EBC);
566 ECase(IMAGE_FILE_MACHINE_I386);
567 ECase(IMAGE_FILE_MACHINE_IA64);
568 ECase(IMAGE_FILE_MACHINE_M32R);
569 ECase(IMAGE_FILE_MACHINE_MIPS16);
570 ECase(IMAGE_FILE_MACHINE_MIPSFPU);
571 ECase(IMAGE_FILE_MACHINE_MIPSFPU16);
572 ECase(IMAGE_FILE_MACHINE_POWERPC);
573 ECase(IMAGE_FILE_MACHINE_POWERPCFP);
574 ECase(IMAGE_FILE_MACHINE_R4000);
575 ECase(IMAGE_FILE_MACHINE_SH3);
576 ECase(IMAGE_FILE_MACHINE_SH3DSP);
577 ECase(IMAGE_FILE_MACHINE_SH4);
578 ECase(IMAGE_FILE_MACHINE_SH5);
579 ECase(IMAGE_FILE_MACHINE_THUMB);
580 ECase(IMAGE_FILE_MACHINE_WCEMIPSV2);
585 struct ScalarEnumerationTraits<COFF::RelocationTypeX86> {
586 static void enumeration(IO &IO, COFF::RelocationTypeX86 &Value) {
587 ECase(IMAGE_REL_I386_ABSOLUTE);
588 ECase(IMAGE_REL_I386_DIR16);
589 ECase(IMAGE_REL_I386_REL16);
590 ECase(IMAGE_REL_I386_DIR32);
591 ECase(IMAGE_REL_I386_DIR32NB);
592 ECase(IMAGE_REL_I386_SEG12);
593 ECase(IMAGE_REL_I386_SECTION);
594 ECase(IMAGE_REL_I386_SECREL);
595 ECase(IMAGE_REL_I386_TOKEN);
596 ECase(IMAGE_REL_I386_SECREL7);
597 ECase(IMAGE_REL_I386_REL32);
598 ECase(IMAGE_REL_AMD64_ABSOLUTE);
599 ECase(IMAGE_REL_AMD64_ADDR64);
600 ECase(IMAGE_REL_AMD64_ADDR32);
601 ECase(IMAGE_REL_AMD64_ADDR32NB);
602 ECase(IMAGE_REL_AMD64_REL32);
603 ECase(IMAGE_REL_AMD64_REL32_1);
604 ECase(IMAGE_REL_AMD64_REL32_2);
605 ECase(IMAGE_REL_AMD64_REL32_3);
606 ECase(IMAGE_REL_AMD64_REL32_4);
607 ECase(IMAGE_REL_AMD64_REL32_5);
608 ECase(IMAGE_REL_AMD64_SECTION);
609 ECase(IMAGE_REL_AMD64_SECREL);
610 ECase(IMAGE_REL_AMD64_SECREL7);
611 ECase(IMAGE_REL_AMD64_TOKEN);
612 ECase(IMAGE_REL_AMD64_SREL32);
613 ECase(IMAGE_REL_AMD64_PAIR);
614 ECase(IMAGE_REL_AMD64_SSPAN32);
621 struct MappingTraits<COFFYAML::Symbol> {
622 static void mapping(IO &IO, COFFYAML::Symbol &S) {
623 IO.mapRequired("SimpleType", S.SimpleType);
624 IO.mapOptional("NumberOfAuxSymbols", S.NumberOfAuxSymbols);
625 IO.mapRequired("Name", S.Name);
626 IO.mapRequired("StorageClass", S.StorageClass);
627 IO.mapOptional("AuxillaryData", S.AuxillaryData); // FIXME: typo
628 IO.mapRequired("ComplexType", S.ComplexType);
629 IO.mapRequired("Value", S.Value);
630 IO.mapRequired("SectionNumber", S.SectionNumber);
635 struct MappingTraits<COFFYAML::Header> {
636 static void mapping(IO &IO, COFFYAML::Header &H) {
637 IO.mapRequired("Machine", H.Machine);
638 IO.mapOptional("Characteristics", H.Characteristics);
643 struct MappingTraits<COFF::relocation> {
644 struct NormalizedType {
646 NormalizedType(IO &) : Type(COFF::RelocationTypeX86(0)) {
648 NormalizedType(IO &, uint16_t T) : Type(COFF::RelocationTypeX86(T)) {
650 uint16_t denormalize(IO &) {
654 COFF::RelocationTypeX86 Type;
656 static void mapping(IO &IO, COFF::relocation &Rel) {
657 MappingNormalization<NormalizedType, uint16_t> foo(IO, Rel.Type);
659 IO.mapRequired("Type", foo->Type);
660 IO.mapRequired("VirtualAddress", Rel.VirtualAddress);
661 IO.mapRequired("SymbolTableIndex", Rel.SymbolTableIndex);
666 struct MappingTraits<COFFYAML::Section> {
667 static void mapping(IO &IO, COFFYAML::Section &Sec) {
668 IO.mapOptional("Relocations", Sec.Relocations);
669 IO.mapRequired("SectionData", Sec.SectionData);
670 IO.mapRequired("Characteristics", Sec.Characteristics);
671 IO.mapRequired("Name", Sec.Name);
676 struct MappingTraits<COFFYAML::Object> {
677 static void mapping(IO &IO, COFFYAML::Object &Obj) {
678 IO.mapRequired("sections", Obj.Sections);
679 IO.mapRequired("header", Obj.HeaderData);
680 IO.mapRequired("symbols", Obj.Symbols);
683 } // end namespace yaml
684 } // end namespace llvm
686 int main(int argc, char **argv) {
687 cl::ParseCommandLineOptions(argc, argv);
688 sys::PrintStackTraceOnErrorSignal();
689 PrettyStackTraceProgram X(argc, argv);
690 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
692 OwningPtr<MemoryBuffer> Buf;
693 if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
696 yaml::Input YIn(Buf->getBuffer());
697 COFFYAML::Object Doc;
700 errs() << "yaml2obj: Failed to parse YAML file!\n";
706 errs() << "yaml2obj: Failed to parse YAML file!\n";
710 if (!layoutCOFF(CP)) {
711 errs() << "yaml2obj: Failed to layout COFF file!\n";
714 writeCOFF(CP, outs());