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