blob: 67488023d5b0b4d73faa07b120aa2eeb2bd973db [file] [log] [blame]
Michael J. Spencera915f242012-08-02 19:16:56 +00001//===- yaml2obj - Convert YAML to a binary object file --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program takes a YAML description of an object file and outputs the
11// binary equivalent.
12//
13// This is used for writing tests that require binary files.
14//
15//===----------------------------------------------------------------------===//
16
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"
Michael J. Spencera915f242012-08-02 19:16:56 +000028#include "llvm/Support/Signals.h"
29#include "llvm/Support/SourceMgr.h"
Rafael Espindola8ec018c2013-04-02 23:56:40 +000030#include "llvm/Support/YAMLTraits.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000031#include "llvm/Support/raw_ostream.h"
32#include "llvm/Support/system_error.h"
Michael J. Spencera915f242012-08-02 19:16:56 +000033#include <vector>
34
35using namespace llvm;
36
37static cl::opt<std::string>
38 Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
39
40template<class T>
Michael J. Spencer1de266b2012-08-02 19:36:30 +000041typename llvm::enable_if_c<std::numeric_limits<T>::is_integer, bool>::type
Michael J. Spencera915f242012-08-02 19:16:56 +000042getAs(const llvm::yaml::ScalarNode *SN, T &Result) {
43 SmallString<4> Storage;
44 StringRef Value = SN->getValue(Storage);
45 if (Value.getAsInteger(0, Result))
46 return false;
47 return true;
48}
49
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.
53struct hex_pair_iterator {
54 StringRef::const_iterator Current, End;
55 typedef SmallVector<char, 2> value_type;
56 value_type Pair;
57 bool IsDone;
58
59 hex_pair_iterator(StringRef C)
60 : Current(C.begin()), End(C.end()), IsDone(false) {
61 // Initalize Pair.
62 ++*this;
63 }
64
65 // End iterator.
66 hex_pair_iterator() : Current(), End(), IsDone(true) {}
67
68 value_type operator *() const {
69 return Pair;
70 }
71
72 hex_pair_iterator operator ++() {
73 // We're at the end of the input.
74 if (Current == End) {
75 IsDone = true;
76 return *this;
77 }
78 Pair = value_type();
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);
85 }
86 // Hit the end without getting 2 hex digits. Pair is invalid.
87 if (Pair.size() != 2)
88 IsDone = true;
89 return *this;
90 }
91
92 bool operator ==(const hex_pair_iterator Other) {
Richard Trieu7b07d692012-08-02 23:22:39 +000093 return (IsDone == Other.IsDone) ||
Michael J. Spencera915f242012-08-02 19:16:56 +000094 (Current == Other.Current && End == Other.End);
95 }
96
97 bool operator !=(const hex_pair_iterator Other) {
98 return !(*this == Other);
99 }
100};
101
102template <class ContainerOut>
103static 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))
108 return false;
109 Out.push_back(Byte);
110 }
111 return true;
112}
113
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000114// 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.
116namespace COFFYAML {
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000117 struct Section {
Rafael Espindola2bce4cc2013-04-05 16:00:31 +0000118 COFF::SectionCharacteristics Characteristics;
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000119 StringRef SectionData;
Rafael Espindola7a7e83a2013-04-19 21:28:07 +0000120 std::vector<COFF::relocation> Relocations;
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000121 StringRef Name;
122 };
123
124 struct Header {
125 COFF::MachineTypes Machine;
Rafael Espindola2bce4cc2013-04-05 16:00:31 +0000126 COFF::Characteristics Characteristics;
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000127 };
128
129 struct Symbol {
130 COFF::SymbolBaseType SimpleType;
131 uint8_t NumberOfAuxSymbols;
132 StringRef Name;
133 COFF::SymbolStorageClass StorageClass;
134 StringRef AuxillaryData;
135 COFF::SymbolComplexType ComplexType;
136 uint32_t Value;
137 uint16_t SectionNumber;
138 };
139
140 struct Object {
141 Header HeaderData;
142 std::vector<Section> Sections;
143 std::vector<Symbol> Symbols;
144 };
145}
146
Michael J. Spencera915f242012-08-02 19:16:56 +0000147/// This parses a yaml stream that represents a COFF object file.
148/// See docs/yaml2obj for the yaml scheema.
149struct COFFParser {
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000150 COFFParser(COFFYAML::Object &Obj) : Obj(Obj) {
Michael J. Spencera915f242012-08-02 19:16:56 +0000151 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);
155 }
156
Rafael Espindola5152e4f2013-04-04 20:30:52 +0000157 void parseHeader() {
158 Header.Machine = Obj.HeaderData.Machine;
Rafael Espindola2bce4cc2013-04-05 16:00:31 +0000159 Header.Characteristics = Obj.HeaderData.Characteristics;
Rafael Espindola5152e4f2013-04-04 20:30:52 +0000160 }
161
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000162 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;
Michael J. Spencera915f242012-08-02 19:16:56 +0000166 Section Sec;
167 std::memset(&Sec.Header, 0, sizeof(Sec.Header));
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000168
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);
175 } else {
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";
Michael J. Spencera915f242012-08-02 19:16:56 +0000181 return false;
182 }
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000183 Sec.Header.Name[0] = '/';
184 std::copy(str.begin(), str.end(), Sec.Header.Name + 1);
185 }
Michael J. Spencera915f242012-08-02 19:16:56 +0000186
Rafael Espindola2bce4cc2013-04-05 16:00:31 +0000187 Sec.Header.Characteristics = YamlSection.Characteristics;
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000188
189 StringRef Data = YamlSection.SectionData;
190 if (!hexStringToByteArray(Data, Sec.Data)) {
191 errs() << "SectionData must be a collection of pairs of hex bytes";
192 return false;
Michael J. Spencera915f242012-08-02 19:16:56 +0000193 }
194 Sections.push_back(Sec);
195 }
196 return true;
197 }
198
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000199 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;
Michael J. Spencera915f242012-08-02 19:16:56 +0000203 Symbol Sym;
204 std::memset(&Sym.Header, 0, sizeof(Sym.Header));
Michael J. Spencera915f242012-08-02 19:16:56 +0000205
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000206 // 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);
212 } else {
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;
217 }
218
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;
224
225 StringRef Data = YamlSymbol.AuxillaryData;
226 if (!hexStringToByteArray(Data, Sym.AuxSymbols)) {
227 errs() << "AuxillaryData must be a collection of pairs of hex bytes";
228 return false;
Michael J. Spencera915f242012-08-02 19:16:56 +0000229 }
230 Symbols.push_back(Sym);
231 }
232 return true;
233 }
234
235 bool parse() {
Rafael Espindola5152e4f2013-04-04 20:30:52 +0000236 parseHeader();
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000237 if (!parseSections())
Michael J. Spencera915f242012-08-02 19:16:56 +0000238 return false;
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000239 if (!parseSymbols())
240 return false;
241 return true;
Michael J. Spencera915f242012-08-02 19:16:56 +0000242 }
243
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;
251 return Index;
252 }
253 return i->second;
254 }
255
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000256 COFFYAML::Object &Obj;
Michael J. Spencera915f242012-08-02 19:16:56 +0000257 COFF::header Header;
258
259 struct Section {
260 COFF::section Header;
261 std::vector<uint8_t> Data;
262 std::vector<COFF::relocation> Relocations;
263 };
264
265 struct Symbol {
266 COFF::symbol Header;
267 std::vector<uint8_t> AuxSymbols;
268 };
269
270 std::vector<Section> Sections;
271 std::vector<Symbol> Symbols;
272 StringMap<unsigned> StringTableMap;
273 std::string StringTable;
274};
275
276// Take a CP and assign addresses and sizes to everything. Returns false if the
277// layout is not valid to do.
278static bool layoutCOFF(COFFParser &CP) {
279 uint32_t SectionTableStart = 0;
280 uint32_t SectionTableSize = 0;
281
282 // The section table starts immediately after the header, including the
283 // optional header.
284 SectionTableStart = sizeof(COFF::header) + CP.Header.SizeOfOptionalHeader;
285 SectionTableSize = sizeof(COFF::section) * CP.Sections.size();
286
287 uint32_t CurrentSectionDataOffset = SectionTableStart + SectionTableSize;
288
289 // Assign each section data address consecutively.
290 for (std::vector<COFFParser::Section>::iterator i = CP.Sections.begin(),
291 e = CP.Sections.end();
292 i != e; ++i) {
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.
298 } else {
299 i->Header.SizeOfRawData = 0;
300 i->Header.PointerToRawData = 0;
301 }
302 }
303
304 uint32_t SymbolTableStart = CurrentSectionDataOffset;
305
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();
310 i != e; ++i) {
311 if (i->AuxSymbols.size() % COFF::SymbolSize != 0) {
312 errs() << "AuxillaryData size not a multiple of symbol size!\n";
313 return false;
314 }
315 i->Header.NumberOfAuxSymbols = i->AuxSymbols.size() / COFF::SymbolSize;
316 NumberOfSymbols += 1 + i->Header.NumberOfAuxSymbols;
317 }
318
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;
323
324 *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0])
325 = CP.StringTable.size();
326
327 return true;
328}
329
330template <typename value_type>
331struct binary_le_impl {
332 value_type Value;
333 binary_le_impl(value_type V) : Value(V) {}
334};
335
336template <typename value_type>
337raw_ostream &operator <<( raw_ostream &OS
338 , const binary_le_impl<value_type> &BLE) {
339 char Buffer[sizeof(BLE.Value)];
Michael J. Spencerc8b18df2013-01-02 20:14:11 +0000340 support::endian::write<value_type, support::little, support::unaligned>(
341 Buffer, BLE.Value);
Michael J. Spencera915f242012-08-02 19:16:56 +0000342 OS.write(Buffer, sizeof(BLE.Value));
343 return OS;
344}
345
346template <typename value_type>
347binary_le_impl<value_type> binary_le(value_type V) {
348 return binary_le_impl<value_type>(V);
349}
350
351void 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);
359
360 // Output section table.
361 for (std::vector<COFFParser::Section>::const_iterator i = CP.Sections.begin(),
362 e = CP.Sections.end();
363 i != e; ++i) {
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);
374 }
375
376 // Output section data.
377 for (std::vector<COFFParser::Section>::const_iterator i = CP.Sections.begin(),
378 e = CP.Sections.end();
379 i != e; ++i) {
380 if (!i->Data.empty())
381 OS.write(reinterpret_cast<const char*>(&i->Data[0]), i->Data.size());
382 }
383
384 // Output symbol table.
385
386 for (std::vector<COFFParser::Symbol>::const_iterator i = CP.Symbols.begin(),
387 e = CP.Symbols.end();
388 i != e; ++i) {
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());
398 }
399
400 // Output string table.
401 OS.write(&CP.StringTable[0], CP.StringTable.size());
402}
403
Rafael Espindola7a7e83a2013-04-19 21:28:07 +0000404LLVM_YAML_IS_SEQUENCE_VECTOR(COFF::relocation)
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000405LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section)
406LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)
407
408namespace llvm {
Rafael Espindola2bce4cc2013-04-05 16:00:31 +0000409
410namespace COFF {
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);
414 }
415
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);
420 }
421}
422
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000423namespace yaml {
Rafael Espindola2bce4cc2013-04-05 16:00:31 +0000424
425#define BCase(X) IO.bitSetCase(Value, #X, COFF::X);
426
427template <>
428struct 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);
465 }
466};
467
468template <>
469struct 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);
486 }
487};
488#undef BCase
489
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000490#define ECase(X) IO.enumCase(Value, #X, COFF::X);
491
492template <>
493struct 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);
499 }
500};
501
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000502template <>
503struct 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);
532 }
533};
534
535template <>
536struct 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);
554 }
555};
556
557template <>
558struct 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);
581 }
582};
583
584template <>
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000585struct 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);
615 }
616};
617
618#undef ECase
619
620template <>
621struct 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);
631 }
632};
633
634template <>
635struct MappingTraits<COFFYAML::Header> {
636 static void mapping(IO &IO, COFFYAML::Header &H) {
637 IO.mapRequired("Machine", H.Machine);
Rafael Espindola5152e4f2013-04-04 20:30:52 +0000638 IO.mapOptional("Characteristics", H.Characteristics);
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000639 }
640};
641
642template <>
Rafael Espindola7a7e83a2013-04-19 21:28:07 +0000643struct MappingTraits<COFF::relocation> {
644 struct NormalizedType {
645 public:
646 NormalizedType(IO &) : Type(COFF::RelocationTypeX86(0)) {
647 }
648 NormalizedType(IO &, uint16_t T) : Type(COFF::RelocationTypeX86(T)) {
649 }
650 uint16_t denormalize(IO &) {
651 return Type;
652 }
653
654 COFF::RelocationTypeX86 Type;
655 };
656 static void mapping(IO &IO, COFF::relocation &Rel) {
657 MappingNormalization<NormalizedType, uint16_t> foo(IO, Rel.Type);
658
659 IO.mapRequired("Type", foo->Type);
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000660 IO.mapRequired("VirtualAddress", Rel.VirtualAddress);
661 IO.mapRequired("SymbolTableIndex", Rel.SymbolTableIndex);
662 }
663};
664
665template <>
666struct 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);
672 }
673};
674
675template <>
676struct 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);
681 }
682};
683} // end namespace yaml
684} // end namespace llvm
685
Michael J. Spencera915f242012-08-02 19:16:56 +0000686int 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.
691
692 OwningPtr<MemoryBuffer> Buf;
693 if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
694 return 1;
695
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000696 yaml::Input YIn(Buf->getBuffer());
697 COFFYAML::Object Doc;
698 YIn >> Doc;
699 if (YIn.error()) {
700 errs() << "yaml2obj: Failed to parse YAML file!\n";
701 return 1;
702 }
703
704 COFFParser CP(Doc);
Michael J. Spencera915f242012-08-02 19:16:56 +0000705 if (!CP.parse()) {
706 errs() << "yaml2obj: Failed to parse YAML file!\n";
707 return 1;
708 }
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000709
Michael J. Spencera915f242012-08-02 19:16:56 +0000710 if (!layoutCOFF(CP)) {
711 errs() << "yaml2obj: Failed to layout COFF file!\n";
712 return 1;
713 }
714 writeCOFF(CP, outs());
715}