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 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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}
36class 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,
60 KEXT_BUNDLE = 0xBu
61 };
62
63 enum class FLAGS : uint32_t {
64 NOUNDEFS = 0x00000001u,
65 INCRLINK = 0x00000002u,
66 DYLDLINK = 0x00000004u,
67 BINDATLOAD = 0x00000008u,
68 PREBOUND = 0x00000010u,
69 SPLIT_SEGS = 0x00000020u,
70 LAZY_INIT = 0x00000040u,
71 TWOLEVEL = 0x00000080u,
72 FORCE_FLAT = 0x00000100u,
73 NOMULTIDEFS = 0x00000200u,
74 NOFIXPREBINDING = 0x00000400u,
75 PREBINDABLE = 0x00000800u,
76 ALLMODSBOUND = 0x00001000u,
77 SUBSECTIONS_VIA_SYMBOLS = 0x00002000u,
78 CANONICAL = 0x00004000u,
79 WEAK_DEFINES = 0x00008000u,
80 BINDS_TO_WEAK = 0x00010000u,
81 ALLOW_STACK_EXECUTION = 0x00020000u,
82 ROOT_SAFE = 0x00040000u,
83 SETUID_SAFE = 0x00080000u,
84 NO_REEXPORTED_DYLIBS = 0x00100000u,
85 PIE = 0x00200000u,
86 DEAD_STRIPPABLE_DYLIB = 0x00400000u,
87 HAS_TLV_DESCRIPTORS = 0x00800000u,
88 NO_HEAP_EXECUTION = 0x01000000u,
89 APP_EXTENSION_SAFE = 0x02000000u
90 };
91
92 static constexpr int ABI64 = 0x01000000;
93
94 enum class CPU_TYPE: int32_t {
95 ANY = -1,
96 X86 = 7,
97 X86_64 = 7 | ABI64,
98 MIPS = 8,
99 MC98000 = 10,
100 HPPA = 11,
101 ARM = 12,
102 ARM64 = 12 | ABI64,
103 MC88000 = 13,
104 SPARC = 14,
105 I860 = 15,
106 ALPHA = 16,
107 POWERPC = 18,
108 POWERPC64 = 18 | ABI64,
109 };
110
111 static constexpr uint32_t CPU_SUBTYPE_MASK = 0xff000000;
112 static constexpr uint32_t CPU_SUBTYPE_LIB64 = 0x80000000;
113
114 static constexpr auto CPU_SUBTYPE_ARM64_ARM64E = 2;
115 MACHO_TYPES magic() const {
119 return magic_;
120 }
121 CPU_TYPE cpu_type() const {
124 return cputype_;
125 }
126 uint32_t cpu_subtype() const {
131 return cpusubtype_;
132 }
133 FILE_TYPE file_type() const {
136 return filetype_;
137 }
138 std::vector<FLAGS> flags_list() const;
141 bool has(FLAGS flag) const;
144 uint32_t nb_cmds() const {
147 return ncmds_;
148 }
149 uint32_t sizeof_cmds() const {
152 return sizeofcmds_;
153 }
154 uint32_t flags() const {
159 return flags_;
160 }
161 uint32_t reserved() const {
164 return reserved_;
165 }
166
167 void add(FLAGS flag);
168
169 void magic(MACHO_TYPES magic) {
170 magic_ = magic;
171 }
172 void cpu_type(CPU_TYPE type) {
173 cputype_ = type;
174 }
175
176 void cpu_subtype(uint32_t cpusubtype) {
177 cpusubtype_ = cpusubtype;
178 }
179
180 void file_type(FILE_TYPE filetype) {
181 filetype_ = filetype;
182 }
183
184 void nb_cmds(uint32_t ncmds) {
185 ncmds_ = ncmds;
186 }
187
188 void sizeof_cmds(uint32_t sizeofcmds) {
189 sizeofcmds_ = sizeofcmds;
190 }
191
192 void flags(uint32_t flags) {
193 flags_ = flags;
194 }
195 bool is_32bit() const {
198 return magic_ == MACHO_TYPES::MH_MAGIC ||
199 magic_ == MACHO_TYPES::MH_CIGAM;
200 }
201 bool is_64bit() const {
204 return magic_ == MACHO_TYPES::MH_MAGIC_64 ||
205 magic_ == MACHO_TYPES::MH_CIGAM_64;
206 }
207
208 void remove(FLAGS flag);
209
210 void reserved(uint32_t reserved) {
211 reserved_ = reserved;
212 }
213
214 Header& operator+=(FLAGS c) {
215 add(c);
216 return *this;
217 }
218 Header& operator-=(FLAGS c) {
219 remove(c);
220 return *this;
221 }
222
223 void accept(Visitor& visitor) const override;
224
225 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr);
226
227 private:
228 template<class T>
229 LIEF_LOCAL Header(const T& header);
230
231 MACHO_TYPES magic_ = MACHO_TYPES::UNKNOWN;
232 CPU_TYPE cputype_ = CPU_TYPE::ANY;
233 uint32_t cpusubtype_;
234 FILE_TYPE filetype_ = FILE_TYPE::UNKNOWN;
235 uint32_t ncmds_ = 0;
236 uint32_t sizeofcmds_ = 0;
237 uint32_t flags_ = 0;
238 uint32_t reserved_ = 0;
239};
240
241LIEF_API const char* to_string(Header::FILE_TYPE e);
242LIEF_API const char* to_string(Header::CPU_TYPE e);
243LIEF_API const char* to_string(Header::FLAGS e);
244
245}
246}
247
248ENABLE_BITMASK_OPERATORS(LIEF::MachO::Header::FLAGS)
249
250#endif
enums.hpp
Object.hpp
LIEF::MachO::BinaryParser
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:75
LIEF::MachO::Header
Class that represents the Mach-O header.
Definition MachO/Header.hpp:38
LIEF::MachO::Header::flags
uint32_t flags() const
Header flags (cf. HEADER_FLAGS)
Definition MachO/Header.hpp:158
LIEF::MachO::Header::nb_cmds
uint32_t nb_cmds() const
Number of LoadCommand present in the Mach-O binary.
Definition MachO/Header.hpp:146
LIEF::MachO::Header::cpu_type
CPU_TYPE cpu_type() const
The CPU architecture targeted by this binary.
Definition MachO/Header.hpp:123
LIEF::MachO::Header::file_type
void file_type(FILE_TYPE filetype)
Definition MachO/Header.hpp:180
LIEF::MachO::Header::is_64bit
bool is_64bit() const
True if the binary is 64-bit.
Definition MachO/Header.hpp:203
LIEF::MachO::Header::FLAGS
FLAGS
Definition MachO/Header.hpp:63
LIEF::MachO::Header::accept
void accept(Visitor &visitor) const override
LIEF::MachO::Header::flags_list
std::vector< FLAGS > flags_list() const
Return the FLAGS as a list.
LIEF::MachO::Header::reserved
void reserved(uint32_t reserved)
Definition MachO/Header.hpp:210
LIEF::MachO::Header::sizeof_cmds
void sizeof_cmds(uint32_t sizeofcmds)
Definition MachO/Header.hpp:188
LIEF::MachO::Header::reserved
uint32_t reserved() const
According to the official documentation, a reserved value.
Definition MachO/Header.hpp:163
LIEF::MachO::Header::operator<<
friend std::ostream & operator<<(std::ostream &os, const Header &hdr)
LIEF::MachO::Header::remove
void remove(FLAGS flag)
LIEF::MachO::Header::Header
Header()=default
LIEF::MachO::Header::magic
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:118
LIEF::MachO::Header::nb_cmds
void nb_cmds(uint32_t ncmds)
Definition MachO/Header.hpp:184
LIEF::MachO::Header::~Header
~Header() override=default
LIEF::MachO::Header::is_32bit
bool is_32bit() const
True if the binary is 32-bit.
Definition MachO/Header.hpp:197
LIEF::MachO::Header::file_type
FILE_TYPE file_type() const
Return the type of the Mach-O file (executable, object, shared library, ...)
Definition MachO/Header.hpp:135
LIEF::MachO::Header::Header
Header(const Header &copy)=default
LIEF::MachO::Header::cpu_type
void cpu_type(CPU_TYPE type)
Definition MachO/Header.hpp:172
LIEF::MachO::Header::operator=
Header & operator=(const Header &copy)=default
LIEF::MachO::Header::FILE_TYPE
FILE_TYPE
Definition MachO/Header.hpp:48
LIEF::MachO::Header::add
void add(FLAGS flag)
LIEF::MachO::Header::CPU_TYPE
CPU_TYPE
Definition MachO/Header.hpp:94
LIEF::MachO::Header::has
bool has(FLAGS flag) const
Check if the given HEADER_FLAGS is present in the header's flags.
LIEF::MachO::Header::cpu_subtype
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:130
LIEF::MachO::Header::magic
void magic(MACHO_TYPES magic)
Definition MachO/Header.hpp:169
LIEF::MachO::Header::sizeof_cmds
uint32_t sizeof_cmds() const
The size of all the LoadCommand.
Definition MachO/Header.hpp:151
LIEF::MachO::Header::operator+=
Header & operator+=(FLAGS c)
Definition MachO/Header.hpp:214
LIEF::MachO::Header::cpu_subtype
void cpu_subtype(uint32_t cpusubtype)
Definition MachO/Header.hpp:176
LIEF::MachO::Header::flags
void flags(uint32_t flags)
Definition MachO/Header.hpp:192
LIEF::MachO::Header::operator-=
Header & operator-=(FLAGS c)
Definition MachO/Header.hpp:218
enums.hpp
ENABLE_BITMASK_OPERATORS
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
LIEF::MachO::details
Definition endianness_support.hpp:59
LIEF::MachO
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF::MachO::MACHO_TYPES
MACHO_TYPES
Definition MachO/enums.hpp:24
LIEF::MachO::MACHO_TYPES::UNKNOWN
@ UNKNOWN
Definition MachO/enums.hpp:25
LIEF::MachO::to_string
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42