LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
BindingInfo.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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
39class LIEF_API BindingInfo : public Object {
40
41 friend class BinaryParser;
43
44 public:
52
53 BindingInfo() = default;
54 BindingInfo(const BindingInfo& other);
56
57 BindingInfo(BindingInfo&&) noexcept = default;
58 BindingInfo& operator=(BindingInfo&&) noexcept = default;
59
60 void swap(BindingInfo& other) noexcept;
61
63 bool has_segment() const {
64 return segment_ != nullptr;
65 }
66
69 const SegmentCommand* segment() const {
70 return segment_;
71 }
73 return segment_;
74 }
75
77 bool has_library() const {
78 return library_ != nullptr;
79 }
80
83 const DylibCommand* library() const {
84 return library_;
85 }
87 return library_;
88 }
89
91 bool has_symbol() const {
92 return symbol_ != nullptr;
93 }
94
97 const Symbol* symbol() const {
98 return symbol_;
99 }
101 return symbol_;
102 }
103
105 virtual uint64_t address() const {
106 return address_;
107 }
108
109 virtual void address(uint64_t addr) {
110 address_ = addr;
111 }
112
113 int32_t library_ordinal() const {
114 return library_ordinal_;
115 }
116
117 void library_ordinal(int32_t ordinal) {
118 library_ordinal_ = ordinal;
119 }
120
122 int64_t addend() const {
123 return addend_;
124 }
125
126 void addend(int64_t addend) {
127 addend_ = addend;
128 }
129
130 bool is_weak_import() const {
131 return is_weak_import_;
132 }
133
134 void set_weak_import(bool val = true) {
135 is_weak_import_ = val;
136 }
137
140 virtual TYPES type() const = 0;
141
142 ~BindingInfo() override = default;
143
144 void accept(Visitor& visitor) const override;
145
146 template<class T>
147 const T* cast() const {
148 static_assert(std::is_base_of<BindingInfo, T>::value, "Require BindingInfo inheritance");
149 if (T::classof(this)) {
150 return static_cast<const T*>(this);
151 }
152 return nullptr;
153 }
154
155 template<class T>
156 T* cast() {
157 return const_cast<T*>(static_cast<const BindingInfo*>(this)->cast<T>());
158 }
159
160 LIEF_API friend std::ostream& operator<<(std::ostream& os, const BindingInfo& binding_info);
161
162 protected:
163 SegmentCommand* segment_ = nullptr;
164 Symbol* symbol_ = nullptr;
165 int32_t library_ordinal_ = 0;
166 int64_t addend_ = 0;
167 bool is_weak_import_ = false;
168 DylibCommand* library_ = nullptr;
169 uint64_t address_ = 0;
170};
171
172}
173}
174#endif
Class used to parse a single binary (i.e. non-FAT).
Definition BinaryParser.hpp:78
DylibCommand * library()
Definition BindingInfo.hpp:86
~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:91
const T * cast() const
Definition BindingInfo.hpp:147
Symbol * symbol()
Definition BindingInfo.hpp:100
bool has_segment() const
Check if a MachO::SegmentCommand is associated with this binding.
Definition BindingInfo.hpp:63
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:69
int32_t library_ordinal() const
Definition BindingInfo.hpp:113
friend class BinaryParser
Definition BindingInfo.hpp:41
BindingInfo(BindingInfo &&) noexcept=default
BindingInfo(const BindingInfo &other)
friend class DyldChainedFixupsCreator
Definition BindingInfo.hpp:42
bool has_library() const
Check if a MachO::DylibCommand is tied with the BindingInfo.
Definition BindingInfo.hpp:77
virtual void address(uint64_t addr)
Definition BindingInfo.hpp:109
void set_weak_import(bool val=true)
Definition BindingInfo.hpp:134
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:97
TYPES
Definition BindingInfo.hpp:45
@ DYLD_INFO
Definition BindingInfo.hpp:47
@ CHAINED
Binding associated with the Dyld info opcodes.
Definition BindingInfo.hpp:48
@ UNKNOWN
Definition BindingInfo.hpp:46
@ INDIRECT_SYMBOL
Internal use.
Definition BindingInfo.hpp:50
@ CHAINED_LIST
Binding associated with the chained fixups.
Definition BindingInfo.hpp:49
void library_ordinal(int32_t ordinal)
Definition BindingInfo.hpp:117
T * cast()
Definition BindingInfo.hpp:156
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:105
void swap(BindingInfo &other) noexcept
bool is_weak_import() const
Definition BindingInfo.hpp:130
SegmentCommand * segment()
Definition BindingInfo.hpp:72
const DylibCommand * library() const
MachO::DylibCommand associated with the BindingInfo or a nullptr if not present.
Definition BindingInfo.hpp:83
void addend(int64_t addend)
Definition BindingInfo.hpp:126
BindingInfo & operator=(const BindingInfo &other)
int64_t addend() const
Value added to the segment's virtual address when bound.
Definition BindingInfo.hpp:122
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:50
Class that represents a Symbol in a Mach-O file.
Definition MachO/Symbol.hpp:47
Definition Visitor.hpp:210
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF namespace.
Definition Abstract/Binary.hpp:40
#define LIEF_API
Definition visibility.h:41