LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
LoadCommand.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_LOAD_COMMAND_H
17#define LIEF_MACHO_LOAD_COMMAND_H
18
19#include <memory>
20#include <vector>
21
22#include "LIEF/Object.hpp"
24#include "LIEF/visibility.h"
25#include "LIEF/span.hpp"
26
27namespace LIEF {
28namespace MachO {
29class Builder;
30class BinaryParser;
31class Binary;
32
33namespace details {
34struct load_command;
35}
36
38class LIEF_API LoadCommand : public Object {
39 friend class Builder;
40 friend class BinaryParser;
41 friend class Binary;
42
43 public:
44 using raw_t = std::vector<uint8_t>;
45
46 // clang-format off
47 enum class TYPE : uint64_t {
49 SEGMENT = 0x00000001u,
50 SYMTAB = 0x00000002u,
51 SYMSEG = 0x00000003u,
52 THREAD = 0x00000004u,
53 UNIXTHREAD = 0x00000005u,
54 LOADFVMLIB = 0x00000006u,
55 IDFVMLIB = 0x00000007u,
56 IDENT = 0x00000008u,
57 FVMFILE = 0x00000009u,
58 PREPAGE = 0x0000000Au,
59 DYSYMTAB = 0x0000000Bu,
60 LOAD_DYLIB = 0x0000000Cu,
61 ID_DYLIB = 0x0000000Du,
62 LOAD_DYLINKER = 0x0000000Eu,
63 ID_DYLINKER = 0x0000000Fu,
64 PREBOUND_DYLIB = 0x00000010u,
65 ROUTINES = 0x00000011u,
66 SUB_FRAMEWORK = 0x00000012u,
67 SUB_UMBRELLA = 0x00000013u,
68 SUB_CLIENT = 0x00000014u,
69 SUB_LIBRARY = 0x00000015u,
70 TWOLEVEL_HINTS = 0x00000016u,
71 PREBIND_CKSUM = 0x00000017u,
72 LOAD_WEAK_DYLIB = 0x80000018u,
73 SEGMENT_64 = 0x00000019u,
74 ROUTINES_64 = 0x0000001Au,
75 UUID = 0x0000001Bu,
76 RPATH = 0x8000001Cu,
77 CODE_SIGNATURE = 0x0000001Du,
78 SEGMENT_SPLIT_INFO = 0x0000001Eu,
79 REEXPORT_DYLIB = 0x8000001Fu,
80 LAZY_LOAD_DYLIB = 0x00000020u,
81 ENCRYPTION_INFO = 0x00000021u,
82 DYLD_INFO = 0x00000022u,
83 DYLD_INFO_ONLY = 0x80000022u,
84 LOAD_UPWARD_DYLIB = 0x80000023u,
85 VERSION_MIN_MACOSX = 0x00000024u,
86 VERSION_MIN_IPHONEOS = 0x00000025u,
87 FUNCTION_STARTS = 0x00000026u,
88 DYLD_ENVIRONMENT = 0x00000027u,
89 MAIN = 0x80000028u,
90 DATA_IN_CODE = 0x00000029u,
91 SOURCE_VERSION = 0x0000002Au,
92 DYLIB_CODE_SIGN_DRS = 0x0000002Bu,
93 ENCRYPTION_INFO_64 = 0x0000002Cu,
94 LINKER_OPTION = 0x0000002Du,
95 LINKER_OPTIMIZATION_HINT = 0x0000002Eu,
96 VERSION_MIN_TVOS = 0x0000002Fu,
97 VERSION_MIN_WATCHOS = 0x00000030u,
98 NOTE = 0x00000031u,
99 BUILD_VERSION = 0x00000032u,
100 DYLD_EXPORTS_TRIE = 0x80000033u,
101 DYLD_CHAINED_FIXUPS = 0x80000034u,
102 FILESET_ENTRY = 0x80000035u,
103 ATOM_INFO = 0x00000036u,
104 FUNCTION_VARIANTS = 0x00000037u,
105 FUNCTION_VARIANT_FIXUPS = 0x00000038u,
106 TARGET_TRIPLE = 0x00000039u,
107 LAZY_LOAD_DYLIB_INFO = 0x0000003Au,
108
109 LIEF_UNKNOWN = 0xffee0001u,
110 };
111 // clang-format on
112
113 public:
114 LoadCommand() = default;
115 LoadCommand(const details::load_command& command);
117 command_(type),
118 size_(size) {}
119
120 LoadCommand& operator=(const LoadCommand& copy) = default;
121 LoadCommand(const LoadCommand& copy) = default;
122
123 void swap(LoadCommand& other) noexcept;
124
125 virtual std::unique_ptr<LoadCommand> clone() const {
126 return std::unique_ptr<LoadCommand>(new LoadCommand(*this));
127 }
128
129 ~LoadCommand() override = default;
130
133 return command_;
134 }
135
137 uint32_t size() const {
138 return size_;
139 }
140
143 return original_data_;
144 }
145
147 uint64_t command_offset() const {
148 return command_offset_;
149 }
150
152 original_data_ = std::move(data);
153 }
154
156 command_ = command;
157 }
158
159 void size(uint32_t size) {
160 size_ = size;
161 }
162
163 void command_offset(uint64_t offset) {
164 command_offset_ = offset;
165 }
166
167 virtual std::ostream& print(std::ostream& os) const;
168
169 void accept(Visitor& visitor) const override;
170
171 static bool is_linkedit_data(const LoadCommand& cmd);
172
173 template<class T>
174 const T* cast() const {
175 static_assert(std::is_base_of<LoadCommand, T>::value,
176 "Require LoadCommand inheritance");
177 if (T::classof(this)) {
178 return static_cast<const T*>(this);
179 }
180 return nullptr;
181 }
182
183 template<class T>
184 T* cast() {
185 return const_cast<T*>(static_cast<const LoadCommand*>(this)->cast<T>());
186 }
187
188
189 LIEF_API friend std::ostream& operator<<(std::ostream& os,
190 const LoadCommand& cmd) {
191 return cmd.print(os);
192 }
193
194 protected:
195 raw_t original_data_;
197 uint32_t size_ = 0;
198 uint64_t command_offset_ = 0;
199};
200
202
203}
204}
205#endif
Class used to parse a single binary (i.e. non-FAT).
Definition BinaryParser.hpp:79
Class which represents a MachO binary.
Definition MachO/Binary.hpp:91
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:64
void command_offset(uint64_t offset)
Definition LoadCommand.hpp:163
void accept(Visitor &visitor) const override
virtual std::ostream & print(std::ostream &os) const
void data(raw_t data)
Definition LoadCommand.hpp:151
LoadCommand(const details::load_command &command)
friend std::ostream & operator<<(std::ostream &os, const LoadCommand &cmd)
Definition LoadCommand.hpp:189
uint32_t size() const
Size of the command (should be greater than sizeof(load_command)).
Definition LoadCommand.hpp:137
friend class BinaryParser
Definition LoadCommand.hpp:40
virtual std::unique_ptr< LoadCommand > clone() const
Definition LoadCommand.hpp:125
static bool is_linkedit_data(const LoadCommand &cmd)
void size(uint32_t size)
Definition LoadCommand.hpp:159
friend class Builder
Definition LoadCommand.hpp:39
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:132
span< const uint8_t > data() const
Raw command.
Definition LoadCommand.hpp:142
void command(LoadCommand::TYPE command)
Definition LoadCommand.hpp:155
void swap(LoadCommand &other) noexcept
friend class Binary
Definition LoadCommand.hpp:41
uint64_t command_offset() const
Offset of the command within the Load Command Table.
Definition LoadCommand.hpp:147
LoadCommand & operator=(const LoadCommand &copy)=default
~LoadCommand() override=default
const T * cast() const
Definition LoadCommand.hpp:174
TYPE
Definition LoadCommand.hpp:47
@ UNKNOWN
Definition LoadCommand.hpp:48
T * cast()
Definition LoadCommand.hpp:184
LoadCommand(const LoadCommand &copy)=default
LoadCommand(LoadCommand::TYPE type, uint32_t size)
Definition LoadCommand.hpp:116
std::vector< uint8_t > raw_t
Definition LoadCommand.hpp:44
Definition Visitor.hpp:212
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition endianness_support.hpp:60
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
@ UNKNOWN
Definition MachO/enums.hpp:24
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF namespace.
Definition Abstract/Binary.hpp:41
tcb::span< ElementType, Extent > span
Definition span.hpp:22
#define LIEF_API
Definition visibility.h:45