LIEF: Library to Instrument Executable Formats Version 0.15.1
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 <set>
21#include <vector>
22
23#include "LIEF/Object.hpp"
24#include "LIEF/visibility.h"
25#include "LIEF/enums.hpp"
26
28
29#include "LIEF/MachO/enums.hpp"
30
31namespace LIEF {
32namespace MachO {
33class BinaryParser;
34
35namespace details {
36struct mach_header_64;
37struct mach_header;
38}
39
41class LIEF_API Header : public Object {
42 friend class BinaryParser;
43 public:
44 Header() = default;
45
46 Header& operator=(const Header& copy) = default;
47 Header(const Header& copy) = default;
48
49 ~Header() override = default;
50
51 enum class FILE_TYPE : uint32_t {
52 UNKNOWN = 0,
53 OBJECT = 0x1u,
54 EXECUTE = 0x2u,
55 FVMLIB = 0x3u,
56 CORE = 0x4u,
57 PRELOAD = 0x5u,
58 DYLIB = 0x6u,
59 DYLINKER = 0x7u,
60 BUNDLE = 0x8u,
61 DYLIB_STUB = 0x9u,
62 DSYM = 0xAu,
63 KEXT_BUNDLE = 0xBu
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,
80 SUBSECTIONS_VIA_SYMBOLS = 0x00002000u,
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
93 };
94
95 static constexpr int ABI64 = 0x01000000;
96
97 enum class CPU_TYPE: int32_t {
98 ANY = -1,
99 X86 = 7,
100 X86_64 = 7 | ABI64,
101 MIPS = 8,
102 MC98000 = 10,
103 ARM = 12,
104 ARM64 = 12 | ABI64,
105 SPARC = 14,
106 POWERPC = 18,
107 POWERPC64 = 18 | ABI64,
108 };
109
110 static constexpr uint32_t CPU_SUBTYPE_MASK = 0xff000000;
111 static constexpr uint32_t CPU_SUBTYPE_LIB64 = 0x80000000;
112
116 return magic_;
117 }
118
121 return cputype_;
122 }
123
127 uint32_t cpu_subtype() const {
128 return cpusubtype_;
129 }
130
133 return filetype_;
134 }
135
137 std::vector<FLAGS> flags_list() const;
138
140 bool has(FLAGS flag) const;
141
143 uint32_t nb_cmds() const {
144 return ncmds_;
145 }
146
148 uint32_t sizeof_cmds() const {
149 return sizeofcmds_;
150 }
151
155 uint32_t flags() const {
156 return flags_;
157 }
158
160 uint32_t reserved() const {
161 return reserved_;
162 }
163
164 void add(FLAGS flag);
165
168
169 std::pair<ARCHITECTURES, std::set<MODES>> abstract_architecture() const;
170
173
174 void magic(MACHO_TYPES magic) {
175 magic_ = magic;
176 }
177 void cpu_type(CPU_TYPE type) {
178 cputype_ = type;
179 }
180
181 void cpu_subtype(uint32_t cpusubtype) {
182 cpusubtype_ = cpusubtype;
183 }
184
185 void file_type(FILE_TYPE filetype) {
186 filetype_ = filetype;
187 }
188
189 void nb_cmds(uint32_t ncmds) {
190 ncmds_ = ncmds;
191 }
192
193 void sizeof_cmds(uint32_t sizeofcmds) {
194 sizeofcmds_ = sizeofcmds;
195 }
196
197 void flags(uint32_t flags) {
198 flags_ = flags;
199 }
200
201 void remove(FLAGS flag);
202
203 void reserved(uint32_t reserved) {
204 reserved_ = reserved;
205 }
206
208 add(c);
209 return *this;
210 }
212 remove(c);
213 return *this;
214 }
215
216 void accept(Visitor& visitor) const override;
217
218 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr);
219
220 private:
221 template<class T>
222 LIEF_LOCAL Header(const T& header);
223
224 MACHO_TYPES magic_ = MACHO_TYPES::UNKNOWN;
225 CPU_TYPE cputype_ = CPU_TYPE::ANY;
226 uint32_t cpusubtype_;
227 FILE_TYPE filetype_ = FILE_TYPE::UNKNOWN;
228 uint32_t ncmds_ = 0;
229 uint32_t sizeofcmds_ = 0;
230 uint32_t flags_ = 0;
231 uint32_t reserved_ = 0;
232};
233
237
238}
239}
240
242
243#endif
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:74
Class that represents the Mach-O header.
Definition MachO/Header.hpp:41
uint32_t flags() const
Header flags (cf. HEADER_FLAGS)
Definition MachO/Header.hpp:155
uint32_t nb_cmds() const
Number of LoadCommand present in the Mach-O binary.
Definition MachO/Header.hpp:143
CPU_TYPE cpu_type() const
The CPU architecture targeted by this binary.
Definition MachO/Header.hpp:120
void file_type(FILE_TYPE filetype)
Definition MachO/Header.hpp:185
FLAGS
Definition MachO/Header.hpp:66
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:203
void sizeof_cmds(uint32_t sizeofcmds)
Definition MachO/Header.hpp:193
uint32_t reserved() const
According to the official documentation, a reserved value.
Definition MachO/Header.hpp:160
friend std::ostream & operator<<(std::ostream &os, const Header &hdr)
void remove(FLAGS flag)
ENDIANNESS abstract_endianness() const
LIEF abstract endiannes.
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:115
void nb_cmds(uint32_t ncmds)
Definition MachO/Header.hpp:189
~Header() override=default
FILE_TYPE file_type() const
Return the type of the Mach-O file (executable, object, shared library, ...)
Definition MachO/Header.hpp:132
OBJECT_TYPES abstract_object_type() const
LIEF abstract object type.
Header(const Header &copy)=default
void cpu_type(CPU_TYPE type)
Definition MachO/Header.hpp:177
std::pair< ARCHITECTURES, std::set< MODES > > abstract_architecture() const
Header & operator=(const Header &copy)=default
FILE_TYPE
Definition MachO/Header.hpp:51
void add(FLAGS flag)
CPU_TYPE
Definition MachO/Header.hpp:97
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:127
void magic(MACHO_TYPES magic)
Definition MachO/Header.hpp:174
uint32_t sizeof_cmds() const
The size of all the LoadCommand.
Definition MachO/Header.hpp:148
Header & operator+=(FLAGS c)
Definition MachO/Header.hpp:207
void cpu_subtype(uint32_t cpusubtype)
Definition MachO/Header.hpp:181
void flags(uint32_t flags)
Definition MachO/Header.hpp:197
Header & operator-=(FLAGS c)
Definition MachO/Header.hpp:211
Definition Object.hpp:25
Definition Visitor.hpp:224
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
MACHO_TYPES
Definition MachO/enums.hpp:24
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF namespace.
Definition Abstract/Binary.hpp:32
OBJECT_TYPES
Definition Abstract/enums.hpp:21
ENDIANNESS
Definition Abstract/enums.hpp:63
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42