LLVM 20.0.0git
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"
23#include <cstdint>
24#include <optional>
25#include <string>
26#include <vector>
27
28namespace llvm {
29namespace MachOYAML {
30
31struct Relocation {
32 // Offset in the section to what is being relocated.
33 llvm::yaml::Hex32 address;
34 // Symbol index if r_extern == 1 else section index.
37 // Real length = 2 ^ length.
42 int32_t value;
43};
44
45struct Section {
46 char sectname[16];
47 char segname[16];
48 llvm::yaml::Hex64 addr;
50 llvm::yaml::Hex32 offset;
52 llvm::yaml::Hex32 reloff;
54 llvm::yaml::Hex32 flags;
55 llvm::yaml::Hex32 reserved1;
56 llvm::yaml::Hex32 reserved2;
57 llvm::yaml::Hex32 reserved3;
58 std::optional<llvm::yaml::BinaryRef> content;
59 std::vector<Relocation> relocations;
60};
61
62struct FileHeader {
63 llvm::yaml::Hex32 magic;
64 llvm::yaml::Hex32 cputype;
65 llvm::yaml::Hex32 cpusubtype;
66 llvm::yaml::Hex32 filetype;
69 llvm::yaml::Hex32 flags;
70 llvm::yaml::Hex32 reserved;
71};
72
74 virtual ~LoadCommand();
75
77 std::vector<Section> Sections;
78 std::vector<MachO::build_tool_version> Tools;
79 std::vector<llvm::yaml::Hex8> PayloadBytes;
80 std::string Content;
82};
83
84struct NListEntry {
86 llvm::yaml::Hex8 n_type;
90};
91
95 std::vector<yaml::Hex64> ExtraData;
96};
97
98struct BindOpcode {
101 std::vector<yaml::Hex64> ULEBExtraData;
102 std::vector<int64_t> SLEBExtraData;
104};
105
109 std::string Name;
110 llvm::yaml::Hex64 Flags = 0;
111 llvm::yaml::Hex64 Address = 0;
112 llvm::yaml::Hex64 Other = 0;
113 std::string ImportName;
114 std::vector<MachOYAML::ExportEntry> Children;
115};
116
118 llvm::yaml::Hex32 Offset;
120 llvm::yaml::Hex16 Kind;
121};
122
124 std::vector<MachOYAML::RebaseOpcode> RebaseOpcodes;
125 std::vector<MachOYAML::BindOpcode> BindOpcodes;
126 std::vector<MachOYAML::BindOpcode> WeakBindOpcodes;
127 std::vector<MachOYAML::BindOpcode> LazyBindOpcodes;
129 std::vector<NListEntry> NameList;
130 std::vector<StringRef> StringTable;
131 std::vector<yaml::Hex32> IndirectSymbols;
132 std::vector<yaml::Hex64> FunctionStarts;
133 std::vector<DataInCodeEntry> DataInCode;
134 std::vector<yaml::Hex8> ChainedFixups;
135
136 bool isEmpty() const;
137};
138
139struct Object {
142 std::vector<LoadCommand> LoadCommands;
143 std::vector<Section> Sections;
145 std::optional<llvm::yaml::BinaryRef> RawLinkEditSegment;
147};
148
149struct FatHeader {
150 llvm::yaml::Hex32 magic;
152};
153
154struct FatArch {
155 llvm::yaml::Hex32 cputype;
156 llvm::yaml::Hex32 cpusubtype;
157 llvm::yaml::Hex64 offset;
160 llvm::yaml::Hex32 reserved;
161};
162
165 std::vector<FatArch> FatArchs;
166 std::vector<Object> Slices;
167};
168
169} // end namespace MachOYAML
170} // end namespace llvm
171
183
184namespace llvm {
185
186class raw_ostream;
187
188namespace yaml {
189
190template <> struct MappingTraits<MachOYAML::FileHeader> {
191 static void mapping(IO &IO, MachOYAML::FileHeader &FileHeader);
192};
193
194template <> struct MappingTraits<MachOYAML::Object> {
195 static void mapping(IO &IO, MachOYAML::Object &Object);
196};
197
198template <> struct MappingTraits<MachOYAML::FatHeader> {
199 static void mapping(IO &IO, MachOYAML::FatHeader &FatHeader);
200};
201
202template <> struct MappingTraits<MachOYAML::FatArch> {
203 static void mapping(IO &IO, MachOYAML::FatArch &FatArch);
204};
205
206template <> struct MappingTraits<MachOYAML::UniversalBinary> {
207 static void mapping(IO &IO, MachOYAML::UniversalBinary &UniversalBinary);
208};
209
210template <> struct MappingTraits<MachOYAML::LoadCommand> {
211 static void mapping(IO &IO, MachOYAML::LoadCommand &LoadCommand);
212};
213
214template <> struct MappingTraits<MachOYAML::LinkEditData> {
215 static void mapping(IO &IO, MachOYAML::LinkEditData &LinkEditData);
216};
217
218template <> struct MappingTraits<MachOYAML::RebaseOpcode> {
219 static void mapping(IO &IO, MachOYAML::RebaseOpcode &RebaseOpcode);
220};
221
222template <> struct MappingTraits<MachOYAML::BindOpcode> {
223 static void mapping(IO &IO, MachOYAML::BindOpcode &BindOpcode);
224};
225
226template <> struct MappingTraits<MachOYAML::ExportEntry> {
227 static void mapping(IO &IO, MachOYAML::ExportEntry &ExportEntry);
228};
229
230template <> struct MappingTraits<MachOYAML::Relocation> {
231 static void mapping(IO &IO, MachOYAML::Relocation &R);
232};
233
234template <> struct MappingTraits<MachOYAML::Section> {
235 static void mapping(IO &IO, MachOYAML::Section &Section);
236 static std::string validate(IO &io, MachOYAML::Section &Section);
237};
238
239template <> struct MappingTraits<MachOYAML::NListEntry> {
240 static void mapping(IO &IO, MachOYAML::NListEntry &NListEntry);
241};
242
243template <> struct MappingTraits<MachO::build_tool_version> {
244 static void mapping(IO &IO, MachO::build_tool_version &tool);
245};
246
247template <> struct MappingTraits<MachOYAML::DataInCodeEntry> {
248 static void mapping(IO &IO, MachOYAML::DataInCodeEntry &DataInCodeEntry);
249};
250
251#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
252 io.enumCase(value, #LCName, MachO::LCName);
253
254template <> struct ScalarEnumerationTraits<MachO::LoadCommandType> {
256#include "llvm/BinaryFormat/MachO.def"
257 io.enumFallback<Hex32>(value);
258 }
259};
260
261#define ENUM_CASE(Enum) io.enumCase(value, #Enum, MachO::Enum);
262
263template <> struct ScalarEnumerationTraits<MachO::RebaseOpcode> {
264 static void enumeration(IO &io, MachO::RebaseOpcode &value) {
265 ENUM_CASE(REBASE_OPCODE_DONE)
266 ENUM_CASE(REBASE_OPCODE_SET_TYPE_IMM)
267 ENUM_CASE(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB)
268 ENUM_CASE(REBASE_OPCODE_ADD_ADDR_ULEB)
269 ENUM_CASE(REBASE_OPCODE_ADD_ADDR_IMM_SCALED)
270 ENUM_CASE(REBASE_OPCODE_DO_REBASE_IMM_TIMES)
271 ENUM_CASE(REBASE_OPCODE_DO_REBASE_ULEB_TIMES)
272 ENUM_CASE(REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB)
273 ENUM_CASE(REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB)
274 io.enumFallback<Hex8>(value);
275 }
276};
277
278template <> struct ScalarEnumerationTraits<MachO::BindOpcode> {
279 static void enumeration(IO &io, MachO::BindOpcode &value) {
280 ENUM_CASE(BIND_OPCODE_DONE)
281 ENUM_CASE(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM)
282 ENUM_CASE(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB)
283 ENUM_CASE(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM)
284 ENUM_CASE(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM)
285 ENUM_CASE(BIND_OPCODE_SET_TYPE_IMM)
286 ENUM_CASE(BIND_OPCODE_SET_ADDEND_SLEB)
287 ENUM_CASE(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB)
288 ENUM_CASE(BIND_OPCODE_ADD_ADDR_ULEB)
289 ENUM_CASE(BIND_OPCODE_DO_BIND)
290 ENUM_CASE(BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB)
291 ENUM_CASE(BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED)
292 ENUM_CASE(BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB)
293 io.enumFallback<Hex8>(value);
294 }
295};
296
297// This trait is used for 16-byte chars in Mach structures used for strings
298using char_16 = char[16];
299
300template <> struct ScalarTraits<char_16> {
301 static void output(const char_16 &Val, void *, raw_ostream &Out);
302 static StringRef input(StringRef Scalar, void *, char_16 &Val);
303 static QuotingType mustQuote(StringRef S);
304};
305
306// This trait is used for UUIDs. It reads and writes them matching otool's
307// formatting style.
309
310template <> struct ScalarTraits<uuid_t> {
311 static void output(const uuid_t &Val, void *, raw_ostream &Out);
312 static StringRef input(StringRef Scalar, void *, uuid_t &Val);
313 static QuotingType mustQuote(StringRef S);
314};
315
316// Load Command struct mapping traits
317
318#define LOAD_COMMAND_STRUCT(LCStruct) \
319 template <> struct MappingTraits<MachO::LCStruct> { \
320 static void mapping(IO &IO, MachO::LCStruct &LoadCommand); \
321 };
322
323#include "llvm/BinaryFormat/MachO.def"
324
325// Extra structures used by load commands
326template <> struct MappingTraits<MachO::dylib> {
327 static void mapping(IO &IO, MachO::dylib &LoadCommand);
328};
329
330template <> struct MappingTraits<MachO::fvmlib> {
331 static void mapping(IO &IO, MachO::fvmlib &LoadCommand);
332};
333
334template <> struct MappingTraits<MachO::section> {
335 static void mapping(IO &IO, MachO::section &LoadCommand);
336};
337
338template <> struct MappingTraits<MachO::section_64> {
339 static void mapping(IO &IO, MachO::section_64 &LoadCommand);
340};
341
342} // end namespace yaml
343
344} // end namespace llvm
345
346#endif // LLVM_OBJECTYAML_MACHOYAML_H
This file declares classes for handling the YAML representation of DWARF Debug Info.
Given that RA is a live value
#define ENUM_CASE(Enum)
Definition: MachOYAML.h:261
#define LLVM_YAML_IS_SEQUENCE_VECTOR(type)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
uint8_t[16] uuid_t
Output a formatted UUID with dash separators.
Definition: raw_ostream.h:296
RebaseOpcode
Definition: MachO.h:242
LoadCommandType
Definition: MachO.h:98
raw_ostream::uuid_t uuid_t
Definition: MachOYAML.h:308
char[16] char_16
Definition: MachOYAML.h:298
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachO::BindOpcode Opcode
Definition: MachOYAML.h:99
std::vector< int64_t > SLEBExtraData
Definition: MachOYAML.h:102
std::vector< yaml::Hex64 > ULEBExtraData
Definition: MachOYAML.h:101
std::vector< MachOYAML::ExportEntry > Children
Definition: MachOYAML.h:114
llvm::yaml::Hex64 Address
Definition: MachOYAML.h:111
llvm::yaml::Hex64 Other
Definition: MachOYAML.h:112
llvm::yaml::Hex64 Flags
Definition: MachOYAML.h:110
llvm::yaml::Hex64 offset
Definition: MachOYAML.h:157
llvm::yaml::Hex32 cputype
Definition: MachOYAML.h:155
llvm::yaml::Hex32 cpusubtype
Definition: MachOYAML.h:156
llvm::yaml::Hex32 reserved
Definition: MachOYAML.h:160
llvm::yaml::Hex32 magic
Definition: MachOYAML.h:150
llvm::yaml::Hex32 filetype
Definition: MachOYAML.h:66
llvm::yaml::Hex32 cpusubtype
Definition: MachOYAML.h:65
llvm::yaml::Hex32 cputype
Definition: MachOYAML.h:64
llvm::yaml::Hex32 flags
Definition: MachOYAML.h:69
llvm::yaml::Hex32 reserved
Definition: MachOYAML.h:70
llvm::yaml::Hex32 magic
Definition: MachOYAML.h:63
std::vector< MachOYAML::BindOpcode > WeakBindOpcodes
Definition: MachOYAML.h:126
std::vector< DataInCodeEntry > DataInCode
Definition: MachOYAML.h:133
std::vector< yaml::Hex8 > ChainedFixups
Definition: MachOYAML.h:134
std::vector< MachOYAML::BindOpcode > LazyBindOpcodes
Definition: MachOYAML.h:127
std::vector< MachOYAML::BindOpcode > BindOpcodes
Definition: MachOYAML.h:125
std::vector< MachOYAML::RebaseOpcode > RebaseOpcodes
Definition: MachOYAML.h:124
std::vector< StringRef > StringTable
Definition: MachOYAML.h:130
std::vector< yaml::Hex32 > IndirectSymbols
Definition: MachOYAML.h:131
std::vector< yaml::Hex64 > FunctionStarts
Definition: MachOYAML.h:132
MachOYAML::ExportEntry ExportTrie
Definition: MachOYAML.h:128
std::vector< NListEntry > NameList
Definition: MachOYAML.h:129
llvm::MachO::macho_load_command Data
Definition: MachOYAML.h:76
std::vector< Section > Sections
Definition: MachOYAML.h:77
std::vector< MachO::build_tool_version > Tools
Definition: MachOYAML.h:78
std::vector< llvm::yaml::Hex8 > PayloadBytes
Definition: MachOYAML.h:79
llvm::yaml::Hex8 n_type
Definition: MachOYAML.h:86
std::vector< LoadCommand > LoadCommands
Definition: MachOYAML.h:142
DWARFYAML::Data DWARF
Definition: MachOYAML.h:146
std::optional< llvm::yaml::BinaryRef > RawLinkEditSegment
Definition: MachOYAML.h:145
LinkEditData LinkEdit
Definition: MachOYAML.h:144
std::vector< Section > Sections
Definition: MachOYAML.h:143
std::vector< yaml::Hex64 > ExtraData
Definition: MachOYAML.h:95
MachO::RebaseOpcode Opcode
Definition: MachOYAML.h:93
llvm::yaml::Hex32 address
Definition: MachOYAML.h:33
std::vector< Relocation > relocations
Definition: MachOYAML.h:59
std::optional< llvm::yaml::BinaryRef > content
Definition: MachOYAML.h:58
llvm::yaml::Hex32 reserved2
Definition: MachOYAML.h:56
llvm::yaml::Hex64 addr
Definition: MachOYAML.h:48
llvm::yaml::Hex32 reserved1
Definition: MachOYAML.h:55
llvm::yaml::Hex32 reloff
Definition: MachOYAML.h:52
llvm::yaml::Hex32 offset
Definition: MachOYAML.h:50
llvm::yaml::Hex32 reserved3
Definition: MachOYAML.h:57
llvm::yaml::Hex32 flags
Definition: MachOYAML.h:54
std::vector< Object > Slices
Definition: MachOYAML.h:166
std::vector< FatArch > FatArchs
Definition: MachOYAML.h:165
static void enumeration(IO &io, MachO::BindOpcode &value)
Definition: MachOYAML.h:279
static void enumeration(IO &io, MachO::LoadCommandType &value)
Definition: MachOYAML.h:255
static void enumeration(IO &io, MachO::RebaseOpcode &value)
Definition: MachOYAML.h:264