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