LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
MachO/Relocation.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_MACHO_RELOCATION_COMMAND_H
16#define LIEF_MACHO_RELOCATION_COMMAND_H
17#include <memory>
18#include <ostream>
19
21
22#include "LIEF/MachO/Header.hpp"
23#include "LIEF/Object.hpp"
24#include "LIEF/visibility.h"
25
26
27namespace LIEF::MachO {
28class BinaryParser;
29class Section;
30class SegmentCommand;
31class Symbol;
32
40
41 friend class BinaryParser;
42
43 public:
46
47 enum class ORIGIN {
49 DYLDINFO = 1,
50 RELOC_TABLE = 2,
51 CHAINED_FIXUPS = 3,
52 };
53
54 static constexpr auto R_SCATTERED = uint32_t(0x80000000);
55 static constexpr auto R_ABS = uint32_t(0);
56
57 Relocation() = default;
58 Relocation(uint64_t address, uint8_t type);
59
61 Relocation(const Relocation& other);
62 void swap(Relocation& other) noexcept;
63
64 ~Relocation() override = default;
65
66 virtual std::unique_ptr<Relocation> clone() const = 0;
67
73 virtual bool is_pc_relative() const = 0;
74
85 virtual uint8_t type() const {
86 return type_;
87 }
88
91 return architecture_;
92 }
93
95 virtual ORIGIN origin() const = 0;
96
98 bool has_symbol() const {
99 return symbol() != nullptr;
100 }
101
105 return symbol_;
106 }
107
109 return symbol_;
110 }
111
113 bool has_section() const {
114 return section() != nullptr;
115 }
116
120 return section_;
121 }
122
124 return section_;
125 }
126
128 bool has_segment() const {
129 return segment() != nullptr;
130 }
131
135 return segment_;
136 }
137
139 return segment_;
140 }
141
142 template<class T>
143 const T* cast() const {
144 static_assert(std::is_base_of_v<Relocation, T>,
145 "Require Relocation inheritance");
146 if (T::classof(*this)) {
147 return static_cast<const T*>(this);
148 }
149 return nullptr;
150 }
151
152 template<class T>
153 T* cast() {
154 return const_cast<T*>(static_cast<const Relocation*>(this)->cast<T>());
155 }
156
157 virtual void pc_relative(bool val) = 0;
158 virtual void type(uint8_t type);
159
160 void accept(Visitor& visitor) const override;
161
162 virtual std::ostream& print(std::ostream& os) const;
163
164 LIEF_API friend std::ostream& operator<<(std::ostream& os,
165 const Relocation& relocation);
166
167 protected:
168 Symbol* symbol_ = nullptr;
169 uint8_t type_ = 0;
171 Section* section_ = nullptr;
172 SegmentCommand* segment_ = nullptr;
173};
174
175
177
178}
179
180#endif
Class used to parse a single binary (i.e. non-FAT).
Definition BinaryParser.hpp:80
CPU_TYPE
Definition MachO/Header.hpp:106
@ ANY
Definition MachO/Header.hpp:107
Header::CPU_TYPE architecture() const
Architecture targeted by this relocation.
Definition MachO/Relocation.hpp:90
const T * cast() const
Definition MachO/Relocation.hpp:143
SegmentCommand * segment()
SegmentCommand associated with the relocation, if any, otherwise a nullptr.
Definition MachO/Relocation.hpp:134
virtual bool is_pc_relative() const =0
Indicates whether the item containing the address to be relocated is part of a CPU instruction that u...
const Section * section() const
Definition MachO/Relocation.hpp:123
const SegmentCommand * segment() const
Definition MachO/Relocation.hpp:138
static constexpr auto R_SCATTERED
Definition MachO/Relocation.hpp:54
Relocation(uint64_t address, uint8_t type)
const Symbol * symbol() const
Definition MachO/Relocation.hpp:108
Section * section()
Section associated with the relocation, if any, otherwise a nullptr.
Definition MachO/Relocation.hpp:119
friend class BinaryParser
Definition MachO/Relocation.hpp:41
virtual std::unique_ptr< Relocation > clone() const =0
bool has_symbol() const
true if the relocation has a symbol associated with
Definition MachO/Relocation.hpp:98
void swap(Relocation &other) noexcept
virtual ORIGIN origin() const =0
Origin of the relocation.
ORIGIN
Definition MachO/Relocation.hpp:47
~Relocation() override=default
virtual std::ostream & print(std::ostream &os) const
bool has_segment() const
true if the relocation has a SegmentCommand associated with
Definition MachO/Relocation.hpp:128
void accept(Visitor &visitor) const override
bool has_section() const
true if the relocation has a section associated with
Definition MachO/Relocation.hpp:113
T * cast()
Definition MachO/Relocation.hpp:153
Relocation(const Relocation &other)
virtual void pc_relative(bool val)=0
Relocation & operator=(const Relocation &other)
friend std::ostream & operator<<(std::ostream &os, const Relocation &relocation)
virtual uint8_t type() const
Type of the relocation according to the Relocation::architecture and/or the Relocation::origin.
Definition MachO/Relocation.hpp:85
virtual uint64_t address() const
Relocation's address.
Definition Abstract/Relocation.hpp:46
static constexpr auto R_ABS
Definition MachO/Relocation.hpp:55
Symbol * symbol()
Symbol associated with the relocation, if any, otherwise a nullptr.
Definition MachO/Relocation.hpp:104
virtual void type(uint8_t type)
Class that represents a Mach-O section.
Definition MachO/Section.hpp:48
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:53
Class that represents a Symbol in a Mach-O file.
Definition MachO/Symbol.hpp:46
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:26
virtual size_t size() const
Relocation size in bits.
Definition Abstract/Relocation.hpp:51
virtual uint64_t address() const
Relocation's address.
Definition Abstract/Relocation.hpp:46
Definition Visitor.hpp:211
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:35
@ UNKNOWN
Definition MachO/enums.hpp:25
const char * to_string(BuildToolVersion::TOOLS tool)
#define LIEF_API
Definition visibility.h:44