Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 1 | //===- 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. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Signals.h" |
| 29 | #include "llvm/Support/SourceMgr.h" |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 30 | #include "llvm/Support/YAMLTraits.h" |
Chandler Carruth | 4ffd89f | 2012-12-04 10:37:14 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
| 32 | #include "llvm/Support/system_error.h" |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 33 | #include <vector> |
| 34 | |
| 35 | using namespace llvm; |
| 36 | |
| 37 | static cl::opt<std::string> |
| 38 | Input(cl::Positional, cl::desc("<input>"), cl::init("-")); |
| 39 | |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 40 | namespace { |
| 41 | |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 42 | template<class T> |
Michael J. Spencer | 1de266b | 2012-08-02 19:36:30 +0000 | [diff] [blame] | 43 | typename llvm::enable_if_c<std::numeric_limits<T>::is_integer, bool>::type |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 44 | getAs(const llvm::yaml::ScalarNode *SN, T &Result) { |
| 45 | SmallString<4> Storage; |
| 46 | StringRef Value = SN->getValue(Storage); |
| 47 | if (Value.getAsInteger(0, Result)) |
| 48 | return false; |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | // Given a container with begin and end with ::value_type of a character type. |
| 53 | // Iterate through pairs of characters in the the set of [a-fA-F0-9] ignoring |
| 54 | // all other characters. |
| 55 | struct hex_pair_iterator { |
| 56 | StringRef::const_iterator Current, End; |
| 57 | typedef SmallVector<char, 2> value_type; |
| 58 | value_type Pair; |
| 59 | bool IsDone; |
| 60 | |
| 61 | hex_pair_iterator(StringRef C) |
| 62 | : Current(C.begin()), End(C.end()), IsDone(false) { |
| 63 | // Initalize Pair. |
| 64 | ++*this; |
| 65 | } |
| 66 | |
| 67 | // End iterator. |
| 68 | hex_pair_iterator() : Current(), End(), IsDone(true) {} |
| 69 | |
| 70 | value_type operator *() const { |
| 71 | return Pair; |
| 72 | } |
| 73 | |
| 74 | hex_pair_iterator operator ++() { |
| 75 | // We're at the end of the input. |
| 76 | if (Current == End) { |
| 77 | IsDone = true; |
| 78 | return *this; |
| 79 | } |
| 80 | Pair = value_type(); |
| 81 | for (; Current != End && Pair.size() != 2; ++Current) { |
| 82 | // Is a valid hex digit. |
| 83 | if ((*Current >= '0' && *Current <= '9') || |
| 84 | (*Current >= 'a' && *Current <= 'f') || |
| 85 | (*Current >= 'A' && *Current <= 'F')) |
| 86 | Pair.push_back(*Current); |
| 87 | } |
| 88 | // Hit the end without getting 2 hex digits. Pair is invalid. |
| 89 | if (Pair.size() != 2) |
| 90 | IsDone = true; |
| 91 | return *this; |
| 92 | } |
| 93 | |
| 94 | bool operator ==(const hex_pair_iterator Other) { |
Richard Trieu | 7b07d69 | 2012-08-02 23:22:39 +0000 | [diff] [blame] | 95 | return (IsDone == Other.IsDone) || |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 96 | (Current == Other.Current && End == Other.End); |
| 97 | } |
| 98 | |
| 99 | bool operator !=(const hex_pair_iterator Other) { |
| 100 | return !(*this == Other); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | template <class ContainerOut> |
| 105 | static bool hexStringToByteArray(StringRef Str, ContainerOut &Out) { |
| 106 | for (hex_pair_iterator I(Str), E; I != E; ++I) { |
| 107 | typename hex_pair_iterator::value_type Pair = *I; |
| 108 | typename ContainerOut::value_type Byte; |
| 109 | if (StringRef(Pair.data(), 2).getAsInteger(16, Byte)) |
| 110 | return false; |
| 111 | Out.push_back(Byte); |
| 112 | } |
| 113 | return true; |
| 114 | } |
| 115 | |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 116 | // The structure of the yaml files is not an exact 1:1 match to COFF. In order |
| 117 | // to use yaml::IO, we use these structures which are closer to the source. |
| 118 | namespace COFFYAML { |
| 119 | struct Relocation { |
| 120 | uint32_t VirtualAddress; |
| 121 | uint32_t SymbolTableIndex; |
| 122 | COFF::RelocationTypeX86 Type; |
| 123 | }; |
| 124 | |
| 125 | struct Section { |
| 126 | std::vector<COFF::SectionCharacteristics> Characteristics; |
| 127 | StringRef SectionData; |
| 128 | std::vector<Relocation> Relocations; |
| 129 | StringRef Name; |
| 130 | }; |
| 131 | |
| 132 | struct Header { |
| 133 | COFF::MachineTypes Machine; |
| 134 | }; |
| 135 | |
| 136 | struct Symbol { |
| 137 | COFF::SymbolBaseType SimpleType; |
| 138 | uint8_t NumberOfAuxSymbols; |
| 139 | StringRef Name; |
| 140 | COFF::SymbolStorageClass StorageClass; |
| 141 | StringRef AuxillaryData; |
| 142 | COFF::SymbolComplexType ComplexType; |
| 143 | uint32_t Value; |
| 144 | uint16_t SectionNumber; |
| 145 | }; |
| 146 | |
| 147 | struct Object { |
| 148 | Header HeaderData; |
| 149 | std::vector<Section> Sections; |
| 150 | std::vector<Symbol> Symbols; |
| 151 | }; |
| 152 | } |
| 153 | |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 154 | /// This parses a yaml stream that represents a COFF object file. |
| 155 | /// See docs/yaml2obj for the yaml scheema. |
| 156 | struct COFFParser { |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 157 | COFFParser(COFFYAML::Object &Obj) : Obj(Obj) { |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 158 | std::memset(&Header, 0, sizeof(Header)); |
| 159 | // A COFF string table always starts with a 4 byte size field. Offsets into |
| 160 | // it include this size, so allocate it now. |
| 161 | StringTable.append(4, 0); |
| 162 | } |
| 163 | |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 164 | bool parseSections() { |
| 165 | for (std::vector<COFFYAML::Section>::iterator i = Obj.Sections.begin(), |
| 166 | e = Obj.Sections.end(); i != e; ++i) { |
| 167 | const COFFYAML::Section &YamlSection = *i; |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 168 | Section Sec; |
| 169 | std::memset(&Sec.Header, 0, sizeof(Sec.Header)); |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 170 | |
| 171 | // If the name is less than 8 bytes, store it in place, otherwise |
| 172 | // store it in the string table. |
| 173 | StringRef Name = YamlSection.Name; |
| 174 | std::fill_n(Sec.Header.Name, unsigned(COFF::NameSize), 0); |
| 175 | if (Name.size() <= COFF::NameSize) { |
| 176 | std::copy(Name.begin(), Name.end(), Sec.Header.Name); |
| 177 | } else { |
| 178 | // Add string to the string table and format the index for output. |
| 179 | unsigned Index = getStringIndex(Name); |
| 180 | std::string str = utostr(Index); |
| 181 | if (str.size() > 7) { |
| 182 | errs() << "String table got too large"; |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 183 | return false; |
| 184 | } |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 185 | Sec.Header.Name[0] = '/'; |
| 186 | std::copy(str.begin(), str.end(), Sec.Header.Name + 1); |
| 187 | } |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 188 | |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 189 | for (std::vector<COFF::SectionCharacteristics>::const_iterator i = |
| 190 | YamlSection.Characteristics.begin(), |
| 191 | e = YamlSection.Characteristics.end(); |
| 192 | i != e; ++i) { |
| 193 | uint32_t Characteristic = *i; |
| 194 | Sec.Header.Characteristics |= Characteristic; |
| 195 | } |
| 196 | |
| 197 | StringRef Data = YamlSection.SectionData; |
| 198 | if (!hexStringToByteArray(Data, Sec.Data)) { |
| 199 | errs() << "SectionData must be a collection of pairs of hex bytes"; |
| 200 | return false; |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 201 | } |
| 202 | Sections.push_back(Sec); |
| 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 207 | bool parseSymbols() { |
| 208 | for (std::vector<COFFYAML::Symbol>::iterator i = Obj.Symbols.begin(), |
| 209 | e = Obj.Symbols.end(); i != e; ++i) { |
| 210 | COFFYAML::Symbol YamlSymbol = *i; |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 211 | Symbol Sym; |
| 212 | std::memset(&Sym.Header, 0, sizeof(Sym.Header)); |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 213 | |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 214 | // If the name is less than 8 bytes, store it in place, otherwise |
| 215 | // store it in the string table. |
| 216 | StringRef Name = YamlSymbol.Name; |
| 217 | std::fill_n(Sym.Header.Name, unsigned(COFF::NameSize), 0); |
| 218 | if (Name.size() <= COFF::NameSize) { |
| 219 | std::copy(Name.begin(), Name.end(), Sym.Header.Name); |
| 220 | } else { |
| 221 | // Add string to the string table and format the index for output. |
| 222 | unsigned Index = getStringIndex(Name); |
| 223 | *reinterpret_cast<support::aligned_ulittle32_t*>( |
| 224 | Sym.Header.Name + 4) = Index; |
| 225 | } |
| 226 | |
| 227 | Sym.Header.Value = YamlSymbol.Value; |
| 228 | Sym.Header.Type |= YamlSymbol.SimpleType; |
| 229 | Sym.Header.Type |= YamlSymbol.ComplexType << COFF::SCT_COMPLEX_TYPE_SHIFT; |
| 230 | Sym.Header.StorageClass = YamlSymbol.StorageClass; |
| 231 | Sym.Header.SectionNumber = YamlSymbol.SectionNumber; |
| 232 | |
| 233 | StringRef Data = YamlSymbol.AuxillaryData; |
| 234 | if (!hexStringToByteArray(Data, Sym.AuxSymbols)) { |
| 235 | errs() << "AuxillaryData must be a collection of pairs of hex bytes"; |
| 236 | return false; |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 237 | } |
| 238 | Symbols.push_back(Sym); |
| 239 | } |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | bool parse() { |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 244 | Header.Machine = Obj.HeaderData.Machine; |
| 245 | if (!parseSections()) |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 246 | return false; |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 247 | if (!parseSymbols()) |
| 248 | return false; |
| 249 | return true; |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | unsigned getStringIndex(StringRef Str) { |
| 253 | StringMap<unsigned>::iterator i = StringTableMap.find(Str); |
| 254 | if (i == StringTableMap.end()) { |
| 255 | unsigned Index = StringTable.size(); |
| 256 | StringTable.append(Str.begin(), Str.end()); |
| 257 | StringTable.push_back(0); |
| 258 | StringTableMap[Str] = Index; |
| 259 | return Index; |
| 260 | } |
| 261 | return i->second; |
| 262 | } |
| 263 | |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 264 | COFFYAML::Object &Obj; |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 265 | COFF::header Header; |
| 266 | |
| 267 | struct Section { |
| 268 | COFF::section Header; |
| 269 | std::vector<uint8_t> Data; |
| 270 | std::vector<COFF::relocation> Relocations; |
| 271 | }; |
| 272 | |
| 273 | struct Symbol { |
| 274 | COFF::symbol Header; |
| 275 | std::vector<uint8_t> AuxSymbols; |
| 276 | }; |
| 277 | |
| 278 | std::vector<Section> Sections; |
| 279 | std::vector<Symbol> Symbols; |
| 280 | StringMap<unsigned> StringTableMap; |
| 281 | std::string StringTable; |
| 282 | }; |
| 283 | |
| 284 | // Take a CP and assign addresses and sizes to everything. Returns false if the |
| 285 | // layout is not valid to do. |
| 286 | static bool layoutCOFF(COFFParser &CP) { |
| 287 | uint32_t SectionTableStart = 0; |
| 288 | uint32_t SectionTableSize = 0; |
| 289 | |
| 290 | // The section table starts immediately after the header, including the |
| 291 | // optional header. |
| 292 | SectionTableStart = sizeof(COFF::header) + CP.Header.SizeOfOptionalHeader; |
| 293 | SectionTableSize = sizeof(COFF::section) * CP.Sections.size(); |
| 294 | |
| 295 | uint32_t CurrentSectionDataOffset = SectionTableStart + SectionTableSize; |
| 296 | |
| 297 | // Assign each section data address consecutively. |
| 298 | for (std::vector<COFFParser::Section>::iterator i = CP.Sections.begin(), |
| 299 | e = CP.Sections.end(); |
| 300 | i != e; ++i) { |
| 301 | if (!i->Data.empty()) { |
| 302 | i->Header.SizeOfRawData = i->Data.size(); |
| 303 | i->Header.PointerToRawData = CurrentSectionDataOffset; |
| 304 | CurrentSectionDataOffset += i->Header.SizeOfRawData; |
| 305 | // TODO: Handle alignment. |
| 306 | } else { |
| 307 | i->Header.SizeOfRawData = 0; |
| 308 | i->Header.PointerToRawData = 0; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | uint32_t SymbolTableStart = CurrentSectionDataOffset; |
| 313 | |
| 314 | // Calculate number of symbols. |
| 315 | uint32_t NumberOfSymbols = 0; |
| 316 | for (std::vector<COFFParser::Symbol>::iterator i = CP.Symbols.begin(), |
| 317 | e = CP.Symbols.end(); |
| 318 | i != e; ++i) { |
| 319 | if (i->AuxSymbols.size() % COFF::SymbolSize != 0) { |
| 320 | errs() << "AuxillaryData size not a multiple of symbol size!\n"; |
| 321 | return false; |
| 322 | } |
| 323 | i->Header.NumberOfAuxSymbols = i->AuxSymbols.size() / COFF::SymbolSize; |
| 324 | NumberOfSymbols += 1 + i->Header.NumberOfAuxSymbols; |
| 325 | } |
| 326 | |
| 327 | // Store all the allocated start addresses in the header. |
| 328 | CP.Header.NumberOfSections = CP.Sections.size(); |
| 329 | CP.Header.NumberOfSymbols = NumberOfSymbols; |
| 330 | CP.Header.PointerToSymbolTable = SymbolTableStart; |
| 331 | |
| 332 | *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0]) |
| 333 | = CP.StringTable.size(); |
| 334 | |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | template <typename value_type> |
| 339 | struct binary_le_impl { |
| 340 | value_type Value; |
| 341 | binary_le_impl(value_type V) : Value(V) {} |
| 342 | }; |
| 343 | |
| 344 | template <typename value_type> |
| 345 | raw_ostream &operator <<( raw_ostream &OS |
| 346 | , const binary_le_impl<value_type> &BLE) { |
| 347 | char Buffer[sizeof(BLE.Value)]; |
Michael J. Spencer | c8b18df | 2013-01-02 20:14:11 +0000 | [diff] [blame] | 348 | support::endian::write<value_type, support::little, support::unaligned>( |
| 349 | Buffer, BLE.Value); |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 350 | OS.write(Buffer, sizeof(BLE.Value)); |
| 351 | return OS; |
| 352 | } |
| 353 | |
| 354 | template <typename value_type> |
| 355 | binary_le_impl<value_type> binary_le(value_type V) { |
| 356 | return binary_le_impl<value_type>(V); |
| 357 | } |
| 358 | |
| 359 | void writeCOFF(COFFParser &CP, raw_ostream &OS) { |
| 360 | OS << binary_le(CP.Header.Machine) |
| 361 | << binary_le(CP.Header.NumberOfSections) |
| 362 | << binary_le(CP.Header.TimeDateStamp) |
| 363 | << binary_le(CP.Header.PointerToSymbolTable) |
| 364 | << binary_le(CP.Header.NumberOfSymbols) |
| 365 | << binary_le(CP.Header.SizeOfOptionalHeader) |
| 366 | << binary_le(CP.Header.Characteristics); |
| 367 | |
| 368 | // Output section table. |
| 369 | for (std::vector<COFFParser::Section>::const_iterator i = CP.Sections.begin(), |
| 370 | e = CP.Sections.end(); |
| 371 | i != e; ++i) { |
| 372 | OS.write(i->Header.Name, COFF::NameSize); |
| 373 | OS << binary_le(i->Header.VirtualSize) |
| 374 | << binary_le(i->Header.VirtualAddress) |
| 375 | << binary_le(i->Header.SizeOfRawData) |
| 376 | << binary_le(i->Header.PointerToRawData) |
| 377 | << binary_le(i->Header.PointerToRelocations) |
| 378 | << binary_le(i->Header.PointerToLineNumbers) |
| 379 | << binary_le(i->Header.NumberOfRelocations) |
| 380 | << binary_le(i->Header.NumberOfLineNumbers) |
| 381 | << binary_le(i->Header.Characteristics); |
| 382 | } |
| 383 | |
| 384 | // Output section data. |
| 385 | for (std::vector<COFFParser::Section>::const_iterator i = CP.Sections.begin(), |
| 386 | e = CP.Sections.end(); |
| 387 | i != e; ++i) { |
| 388 | if (!i->Data.empty()) |
| 389 | OS.write(reinterpret_cast<const char*>(&i->Data[0]), i->Data.size()); |
| 390 | } |
| 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 Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 412 | } |
| 413 | |
| 414 | LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Relocation) |
| 415 | LLVM_YAML_IS_SEQUENCE_VECTOR(COFF::SectionCharacteristics) |
| 416 | LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section) |
| 417 | LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol) |
| 418 | |
| 419 | namespace llvm { |
| 420 | namespace yaml { |
| 421 | #define ECase(X) IO.enumCase(Value, #X, COFF::X); |
| 422 | |
| 423 | template <> |
| 424 | struct ScalarEnumerationTraits<COFF::SymbolComplexType> { |
| 425 | static void enumeration(IO &IO, COFF::SymbolComplexType &Value) { |
| 426 | ECase(IMAGE_SYM_DTYPE_NULL); |
| 427 | ECase(IMAGE_SYM_DTYPE_POINTER); |
| 428 | ECase(IMAGE_SYM_DTYPE_FUNCTION); |
| 429 | ECase(IMAGE_SYM_DTYPE_ARRAY); |
| 430 | } |
| 431 | }; |
| 432 | |
| 433 | // FIXME: We cannot use ScalarBitSetTraits because of |
| 434 | // IMAGE_SYM_CLASS_END_OF_FUNCTION which is -1. |
| 435 | template <> |
| 436 | struct ScalarEnumerationTraits<COFF::SymbolStorageClass> { |
| 437 | static void enumeration(IO &IO, COFF::SymbolStorageClass &Value) { |
| 438 | ECase(IMAGE_SYM_CLASS_END_OF_FUNCTION); |
| 439 | ECase(IMAGE_SYM_CLASS_NULL); |
| 440 | ECase(IMAGE_SYM_CLASS_AUTOMATIC); |
| 441 | ECase(IMAGE_SYM_CLASS_EXTERNAL); |
| 442 | ECase(IMAGE_SYM_CLASS_STATIC); |
| 443 | ECase(IMAGE_SYM_CLASS_REGISTER); |
| 444 | ECase(IMAGE_SYM_CLASS_EXTERNAL_DEF); |
| 445 | ECase(IMAGE_SYM_CLASS_LABEL); |
| 446 | ECase(IMAGE_SYM_CLASS_UNDEFINED_LABEL); |
| 447 | ECase(IMAGE_SYM_CLASS_MEMBER_OF_STRUCT); |
| 448 | ECase(IMAGE_SYM_CLASS_ARGUMENT); |
| 449 | ECase(IMAGE_SYM_CLASS_STRUCT_TAG); |
| 450 | ECase(IMAGE_SYM_CLASS_MEMBER_OF_UNION); |
| 451 | ECase(IMAGE_SYM_CLASS_UNION_TAG); |
| 452 | ECase(IMAGE_SYM_CLASS_TYPE_DEFINITION); |
| 453 | ECase(IMAGE_SYM_CLASS_UNDEFINED_STATIC); |
| 454 | ECase(IMAGE_SYM_CLASS_ENUM_TAG); |
| 455 | ECase(IMAGE_SYM_CLASS_MEMBER_OF_ENUM); |
| 456 | ECase(IMAGE_SYM_CLASS_REGISTER_PARAM); |
| 457 | ECase(IMAGE_SYM_CLASS_BIT_FIELD); |
| 458 | ECase(IMAGE_SYM_CLASS_BLOCK); |
| 459 | ECase(IMAGE_SYM_CLASS_FUNCTION); |
| 460 | ECase(IMAGE_SYM_CLASS_END_OF_STRUCT); |
| 461 | ECase(IMAGE_SYM_CLASS_FILE); |
| 462 | ECase(IMAGE_SYM_CLASS_SECTION); |
| 463 | ECase(IMAGE_SYM_CLASS_WEAK_EXTERNAL); |
| 464 | ECase(IMAGE_SYM_CLASS_CLR_TOKEN); |
| 465 | } |
| 466 | }; |
| 467 | |
| 468 | template <> |
| 469 | struct ScalarEnumerationTraits<COFF::SymbolBaseType> { |
| 470 | static void enumeration(IO &IO, COFF::SymbolBaseType &Value) { |
| 471 | ECase(IMAGE_SYM_TYPE_NULL); |
| 472 | ECase(IMAGE_SYM_TYPE_VOID); |
| 473 | ECase(IMAGE_SYM_TYPE_CHAR); |
| 474 | ECase(IMAGE_SYM_TYPE_SHORT); |
| 475 | ECase(IMAGE_SYM_TYPE_INT); |
| 476 | ECase(IMAGE_SYM_TYPE_LONG); |
| 477 | ECase(IMAGE_SYM_TYPE_FLOAT); |
| 478 | ECase(IMAGE_SYM_TYPE_DOUBLE); |
| 479 | ECase(IMAGE_SYM_TYPE_STRUCT); |
| 480 | ECase(IMAGE_SYM_TYPE_UNION); |
| 481 | ECase(IMAGE_SYM_TYPE_ENUM); |
| 482 | ECase(IMAGE_SYM_TYPE_MOE); |
| 483 | ECase(IMAGE_SYM_TYPE_BYTE); |
| 484 | ECase(IMAGE_SYM_TYPE_WORD); |
| 485 | ECase(IMAGE_SYM_TYPE_UINT); |
| 486 | ECase(IMAGE_SYM_TYPE_DWORD); |
| 487 | } |
| 488 | }; |
| 489 | |
| 490 | template <> |
| 491 | struct ScalarEnumerationTraits<COFF::MachineTypes> { |
| 492 | static void enumeration(IO &IO, COFF::MachineTypes &Value) { |
| 493 | ECase(IMAGE_FILE_MACHINE_UNKNOWN); |
| 494 | ECase(IMAGE_FILE_MACHINE_AM33); |
| 495 | ECase(IMAGE_FILE_MACHINE_AMD64); |
| 496 | ECase(IMAGE_FILE_MACHINE_ARM); |
| 497 | ECase(IMAGE_FILE_MACHINE_ARMV7); |
| 498 | ECase(IMAGE_FILE_MACHINE_EBC); |
| 499 | ECase(IMAGE_FILE_MACHINE_I386); |
| 500 | ECase(IMAGE_FILE_MACHINE_IA64); |
| 501 | ECase(IMAGE_FILE_MACHINE_M32R); |
| 502 | ECase(IMAGE_FILE_MACHINE_MIPS16); |
| 503 | ECase(IMAGE_FILE_MACHINE_MIPSFPU); |
| 504 | ECase(IMAGE_FILE_MACHINE_MIPSFPU16); |
| 505 | ECase(IMAGE_FILE_MACHINE_POWERPC); |
| 506 | ECase(IMAGE_FILE_MACHINE_POWERPCFP); |
| 507 | ECase(IMAGE_FILE_MACHINE_R4000); |
| 508 | ECase(IMAGE_FILE_MACHINE_SH3); |
| 509 | ECase(IMAGE_FILE_MACHINE_SH3DSP); |
| 510 | ECase(IMAGE_FILE_MACHINE_SH4); |
| 511 | ECase(IMAGE_FILE_MACHINE_SH5); |
| 512 | ECase(IMAGE_FILE_MACHINE_THUMB); |
| 513 | ECase(IMAGE_FILE_MACHINE_WCEMIPSV2); |
| 514 | } |
| 515 | }; |
| 516 | |
| 517 | template <> |
| 518 | struct ScalarEnumerationTraits<COFF::SectionCharacteristics> { |
| 519 | static void enumeration(IO &IO, COFF::SectionCharacteristics &Value) { |
| 520 | ECase(IMAGE_SCN_TYPE_NO_PAD); |
| 521 | ECase(IMAGE_SCN_CNT_CODE); |
| 522 | ECase(IMAGE_SCN_CNT_INITIALIZED_DATA); |
| 523 | ECase(IMAGE_SCN_CNT_UNINITIALIZED_DATA); |
| 524 | ECase(IMAGE_SCN_LNK_OTHER); |
| 525 | ECase(IMAGE_SCN_LNK_INFO); |
| 526 | ECase(IMAGE_SCN_LNK_REMOVE); |
| 527 | ECase(IMAGE_SCN_LNK_COMDAT); |
| 528 | ECase(IMAGE_SCN_GPREL); |
| 529 | ECase(IMAGE_SCN_MEM_PURGEABLE); |
| 530 | ECase(IMAGE_SCN_MEM_16BIT); |
| 531 | ECase(IMAGE_SCN_MEM_LOCKED); |
| 532 | ECase(IMAGE_SCN_MEM_PRELOAD); |
| 533 | ECase(IMAGE_SCN_ALIGN_1BYTES); |
| 534 | ECase(IMAGE_SCN_ALIGN_2BYTES); |
| 535 | ECase(IMAGE_SCN_ALIGN_4BYTES); |
| 536 | ECase(IMAGE_SCN_ALIGN_8BYTES); |
| 537 | ECase(IMAGE_SCN_ALIGN_16BYTES); |
| 538 | ECase(IMAGE_SCN_ALIGN_32BYTES); |
| 539 | ECase(IMAGE_SCN_ALIGN_64BYTES); |
| 540 | ECase(IMAGE_SCN_ALIGN_128BYTES); |
| 541 | ECase(IMAGE_SCN_ALIGN_256BYTES); |
| 542 | ECase(IMAGE_SCN_ALIGN_512BYTES); |
| 543 | ECase(IMAGE_SCN_ALIGN_1024BYTES); |
| 544 | ECase(IMAGE_SCN_ALIGN_2048BYTES); |
| 545 | ECase(IMAGE_SCN_ALIGN_4096BYTES); |
| 546 | ECase(IMAGE_SCN_ALIGN_8192BYTES); |
| 547 | ECase(IMAGE_SCN_LNK_NRELOC_OVFL); |
| 548 | ECase(IMAGE_SCN_MEM_DISCARDABLE); |
| 549 | ECase(IMAGE_SCN_MEM_NOT_CACHED); |
| 550 | ECase(IMAGE_SCN_MEM_NOT_PAGED); |
| 551 | ECase(IMAGE_SCN_MEM_SHARED); |
| 552 | ECase(IMAGE_SCN_MEM_EXECUTE); |
| 553 | ECase(IMAGE_SCN_MEM_READ); |
| 554 | ECase(IMAGE_SCN_MEM_WRITE); |
| 555 | } |
| 556 | }; |
| 557 | |
| 558 | template <> |
| 559 | struct ScalarEnumerationTraits<COFF::RelocationTypeX86> { |
| 560 | static void enumeration(IO &IO, COFF::RelocationTypeX86 &Value) { |
| 561 | ECase(IMAGE_REL_I386_ABSOLUTE); |
| 562 | ECase(IMAGE_REL_I386_DIR16); |
| 563 | ECase(IMAGE_REL_I386_REL16); |
| 564 | ECase(IMAGE_REL_I386_DIR32); |
| 565 | ECase(IMAGE_REL_I386_DIR32NB); |
| 566 | ECase(IMAGE_REL_I386_SEG12); |
| 567 | ECase(IMAGE_REL_I386_SECTION); |
| 568 | ECase(IMAGE_REL_I386_SECREL); |
| 569 | ECase(IMAGE_REL_I386_TOKEN); |
| 570 | ECase(IMAGE_REL_I386_SECREL7); |
| 571 | ECase(IMAGE_REL_I386_REL32); |
| 572 | ECase(IMAGE_REL_AMD64_ABSOLUTE); |
| 573 | ECase(IMAGE_REL_AMD64_ADDR64); |
| 574 | ECase(IMAGE_REL_AMD64_ADDR32); |
| 575 | ECase(IMAGE_REL_AMD64_ADDR32NB); |
| 576 | ECase(IMAGE_REL_AMD64_REL32); |
| 577 | ECase(IMAGE_REL_AMD64_REL32_1); |
| 578 | ECase(IMAGE_REL_AMD64_REL32_2); |
| 579 | ECase(IMAGE_REL_AMD64_REL32_3); |
| 580 | ECase(IMAGE_REL_AMD64_REL32_4); |
| 581 | ECase(IMAGE_REL_AMD64_REL32_5); |
| 582 | ECase(IMAGE_REL_AMD64_SECTION); |
| 583 | ECase(IMAGE_REL_AMD64_SECREL); |
| 584 | ECase(IMAGE_REL_AMD64_SECREL7); |
| 585 | ECase(IMAGE_REL_AMD64_TOKEN); |
| 586 | ECase(IMAGE_REL_AMD64_SREL32); |
| 587 | ECase(IMAGE_REL_AMD64_PAIR); |
| 588 | ECase(IMAGE_REL_AMD64_SSPAN32); |
| 589 | } |
| 590 | }; |
| 591 | |
| 592 | #undef ECase |
| 593 | |
| 594 | template <> |
| 595 | struct MappingTraits<COFFYAML::Symbol> { |
| 596 | static void mapping(IO &IO, COFFYAML::Symbol &S) { |
| 597 | IO.mapRequired("SimpleType", S.SimpleType); |
| 598 | IO.mapOptional("NumberOfAuxSymbols", S.NumberOfAuxSymbols); |
| 599 | IO.mapRequired("Name", S.Name); |
| 600 | IO.mapRequired("StorageClass", S.StorageClass); |
| 601 | IO.mapOptional("AuxillaryData", S.AuxillaryData); // FIXME: typo |
| 602 | IO.mapRequired("ComplexType", S.ComplexType); |
| 603 | IO.mapRequired("Value", S.Value); |
| 604 | IO.mapRequired("SectionNumber", S.SectionNumber); |
| 605 | } |
| 606 | }; |
| 607 | |
| 608 | template <> |
| 609 | struct MappingTraits<COFFYAML::Header> { |
| 610 | static void mapping(IO &IO, COFFYAML::Header &H) { |
| 611 | IO.mapRequired("Machine", H.Machine); |
| 612 | } |
| 613 | }; |
| 614 | |
| 615 | template <> |
| 616 | struct MappingTraits<COFFYAML::Relocation> { |
| 617 | static void mapping(IO &IO, COFFYAML::Relocation &Rel) { |
| 618 | IO.mapRequired("Type", Rel.Type); |
| 619 | IO.mapRequired("VirtualAddress", Rel.VirtualAddress); |
| 620 | IO.mapRequired("SymbolTableIndex", Rel.SymbolTableIndex); |
| 621 | } |
| 622 | }; |
| 623 | |
| 624 | template <> |
| 625 | struct MappingTraits<COFFYAML::Section> { |
| 626 | static void mapping(IO &IO, COFFYAML::Section &Sec) { |
| 627 | IO.mapOptional("Relocations", Sec.Relocations); |
| 628 | IO.mapRequired("SectionData", Sec.SectionData); |
| 629 | IO.mapRequired("Characteristics", Sec.Characteristics); |
| 630 | IO.mapRequired("Name", Sec.Name); |
| 631 | } |
| 632 | }; |
| 633 | |
| 634 | template <> |
| 635 | struct MappingTraits<COFFYAML::Object> { |
| 636 | static void mapping(IO &IO, COFFYAML::Object &Obj) { |
| 637 | IO.mapRequired("sections", Obj.Sections); |
| 638 | IO.mapRequired("header", Obj.HeaderData); |
| 639 | IO.mapRequired("symbols", Obj.Symbols); |
| 640 | } |
| 641 | }; |
| 642 | } // end namespace yaml |
| 643 | } // end namespace llvm |
| 644 | |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 645 | int main(int argc, char **argv) { |
| 646 | cl::ParseCommandLineOptions(argc, argv); |
| 647 | sys::PrintStackTraceOnErrorSignal(); |
| 648 | PrettyStackTraceProgram X(argc, argv); |
| 649 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 650 | |
| 651 | OwningPtr<MemoryBuffer> Buf; |
| 652 | if (MemoryBuffer::getFileOrSTDIN(Input, Buf)) |
| 653 | return 1; |
| 654 | |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 655 | yaml::Input YIn(Buf->getBuffer()); |
| 656 | COFFYAML::Object Doc; |
| 657 | YIn >> Doc; |
| 658 | if (YIn.error()) { |
| 659 | errs() << "yaml2obj: Failed to parse YAML file!\n"; |
| 660 | return 1; |
| 661 | } |
| 662 | |
| 663 | COFFParser CP(Doc); |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 664 | if (!CP.parse()) { |
| 665 | errs() << "yaml2obj: Failed to parse YAML file!\n"; |
| 666 | return 1; |
| 667 | } |
Rafael Espindola | 8ec018c | 2013-04-02 23:56:40 +0000 | [diff] [blame^] | 668 | |
Michael J. Spencer | a915f24 | 2012-08-02 19:16:56 +0000 | [diff] [blame] | 669 | if (!layoutCOFF(CP)) { |
| 670 | errs() << "yaml2obj: Failed to layout COFF file!\n"; |
| 671 | return 1; |
| 672 | } |
| 673 | writeCOFF(CP, outs()); |
| 674 | } |