LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
MachO/Header.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_MACHO_HEADER_H
17#define LIEF_MACHO_HEADER_H
18
19#include <ostream>
20#include <vector>
21
22#include "LIEF/Object.hpp"
23#include "LIEF/visibility.h"
24#include "LIEF/enums.hpp"
25
26#include "LIEF/MachO/enums.hpp"
27
28namespace LIEF {
29namespace MachO {
30class BinaryParser;
31
32namespace details {
33struct mach_header_64;
34struct mach_header;
35}
36
38class LIEF_API Header : public Object {
39 friend class BinaryParser;
40 public:
41 Header() = default;
42
43 Header& operator=(const Header& copy) = default;
44 Header(const Header& copy) = default;
45
46 ~Header() override = default;
47
48 enum class FILE_TYPE : uint32_t {
50 OBJECT = 0x1u,
51 EXECUTE = 0x2u,
52 FVMLIB = 0x3u,
53 CORE = 0x4u,
54 PRELOAD = 0x5u,
55 DYLIB = 0x6u,
56 DYLINKER = 0x7u,
57 BUNDLE = 0x8u,
58 DYLIB_STUB = 0x9u,
59 DSYM = 0xAu,
61 FILESET = 0xCu,
63 GPU_DYLIB = 0xEu,
64 };
65
66 enum class FLAGS : uint32_t {
67 NOUNDEFS = 0x00000001u,
68 INCRLINK = 0x00000002u,
69 DYLDLINK = 0x00000004u,
70 BINDATLOAD = 0x00000008u,
71 PREBOUND = 0x00000010u,
72 SPLIT_SEGS = 0x00000020u,
73 LAZY_INIT = 0x00000040u,
74 TWOLEVEL = 0x00000080u,
75 FORCE_FLAT = 0x00000100u,
76 NOMULTIDEFS = 0x00000200u,
77 NOFIXPREBINDING = 0x00000400u,
78 PREBINDABLE = 0x00000800u,
79 ALLMODSBOUND = 0x00001000u,
81 CANONICAL = 0x00004000u,
82 WEAK_DEFINES = 0x00008000u,
83 BINDS_TO_WEAK = 0x00010000u,
84 ALLOW_STACK_EXECUTION = 0x00020000u,
85 ROOT_SAFE = 0x00040000u,
86 SETUID_SAFE = 0x00080000u,
87 NO_REEXPORTED_DYLIBS = 0x00100000u,
88 PIE = 0x00200000u,
89 DEAD_STRIPPABLE_DYLIB = 0x00400000u,
90 HAS_TLV_DESCRIPTORS = 0x00800000u,
91 NO_HEAP_EXECUTION = 0x01000000u,
92 APP_EXTENSION_SAFE = 0x02000000u,
94 SIM_SUPPORT = 0x08000000u,
95 IMPLICIT_PAGEZERO = 0x10000000u,
96 DYLIB_IN_CACHE = 0x80000000u,
97 };
98
99 static constexpr int ABI64 = 0x01000000;
100
101 enum class CPU_TYPE: int32_t {
102 ANY = -1,
103 X86 = 7,
105 MIPS = 8,
107 HPPA = 11,
108 ARM = 12,
109 ARM64 = 12 | ABI64,
111 SPARC = 14,
112 I860 = 15,
113 ALPHA = 16,
119 AIR64 = 23 | ABI64,
120 };
121
122 static constexpr uint32_t SUBTYPE_MASK = 0xff000000;
123 static constexpr uint32_t SUBTYPE_LIB64 = 0x80000000;
124
125 static constexpr auto CPU_SUBTYPE_ARM64_ARM64E = 2;
126
130 return magic_;
131 }
132
135 return cputype_;
136 }
137
141 uint32_t cpu_subtype() const {
142 return cpusubtype_;
143 }
144
147 return filetype_;
148 }
149
151 std::vector<FLAGS> flags_list() const;
152
154 bool has(FLAGS flag) const;
155
157 uint32_t nb_cmds() const {
158 return ncmds_;
159 }
160
162 uint32_t sizeof_cmds() const {
163 return sizeofcmds_;
164 }
165
169 uint32_t flags() const {
170 return flags_;
171 }
172
174 uint32_t reserved() const {
175 return reserved_;
176 }
177
178 void add(FLAGS flag);
179
181 magic_ = magic;
182 }
183 void cpu_type(CPU_TYPE type) {
184 cputype_ = type;
185 }
186
187 void cpu_subtype(uint32_t cpusubtype) {
188 cpusubtype_ = cpusubtype;
189 }
190
191 void file_type(FILE_TYPE filetype) {
192 filetype_ = filetype;
193 }
194
195 void nb_cmds(uint32_t ncmds) {
196 ncmds_ = ncmds;
197 }
198
199 void sizeof_cmds(uint32_t sizeofcmds) {
200 sizeofcmds_ = sizeofcmds;
201 }
202
203 void flags(uint32_t flags) {
204 flags_ = flags;
205 }
206
208 bool is_32bit() const {
209 return magic_ == MACHO_TYPES::MAGIC ||
210 magic_ == MACHO_TYPES::CIGAM;
211 }
212
214 bool is_64bit() const {
215 return magic_ == MACHO_TYPES::MAGIC_64 ||
216 magic_ == MACHO_TYPES::CIGAM_64;
217 }
218
219 void remove(FLAGS flag);
220
221 void reserved(uint32_t reserved) {
222 reserved_ = reserved;
223 }
224
226 add(c);
227 return *this;
228 }
230 remove(c);
231 return *this;
232 }
233
234 void accept(Visitor& visitor) const override;
235
236 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr);
237
238 private:
239 template<class T>
240 LIEF_LOCAL Header(const T& header);
241
243 CPU_TYPE cputype_ = CPU_TYPE::ANY;
244 uint32_t cpusubtype_;
245 FILE_TYPE filetype_ = FILE_TYPE::UNKNOWN;
246 uint32_t ncmds_ = 0;
247 uint32_t sizeofcmds_ = 0;
248 uint32_t flags_ = 0;
249 uint32_t reserved_ = 0;
250};
251
255
256}
257}
258
260
261#endif
Class used to parse a single binary (i.e. non-FAT).
Definition BinaryParser.hpp:78
uint32_t flags() const
Header flags (cf. HEADER_FLAGS).
Definition MachO/Header.hpp:169
uint32_t nb_cmds() const
Number of LoadCommand present in the Mach-O binary.
Definition MachO/Header.hpp:157
CPU_TYPE cpu_type() const
The CPU architecture targeted by this binary.
Definition MachO/Header.hpp:134
void file_type(FILE_TYPE filetype)
Definition MachO/Header.hpp:191
bool is_64bit() const
True if the binary is 64-bit.
Definition MachO/Header.hpp:214
FLAGS
Definition MachO/Header.hpp:66
@ ALLMODSBOUND
Definition MachO/Header.hpp:79
@ TWOLEVEL
Definition MachO/Header.hpp:74
@ PREBOUND
Definition MachO/Header.hpp:71
@ NO_HEAP_EXECUTION
Definition MachO/Header.hpp:91
@ BINDATLOAD
Definition MachO/Header.hpp:70
@ HAS_TLV_DESCRIPTORS
Definition MachO/Header.hpp:90
@ CANONICAL
Definition MachO/Header.hpp:81
@ ALLOW_STACK_EXECUTION
Definition MachO/Header.hpp:84
@ SPLIT_SEGS
Definition MachO/Header.hpp:72
@ APP_EXTENSION_SAFE
Definition MachO/Header.hpp:92
@ NOMULTIDEFS
Definition MachO/Header.hpp:76
@ WEAK_DEFINES
Definition MachO/Header.hpp:82
@ DEAD_STRIPPABLE_DYLIB
Definition MachO/Header.hpp:89
@ LAZY_INIT
Definition MachO/Header.hpp:73
@ PIE
Definition MachO/Header.hpp:88
@ INCRLINK
Definition MachO/Header.hpp:68
@ SIM_SUPPORT
Definition MachO/Header.hpp:94
@ PREBINDABLE
Definition MachO/Header.hpp:78
@ NOFIXPREBINDING
Definition MachO/Header.hpp:77
@ SETUID_SAFE
Definition MachO/Header.hpp:86
@ NLIST_OUTOFSYNC_WITH_DYLDINFO
Definition MachO/Header.hpp:93
@ FORCE_FLAT
Definition MachO/Header.hpp:75
@ SUBSECTIONS_VIA_SYMBOLS
Definition MachO/Header.hpp:80
@ DYLIB_IN_CACHE
Definition MachO/Header.hpp:96
@ NOUNDEFS
Definition MachO/Header.hpp:67
@ BINDS_TO_WEAK
Definition MachO/Header.hpp:83
@ NO_REEXPORTED_DYLIBS
Definition MachO/Header.hpp:87
@ ROOT_SAFE
Definition MachO/Header.hpp:85
@ DYLDLINK
Definition MachO/Header.hpp:69
@ IMPLICIT_PAGEZERO
Definition MachO/Header.hpp:95
void accept(Visitor &visitor) const override
std::vector< FLAGS > flags_list() const
Return the FLAGS as a list.
void reserved(uint32_t reserved)
Definition MachO/Header.hpp:221
void sizeof_cmds(uint32_t sizeofcmds)
Definition MachO/Header.hpp:199
uint32_t reserved() const
According to the official documentation, a reserved value.
Definition MachO/Header.hpp:174
friend std::ostream & operator<<(std::ostream &os, const Header &hdr)
void remove(FLAGS flag)
friend class BinaryParser
Definition MachO/Header.hpp:39
MACHO_TYPES magic() const
The Mach-O magic bytes. These bytes determine whether it is a 32 bits Mach-O, a 64 bits Mach-O files ...
Definition MachO/Header.hpp:129
void nb_cmds(uint32_t ncmds)
Definition MachO/Header.hpp:195
~Header() override=default
bool is_32bit() const
True if the binary is 32-bit.
Definition MachO/Header.hpp:208
FILE_TYPE file_type() const
Return the type of the Mach-O file (executable, object, shared library, ...).
Definition MachO/Header.hpp:146
Header(const Header &copy)=default
void cpu_type(CPU_TYPE type)
Definition MachO/Header.hpp:183
Header & operator=(const Header &copy)=default
FILE_TYPE
Definition MachO/Header.hpp:48
@ KEXT_BUNDLE
Definition MachO/Header.hpp:60
@ GPU_DYLIB
Definition MachO/Header.hpp:63
@ DYLIB
Definition MachO/Header.hpp:55
@ GPU_EXECUTE
Definition MachO/Header.hpp:62
@ FILESET
Definition MachO/Header.hpp:61
@ EXECUTE
Definition MachO/Header.hpp:51
@ DSYM
Definition MachO/Header.hpp:59
@ UNKNOWN
Definition MachO/Header.hpp:49
@ OBJECT
Definition MachO/Header.hpp:50
@ DYLIB_STUB
Definition MachO/Header.hpp:58
@ FVMLIB
Definition MachO/Header.hpp:52
@ CORE
Definition MachO/Header.hpp:53
@ PRELOAD
Definition MachO/Header.hpp:54
@ BUNDLE
Definition MachO/Header.hpp:57
@ DYLINKER
Definition MachO/Header.hpp:56
static constexpr auto CPU_SUBTYPE_ARM64_ARM64E
Definition MachO/Header.hpp:125
void add(FLAGS flag)
static constexpr uint32_t SUBTYPE_LIB64
Definition MachO/Header.hpp:123
CPU_TYPE
Definition MachO/Header.hpp:101
@ ALPHA
Definition MachO/Header.hpp:113
@ I860
Definition MachO/Header.hpp:112
@ MIPS
Definition MachO/Header.hpp:105
@ AMD_GPU
Definition MachO/Header.hpp:117
@ ARM
Definition MachO/Header.hpp:108
@ MC98000
Definition MachO/Header.hpp:106
@ X86_64
Definition MachO/Header.hpp:104
@ ANY
Definition MachO/Header.hpp:102
@ INTEL_GPU
Definition MachO/Header.hpp:118
@ SPARC
Definition MachO/Header.hpp:111
@ MC88000
Definition MachO/Header.hpp:110
@ ARM64
Definition MachO/Header.hpp:109
@ AIR64
Definition MachO/Header.hpp:119
@ POWERPC64
Definition MachO/Header.hpp:115
@ POWERPC
Definition MachO/Header.hpp:114
@ HPPA
Definition MachO/Header.hpp:107
@ APPLE_GPU
Definition MachO/Header.hpp:116
@ X86
Definition MachO/Header.hpp:103
bool has(FLAGS flag) const
Check if the given HEADER_FLAGS is present in the header's flags.
uint32_t cpu_subtype() const
Return the CPU subtype supported by the Mach-O binary. For ARM architectures, this value could repres...
Definition MachO/Header.hpp:141
void magic(MACHO_TYPES magic)
Definition MachO/Header.hpp:180
uint32_t sizeof_cmds() const
The size of all the LoadCommand.
Definition MachO/Header.hpp:162
Header & operator+=(FLAGS c)
Definition MachO/Header.hpp:225
void cpu_subtype(uint32_t cpusubtype)
Definition MachO/Header.hpp:187
static constexpr uint32_t SUBTYPE_MASK
Definition MachO/Header.hpp:122
static constexpr int ABI64
Definition MachO/Header.hpp:99
void flags(uint32_t flags)
Definition MachO/Header.hpp:203
Header & operator-=(FLAGS c)
Definition MachO/Header.hpp:229
Definition Visitor.hpp:210
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
Definition endianness_support.hpp:59
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
MACHO_TYPES
Definition MachO/enums.hpp:23
@ MAGIC_64
64-bit big-endian magic
Definition MachO/enums.hpp:27
@ CIGAM_64
64-bit little-endian magic
Definition MachO/enums.hpp:28
@ UNKNOWN
Definition MachO/enums.hpp:24
@ MAGIC
32-bit big-endian magic
Definition MachO/enums.hpp:25
@ CIGAM
32-bit little-endian magic
Definition MachO/enums.hpp:26
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF namespace.
Definition Abstract/Binary.hpp:40
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42