LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
BindingInfo.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_BINDING_INFO_H
17#define LIEF_MACHO_BINDING_INFO_H
18#include <ostream>
19#include <cstdint>
20
21#include "LIEF/visibility.h"
22#include "LIEF/Object.hpp"
23
24namespace LIEF {
25namespace MachO {
26class DylibCommand;
27class SegmentCommand;
28class Symbol;
29class BinaryParser;
31
40class LIEF_API BindingInfo : public Object {
41
42 friend class BinaryParser;
44
45 public:
46 enum class TYPES {
48 DYLD_INFO,
49 CHAINED,
50 CHAINED_LIST,
51 INDIRECT_SYMBOL,
52 };
53
54 BindingInfo() = default;
55 BindingInfo(const BindingInfo& other);
57
58 BindingInfo(BindingInfo&&) noexcept = default;
59 BindingInfo& operator=(BindingInfo&&) noexcept = default;
60
61 void swap(BindingInfo& other) noexcept;
62
64 bool has_segment() const {
65 return segment_ != nullptr;
66 }
67
70 const SegmentCommand* segment() const {
71 return segment_;
72 }
74 return segment_;
75 }
76
78 bool has_library() const {
79 return library_ != nullptr;
80 }
81
84 const DylibCommand* library() const {
85 return library_;
86 }
88 return library_;
89 }
90
92 bool has_symbol() const {
93 return symbol_ != nullptr;
94 }
95
98 const Symbol* symbol() const {
99 return symbol_;
100 }
102 return symbol_;
103 }
104
106 virtual uint64_t address() const {
107 return address_;
108 }
109
110 virtual void address(uint64_t addr) {
111 address_ = addr;
112 }
113
114 int32_t library_ordinal() const {
115 return library_ordinal_;
116 }
117
118 void library_ordinal(int32_t ordinal) {
119 library_ordinal_ = ordinal;
120 }
121
123 int64_t addend() const {
124 return addend_;
125 }
126
127 void addend(int64_t addend) {
128 addend_ = addend;
129 }
130
131 bool is_weak_import() const {
132 return is_weak_import_;
133 }
134
135 void set_weak_import(bool val = true) {
136 is_weak_import_ = val;
137 }
138
141 virtual TYPES type() const = 0;
142
143 ~BindingInfo() override = default;
144
145 void accept(Visitor& visitor) const override;
146
147 template<class T>
148 const T* cast() const {
149 static_assert(std::is_base_of<BindingInfo, T>::value,
150 "Require BindingInfo inheritance");
151 if (T::classof(this)) {
152 return static_cast<const T*>(this);
153 }
154 return nullptr;
155 }
156
157 template<class T>
158 T* cast() {
159 return const_cast<T*>(static_cast<const BindingInfo*>(this)->cast<T>());
160 }
161
162 LIEF_API friend std::ostream& operator<<(std::ostream& os,
163 const BindingInfo& binding_info);
164
165 protected:
166 SegmentCommand* segment_ = nullptr;
167 Symbol* symbol_ = nullptr;
168 int32_t library_ordinal_ = 0;
169 int64_t addend_ = 0;
170 bool is_weak_import_ = false;
171 DylibCommand* library_ = nullptr;
172 uint64_t address_ = 0;
173};
174
175}
176}
177#endif
Class used to parse a single binary (i.e. non-FAT).
Definition BinaryParser.hpp:78
DylibCommand * library()
Definition BindingInfo.hpp:87
~BindingInfo() override=default
void accept(Visitor &visitor) const override
bool has_symbol() const
Check if a MachO::Symbol is associated with the BindingInfo.
Definition BindingInfo.hpp:92
const T * cast() const
Definition BindingInfo.hpp:148
Symbol * symbol()
Definition BindingInfo.hpp:101
bool has_segment() const
Check if a MachO::SegmentCommand is associated with this binding.
Definition BindingInfo.hpp:64
const SegmentCommand * segment() const
The MachO::SegmentCommand associated with the BindingInfo or a nullptr of it is not bind to a Segment...
Definition BindingInfo.hpp:70
int32_t library_ordinal() const
Definition BindingInfo.hpp:114
friend class BinaryParser
Definition BindingInfo.hpp:42
BindingInfo(BindingInfo &&) noexcept=default
BindingInfo(const BindingInfo &other)
friend class DyldChainedFixupsCreator
Definition BindingInfo.hpp:43
bool has_library() const
Check if a MachO::DylibCommand is tied with the BindingInfo.
Definition BindingInfo.hpp:78
virtual void address(uint64_t addr)
Definition BindingInfo.hpp:110
void set_weak_import(bool val=true)
Definition BindingInfo.hpp:135
friend std::ostream & operator<<(std::ostream &os, const BindingInfo &binding_info)
const Symbol * symbol() const
MachO::Symbol associated with the BindingInfo or a nullptr if not present.
Definition BindingInfo.hpp:98
TYPES
Definition BindingInfo.hpp:46
void library_ordinal(int32_t ordinal)
Definition BindingInfo.hpp:118
T * cast()
Definition BindingInfo.hpp:158
virtual TYPES type() const =0
The type of the binding. This type provides the origin of the binding (LC_DYLD_INFO or LC_DYLD_CHAINE...
virtual uint64_t address() const
Address of the binding.
Definition BindingInfo.hpp:106
void swap(BindingInfo &other) noexcept
bool is_weak_import() const
Definition BindingInfo.hpp:131
SegmentCommand * segment()
Definition BindingInfo.hpp:73
const DylibCommand * library() const
MachO::DylibCommand associated with the BindingInfo or a nullptr if not present.
Definition BindingInfo.hpp:84
void addend(int64_t addend)
Definition BindingInfo.hpp:127
BindingInfo & operator=(const BindingInfo &other)
int64_t addend() const
Value added to the segment's virtual address when bound.
Definition BindingInfo.hpp:123
Definition DyldChainedFixupsCreator.hpp:41
Class which represents a library dependency.
Definition DylibCommand.hpp:34
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:51
Class that represents a Symbol in a Mach-O file.
Definition MachO/Symbol.hpp:47
Definition Visitor.hpp:212
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
@ UNKNOWN
Definition MachO/enums.hpp:24
LIEF namespace.
Definition Abstract/Binary.hpp:40
#define LIEF_API
Definition visibility.h:43