LIEF: Library to Instrument Executable Formats Version 0.16.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 ARM = 12,
101 ARM64 = 12 | ABI64,
102 SPARC = 14,
103 POWERPC = 18,
104 POWERPC64 = 18 | ABI64,
105 };
106
107 static constexpr uint32_t CPU_SUBTYPE_MASK = 0xff000000;
108 static constexpr uint32_t CPU_SUBTYPE_LIB64 = 0x80000000;
109
110 static constexpr auto CPU_SUBTYPE_ARM64_ARM64E = 2;
111 MACHO_TYPES magic() const {
115 return magic_;
116 }
117 CPU_TYPE cpu_type() const {
120 return cputype_;
121 }
122 uint32_t cpu_subtype() const {
127 return cpusubtype_;
128 }
129 FILE_TYPE file_type() const {
132 return filetype_;
133 }
134 std::vector<FLAGS> flags_list() const;
137 bool has(FLAGS flag) const;
140 uint32_t nb_cmds() const {
143 return ncmds_;
144 }
145 uint32_t sizeof_cmds() const {
148 return sizeofcmds_;
149 }
150 uint32_t flags() const {
155 return flags_;
156 }
157 uint32_t reserved() const {
160 return reserved_;
161 }
162
163 void add(FLAGS flag);
164
165 void magic(MACHO_TYPES magic) {
166 magic_ = magic;
167 }
168 void cpu_type(CPU_TYPE type) {
169 cputype_ = type;
170 }
171
172 void cpu_subtype(uint32_t cpusubtype) {
173 cpusubtype_ = cpusubtype;
174 }
175
176 void file_type(FILE_TYPE filetype) {
177 filetype_ = filetype;
178 }
179
180 void nb_cmds(uint32_t ncmds) {
181 ncmds_ = ncmds;
182 }
183
184 void sizeof_cmds(uint32_t sizeofcmds) {
185 sizeofcmds_ = sizeofcmds;
186 }
187
188 void flags(uint32_t flags) {
189 flags_ = flags;
190 }
191
192 void remove(FLAGS flag);
193
194 void reserved(uint32_t reserved) {
195 reserved_ = reserved;
196 }
197
198 Header& operator+=(FLAGS c) {
199 add(c);
200 return *this;
201 }
202 Header& operator-=(FLAGS c) {
203 remove(c);
204 return *this;
205 }
206
207 void accept(Visitor& visitor) const override;
208
209 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr);
210
211 private:
212 template<class T>
213 LIEF_LOCAL Header(const T& header);
214
215 MACHO_TYPES magic_ = MACHO_TYPES::UNKNOWN;
216 CPU_TYPE cputype_ = CPU_TYPE::ANY;
217 uint32_t cpusubtype_;
218 FILE_TYPE filetype_ = FILE_TYPE::UNKNOWN;
219 uint32_t ncmds_ = 0;
220 uint32_t sizeofcmds_ = 0;
221 uint32_t flags_ = 0;
222 uint32_t reserved_ = 0;
223};
224
225LIEF_API const char* to_string(Header::FILE_TYPE e);
226LIEF_API const char* to_string(Header::CPU_TYPE e);
227LIEF_API const char* to_string(Header::FLAGS e);
228
229}
230}
231
232ENABLE_BITMASK_OPERATORS(LIEF::MachO::Header::FLAGS)
233
234#endif
enums.hpp
Object.hpp
LIEF::MachO::BinaryParser
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:74
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:154
LIEF::MachO::Header::nb_cmds
uint32_t nb_cmds() const
Number of LoadCommand present in the Mach-O binary.
Definition MachO/Header.hpp:142
LIEF::MachO::Header::cpu_type
CPU_TYPE cpu_type() const
The CPU architecture targeted by this binary.
Definition MachO/Header.hpp:119
LIEF::MachO::Header::file_type
void file_type(FILE_TYPE filetype)
Definition MachO/Header.hpp:176
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:194
LIEF::MachO::Header::sizeof_cmds
void sizeof_cmds(uint32_t sizeofcmds)
Definition MachO/Header.hpp:184
LIEF::MachO::Header::reserved
uint32_t reserved() const
According to the official documentation, a reserved value.
Definition MachO/Header.hpp:159
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:114
LIEF::MachO::Header::nb_cmds
void nb_cmds(uint32_t ncmds)
Definition MachO/Header.hpp:180
LIEF::MachO::Header::~Header
~Header() override=default
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:131
LIEF::MachO::Header::Header
Header(const Header &copy)=default
LIEF::MachO::Header::cpu_type
void cpu_type(CPU_TYPE type)
Definition MachO/Header.hpp:168
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:126
LIEF::MachO::Header::magic
void magic(MACHO_TYPES magic)
Definition MachO/Header.hpp:165
LIEF::MachO::Header::sizeof_cmds
uint32_t sizeof_cmds() const
The size of all the LoadCommand.
Definition MachO/Header.hpp:147
LIEF::MachO::Header::operator+=
Header & operator+=(FLAGS c)
Definition MachO/Header.hpp:198
LIEF::MachO::Header::cpu_subtype
void cpu_subtype(uint32_t cpusubtype)
Definition MachO/Header.hpp:172
LIEF::MachO::Header::flags
void flags(uint32_t flags)
Definition MachO/Header.hpp:188
LIEF::MachO::Header::operator-=
Header & operator-=(FLAGS c)
Definition MachO/Header.hpp:202
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