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