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