LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
DylibCommand.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_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}
32class LIEF_API DylibCommand : public LoadCommand {
35 public:
36 using version_t = std::array<uint16_t, 3>;
37
38 public: 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 static uint32_t version2int(version_t version) {
50 return (version[2]) | (version[1] << 8) | (version[0] << 16);
51 }
52 static DylibCommand weak_dylib(const std::string& name,
55 uint32_t timestamp = 0,
56 uint32_t current_version = 0,
57 uint32_t compat_version = 0);
58 static DylibCommand id_dylib(const std::string& name,
61 uint32_t timestamp = 0,
62 uint32_t current_version = 0,
63 uint32_t compat_version = 0);
64 static DylibCommand load_dylib(const std::string& name,
67 uint32_t timestamp = 2,
68 uint32_t current_version = 0,
69 uint32_t compat_version = 0);
70 static DylibCommand reexport_dylib(const std::string& name,
73 uint32_t timestamp = 0,
74 uint32_t current_version = 0,
75 uint32_t compat_version = 0);
76 static DylibCommand load_upward_dylib(const std::string& name,
79 uint32_t timestamp = 0,
80 uint32_t current_version = 0,
81 uint32_t compat_version = 0);
82 static DylibCommand lazy_load_dylib(const std::string& name,
85 uint32_t timestamp = 0,
86 uint32_t current_version = 0,
87 uint32_t compat_version = 0);
88
89 public:
90 DylibCommand() = default;
91 DylibCommand(const details::dylib_command& cmd);
92
93 DylibCommand& operator=(const DylibCommand& copy) = default;
94 DylibCommand(const DylibCommand& copy) = default;
95
96 ~DylibCommand() override = default;
97
98 std::unique_ptr<LoadCommand> clone() const override {
99 return std::unique_ptr<DylibCommand>(new DylibCommand(*this));
100 }
101 const std::string& name() const {
104 return name_;
105 }
106 uint32_t timestamp() const {
109 return timestamp_;
110 }
111 version_t current_version() const {
114 return int2version(current_version_);
115 }
116 version_t compatibility_version() const {
119 return int2version(compatibility_version_);
120 }
121
122 void name(std::string name) {
123 name_ = std::move(name);
124 }
125 void timestamp(uint32_t timestamp) {
126 timestamp_ = timestamp;
127 }
128 void current_version(version_t version) {
129 current_version_ = version2int(version);
130 }
131 void compatibility_version(version_t version) {
132 compatibility_version_ = version2int(version);
133 }
134
135 std::ostream& print(std::ostream& os) const override;
136
137 void accept(Visitor& visitor) const override;
138
139 static bool classof(const LoadCommand* cmd) {
140 const LoadCommand::TYPE type = cmd->command();
141 return type == LoadCommand::TYPE::LOAD_WEAK_DYLIB ||
142 type == LoadCommand::TYPE::ID_DYLIB ||
143 type == LoadCommand::TYPE::LOAD_DYLIB ||
144 type == LoadCommand::TYPE::LOAD_UPWARD_DYLIB ||
145 type == LoadCommand::TYPE::REEXPORT_DYLIB ||
146 type == LoadCommand::TYPE::LOAD_UPWARD_DYLIB ||
147 type == LoadCommand::TYPE::LAZY_LOAD_DYLIB;
148 }
149
150 private:
151 LIEF_LOCAL static DylibCommand create(
152 LoadCommand::TYPE type, const std::string& name, uint32_t timestamp,
153 uint32_t current_version, uint32_t compat_version);
154
155 std::string name_;
156 uint32_t timestamp_ = 0;
157 uint32_t current_version_ = 0;
158 uint32_t compatibility_version_ = 0;
159};
160
161
162}
163}
164#endif
LoadCommand.hpp
LIEF::MachO::DylibCommand
Class which represents a library dependency.
Definition DylibCommand.hpp:34
LIEF::MachO::DylibCommand::DylibCommand
DylibCommand()=default
LIEF::MachO::DylibCommand::reexport_dylib
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.
LIEF::MachO::DylibCommand::current_version
void current_version(version_t version)
Definition DylibCommand.hpp:128
LIEF::MachO::DylibCommand::timestamp
void timestamp(uint32_t timestamp)
Definition DylibCommand.hpp:125
LIEF::MachO::DylibCommand::timestamp
uint32_t timestamp() const
Date and Time when the shared library was built.
Definition DylibCommand.hpp:108
LIEF::MachO::DylibCommand::print
std::ostream & print(std::ostream &os) const override
LIEF::MachO::DylibCommand::version2int
static uint32_t version2int(version_t version)
Helper to convert a version array into an integer.
Definition DylibCommand.hpp:49
LIEF::MachO::DylibCommand::int2version
static version_t int2version(uint32_t version)
Helper to convert an integer into a version array.
Definition DylibCommand.hpp:40
LIEF::MachO::DylibCommand::lazy_load_dylib
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.
LIEF::MachO::DylibCommand::classof
static bool classof(const LoadCommand *cmd)
Definition DylibCommand.hpp:139
LIEF::MachO::DylibCommand::DylibCommand
DylibCommand(const DylibCommand &copy)=default
LIEF::MachO::DylibCommand::accept
void accept(Visitor &visitor) const override
LIEF::MachO::DylibCommand::clone
std::unique_ptr< LoadCommand > clone() const override
Definition DylibCommand.hpp:98
LIEF::MachO::DylibCommand::compatibility_version
version_t compatibility_version() const
Compatibility version of the shared library.
Definition DylibCommand.hpp:118
LIEF::MachO::DylibCommand::current_version
version_t current_version() const
Current version of the shared library.
Definition DylibCommand.hpp:113
LIEF::MachO::DylibCommand::weak_dylib
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.
LIEF::MachO::DylibCommand::DylibCommand
DylibCommand(const details::dylib_command &cmd)
LIEF::MachO::DylibCommand::operator=
DylibCommand & operator=(const DylibCommand &copy)=default
LIEF::MachO::DylibCommand::compatibility_version
void compatibility_version(version_t version)
Definition DylibCommand.hpp:131
LIEF::MachO::DylibCommand::id_dylib
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.
LIEF::MachO::DylibCommand::load_dylib
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.
LIEF::MachO::DylibCommand::name
void name(std::string name)
Definition DylibCommand.hpp:122
LIEF::MachO::DylibCommand::~DylibCommand
~DylibCommand() override=default
LIEF::MachO::DylibCommand::load_upward_dylib
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.
LIEF::MachO::DylibCommand::name
const std::string & name() const
Library name.
Definition DylibCommand.hpp:103
LIEF::MachO::LoadCommand
Based class for the Mach-O load commands.
Definition LoadCommand.hpp:37
LIEF::MachO::LoadCommand::command
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:124
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
LIEF namespace.
Definition Abstract/Binary.hpp:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42