LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
MachO/Header.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 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
41 public:
42 Header() = default;
43
44 Header& operator=(const Header& copy) = default;
45 Header(const Header& copy) = default;
46
47 ~Header() override = default;
48
49 // clang-format off
50 enum class FILE_TYPE : uint32_t {
52 OBJECT = 0x1u,
53 EXECUTE = 0x2u,
54 FVMLIB = 0x3u,
55 CORE = 0x4u,
56 PRELOAD = 0x5u,
57 DYLIB = 0x6u,
58 DYLINKER = 0x7u,
59 BUNDLE = 0x8u,
60 DYLIB_STUB = 0x9u,
61 DSYM = 0xAu,
62 KEXT_BUNDLE = 0xBu,
63 FILESET = 0xCu,
64 GPU_EXECUTE = 0xDu,
65 GPU_DYLIB = 0xEu,
66 };
67 // clang-format on
68
69 // clang-format off
70 enum class FLAGS : uint32_t {
71 NOUNDEFS = 0x00000001u,
72 INCRLINK = 0x00000002u,
73 DYLDLINK = 0x00000004u,
74 BINDATLOAD = 0x00000008u,
75 PREBOUND = 0x00000010u,
76 SPLIT_SEGS = 0x00000020u,
77 LAZY_INIT = 0x00000040u,
78 TWOLEVEL = 0x00000080u,
79 FORCE_FLAT = 0x00000100u,
80 NOMULTIDEFS = 0x00000200u,
81 NOFIXPREBINDING = 0x00000400u,
82 PREBINDABLE = 0x00000800u,
83 ALLMODSBOUND = 0x00001000u,
84 SUBSECTIONS_VIA_SYMBOLS = 0x00002000u,
85 CANONICAL = 0x00004000u,
86 WEAK_DEFINES = 0x00008000u,
87 BINDS_TO_WEAK = 0x00010000u,
88 ALLOW_STACK_EXECUTION = 0x00020000u,
89 ROOT_SAFE = 0x00040000u,
90 SETUID_SAFE = 0x00080000u,
91 NO_REEXPORTED_DYLIBS = 0x00100000u,
92 PIE = 0x00200000u,
93 DEAD_STRIPPABLE_DYLIB = 0x00400000u,
94 HAS_TLV_DESCRIPTORS = 0x00800000u,
95 NO_HEAP_EXECUTION = 0x01000000u,
96 APP_EXTENSION_SAFE = 0x02000000u,
97 NLIST_OUTOFSYNC_WITH_DYLDINFO = 0x04000000u,
98 SIM_SUPPORT = 0x08000000u,
99 IMPLICIT_PAGEZERO = 0x10000000u,
100 DYLIB_IN_CACHE = 0x80000000u,
101 };
102 // clang-format on
103
104 static constexpr int ABI64 = 0x01000000;
105
106 // clang-format off
107 enum class CPU_TYPE : int32_t {
108 ANY = -1,
109 X86 = 7,
110 X86_64 = 7 | ABI64,
111 MIPS = 8,
112 MC98000 = 10,
113 HPPA = 11,
114 ARM = 12,
115 ARM64 = 12 | ABI64,
116 MC88000 = 13,
117 SPARC = 14,
118 I860 = 15,
119 ALPHA = 16,
120 POWERPC = 18,
121 POWERPC64 = 18 | ABI64,
122 APPLE_GPU = 19 | ABI64,
123 AMD_GPU = 20 | ABI64,
124 INTEL_GPU = 21 | ABI64,
125 AIR64 = 23 | ABI64,
126 };
127 // clang-format on
128
129 static constexpr uint32_t SUBTYPE_MASK = 0xff000000;
130 static constexpr uint32_t SUBTYPE_LIB64 = 0x80000000;
131
132 static constexpr auto CPU_SUBTYPE_ARM64_ARM64E = 2;
133
137 return magic_;
138 }
139
142 return cputype_;
143 }
144
148 uint32_t cpu_subtype() const {
149 return cpusubtype_;
150 }
151
154 return filetype_;
155 }
156
158 std::vector<FLAGS> flags_list() const;
159
161 bool has(FLAGS flag) const;
162
164 uint32_t nb_cmds() const {
165 return ncmds_;
166 }
167
169 uint32_t sizeof_cmds() const {
170 return sizeofcmds_;
171 }
172
176 uint32_t flags() const {
177 return flags_;
178 }
179
181 uint32_t reserved() const {
182 return reserved_;
183 }
184
185 void add(FLAGS flag);
186
188 magic_ = magic;
189 }
190 void cpu_type(CPU_TYPE type) {
191 cputype_ = type;
192 }
193
194 void cpu_subtype(uint32_t cpusubtype) {
195 cpusubtype_ = cpusubtype;
196 }
197
198 void file_type(FILE_TYPE filetype) {
199 filetype_ = filetype;
200 }
201
202 void nb_cmds(uint32_t ncmds) {
203 ncmds_ = ncmds;
204 }
205
206 void sizeof_cmds(uint32_t sizeofcmds) {
207 sizeofcmds_ = sizeofcmds;
208 }
209
210 void flags(uint32_t flags) {
211 flags_ = flags;
212 }
213
215 bool is_32bit() const {
216 return magic_ == MACHO_TYPES::MAGIC || magic_ == MACHO_TYPES::CIGAM;
217 }
218
220 bool is_64bit() const {
221 return magic_ == MACHO_TYPES::MAGIC_64 || magic_ == MACHO_TYPES::CIGAM_64;
222 }
223
224 void remove(FLAGS flag);
225
226 void reserved(uint32_t reserved) {
227 reserved_ = reserved;
228 }
229
231 add(c);
232 return *this;
233 }
235 remove(c);
236 return *this;
237 }
238
239 void accept(Visitor& visitor) const override;
240
241 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr);
242
243 private:
244 template<class T>
245 LIEF_LOCAL Header(const T& header);
246
248 CPU_TYPE cputype_ = CPU_TYPE::ANY;
249 uint32_t cpusubtype_;
250 FILE_TYPE filetype_ = FILE_TYPE::UNKNOWN;
251 uint32_t ncmds_ = 0;
252 uint32_t sizeofcmds_ = 0;
253 uint32_t flags_ = 0;
254 uint32_t reserved_ = 0;
255};
256
260
261}
262}
263
265
266#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:176
uint32_t nb_cmds() const
Number of LoadCommand present in the Mach-O binary.
Definition MachO/Header.hpp:164
CPU_TYPE cpu_type() const
The CPU architecture targeted by this binary.
Definition MachO/Header.hpp:141
void file_type(FILE_TYPE filetype)
Definition MachO/Header.hpp:198
bool is_64bit() const
True if the binary is 64-bit.
Definition MachO/Header.hpp:220
FLAGS
Definition MachO/Header.hpp:70
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:226
void sizeof_cmds(uint32_t sizeofcmds)
Definition MachO/Header.hpp:206
uint32_t reserved() const
According to the official documentation, a reserved value.
Definition MachO/Header.hpp:181
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:136
void nb_cmds(uint32_t ncmds)
Definition MachO/Header.hpp:202
~Header() override=default
bool is_32bit() const
True if the binary is 32-bit.
Definition MachO/Header.hpp:215
FILE_TYPE file_type() const
Return the type of the Mach-O file (executable, object, shared library, ...).
Definition MachO/Header.hpp:153
Header(const Header &copy)=default
void cpu_type(CPU_TYPE type)
Definition MachO/Header.hpp:190
Header & operator=(const Header &copy)=default
FILE_TYPE
Definition MachO/Header.hpp:50
@ UNKNOWN
Definition MachO/Header.hpp:51
static constexpr auto CPU_SUBTYPE_ARM64_ARM64E
Definition MachO/Header.hpp:132
void add(FLAGS flag)
static constexpr uint32_t SUBTYPE_LIB64
Definition MachO/Header.hpp:130
CPU_TYPE
Definition MachO/Header.hpp:107
@ ANY
Definition MachO/Header.hpp:108
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:148
void magic(MACHO_TYPES magic)
Definition MachO/Header.hpp:187
uint32_t sizeof_cmds() const
The size of all the LoadCommand.
Definition MachO/Header.hpp:169
Header & operator+=(FLAGS c)
Definition MachO/Header.hpp:230
void cpu_subtype(uint32_t cpusubtype)
Definition MachO/Header.hpp:194
static constexpr uint32_t SUBTYPE_MASK
Definition MachO/Header.hpp:129
static constexpr int ABI64
Definition MachO/Header.hpp:104
void flags(uint32_t flags)
Definition MachO/Header.hpp:210
Header & operator-=(FLAGS c)
Definition MachO/Header.hpp:234
Definition Visitor.hpp:212
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
Definition endianness_support.hpp:60
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:43
#define LIEF_LOCAL
Definition visibility.h:44