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 static_cast<uint16_t>(version >> 16),
45 static_cast<uint16_t>((version >> 8) & 0xFF),
46 static_cast<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(const std::string& name, uint32_t timestamp = 0,
57 uint32_t current_version = 0,
58 uint32_t compat_version = 0);
59
61 static DylibCommand id_dylib(const std::string& name, uint32_t timestamp = 0,
62 uint32_t current_version = 0,
63 uint32_t compat_version = 0);
64
66 static DylibCommand load_dylib(const std::string& name, uint32_t timestamp = 2,
67 uint32_t current_version = 0,
68 uint32_t compat_version = 0);
69
71 static DylibCommand reexport_dylib(const std::string& name,
72 uint32_t timestamp = 0,
73 uint32_t current_version = 0,
74 uint32_t compat_version = 0);
75
77 static DylibCommand load_upward_dylib(const std::string& name,
78 uint32_t timestamp = 0,
79 uint32_t current_version = 0,
80 uint32_t compat_version = 0);
81
83 static DylibCommand lazy_load_dylib(const std::string& name,
84 uint32_t timestamp = 0,
85 uint32_t current_version = 0,
86 uint32_t compat_version = 0);
87
88 public:
89 DylibCommand() = default;
90 DylibCommand(const details::dylib_command& cmd);
91
92 DylibCommand& operator=(const DylibCommand& copy) = default;
93 DylibCommand(const DylibCommand& copy) = default;
94
95 ~DylibCommand() override = default;
96
97 std::unique_ptr<LoadCommand> clone() const override {
98 return std::make_unique<DylibCommand>(*this);
99 }
100
102 std::string_view name() const LIEF_LIFETIMEBOUND {
103 return name_;
104 }
105
107 uint32_t name_offset() const {
108 return name_offset_;
109 }
110
112 uint32_t timestamp() const {
113 return timestamp_;
114 }
115
118 return int2version(current_version_);
119 }
120
123 return int2version(compatibility_version_);
124 }
125
126 void name(std::string name) {
127 name_ = std::move(name);
128 }
129 void timestamp(uint32_t timestamp) {
130 timestamp_ = timestamp;
131 }
133 current_version_ = version2int(version);
134 }
136 compatibility_version_ = version2int(version);
137 }
138
139 std::ostream& print(std::ostream& os) const override;
140
141 void accept(Visitor& visitor) const override;
142
143 static bool classof(const LoadCommand* cmd) {
144 const LoadCommand::TYPE type = cmd->command();
151 }
152
153 private:
155 create(LoadCommand::TYPE type, const std::string& name, uint32_t timestamp,
156 uint32_t current_version, 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
static DylibCommand reexport_dylib(const std::string &name, uint32_t timestamp=0, uint32_t current_version=0, uint32_t compat_version=0)
Factory function to generate a LC_REEXPORT_DYLIB library.
void current_version(version_t version)
Definition DylibCommand.hpp:132
void timestamp(uint32_t timestamp)
Definition DylibCommand.hpp:129
uint32_t timestamp() const
Date and Time when the shared library was built.
Definition DylibCommand.hpp:112
std::ostream & print(std::ostream &os) const override
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 lazy_load_dylib(const std::string &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.
static bool classof(const LoadCommand *cmd)
Definition DylibCommand.hpp:143
DylibCommand(const DylibCommand &copy)=default
void accept(Visitor &visitor) const override
std::unique_ptr< LoadCommand > clone() const override
Definition DylibCommand.hpp:97
version_t compatibility_version() const
Compatibility version of the shared library.
Definition DylibCommand.hpp:122
version_t current_version() const
Current version of the shared library.
Definition DylibCommand.hpp:117
static DylibCommand weak_dylib(const std::string &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.
DylibCommand(const details::dylib_command &cmd)
DylibCommand & operator=(const DylibCommand &copy)=default
void compatibility_version(version_t version)
Definition DylibCommand.hpp:135
static DylibCommand id_dylib(const std::string &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 DylibCommand load_dylib(const std::string &name, uint32_t timestamp=2, uint32_t current_version=0, uint32_t compat_version=0)
Factory function to generate a LC_LOAD_DYLIB library.
void name(std::string name)
Definition DylibCommand.hpp:126
std::string_view name() const
Library name.
Definition DylibCommand.hpp:102
uint32_t name_offset() const
Original string offset of the name.
Definition DylibCommand.hpp:107
~DylibCommand() override=default
static DylibCommand load_upward_dylib(const std::string &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.
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