Use just a ArrayRef<uint8_t> to store both hex string and binary data.
[oota-llvm.git] / include / llvm / Object / COFFYAML.h
1 //===- COFFYAML.h - COFF YAMLIO implementation ------------------*- C++ -*-===//
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 file declares classes for handling the YAML representation of COFF.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_COFFYAML_H
15 #define LLVM_OBJECT_COFFYAML_H
16
17
18 #include "llvm/Support/COFF.h"
19 #include "llvm/Support/YAMLTraits.h"
20
21 namespace llvm {
22
23 namespace COFF {
24 inline Characteristics operator|(Characteristics a, Characteristics b) {
25   uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
26   return static_cast<Characteristics>(Ret);
27 }
28
29 inline SectionCharacteristics operator|(SectionCharacteristics a,
30                                         SectionCharacteristics b) {
31   uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
32   return static_cast<SectionCharacteristics>(Ret);
33 }
34 }
35
36 // The structure of the yaml files is not an exact 1:1 match to COFF. In order
37 // to use yaml::IO, we use these structures which are closer to the source.
38 namespace COFFYAML {
39   /// In an object file this is just a binary blob. In an yaml file it is an hex
40   /// string. Using this avoid having to allocate temporary strings.
41   /// FIXME: not COFF specific.
42   class BinaryRef {
43     ArrayRef<uint8_t> Data;
44     bool isBinary;
45   public:
46     BinaryRef(ArrayRef<uint8_t> Data) : Data(Data), isBinary(true) {}
47     BinaryRef(StringRef Data)
48         : Data(reinterpret_cast<const uint8_t *>(Data.data()), Data.size()),
49           isBinary(false) {}
50     BinaryRef() : isBinary(false) {}
51     StringRef getHex() const {
52       assert(!isBinary);
53       return StringRef(reinterpret_cast<const char*>(Data.data()), Data.size());
54     }
55     ArrayRef<uint8_t> getBinary() const {
56       assert(isBinary);
57       return Data;
58     }
59   };
60
61   struct Section {
62     COFF::section Header;
63     unsigned Alignment;
64     BinaryRef SectionData;
65     std::vector<COFF::relocation> Relocations;
66     StringRef Name;
67     Section();
68   };
69
70   struct Symbol {
71     COFF::symbol Header;
72     COFF::SymbolBaseType SimpleType;
73     COFF::SymbolComplexType ComplexType;
74     BinaryRef AuxiliaryData;
75     StringRef Name;
76     Symbol();
77   };
78
79   struct Object {
80     COFF::header Header;
81     std::vector<Section> Sections;
82     std::vector<Symbol> Symbols;
83     Object();
84   };
85 }
86 }
87
88 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section)
89 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)
90 LLVM_YAML_IS_SEQUENCE_VECTOR(COFF::relocation)
91
92 namespace llvm {
93 namespace yaml {
94
95 template<>
96 struct ScalarTraits<COFFYAML::BinaryRef> {
97   static void output(const COFFYAML::BinaryRef &, void*, llvm::raw_ostream &);
98   static StringRef input(StringRef, void*, COFFYAML::BinaryRef &);
99 };
100
101 template <>
102 struct ScalarEnumerationTraits<COFF::MachineTypes> {
103   static void enumeration(IO &IO, COFF::MachineTypes &Value);
104 };
105
106 template <>
107 struct ScalarEnumerationTraits<COFF::SymbolBaseType> {
108   static void enumeration(IO &IO, COFF::SymbolBaseType &Value);
109 };
110
111 template <>
112 struct ScalarEnumerationTraits<COFF::SymbolStorageClass> {
113   static void enumeration(IO &IO, COFF::SymbolStorageClass &Value);
114 };
115
116 template <>
117 struct ScalarEnumerationTraits<COFF::SymbolComplexType> {
118   static void enumeration(IO &IO, COFF::SymbolComplexType &Value);
119 };
120
121 template <>
122 struct ScalarEnumerationTraits<COFF::RelocationTypeX86> {
123   static void enumeration(IO &IO, COFF::RelocationTypeX86 &Value);
124 };
125
126 template <>
127 struct ScalarBitSetTraits<COFF::Characteristics> {
128   static void bitset(IO &IO, COFF::Characteristics &Value);
129 };
130
131 template <>
132 struct ScalarBitSetTraits<COFF::SectionCharacteristics> {
133   static void bitset(IO &IO, COFF::SectionCharacteristics &Value);
134 };
135
136 template <>
137 struct MappingTraits<COFF::relocation> {
138   static void mapping(IO &IO, COFF::relocation &Rel);
139 };
140
141 template <>
142 struct MappingTraits<COFF::header> {
143   static void mapping(IO &IO, COFF::header &H);
144 };
145
146 template <>
147 struct MappingTraits<COFFYAML::Symbol> {
148   static void mapping(IO &IO, COFFYAML::Symbol &S);
149 };
150
151 template <>
152 struct MappingTraits<COFFYAML::Section> {
153   static void mapping(IO &IO, COFFYAML::Section &Sec);
154 };
155
156 template <>
157 struct MappingTraits<COFFYAML::Object> {
158   static void mapping(IO &IO, COFFYAML::Object &Obj);
159 };
160
161 } // end namespace yaml
162 } // end namespace llvm
163
164 #endif