1 //===- MachOYAML.h - Mach-O YAMLIO implementation ---------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file declares classes for handling the YAML representation
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_OBJECTYAML_MACHOYAML_H
16 #define LLVM_OBJECTYAML_MACHOYAML_H
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/BinaryFormat/MachO.h"
20 #include "llvm/ObjectYAML/DWARFYAML.h"
21 #include "llvm/ObjectYAML/YAML.h"
22 #include "llvm/Support/YAMLTraits.h"
33 llvm::yaml::Hex64 addr
;
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 Optional
<llvm::yaml::BinaryRef
> content
;
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
;
58 virtual ~LoadCommand();
60 llvm::MachO::macho_load_command Data
;
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
;
70 llvm::yaml::Hex8 n_type
;
77 MachO::RebaseOpcode Opcode
;
79 std::vector
<yaml::Hex64
> ExtraData
;
83 MachO::BindOpcode Opcode
;
85 std::vector
<yaml::Hex64
> ULEBExtraData
;
86 std::vector
<int64_t> SLEBExtraData
;
91 uint64_t TerminalSize
= 0;
92 uint64_t NodeOffset
= 0;
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
;
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
;
106 MachOYAML::ExportEntry ExportTrie
;
107 std::vector
<NListEntry
> NameList
;
108 std::vector
<StringRef
> StringTable
;
110 bool isEmpty() const;
116 std::vector
<LoadCommand
> LoadCommands
;
117 std::vector
<Section
> Sections
;
118 LinkEditData LinkEdit
;
119 DWARFYAML::Data DWARF
;
123 llvm::yaml::Hex32 magic
;
128 llvm::yaml::Hex32 cputype
;
129 llvm::yaml::Hex32 cpusubtype
;
130 llvm::yaml::Hex64 offset
;
133 llvm::yaml::Hex32 reserved
;
136 struct UniversalBinary
{
138 std::vector
<FatArch
> FatArchs
;
139 std::vector
<Object
> Slices
;
142 } // end namespace MachOYAML
143 } // end namespace llvm
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
)
161 template <> struct MappingTraits
<MachOYAML::FileHeader
> {
162 static void mapping(IO
&IO
, MachOYAML::FileHeader
&FileHeader
);
165 template <> struct MappingTraits
<MachOYAML::Object
> {
166 static void mapping(IO
&IO
, MachOYAML::Object
&Object
);
169 template <> struct MappingTraits
<MachOYAML::FatHeader
> {
170 static void mapping(IO
&IO
, MachOYAML::FatHeader
&FatHeader
);
173 template <> struct MappingTraits
<MachOYAML::FatArch
> {
174 static void mapping(IO
&IO
, MachOYAML::FatArch
&FatArch
);
177 template <> struct MappingTraits
<MachOYAML::UniversalBinary
> {
178 static void mapping(IO
&IO
, MachOYAML::UniversalBinary
&UniversalBinary
);
181 template <> struct MappingTraits
<MachOYAML::LoadCommand
> {
182 static void mapping(IO
&IO
, MachOYAML::LoadCommand
&LoadCommand
);
185 template <> struct MappingTraits
<MachOYAML::LinkEditData
> {
186 static void mapping(IO
&IO
, MachOYAML::LinkEditData
&LinkEditData
);
189 template <> struct MappingTraits
<MachOYAML::RebaseOpcode
> {
190 static void mapping(IO
&IO
, MachOYAML::RebaseOpcode
&RebaseOpcode
);
193 template <> struct MappingTraits
<MachOYAML::BindOpcode
> {
194 static void mapping(IO
&IO
, MachOYAML::BindOpcode
&BindOpcode
);
197 template <> struct MappingTraits
<MachOYAML::ExportEntry
> {
198 static void mapping(IO
&IO
, MachOYAML::ExportEntry
&ExportEntry
);
201 template <> struct MappingTraits
<MachOYAML::Section
> {
202 static void mapping(IO
&IO
, MachOYAML::Section
&Section
);
203 static StringRef
validate(IO
&io
, MachOYAML::Section
&Section
);
206 template <> struct MappingTraits
<MachOYAML::NListEntry
> {
207 static void mapping(IO
&IO
, MachOYAML::NListEntry
&NListEntry
);
210 template <> struct MappingTraits
<MachO::build_tool_version
> {
211 static void mapping(IO
&IO
, MachO::build_tool_version
&tool
);
214 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
215 io.enumCase(value, #LCName, MachO::LCName);
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
);
224 #define ENUM_CASE(Enum) io.enumCase(value, #Enum, MachO::Enum);
226 template <> struct ScalarEnumerationTraits
<MachO::RebaseOpcode
> {
227 static void enumeration(IO
&io
, MachO::RebaseOpcode
&value
) {
228 ENUM_CASE(REBASE_OPCODE_DONE
)
229 ENUM_CASE(REBASE_OPCODE_SET_TYPE_IMM
)
230 ENUM_CASE(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
)
231 ENUM_CASE(REBASE_OPCODE_ADD_ADDR_ULEB
)
232 ENUM_CASE(REBASE_OPCODE_ADD_ADDR_IMM_SCALED
)
233 ENUM_CASE(REBASE_OPCODE_DO_REBASE_IMM_TIMES
)
234 ENUM_CASE(REBASE_OPCODE_DO_REBASE_ULEB_TIMES
)
235 ENUM_CASE(REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB
)
236 ENUM_CASE(REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB
)
237 io
.enumFallback
<Hex8
>(value
);
241 template <> struct ScalarEnumerationTraits
<MachO::BindOpcode
> {
242 static void enumeration(IO
&io
, MachO::BindOpcode
&value
) {
243 ENUM_CASE(BIND_OPCODE_DONE
)
244 ENUM_CASE(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM
)
245 ENUM_CASE(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB
)
246 ENUM_CASE(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM
)
247 ENUM_CASE(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
)
248 ENUM_CASE(BIND_OPCODE_SET_TYPE_IMM
)
249 ENUM_CASE(BIND_OPCODE_SET_ADDEND_SLEB
)
250 ENUM_CASE(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
)
251 ENUM_CASE(BIND_OPCODE_ADD_ADDR_ULEB
)
252 ENUM_CASE(BIND_OPCODE_DO_BIND
)
253 ENUM_CASE(BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB
)
254 ENUM_CASE(BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED
)
255 ENUM_CASE(BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB
)
256 io
.enumFallback
<Hex8
>(value
);
260 // This trait is used for 16-byte chars in Mach structures used for strings
261 using char_16
= char[16];
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
);
269 // This trait is used for UUIDs. It reads and writes them matching otool's
271 using uuid_t
= raw_ostream::uuid_t
;
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
);
279 // Load Command struct mapping traits
281 #define LOAD_COMMAND_STRUCT(LCStruct) \
282 template <> struct MappingTraits<MachO::LCStruct> { \
283 static void mapping(IO &IO, MachO::LCStruct &LoadCommand); \
286 #include "llvm/BinaryFormat/MachO.def"
288 // Extra structures used by load commands
289 template <> struct MappingTraits
<MachO::dylib
> {
290 static void mapping(IO
&IO
, MachO::dylib
&LoadCommand
);
293 template <> struct MappingTraits
<MachO::fvmlib
> {
294 static void mapping(IO
&IO
, MachO::fvmlib
&LoadCommand
);
297 template <> struct MappingTraits
<MachO::section
> {
298 static void mapping(IO
&IO
, MachO::section
&LoadCommand
);
301 template <> struct MappingTraits
<MachO::section_64
> {
302 static void mapping(IO
&IO
, MachO::section_64
&LoadCommand
);
305 } // end namespace yaml
307 } // end namespace llvm
309 #endif // LLVM_OBJECTYAML_MACHOYAML_H