blob: 7f8663b8243d9440605b40225c75b761b8e50b96 [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
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000124 struct Symbol {
125 COFF::SymbolBaseType SimpleType;
126 uint8_t NumberOfAuxSymbols;
127 StringRef Name;
128 COFF::SymbolStorageClass StorageClass;
129 StringRef AuxillaryData;
130 COFF::SymbolComplexType ComplexType;
131 uint32_t Value;
132 uint16_t SectionNumber;
133 };
134
135 struct Object {
Rafael Espindolaf59a2a82013-04-20 02:02:25 +0000136 COFF::header HeaderData;
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000137 std::vector<Section> Sections;
138 std::vector<Symbol> Symbols;
139 };
140}
141
Michael J. Spencera915f242012-08-02 19:16:56 +0000142/// This parses a yaml stream that represents a COFF object file.
143/// See docs/yaml2obj for the yaml scheema.
144struct COFFParser {
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000145 COFFParser(COFFYAML::Object &Obj) : Obj(Obj) {
Michael J. Spencera915f242012-08-02 19:16:56 +0000146 std::memset(&Header, 0, sizeof(Header));
147 // A COFF string table always starts with a 4 byte size field. Offsets into
148 // it include this size, so allocate it now.
149 StringTable.append(4, 0);
150 }
151
Rafael Espindola5152e4f2013-04-04 20:30:52 +0000152 void parseHeader() {
153 Header.Machine = Obj.HeaderData.Machine;
Rafael Espindola2bce4cc2013-04-05 16:00:31 +0000154 Header.Characteristics = Obj.HeaderData.Characteristics;
Rafael Espindola5152e4f2013-04-04 20:30:52 +0000155 }
156
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000157 bool parseSections() {
158 for (std::vector<COFFYAML::Section>::iterator i = Obj.Sections.begin(),
159 e = Obj.Sections.end(); i != e; ++i) {
160 const COFFYAML::Section &YamlSection = *i;
Michael J. Spencera915f242012-08-02 19:16:56 +0000161 Section Sec;
162 std::memset(&Sec.Header, 0, sizeof(Sec.Header));
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000163
164 // If the name is less than 8 bytes, store it in place, otherwise
165 // store it in the string table.
166 StringRef Name = YamlSection.Name;
167 std::fill_n(Sec.Header.Name, unsigned(COFF::NameSize), 0);
168 if (Name.size() <= COFF::NameSize) {
169 std::copy(Name.begin(), Name.end(), Sec.Header.Name);
170 } else {
171 // Add string to the string table and format the index for output.
172 unsigned Index = getStringIndex(Name);
173 std::string str = utostr(Index);
174 if (str.size() > 7) {
175 errs() << "String table got too large";
Michael J. Spencera915f242012-08-02 19:16:56 +0000176 return false;
177 }
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000178 Sec.Header.Name[0] = '/';
179 std::copy(str.begin(), str.end(), Sec.Header.Name + 1);
180 }
Michael J. Spencera915f242012-08-02 19:16:56 +0000181
Rafael Espindola2bce4cc2013-04-05 16:00:31 +0000182 Sec.Header.Characteristics = YamlSection.Characteristics;
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000183
184 StringRef Data = YamlSection.SectionData;
185 if (!hexStringToByteArray(Data, Sec.Data)) {
186 errs() << "SectionData must be a collection of pairs of hex bytes";
187 return false;
Michael J. Spencera915f242012-08-02 19:16:56 +0000188 }
Rafael Espindola120cf572013-04-23 15:53:02 +0000189 Sec.Relocations = YamlSection.Relocations;
Michael J. Spencera915f242012-08-02 19:16:56 +0000190 Sections.push_back(Sec);
191 }
192 return true;
193 }
194
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000195 bool parseSymbols() {
196 for (std::vector<COFFYAML::Symbol>::iterator i = Obj.Symbols.begin(),
197 e = Obj.Symbols.end(); i != e; ++i) {
198 COFFYAML::Symbol YamlSymbol = *i;
Michael J. Spencera915f242012-08-02 19:16:56 +0000199 Symbol Sym;
200 std::memset(&Sym.Header, 0, sizeof(Sym.Header));
Michael J. Spencera915f242012-08-02 19:16:56 +0000201
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000202 // If the name is less than 8 bytes, store it in place, otherwise
203 // store it in the string table.
204 StringRef Name = YamlSymbol.Name;
205 std::fill_n(Sym.Header.Name, unsigned(COFF::NameSize), 0);
206 if (Name.size() <= COFF::NameSize) {
207 std::copy(Name.begin(), Name.end(), Sym.Header.Name);
208 } else {
209 // Add string to the string table and format the index for output.
210 unsigned Index = getStringIndex(Name);
211 *reinterpret_cast<support::aligned_ulittle32_t*>(
212 Sym.Header.Name + 4) = Index;
213 }
214
215 Sym.Header.Value = YamlSymbol.Value;
216 Sym.Header.Type |= YamlSymbol.SimpleType;
217 Sym.Header.Type |= YamlSymbol.ComplexType << COFF::SCT_COMPLEX_TYPE_SHIFT;
218 Sym.Header.StorageClass = YamlSymbol.StorageClass;
219 Sym.Header.SectionNumber = YamlSymbol.SectionNumber;
220
221 StringRef Data = YamlSymbol.AuxillaryData;
222 if (!hexStringToByteArray(Data, Sym.AuxSymbols)) {
223 errs() << "AuxillaryData must be a collection of pairs of hex bytes";
224 return false;
Michael J. Spencera915f242012-08-02 19:16:56 +0000225 }
226 Symbols.push_back(Sym);
227 }
228 return true;
229 }
230
231 bool parse() {
Rafael Espindola5152e4f2013-04-04 20:30:52 +0000232 parseHeader();
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000233 if (!parseSections())
Michael J. Spencera915f242012-08-02 19:16:56 +0000234 return false;
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000235 if (!parseSymbols())
236 return false;
237 return true;
Michael J. Spencera915f242012-08-02 19:16:56 +0000238 }
239
240 unsigned getStringIndex(StringRef Str) {
241 StringMap<unsigned>::iterator i = StringTableMap.find(Str);
242 if (i == StringTableMap.end()) {
243 unsigned Index = StringTable.size();
244 StringTable.append(Str.begin(), Str.end());
245 StringTable.push_back(0);
246 StringTableMap[Str] = Index;
247 return Index;
248 }
249 return i->second;
250 }
251
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000252 COFFYAML::Object &Obj;
Michael J. Spencera915f242012-08-02 19:16:56 +0000253 COFF::header Header;
254
255 struct Section {
256 COFF::section Header;
257 std::vector<uint8_t> Data;
258 std::vector<COFF::relocation> Relocations;
259 };
260
261 struct Symbol {
262 COFF::symbol Header;
263 std::vector<uint8_t> AuxSymbols;
264 };
265
266 std::vector<Section> Sections;
267 std::vector<Symbol> Symbols;
268 StringMap<unsigned> StringTableMap;
269 std::string StringTable;
270};
271
272// Take a CP and assign addresses and sizes to everything. Returns false if the
273// layout is not valid to do.
274static bool layoutCOFF(COFFParser &CP) {
275 uint32_t SectionTableStart = 0;
276 uint32_t SectionTableSize = 0;
277
278 // The section table starts immediately after the header, including the
279 // optional header.
280 SectionTableStart = sizeof(COFF::header) + CP.Header.SizeOfOptionalHeader;
281 SectionTableSize = sizeof(COFF::section) * CP.Sections.size();
282
283 uint32_t CurrentSectionDataOffset = SectionTableStart + SectionTableSize;
284
285 // Assign each section data address consecutively.
286 for (std::vector<COFFParser::Section>::iterator i = CP.Sections.begin(),
287 e = CP.Sections.end();
288 i != e; ++i) {
289 if (!i->Data.empty()) {
290 i->Header.SizeOfRawData = i->Data.size();
291 i->Header.PointerToRawData = CurrentSectionDataOffset;
292 CurrentSectionDataOffset += i->Header.SizeOfRawData;
Rafael Espindola120cf572013-04-23 15:53:02 +0000293 if (!i->Relocations.empty()) {
294 i->Header.PointerToRelocations = CurrentSectionDataOffset;
295 i->Header.NumberOfRelocations = i->Relocations.size();
296 CurrentSectionDataOffset += i->Header.NumberOfRelocations *
297 COFF::RelocationSize;
298 }
Michael J. Spencera915f242012-08-02 19:16:56 +0000299 // TODO: Handle alignment.
300 } else {
301 i->Header.SizeOfRawData = 0;
302 i->Header.PointerToRawData = 0;
303 }
304 }
305
306 uint32_t SymbolTableStart = CurrentSectionDataOffset;
307
308 // Calculate number of symbols.
309 uint32_t NumberOfSymbols = 0;
310 for (std::vector<COFFParser::Symbol>::iterator i = CP.Symbols.begin(),
311 e = CP.Symbols.end();
312 i != e; ++i) {
313 if (i->AuxSymbols.size() % COFF::SymbolSize != 0) {
314 errs() << "AuxillaryData size not a multiple of symbol size!\n";
315 return false;
316 }
317 i->Header.NumberOfAuxSymbols = i->AuxSymbols.size() / COFF::SymbolSize;
318 NumberOfSymbols += 1 + i->Header.NumberOfAuxSymbols;
319 }
320
321 // Store all the allocated start addresses in the header.
322 CP.Header.NumberOfSections = CP.Sections.size();
323 CP.Header.NumberOfSymbols = NumberOfSymbols;
324 CP.Header.PointerToSymbolTable = SymbolTableStart;
325
326 *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0])
327 = CP.StringTable.size();
328
329 return true;
330}
331
332template <typename value_type>
333struct binary_le_impl {
334 value_type Value;
335 binary_le_impl(value_type V) : Value(V) {}
336};
337
338template <typename value_type>
339raw_ostream &operator <<( raw_ostream &OS
340 , const binary_le_impl<value_type> &BLE) {
341 char Buffer[sizeof(BLE.Value)];
Michael J. Spencerc8b18df2013-01-02 20:14:11 +0000342 support::endian::write<value_type, support::little, support::unaligned>(
343 Buffer, BLE.Value);
Michael J. Spencera915f242012-08-02 19:16:56 +0000344 OS.write(Buffer, sizeof(BLE.Value));
345 return OS;
346}
347
348template <typename value_type>
349binary_le_impl<value_type> binary_le(value_type V) {
350 return binary_le_impl<value_type>(V);
351}
352
353void writeCOFF(COFFParser &CP, raw_ostream &OS) {
354 OS << binary_le(CP.Header.Machine)
355 << binary_le(CP.Header.NumberOfSections)
356 << binary_le(CP.Header.TimeDateStamp)
357 << binary_le(CP.Header.PointerToSymbolTable)
358 << binary_le(CP.Header.NumberOfSymbols)
359 << binary_le(CP.Header.SizeOfOptionalHeader)
360 << binary_le(CP.Header.Characteristics);
361
362 // Output section table.
363 for (std::vector<COFFParser::Section>::const_iterator i = CP.Sections.begin(),
364 e = CP.Sections.end();
365 i != e; ++i) {
366 OS.write(i->Header.Name, COFF::NameSize);
367 OS << binary_le(i->Header.VirtualSize)
368 << binary_le(i->Header.VirtualAddress)
369 << binary_le(i->Header.SizeOfRawData)
370 << binary_le(i->Header.PointerToRawData)
371 << binary_le(i->Header.PointerToRelocations)
372 << binary_le(i->Header.PointerToLineNumbers)
373 << binary_le(i->Header.NumberOfRelocations)
374 << binary_le(i->Header.NumberOfLineNumbers)
375 << binary_le(i->Header.Characteristics);
376 }
377
378 // Output section data.
379 for (std::vector<COFFParser::Section>::const_iterator i = CP.Sections.begin(),
380 e = CP.Sections.end();
381 i != e; ++i) {
382 if (!i->Data.empty())
383 OS.write(reinterpret_cast<const char*>(&i->Data[0]), i->Data.size());
Rafael Espindola120cf572013-04-23 15:53:02 +0000384 for (unsigned I2 = 0, E2 = i->Relocations.size(); I2 != E2; ++I2) {
385 const COFF::relocation &R = i->Relocations[I2];
386 OS << binary_le(R.VirtualAddress)
387 << binary_le(R.SymbolTableIndex)
388 << binary_le(R.Type);
389 }
Michael J. Spencera915f242012-08-02 19:16:56 +0000390 }
391
392 // Output symbol table.
393
394 for (std::vector<COFFParser::Symbol>::const_iterator i = CP.Symbols.begin(),
395 e = CP.Symbols.end();
396 i != e; ++i) {
397 OS.write(i->Header.Name, COFF::NameSize);
398 OS << binary_le(i->Header.Value)
399 << binary_le(i->Header.SectionNumber)
400 << binary_le(i->Header.Type)
401 << binary_le(i->Header.StorageClass)
402 << binary_le(i->Header.NumberOfAuxSymbols);
403 if (!i->AuxSymbols.empty())
404 OS.write( reinterpret_cast<const char*>(&i->AuxSymbols[0])
405 , i->AuxSymbols.size());
406 }
407
408 // Output string table.
409 OS.write(&CP.StringTable[0], CP.StringTable.size());
410}
411
Rafael Espindola7a7e83a2013-04-19 21:28:07 +0000412LLVM_YAML_IS_SEQUENCE_VECTOR(COFF::relocation)
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000413LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section)
414LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)
415
416namespace llvm {
Rafael Espindola2bce4cc2013-04-05 16:00:31 +0000417
418namespace COFF {
419 Characteristics operator|(Characteristics a, Characteristics b) {
420 uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
421 return static_cast<Characteristics>(Ret);
422 }
423
424 SectionCharacteristics
425 operator|(SectionCharacteristics a, SectionCharacteristics b) {
426 uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
427 return static_cast<SectionCharacteristics>(Ret);
428 }
429}
430
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000431namespace yaml {
Rafael Espindola2bce4cc2013-04-05 16:00:31 +0000432
433#define BCase(X) IO.bitSetCase(Value, #X, COFF::X);
434
435template <>
436struct ScalarBitSetTraits<COFF::SectionCharacteristics> {
437 static void bitset(IO &IO, COFF::SectionCharacteristics &Value) {
438 BCase(IMAGE_SCN_TYPE_NO_PAD);
439 BCase(IMAGE_SCN_CNT_CODE);
440 BCase(IMAGE_SCN_CNT_INITIALIZED_DATA);
441 BCase(IMAGE_SCN_CNT_UNINITIALIZED_DATA);
442 BCase(IMAGE_SCN_LNK_OTHER);
443 BCase(IMAGE_SCN_LNK_INFO);
444 BCase(IMAGE_SCN_LNK_REMOVE);
445 BCase(IMAGE_SCN_LNK_COMDAT);
446 BCase(IMAGE_SCN_GPREL);
447 BCase(IMAGE_SCN_MEM_PURGEABLE);
448 BCase(IMAGE_SCN_MEM_16BIT);
449 BCase(IMAGE_SCN_MEM_LOCKED);
450 BCase(IMAGE_SCN_MEM_PRELOAD);
451 BCase(IMAGE_SCN_ALIGN_1BYTES);
452 BCase(IMAGE_SCN_ALIGN_2BYTES);
453 BCase(IMAGE_SCN_ALIGN_4BYTES);
454 BCase(IMAGE_SCN_ALIGN_8BYTES);
455 BCase(IMAGE_SCN_ALIGN_16BYTES);
456 BCase(IMAGE_SCN_ALIGN_32BYTES);
457 BCase(IMAGE_SCN_ALIGN_64BYTES);
458 BCase(IMAGE_SCN_ALIGN_128BYTES);
459 BCase(IMAGE_SCN_ALIGN_256BYTES);
460 BCase(IMAGE_SCN_ALIGN_512BYTES);
461 BCase(IMAGE_SCN_ALIGN_1024BYTES);
462 BCase(IMAGE_SCN_ALIGN_2048BYTES);
463 BCase(IMAGE_SCN_ALIGN_4096BYTES);
464 BCase(IMAGE_SCN_ALIGN_8192BYTES);
465 BCase(IMAGE_SCN_LNK_NRELOC_OVFL);
466 BCase(IMAGE_SCN_MEM_DISCARDABLE);
467 BCase(IMAGE_SCN_MEM_NOT_CACHED);
468 BCase(IMAGE_SCN_MEM_NOT_PAGED);
469 BCase(IMAGE_SCN_MEM_SHARED);
470 BCase(IMAGE_SCN_MEM_EXECUTE);
471 BCase(IMAGE_SCN_MEM_READ);
472 BCase(IMAGE_SCN_MEM_WRITE);
473 }
474};
475
476template <>
477struct ScalarBitSetTraits<COFF::Characteristics> {
478 static void bitset(IO &IO, COFF::Characteristics &Value) {
479 BCase(IMAGE_FILE_RELOCS_STRIPPED);
480 BCase(IMAGE_FILE_EXECUTABLE_IMAGE);
481 BCase(IMAGE_FILE_LINE_NUMS_STRIPPED);
482 BCase(IMAGE_FILE_LOCAL_SYMS_STRIPPED);
483 BCase(IMAGE_FILE_AGGRESSIVE_WS_TRIM);
484 BCase(IMAGE_FILE_LARGE_ADDRESS_AWARE);
485 BCase(IMAGE_FILE_BYTES_REVERSED_LO);
486 BCase(IMAGE_FILE_32BIT_MACHINE);
487 BCase(IMAGE_FILE_DEBUG_STRIPPED);
488 BCase(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP);
489 BCase(IMAGE_FILE_NET_RUN_FROM_SWAP);
490 BCase(IMAGE_FILE_SYSTEM);
491 BCase(IMAGE_FILE_DLL);
492 BCase(IMAGE_FILE_UP_SYSTEM_ONLY);
493 BCase(IMAGE_FILE_BYTES_REVERSED_HI);
494 }
495};
496#undef BCase
497
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000498#define ECase(X) IO.enumCase(Value, #X, COFF::X);
499
500template <>
501struct ScalarEnumerationTraits<COFF::SymbolComplexType> {
502 static void enumeration(IO &IO, COFF::SymbolComplexType &Value) {
503 ECase(IMAGE_SYM_DTYPE_NULL);
504 ECase(IMAGE_SYM_DTYPE_POINTER);
505 ECase(IMAGE_SYM_DTYPE_FUNCTION);
506 ECase(IMAGE_SYM_DTYPE_ARRAY);
507 }
508};
509
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000510template <>
511struct ScalarEnumerationTraits<COFF::SymbolStorageClass> {
512 static void enumeration(IO &IO, COFF::SymbolStorageClass &Value) {
513 ECase(IMAGE_SYM_CLASS_END_OF_FUNCTION);
514 ECase(IMAGE_SYM_CLASS_NULL);
515 ECase(IMAGE_SYM_CLASS_AUTOMATIC);
516 ECase(IMAGE_SYM_CLASS_EXTERNAL);
517 ECase(IMAGE_SYM_CLASS_STATIC);
518 ECase(IMAGE_SYM_CLASS_REGISTER);
519 ECase(IMAGE_SYM_CLASS_EXTERNAL_DEF);
520 ECase(IMAGE_SYM_CLASS_LABEL);
521 ECase(IMAGE_SYM_CLASS_UNDEFINED_LABEL);
522 ECase(IMAGE_SYM_CLASS_MEMBER_OF_STRUCT);
523 ECase(IMAGE_SYM_CLASS_ARGUMENT);
524 ECase(IMAGE_SYM_CLASS_STRUCT_TAG);
525 ECase(IMAGE_SYM_CLASS_MEMBER_OF_UNION);
526 ECase(IMAGE_SYM_CLASS_UNION_TAG);
527 ECase(IMAGE_SYM_CLASS_TYPE_DEFINITION);
528 ECase(IMAGE_SYM_CLASS_UNDEFINED_STATIC);
529 ECase(IMAGE_SYM_CLASS_ENUM_TAG);
530 ECase(IMAGE_SYM_CLASS_MEMBER_OF_ENUM);
531 ECase(IMAGE_SYM_CLASS_REGISTER_PARAM);
532 ECase(IMAGE_SYM_CLASS_BIT_FIELD);
533 ECase(IMAGE_SYM_CLASS_BLOCK);
534 ECase(IMAGE_SYM_CLASS_FUNCTION);
535 ECase(IMAGE_SYM_CLASS_END_OF_STRUCT);
536 ECase(IMAGE_SYM_CLASS_FILE);
537 ECase(IMAGE_SYM_CLASS_SECTION);
538 ECase(IMAGE_SYM_CLASS_WEAK_EXTERNAL);
539 ECase(IMAGE_SYM_CLASS_CLR_TOKEN);
540 }
541};
542
543template <>
544struct ScalarEnumerationTraits<COFF::SymbolBaseType> {
545 static void enumeration(IO &IO, COFF::SymbolBaseType &Value) {
546 ECase(IMAGE_SYM_TYPE_NULL);
547 ECase(IMAGE_SYM_TYPE_VOID);
548 ECase(IMAGE_SYM_TYPE_CHAR);
549 ECase(IMAGE_SYM_TYPE_SHORT);
550 ECase(IMAGE_SYM_TYPE_INT);
551 ECase(IMAGE_SYM_TYPE_LONG);
552 ECase(IMAGE_SYM_TYPE_FLOAT);
553 ECase(IMAGE_SYM_TYPE_DOUBLE);
554 ECase(IMAGE_SYM_TYPE_STRUCT);
555 ECase(IMAGE_SYM_TYPE_UNION);
556 ECase(IMAGE_SYM_TYPE_ENUM);
557 ECase(IMAGE_SYM_TYPE_MOE);
558 ECase(IMAGE_SYM_TYPE_BYTE);
559 ECase(IMAGE_SYM_TYPE_WORD);
560 ECase(IMAGE_SYM_TYPE_UINT);
561 ECase(IMAGE_SYM_TYPE_DWORD);
562 }
563};
564
565template <>
566struct ScalarEnumerationTraits<COFF::MachineTypes> {
567 static void enumeration(IO &IO, COFF::MachineTypes &Value) {
568 ECase(IMAGE_FILE_MACHINE_UNKNOWN);
569 ECase(IMAGE_FILE_MACHINE_AM33);
570 ECase(IMAGE_FILE_MACHINE_AMD64);
571 ECase(IMAGE_FILE_MACHINE_ARM);
572 ECase(IMAGE_FILE_MACHINE_ARMV7);
573 ECase(IMAGE_FILE_MACHINE_EBC);
574 ECase(IMAGE_FILE_MACHINE_I386);
575 ECase(IMAGE_FILE_MACHINE_IA64);
576 ECase(IMAGE_FILE_MACHINE_M32R);
577 ECase(IMAGE_FILE_MACHINE_MIPS16);
578 ECase(IMAGE_FILE_MACHINE_MIPSFPU);
579 ECase(IMAGE_FILE_MACHINE_MIPSFPU16);
580 ECase(IMAGE_FILE_MACHINE_POWERPC);
581 ECase(IMAGE_FILE_MACHINE_POWERPCFP);
582 ECase(IMAGE_FILE_MACHINE_R4000);
583 ECase(IMAGE_FILE_MACHINE_SH3);
584 ECase(IMAGE_FILE_MACHINE_SH3DSP);
585 ECase(IMAGE_FILE_MACHINE_SH4);
586 ECase(IMAGE_FILE_MACHINE_SH5);
587 ECase(IMAGE_FILE_MACHINE_THUMB);
588 ECase(IMAGE_FILE_MACHINE_WCEMIPSV2);
589 }
590};
591
592template <>
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000593struct ScalarEnumerationTraits<COFF::RelocationTypeX86> {
594 static void enumeration(IO &IO, COFF::RelocationTypeX86 &Value) {
595 ECase(IMAGE_REL_I386_ABSOLUTE);
596 ECase(IMAGE_REL_I386_DIR16);
597 ECase(IMAGE_REL_I386_REL16);
598 ECase(IMAGE_REL_I386_DIR32);
599 ECase(IMAGE_REL_I386_DIR32NB);
600 ECase(IMAGE_REL_I386_SEG12);
601 ECase(IMAGE_REL_I386_SECTION);
602 ECase(IMAGE_REL_I386_SECREL);
603 ECase(IMAGE_REL_I386_TOKEN);
604 ECase(IMAGE_REL_I386_SECREL7);
605 ECase(IMAGE_REL_I386_REL32);
606 ECase(IMAGE_REL_AMD64_ABSOLUTE);
607 ECase(IMAGE_REL_AMD64_ADDR64);
608 ECase(IMAGE_REL_AMD64_ADDR32);
609 ECase(IMAGE_REL_AMD64_ADDR32NB);
610 ECase(IMAGE_REL_AMD64_REL32);
611 ECase(IMAGE_REL_AMD64_REL32_1);
612 ECase(IMAGE_REL_AMD64_REL32_2);
613 ECase(IMAGE_REL_AMD64_REL32_3);
614 ECase(IMAGE_REL_AMD64_REL32_4);
615 ECase(IMAGE_REL_AMD64_REL32_5);
616 ECase(IMAGE_REL_AMD64_SECTION);
617 ECase(IMAGE_REL_AMD64_SECREL);
618 ECase(IMAGE_REL_AMD64_SECREL7);
619 ECase(IMAGE_REL_AMD64_TOKEN);
620 ECase(IMAGE_REL_AMD64_SREL32);
621 ECase(IMAGE_REL_AMD64_PAIR);
622 ECase(IMAGE_REL_AMD64_SSPAN32);
623 }
624};
625
626#undef ECase
627
628template <>
629struct MappingTraits<COFFYAML::Symbol> {
630 static void mapping(IO &IO, COFFYAML::Symbol &S) {
631 IO.mapRequired("SimpleType", S.SimpleType);
632 IO.mapOptional("NumberOfAuxSymbols", S.NumberOfAuxSymbols);
633 IO.mapRequired("Name", S.Name);
634 IO.mapRequired("StorageClass", S.StorageClass);
635 IO.mapOptional("AuxillaryData", S.AuxillaryData); // FIXME: typo
636 IO.mapRequired("ComplexType", S.ComplexType);
637 IO.mapRequired("Value", S.Value);
638 IO.mapRequired("SectionNumber", S.SectionNumber);
639 }
640};
641
642template <>
Rafael Espindolaf59a2a82013-04-20 02:02:25 +0000643struct MappingTraits<COFF::header> {
644 struct NMachine {
645 NMachine(IO&) : Machine(COFF::MachineTypes(0)) {
646 }
647 NMachine(IO&, uint16_t M) : Machine(COFF::MachineTypes(M)) {
648 }
649 uint16_t denormalize(IO &) {
650 return Machine;
651 }
652 COFF::MachineTypes Machine;
653 };
654
655 struct NCharacteristics {
656 NCharacteristics(IO&) : Characteristics(COFF::Characteristics(0)) {
657 }
658 NCharacteristics(IO&, uint16_t C) :
659 Characteristics(COFF::Characteristics(C)) {
660 }
661 uint16_t denormalize(IO &) {
662 return Characteristics;
663 }
664
665 COFF::Characteristics Characteristics;
666 };
667
668 static void mapping(IO &IO, COFF::header &H) {
669 MappingNormalization<NMachine, uint16_t> NM(IO, H.Machine);
670 MappingNormalization<NCharacteristics, uint16_t> NC(IO, H.Characteristics);
671
672 IO.mapRequired("Machine", NM->Machine);
673 IO.mapOptional("Characteristics", NC->Characteristics);
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000674 }
675};
676
677template <>
Rafael Espindola7a7e83a2013-04-19 21:28:07 +0000678struct MappingTraits<COFF::relocation> {
Rafael Espindolaf59a2a82013-04-20 02:02:25 +0000679 struct NType {
680 NType(IO &) : Type(COFF::RelocationTypeX86(0)) {
Rafael Espindola7a7e83a2013-04-19 21:28:07 +0000681 }
Rafael Espindolaf59a2a82013-04-20 02:02:25 +0000682 NType(IO &, uint16_t T) : Type(COFF::RelocationTypeX86(T)) {
Rafael Espindola7a7e83a2013-04-19 21:28:07 +0000683 }
684 uint16_t denormalize(IO &) {
685 return Type;
686 }
Rafael Espindola7a7e83a2013-04-19 21:28:07 +0000687 COFF::RelocationTypeX86 Type;
688 };
Rafael Espindola7a7e83a2013-04-19 21:28:07 +0000689
Rafael Espindolaf59a2a82013-04-20 02:02:25 +0000690 static void mapping(IO &IO, COFF::relocation &Rel) {
691 MappingNormalization<NType, uint16_t> NT(IO, Rel.Type);
692
693 IO.mapRequired("Type", NT->Type);
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000694 IO.mapRequired("VirtualAddress", Rel.VirtualAddress);
695 IO.mapRequired("SymbolTableIndex", Rel.SymbolTableIndex);
696 }
697};
698
699template <>
700struct MappingTraits<COFFYAML::Section> {
701 static void mapping(IO &IO, COFFYAML::Section &Sec) {
702 IO.mapOptional("Relocations", Sec.Relocations);
703 IO.mapRequired("SectionData", Sec.SectionData);
704 IO.mapRequired("Characteristics", Sec.Characteristics);
705 IO.mapRequired("Name", Sec.Name);
706 }
707};
708
709template <>
710struct MappingTraits<COFFYAML::Object> {
711 static void mapping(IO &IO, COFFYAML::Object &Obj) {
712 IO.mapRequired("sections", Obj.Sections);
713 IO.mapRequired("header", Obj.HeaderData);
714 IO.mapRequired("symbols", Obj.Symbols);
715 }
716};
717} // end namespace yaml
718} // end namespace llvm
719
Michael J. Spencera915f242012-08-02 19:16:56 +0000720int main(int argc, char **argv) {
721 cl::ParseCommandLineOptions(argc, argv);
722 sys::PrintStackTraceOnErrorSignal();
723 PrettyStackTraceProgram X(argc, argv);
724 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
725
726 OwningPtr<MemoryBuffer> Buf;
727 if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
728 return 1;
729
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000730 yaml::Input YIn(Buf->getBuffer());
731 COFFYAML::Object Doc;
732 YIn >> Doc;
733 if (YIn.error()) {
734 errs() << "yaml2obj: Failed to parse YAML file!\n";
735 return 1;
736 }
737
738 COFFParser CP(Doc);
Michael J. Spencera915f242012-08-02 19:16:56 +0000739 if (!CP.parse()) {
740 errs() << "yaml2obj: Failed to parse YAML file!\n";
741 return 1;
742 }
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000743
Michael J. Spencera915f242012-08-02 19:16:56 +0000744 if (!layoutCOFF(CP)) {
745 errs() << "yaml2obj: Failed to layout COFF file!\n";
746 return 1;
747 }
748 writeCOFF(CP, outs());
749}