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