LLVM  8.0.1
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 /// 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/ADT/StringRef.h"
23 #include <cstdint>
24 #include <string>
25 #include <vector>
26 
27 namespace llvm {
28 namespace MachOYAML {
29 
30 struct Section {
31  char sectname[16];
32  char segname[16];
33  llvm::yaml::Hex64 addr;
34  uint64_t size;
35  llvm::yaml::Hex32 offset;
37  llvm::yaml::Hex32 reloff;
39  llvm::yaml::Hex32 flags;
40  llvm::yaml::Hex32 reserved1;
41  llvm::yaml::Hex32 reserved2;
42  llvm::yaml::Hex32 reserved3;
43 };
44 
45 struct FileHeader {
46  llvm::yaml::Hex32 magic;
47  llvm::yaml::Hex32 cputype;
48  llvm::yaml::Hex32 cpusubtype;
49  llvm::yaml::Hex32 filetype;
52  llvm::yaml::Hex32 flags;
53  llvm::yaml::Hex32 reserved;
54 };
55 
56 struct LoadCommand {
57  virtual ~LoadCommand();
58 
60  std::vector<Section> Sections;
61  std::vector<MachO::build_tool_version> Tools;
62  std::vector<llvm::yaml::Hex8> PayloadBytes;
63  std::string PayloadString;
64  uint64_t ZeroPadBytes;
65 };
66 
67 struct NListEntry {
69  llvm::yaml::Hex8 n_type;
70  uint8_t n_sect;
71  uint16_t n_desc;
72  uint64_t n_value;
73 };
74 
75 struct RebaseOpcode {
77  uint8_t Imm;
78  std::vector<yaml::Hex64> ExtraData;
79 };
80 
81 struct BindOpcode {
83  uint8_t Imm;
84  std::vector<yaml::Hex64> ULEBExtraData;
85  std::vector<int64_t> SLEBExtraData;
87 };
88 
89 struct ExportEntry {
90  uint64_t TerminalSize = 0;
91  uint64_t NodeOffset = 0;
92  std::string Name;
93  llvm::yaml::Hex64 Flags = 0;
94  llvm::yaml::Hex64 Address = 0;
95  llvm::yaml::Hex64 Other = 0;
96  std::string ImportName;
97  std::vector<MachOYAML::ExportEntry> Children;
98 };
99 
100 struct LinkEditData {
101  std::vector<MachOYAML::RebaseOpcode> RebaseOpcodes;
102  std::vector<MachOYAML::BindOpcode> BindOpcodes;
103  std::vector<MachOYAML::BindOpcode> WeakBindOpcodes;
104  std::vector<MachOYAML::BindOpcode> LazyBindOpcodes;
106  std::vector<NListEntry> NameList;
107  std::vector<StringRef> StringTable;
108 
109  bool isEmpty() const;
110 };
111 
112 struct Object {
115  std::vector<LoadCommand> LoadCommands;
116  std::vector<Section> Sections;
119 };
120 
121 struct FatHeader {
122  llvm::yaml::Hex32 magic;
124 };
125 
126 struct FatArch {
127  llvm::yaml::Hex32 cputype;
128  llvm::yaml::Hex32 cpusubtype;
129  llvm::yaml::Hex64 offset;
130  uint64_t size;
132  llvm::yaml::Hex32 reserved;
133 };
134 
137  std::vector<FatArch> FatArchs;
138  std::vector<Object> Slices;
139 };
140 
141 } // end namespace MachOYAML
142 } // end namespace llvm
143 
144 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::LoadCommand)
145 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Section)
146 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::RebaseOpcode)
147 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::BindOpcode)
148 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::ExportEntry)
149 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::NListEntry)
150 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Object)
151 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::FatArch)
152 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachO::build_tool_version)
153 
154 namespace llvm {
155 
156 class raw_ostream;
157 
158 namespace yaml {
159 
160 template <> struct MappingTraits<MachOYAML::FileHeader> {
161  static void mapping(IO &IO, MachOYAML::FileHeader &FileHeader);
162 };
163 
164 template <> struct MappingTraits<MachOYAML::Object> {
165  static void mapping(IO &IO, MachOYAML::Object &Object);
166 };
167 
168 template <> struct MappingTraits<MachOYAML::FatHeader> {
169  static void mapping(IO &IO, MachOYAML::FatHeader &FatHeader);
170 };
171 
172 template <> struct MappingTraits<MachOYAML::FatArch> {
173  static void mapping(IO &IO, MachOYAML::FatArch &FatArch);
174 };
175 
176 template <> struct MappingTraits<MachOYAML::UniversalBinary> {
177  static void mapping(IO &IO, MachOYAML::UniversalBinary &UniversalBinary);
178 };
179 
180 template <> struct MappingTraits<MachOYAML::LoadCommand> {
181  static void mapping(IO &IO, MachOYAML::LoadCommand &LoadCommand);
182 };
183 
184 template <> struct MappingTraits<MachOYAML::LinkEditData> {
185  static void mapping(IO &IO, MachOYAML::LinkEditData &LinkEditData);
186 };
187 
188 template <> struct MappingTraits<MachOYAML::RebaseOpcode> {
189  static void mapping(IO &IO, MachOYAML::RebaseOpcode &RebaseOpcode);
190 };
191 
192 template <> struct MappingTraits<MachOYAML::BindOpcode> {
193  static void mapping(IO &IO, MachOYAML::BindOpcode &BindOpcode);
194 };
195 
196 template <> struct MappingTraits<MachOYAML::ExportEntry> {
197  static void mapping(IO &IO, MachOYAML::ExportEntry &ExportEntry);
198 };
199 
200 template <> struct MappingTraits<MachOYAML::Section> {
201  static void mapping(IO &IO, MachOYAML::Section &Section);
202 };
203 
204 template <> struct MappingTraits<MachOYAML::NListEntry> {
205  static void mapping(IO &IO, MachOYAML::NListEntry &NListEntry);
206 };
207 
208 template <> struct MappingTraits<MachO::build_tool_version> {
209  static void mapping(IO &IO, MachO::build_tool_version &tool);
210 };
211 
212 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
213  io.enumCase(value, #LCName, MachO::LCName);
214 
215 template <> struct ScalarEnumerationTraits<MachO::LoadCommandType> {
216  static void enumeration(IO &io, MachO::LoadCommandType &value) {
217 #include "llvm/BinaryFormat/MachO.def"
218  io.enumFallback<Hex32>(value);
219  }
220 };
221 
222 #define ENUM_CASE(Enum) io.enumCase(value, #Enum, MachO::Enum);
223 
224 template <> struct ScalarEnumerationTraits<MachO::RebaseOpcode> {
225  static void enumeration(IO &io, MachO::RebaseOpcode &value) {
235  io.enumFallback<Hex8>(value);
236  }
237 };
238 
239 template <> struct ScalarEnumerationTraits<MachO::BindOpcode> {
240  static void enumeration(IO &io, MachO::BindOpcode &value) {
254  io.enumFallback<Hex8>(value);
255  }
256 };
257 
258 // This trait is used for 16-byte chars in Mach structures used for strings
259 using char_16 = char[16];
260 
261 template <> struct ScalarTraits<char_16> {
262  static void output(const char_16 &Val, void *, raw_ostream &Out);
263  static StringRef input(StringRef Scalar, void *, char_16 &Val);
264  static QuotingType mustQuote(StringRef S);
265 };
266 
267 // This trait is used for UUIDs. It reads and writes them matching otool's
268 // formatting style.
270 
271 template <> struct ScalarTraits<uuid_t> {
272  static void output(const uuid_t &Val, void *, raw_ostream &Out);
273  static StringRef input(StringRef Scalar, void *, uuid_t &Val);
274  static QuotingType mustQuote(StringRef S);
275 };
276 
277 // Load Command struct mapping traits
278 
279 #define LOAD_COMMAND_STRUCT(LCStruct) \
280  template <> struct MappingTraits<MachO::LCStruct> { \
281  static void mapping(IO &IO, MachO::LCStruct &LoadCommand); \
282  };
283 
284 #include "llvm/BinaryFormat/MachO.def"
285 
286 // Extra structures used by load commands
287 template <> struct MappingTraits<MachO::dylib> {
288  static void mapping(IO &IO, MachO::dylib &LoadCommand);
289 };
290 
291 template <> struct MappingTraits<MachO::fvmlib> {
292  static void mapping(IO &IO, MachO::fvmlib &LoadCommand);
293 };
294 
295 template <> struct MappingTraits<MachO::section> {
296  static void mapping(IO &IO, MachO::section &LoadCommand);
297 };
298 
299 template <> struct MappingTraits<MachO::section_64> {
300  static void mapping(IO &IO, MachO::section_64 &LoadCommand);
301 };
302 
303 } // end namespace yaml
304 
305 } // end namespace llvm
306 
307 #endif // LLVM_OBJECTYAML_MACHOYAML_H
LoadCommandType
Definition: MachO.h:92
uint8_t[16] uuid_t
Output a formatted UUID with dash separators.
Definition: raw_ostream.h:219
This class represents lattice values for constants.
Definition: AllocatorList.h:24
llvm::yaml::Hex64 addr
Definition: MachOYAML.h:33
std::vector< Object > Slices
Definition: MachOYAML.h:138
static void enumeration(IO &io, MachO::RebaseOpcode &value)
Definition: MachOYAML.h:225
std::vector< MachOYAML::ExportEntry > Children
Definition: MachOYAML.h:97
std::vector< llvm::yaml::Hex8 > PayloadBytes
Definition: MachOYAML.h:62
llvm::yaml::Hex32 magic
Definition: MachOYAML.h:122
static void enumeration(IO &io, MachO::LoadCommandType &value)
Definition: MachOYAML.h:216
llvm::yaml::Hex64 offset
Definition: MachOYAML.h:129
std::vector< int64_t > SLEBExtraData
Definition: MachOYAML.h:85
char[16] char_16
Definition: MachOYAML.h:259
std::vector< Section > Sections
Definition: MachOYAML.h:116
DWARFYAML::Data DWARF
Definition: MachOYAML.h:118
std::vector< MachO::build_tool_version > Tools
Definition: MachOYAML.h:61
llvm::yaml::Hex32 cputype
Definition: MachOYAML.h:47
llvm::yaml::Hex32 flags
Definition: MachOYAML.h:52
std::vector< MachOYAML::RebaseOpcode > RebaseOpcodes
Definition: MachOYAML.h:101
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:784
llvm::yaml::Hex32 cputype
Definition: MachOYAML.h:127
llvm::yaml::Hex32 cpusubtype
Definition: MachOYAML.h:128
std::vector< yaml::Hex64 > ExtraData
Definition: MachOYAML.h:78
LinkEditData LinkEdit
Definition: MachOYAML.h:117
std::vector< LoadCommand > LoadCommands
Definition: MachOYAML.h:115
#define ENUM_CASE(Enum)
Definition: MachOYAML.h:222
std::vector< MachOYAML::BindOpcode > WeakBindOpcodes
Definition: MachOYAML.h:103
MachOYAML::ExportEntry ExportTrie
Definition: MachOYAML.h:105
llvm::yaml::Hex32 offset
Definition: MachOYAML.h:35
std::vector< yaml::Hex64 > ULEBExtraData
Definition: MachOYAML.h:84
RebaseOpcode
Definition: MachO.h:232
MachO::RebaseOpcode Opcode
Definition: MachOYAML.h:76
std::vector< MachOYAML::BindOpcode > BindOpcodes
Definition: MachOYAML.h:102
MachO::BindOpcode Opcode
Definition: MachOYAML.h:82
std::vector< FatArch > FatArchs
Definition: MachOYAML.h:137
llvm::yaml::Hex32 reserved
Definition: MachOYAML.h:53
llvm::yaml::Hex32 reserved3
Definition: MachOYAML.h:42
This file declares classes for handling the YAML representation of DWARF Debug Info.
llvm::yaml::Hex32 cpusubtype
Definition: MachOYAML.h:48
llvm::yaml::Hex32 reloff
Definition: MachOYAML.h:37
std::vector< NListEntry > NameList
Definition: MachOYAML.h:106
std::vector< Section > Sections
Definition: MachOYAML.h:60
llvm::yaml::Hex32 reserved2
Definition: MachOYAML.h:41
llvm::yaml::Hex8 n_type
Definition: MachOYAML.h:69
llvm::yaml::Hex32 flags
Definition: MachOYAML.h:39
llvm::yaml::Hex32 filetype
Definition: MachOYAML.h:49
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
std::vector< StringRef > StringTable
Definition: MachOYAML.h:107
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
llvm::MachO::macho_load_command Data
Definition: MachOYAML.h:59
llvm::yaml::Hex32 reserved
Definition: MachOYAML.h:132
std::vector< MachOYAML::BindOpcode > LazyBindOpcodes
Definition: MachOYAML.h:104
static void enumeration(IO &io, MachO::BindOpcode &value)
Definition: MachOYAML.h:240
llvm::yaml::Hex32 reserved1
Definition: MachOYAML.h:40
llvm::yaml::Hex32 magic
Definition: MachOYAML.h:46
raw_ostream::uuid_t uuid_t
Definition: MachOYAML.h:269