LLVM  4.0.0
MachOYAML.h
Go to the documentation of this file.
1 //===- MachOYAML.h - Mach-O 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 /// \file
11 /// \brief This file declares classes for handling the YAML representation
12 /// of Mach-O.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_OBJECTYAML_MACHOYAML_H
17 #define LLVM_OBJECTYAML_MACHOYAML_H
18 
19 #include "llvm/ObjectYAML/YAML.h"
21 #include "llvm/Support/MachO.h"
22 
23 namespace llvm {
24 namespace MachOYAML {
25 
26 struct Section {
27  char sectname[16];
28  char segname[16];
29  llvm::yaml::Hex64 addr;
30  uint64_t size;
31  llvm::yaml::Hex32 offset;
33  llvm::yaml::Hex32 reloff;
35  llvm::yaml::Hex32 flags;
36  llvm::yaml::Hex32 reserved1;
37  llvm::yaml::Hex32 reserved2;
38  llvm::yaml::Hex32 reserved3;
39 };
40 
41 struct FileHeader {
42  llvm::yaml::Hex32 magic;
43  llvm::yaml::Hex32 cputype;
44  llvm::yaml::Hex32 cpusubtype;
45  llvm::yaml::Hex32 filetype;
48  llvm::yaml::Hex32 flags;
49  llvm::yaml::Hex32 reserved;
50 };
51 
52 struct LoadCommand {
53  virtual ~LoadCommand();
55  std::vector<Section> Sections;
56  std::vector<llvm::yaml::Hex8> PayloadBytes;
57  std::string PayloadString;
58  uint64_t ZeroPadBytes;
59 };
60 
61 struct NListEntry {
63  llvm::yaml::Hex8 n_type;
64  uint8_t n_sect;
65  uint16_t n_desc;
66  uint64_t n_value;
67 };
68 struct RebaseOpcode {
70  uint8_t Imm;
71  std::vector<yaml::Hex64> ExtraData;
72 };
73 
74 struct BindOpcode {
76  uint8_t Imm;
77  std::vector<yaml::Hex64> ULEBExtraData;
78  std::vector<int64_t> SLEBExtraData;
80 };
81 
82 struct ExportEntry {
84  : TerminalSize(0), NodeOffset(0), Name(), Flags(0), Address(0), Other(0),
85  ImportName(), Children() {}
86  uint64_t TerminalSize;
87  uint64_t NodeOffset;
88  std::string Name;
89  llvm::yaml::Hex64 Flags;
90  llvm::yaml::Hex64 Address;
91  llvm::yaml::Hex64 Other;
92  std::string ImportName;
93  std::vector<MachOYAML::ExportEntry> Children;
94 };
95 
96 struct LinkEditData {
97  std::vector<MachOYAML::RebaseOpcode> RebaseOpcodes;
98  std::vector<MachOYAML::BindOpcode> BindOpcodes;
99  std::vector<MachOYAML::BindOpcode> WeakBindOpcodes;
100  std::vector<MachOYAML::BindOpcode> LazyBindOpcodes;
102  std::vector<NListEntry> NameList;
103  std::vector<StringRef> StringTable;
104 
105  bool isEmpty() const;
106 };
107 
108 struct Object {
111  std::vector<LoadCommand> LoadCommands;
112  std::vector<Section> Sections;
115 };
116 
117 struct FatHeader {
118  llvm::yaml::Hex32 magic;
120 };
121 
122 struct FatArch {
123  llvm::yaml::Hex32 cputype;
124  llvm::yaml::Hex32 cpusubtype;
125  llvm::yaml::Hex64 offset;
126  uint64_t size;
128  llvm::yaml::Hex32 reserved;
129 };
130 
133  std::vector<FatArch> FatArchs;
134  std::vector<Object> Slices;
135 };
136 
137 } // namespace llvm::MachOYAML
138 } // namespace llvm
139 
141 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Section)
145 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::ExportEntry)
146 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::NListEntry)
147 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Object)
148 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::FatArch)
149 
150 namespace llvm {
151 namespace yaml {
152 
153 template <> struct MappingTraits<MachOYAML::FileHeader> {
154  static void mapping(IO &IO, MachOYAML::FileHeader &FileHeader);
155 };
156 
157 template <> struct MappingTraits<MachOYAML::Object> {
158  static void mapping(IO &IO, MachOYAML::Object &Object);
159 };
160 
161 template <> struct MappingTraits<MachOYAML::FatHeader> {
162  static void mapping(IO &IO, MachOYAML::FatHeader &FatHeader);
163 };
164 
165 template <> struct MappingTraits<MachOYAML::FatArch> {
166  static void mapping(IO &IO, MachOYAML::FatArch &FatArch);
167 };
168 
169 template <> struct MappingTraits<MachOYAML::UniversalBinary> {
170  static void mapping(IO &IO, MachOYAML::UniversalBinary &UniversalBinary);
171 };
172 
173 template <> struct MappingTraits<MachOYAML::LoadCommand> {
174  static void mapping(IO &IO, MachOYAML::LoadCommand &LoadCommand);
175 };
176 
177 template <> struct MappingTraits<MachOYAML::LinkEditData> {
178  static void mapping(IO &IO, MachOYAML::LinkEditData &LinkEditData);
179 };
180 
181 template <> struct MappingTraits<MachOYAML::RebaseOpcode> {
182  static void mapping(IO &IO, MachOYAML::RebaseOpcode &RebaseOpcode);
183 };
184 
185 template <> struct MappingTraits<MachOYAML::BindOpcode> {
186  static void mapping(IO &IO, MachOYAML::BindOpcode &BindOpcode);
187 };
188 
189 template <> struct MappingTraits<MachOYAML::ExportEntry> {
190  static void mapping(IO &IO, MachOYAML::ExportEntry &ExportEntry);
191 };
192 
193 template <> struct MappingTraits<MachOYAML::Section> {
194  static void mapping(IO &IO, MachOYAML::Section &Section);
195 };
196 
197 template <> struct MappingTraits<MachOYAML::NListEntry> {
198  static void mapping(IO &IO, MachOYAML::NListEntry &NListEntry);
199 };
200 
201 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
202  io.enumCase(value, #LCName, MachO::LCName);
203 
204 template <> struct ScalarEnumerationTraits<MachO::LoadCommandType> {
205  static void enumeration(IO &io, MachO::LoadCommandType &value) {
206 #include "llvm/Support/MachO.def"
207  io.enumFallback<Hex32>(value);
208  }
209 };
210 
211 #define ENUM_CASE(Enum) io.enumCase(value, #Enum, MachO::Enum);
212 
213 template <> struct ScalarEnumerationTraits<MachO::RebaseOpcode> {
214  static void enumeration(IO &io, MachO::RebaseOpcode &value) {
224  io.enumFallback<Hex8>(value);
225  }
226 };
227 
228 template <> struct ScalarEnumerationTraits<MachO::BindOpcode> {
229  static void enumeration(IO &io, MachO::BindOpcode &value) {
243  io.enumFallback<Hex8>(value);
244  }
245 };
246 
247 // This trait is used for 16-byte chars in Mach structures used for strings
248 typedef char char_16[16];
249 
250 template <> struct ScalarTraits<char_16> {
251  static void output(const char_16 &Val, void *, llvm::raw_ostream &Out);
252 
253  static StringRef input(StringRef Scalar, void *, char_16 &Val);
254  static bool mustQuote(StringRef S);
255 };
256 
257 // This trait is used for UUIDs. It reads and writes them matching otool's
258 // formatting style.
259 typedef uint8_t uuid_t[16];
260 
261 template <> struct ScalarTraits<uuid_t> {
262  static void output(const uuid_t &Val, void *, llvm::raw_ostream &Out);
263 
264  static StringRef input(StringRef Scalar, void *, uuid_t &Val);
265  static bool mustQuote(StringRef S);
266 };
267 
268 // Load Command struct mapping traits
269 
270 #define LOAD_COMMAND_STRUCT(LCStruct) \
271  template <> struct MappingTraits<MachO::LCStruct> { \
272  static void mapping(IO &IO, MachO::LCStruct &LoadCommand); \
273  };
274 
275 #include "llvm/Support/MachO.def"
276 
277 // Extra structures used by load commands
278 template <> struct MappingTraits<MachO::dylib> {
279  static void mapping(IO &IO, MachO::dylib &LoadCommand);
280 };
281 
282 template <> struct MappingTraits<MachO::fvmlib> {
283  static void mapping(IO &IO, MachO::fvmlib &LoadCommand);
284 };
285 
286 template <> struct MappingTraits<MachO::section> {
287  static void mapping(IO &IO, MachO::section &LoadCommand);
288 };
289 
290 template <> struct MappingTraits<MachO::section_64> {
291  static void mapping(IO &IO, MachO::section_64 &LoadCommand);
292 };
293 
294 } // namespace llvm::yaml
295 
296 } // namespace llvm
297 
298 #endif
llvm::yaml::Hex64 addr
Definition: MachOYAML.h:29
std::vector< Object > Slices
Definition: MachOYAML.h:134
static void enumeration(IO &io, MachO::RebaseOpcode &value)
Definition: MachOYAML.h:214
std::vector< MachOYAML::ExportEntry > Children
Definition: MachOYAML.h:93
std::vector< llvm::yaml::Hex8 > PayloadBytes
Definition: MachOYAML.h:56
ExportEntry encapsulates the current-state-of-the-walk used when doing a non-recursive walk of the tr...
Definition: Object/MachO.h:57
llvm::yaml::Hex32 magic
Definition: MachOYAML.h:118
static void enumeration(IO &io, MachO::LoadCommandType &value)
Definition: MachOYAML.h:205
llvm::yaml::Hex64 offset
Definition: MachOYAML.h:125
std::vector< int64_t > SLEBExtraData
Definition: MachOYAML.h:78
std::vector< Section > Sections
Definition: MachOYAML.h:112
#define LLVM_YAML_IS_SEQUENCE_VECTOR(_type)
Utility for declaring that a std::vector of a particular type should be considered a YAML sequence...
Definition: YAMLTraits.h:1565
DWARFYAML::Data DWARF
Definition: MachOYAML.h:114
llvm::yaml::Hex32 cputype
Definition: MachOYAML.h:43
llvm::yaml::Hex32 flags
Definition: MachOYAML.h:48
std::vector< MachOYAML::RebaseOpcode > RebaseOpcodes
Definition: MachOYAML.h:97
void enumFallback(T &Val)
Definition: YAMLTraits.h:596
llvm::yaml::Hex32 cputype
Definition: MachOYAML.h:123
llvm::yaml::Hex32 cpusubtype
Definition: MachOYAML.h:124
This class should be specialized by any type that needs to be converted to/from a YAML mapping...
std::vector< yaml::Hex64 > ExtraData
Definition: MachOYAML.h:71
char char_16[16]
Definition: MachOYAML.h:248
LinkEditData LinkEdit
Definition: MachOYAML.h:113
std::vector< LoadCommand > LoadCommands
Definition: MachOYAML.h:111
#define ENUM_CASE(Enum)
Definition: MachOYAML.h:211
uint8_t uuid_t[16]
Definition: MachOYAML.h:259
llvm::yaml::Hex64 Address
Definition: MachOYAML.h:90
llvm::yaml::Hex64 Flags
Definition: MachOYAML.h:89
std::vector< MachOYAML::BindOpcode > WeakBindOpcodes
Definition: MachOYAML.h:99
MachOYAML::ExportEntry ExportTrie
Definition: MachOYAML.h:101
llvm::yaml::Hex32 offset
Definition: MachOYAML.h:31
std::vector< yaml::Hex64 > ULEBExtraData
Definition: MachOYAML.h:77
MachO::RebaseOpcode Opcode
Definition: MachOYAML.h:69
std::vector< MachOYAML::BindOpcode > BindOpcodes
Definition: MachOYAML.h:98
MachO::BindOpcode Opcode
Definition: MachOYAML.h:75
This class should be specialized by any integral type that converts to/from a YAML scalar where there...
Definition: YAMLTraits.h:97
std::vector< FatArch > FatArchs
Definition: MachOYAML.h:133
llvm::yaml::Hex32 reserved
Definition: MachOYAML.h:49
llvm::yaml::Hex64 Other
Definition: MachOYAML.h:91
This class should be specialized by type that requires custom conversion to/from a yaml scalar...
Definition: YAMLTraits.h:136
llvm::yaml::Hex32 reserved3
Definition: MachOYAML.h:38
This file declares classes for handling the YAML representation of DWARF Debug Info.
llvm::yaml::Hex32 cpusubtype
Definition: MachOYAML.h:44
llvm::yaml::Hex32 reloff
Definition: MachOYAML.h:33
std::vector< NListEntry > NameList
Definition: MachOYAML.h:102
std::vector< Section > Sections
Definition: MachOYAML.h:55
llvm::yaml::Hex32 reserved2
Definition: MachOYAML.h:37
llvm::yaml::Hex8 n_type
Definition: MachOYAML.h:63
llvm::yaml::Hex32 flags
Definition: MachOYAML.h:35
llvm::yaml::Hex32 filetype
Definition: MachOYAML.h:45
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
std::vector< StringRef > StringTable
Definition: MachOYAML.h:103
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
llvm::MachO::macho_load_command Data
Definition: MachOYAML.h:54
llvm::yaml::Hex32 reserved
Definition: MachOYAML.h:128
std::vector< MachOYAML::BindOpcode > LazyBindOpcodes
Definition: MachOYAML.h:100
static void enumeration(IO &io, MachO::BindOpcode &value)
Definition: MachOYAML.h:229
llvm::yaml::Hex32 reserved1
Definition: MachOYAML.h:36
llvm::yaml::Hex32 magic
Definition: MachOYAML.h:42