LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
MachO/Header.hpp
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
27#include "LIEF/Abstract/enums.hpp"
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 {
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: int {
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
113 return magic_;
114 }
115
117 CPU_TYPE cpu_type() const {
118 return cputype_;
119 }
120
124 uint32_t cpu_subtype() const {
125 return cpusubtype_;
126 }
127
129 FILE_TYPE file_type() const {
130 return filetype_;
131 }
132
134 std::vector<FLAGS> flags_list() const;
135
137 bool has(FLAGS flag) const;
138
140 uint32_t nb_cmds() const {
141 return ncmds_;
142 }
143
145 uint32_t sizeof_cmds() const {
146 return sizeofcmds_;
147 }
148
152 uint32_t flags() const {
153 return flags_;
154 }
155
157 uint32_t reserved() const {
158 return reserved_;
159 }
160
161 void add(FLAGS flag);
162
164 OBJECT_TYPES abstract_object_type() const;
165
166 std::pair<ARCHITECTURES, std::set<MODES>> abstract_architecture() const;
167
169 ENDIANNESS abstract_endianness() const;
170
171 void magic(MACHO_TYPES magic) {
172 magic_ = magic;
173 }
174 void cpu_type(CPU_TYPE type) {
175 cputype_ = type;
176 }
177
178 void cpu_subtype(uint32_t cpusubtype) {
179 cpusubtype_ = cpusubtype;
180 }
181
182 void file_type(FILE_TYPE filetype) {
183 filetype_ = filetype;
184 }
185
186 void nb_cmds(uint32_t ncmds) {
187 ncmds_ = ncmds;
188 }
189
190 void sizeof_cmds(uint32_t sizeofcmds) {
191 sizeofcmds_ = sizeofcmds;
192 }
193
194 void flags(uint32_t flags) {
195 flags_ = flags;
196 }
197
198 void remove(FLAGS flag);
199
200 void reserved(uint32_t reserved) {
201 reserved_ = reserved;
202 }
203
204 Header& operator+=(FLAGS c) {
205 add(c);
206 return *this;
207 }
208 Header& operator-=(FLAGS c) {
209 remove(c);
210 return *this;
211 }
212
213 void accept(Visitor& visitor) const override;
214
215 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& hdr);
216
217 private:
218 template<class T>
219 LIEF_LOCAL Header(const T& header);
220
221 MACHO_TYPES magic_ = MACHO_TYPES::UNKNOWN;
222 CPU_TYPE cputype_ = CPU_TYPE::ANY;
223 uint32_t cpusubtype_;
224 FILE_TYPE filetype_ = FILE_TYPE::UNKNOWN;
225 uint32_t ncmds_ = 0;
226 uint32_t sizeofcmds_ = 0;
227 uint32_t flags_ = 0;
228 uint32_t reserved_ = 0;
229};
230
231LIEF_API const char* to_string(Header::FILE_TYPE e);
232LIEF_API const char* to_string(Header::CPU_TYPE e);
233LIEF_API const char* to_string(Header::FLAGS e);
234
235}
236}
237
238ENABLE_BITMASK_OPERATORS(LIEF::MachO::Header::FLAGS)
239
240#endif
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:73
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:152
uint32_t nb_cmds() const
Number of LoadCommand present in the Mach-O binary.
Definition MachO/Header.hpp:140
CPU_TYPE cpu_type() const
The CPU architecture targeted by this binary.
Definition MachO/Header.hpp:117
std::vector< FLAGS > flags_list() const
Return the FLAGS as a list.
uint32_t reserved() const
According to the official documentation, a reserved value.
Definition MachO/Header.hpp:157
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:112
FILE_TYPE file_type() const
Return the type of the Mach-O file (executable, object, shared library, ...)
Definition MachO/Header.hpp:129
OBJECT_TYPES abstract_object_type() const
LIEF abstract object type.
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:124
uint32_t sizeof_cmds() const
The size of all the LoadCommand.
Definition MachO/Header.hpp:145
Definition Object.hpp:25
MACHO_TYPES
Definition MachO/enums.hpp:24
LIEF namespace.
Definition Abstract/Binary.hpp:32