LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
DylibCommand.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_DYLIB_COMMAND_H
17#define LIEF_MACHO_DYLIB_COMMAND_H
18#include <string_view>
19#include <array>
20#include <memory>
21#include <ostream>
22#include <string>
23
24#include "LIEF/visibility.h"
25
27
28
29namespace LIEF::MachO {
30
31namespace details {
32struct dylib_command;
33}
34
37 public:
38 using version_t = std::array<uint16_t, 3>;
39
40 public:
42 static version_t int2version(uint32_t version) {
43 return {{
44 uint16_t(version >> 16),
45 uint16_t((version >> 8) & 0xFF),
46 uint16_t(version & 0xFF),
47 }};
48 }
49
51 static uint32_t version2int(version_t version) {
52 return (version[2]) | (version[1] << 8) | (version[0] << 16);
53 }
54
56 static DylibCommand weak_dylib(std::string_view name, uint32_t timestamp = 0,
57 uint32_t current_version = 0,
58 uint32_t compat_version = 0);
59
61 static DylibCommand id_dylib(std::string_view name, uint32_t timestamp = 0,
62 uint32_t current_version = 0,
63 uint32_t compat_version = 0);
64
66 static DylibCommand load_dylib(std::string_view name, uint32_t timestamp = 2,
67 uint32_t current_version = 0,
68 uint32_t compat_version = 0);
69
71 static DylibCommand reexport_dylib(std::string_view name, uint32_t timestamp = 0,
72 uint32_t current_version = 0,
73 uint32_t compat_version = 0);
74
76 static DylibCommand load_upward_dylib(std::string_view name,
77 uint32_t timestamp = 0,
78 uint32_t current_version = 0,
79 uint32_t compat_version = 0);
80
82 static DylibCommand lazy_load_dylib(std::string_view name,
83 uint32_t timestamp = 0,
84 uint32_t current_version = 0,
85 uint32_t compat_version = 0);
86
87 public:
88 DylibCommand() = default;
89 DylibCommand(const details::dylib_command& cmd);
90
91 DylibCommand& operator=(const DylibCommand& copy) = default;
92 DylibCommand(const DylibCommand& copy) = default;
93
94 ~DylibCommand() override = default;
95
96 std::unique_ptr<LoadCommand> clone() const override {
97 return std::make_unique<DylibCommand>(*this);
98 }
99
101 std::string_view name() const LIEF_LIFETIMEBOUND {
102 return name_;
103 }
104
106 uint32_t name_offset() const {
107 return name_offset_;
108 }
109
111 uint32_t timestamp() const {
112 return timestamp_;
113 }
114
117 return int2version(current_version_);
118 }
119
122 return int2version(compatibility_version_);
123 }
124
125 void name(std::string name) {
126 name_ = std::move(name);
127 }
128 void timestamp(uint32_t timestamp) {
129 timestamp_ = timestamp;
130 }
132 current_version_ = version2int(version);
133 }
135 compatibility_version_ = version2int(version);
136 }
137
138 std::ostream& print(std::ostream& os) const override;
139
140 void accept(Visitor& visitor) const override;
141
142 static bool classof(const LoadCommand* cmd) {
143 const LoadCommand::TYPE type = cmd->command();
150 }
151
152 private:
153 LIEF_LOCAL static DylibCommand create(LoadCommand::TYPE type,
154 std::string_view name, uint32_t timestamp,
155 uint32_t current_version,
156 uint32_t compat_version);
157
158 std::string name_;
159 uint32_t name_offset_ = 0;
160 uint32_t timestamp_ = 0;
161 uint32_t current_version_ = 0;
162 uint32_t compatibility_version_ = 0;
163};
164
165
166}
167
168#endif
Class which represents a library dependency.
Definition DylibCommand.hpp:36
void current_version(version_t version)
Definition DylibCommand.hpp:131
void timestamp(uint32_t timestamp)
Definition DylibCommand.hpp:128
uint32_t timestamp() const
Date and Time when the shared library was built.
Definition DylibCommand.hpp:111
static DylibCommand load_upward_dylib(std::string_view name, uint32_t timestamp=0, uint32_t current_version=0, uint32_t compat_version=0)
Factory function to generate a LC_LOAD_UPWARD_DYLIB library.
std::ostream & print(std::ostream &os) const override
static DylibCommand load_dylib(std::string_view name, uint32_t timestamp=2, uint32_t current_version=0, uint32_t compat_version=0)
Factory function to generate a LC_LOAD_DYLIB library.
static uint32_t version2int(version_t version)
Helper to convert a version array into an integer.
Definition DylibCommand.hpp:51
std::array< uint16_t, 3 > version_t
Definition DylibCommand.hpp:38
static version_t int2version(uint32_t version)
Helper to convert an integer into a version array.
Definition DylibCommand.hpp:42
static DylibCommand weak_dylib(std::string_view name, uint32_t timestamp=0, uint32_t current_version=0, uint32_t compat_version=0)
Factory function to generate a LC_LOAD_WEAK_DYLIB library.
static DylibCommand id_dylib(std::string_view name, uint32_t timestamp=0, uint32_t current_version=0, uint32_t compat_version=0)
Factory function to generate a LC_ID_DYLIB library.
static bool classof(const LoadCommand *cmd)
Definition DylibCommand.hpp:142
DylibCommand(const DylibCommand &copy)=default
void accept(Visitor &visitor) const override
std::unique_ptr< LoadCommand > clone() const override
Definition DylibCommand.hpp:96
version_t compatibility_version() const
Compatibility version of the shared library.
Definition DylibCommand.hpp:121
version_t current_version() const
Current version of the shared library.
Definition DylibCommand.hpp:116
static DylibCommand reexport_dylib(std::string_view name, uint32_t timestamp=0, uint32_t current_version=0, uint32_t compat_version=0)
Factory function to generate a LC_REEXPORT_DYLIB library.
DylibCommand(const details::dylib_command &cmd)
static DylibCommand lazy_load_dylib(std::string_view name, uint32_t timestamp=0, uint32_t current_version=0, uint32_t compat_version=0)
Factory function to generate a LC_LAZY_LOAD_DYLIB library.
DylibCommand & operator=(const DylibCommand &copy)=default
void compatibility_version(version_t version)
Definition DylibCommand.hpp:134
void name(std::string name)
Definition DylibCommand.hpp:125
std::string_view name() const
Library name.
Definition DylibCommand.hpp:101
uint32_t name_offset() const
Original string offset of the name.
Definition DylibCommand.hpp:106
~DylibCommand() override=default
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:132
TYPE
Definition LoadCommand.hpp:47
@ LOAD_WEAK_DYLIB
Definition LoadCommand.hpp:72
@ LOAD_DYLIB
Definition LoadCommand.hpp:60
@ ID_DYLIB
Definition LoadCommand.hpp:61
@ LOAD_UPWARD_DYLIB
Definition LoadCommand.hpp:84
@ REEXPORT_DYLIB
Definition LoadCommand.hpp:79
@ LAZY_LOAD_DYLIB
Definition LoadCommand.hpp:80
Definition Visitor.hpp:212
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition endianness_support.hpp:59
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
lief_version_t version()
Return the current version.
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46