LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
MachO/Relocation.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_RELOCATION_COMMAND_H
17#define LIEF_MACHO_RELOCATION_COMMAND_H
18#include <ostream>
19#include <memory>
20
22
23#include "LIEF/MachO/Header.hpp"
24#include "LIEF/visibility.h"
25#include "LIEF/Object.hpp"
26
27namespace LIEF {
28namespace MachO {
29class BinaryParser;
30class Section;
31class SegmentCommand;
32class Symbol;
33class LIEF_API Relocation : public LIEF::Relocation {
41
42 friend class BinaryParser;
43
44 public:
47
48 enum class ORIGIN {
50 DYLDINFO = 1,
51 RELOC_TABLE = 2,
52 CHAINED_FIXUPS = 3,
53 };
54
55 static constexpr auto R_SCATTERED = uint32_t(0x80000000);
56 static constexpr auto R_ABS = uint32_t(0);
57
58 Relocation() = default;
59 Relocation(uint64_t address, uint8_t type);
60
61 Relocation& operator=(const Relocation& other);
62 Relocation(const Relocation& other);
63 void swap(Relocation& other) noexcept;
64
65 ~Relocation() override = default;
66
67 virtual std::unique_ptr<Relocation> clone() const = 0;
68 virtual bool is_pc_relative() const = 0;
75 virtual uint8_t type() const {
87 return type_;
88 }
89 Header::CPU_TYPE architecture() const {
92 return architecture_;
93 }
94 virtual ORIGIN origin() const = 0;
97 bool has_symbol() const {
100 return symbol() != nullptr;
101 }
102 Symbol* symbol() {
106 return symbol_;
107 }
108 const Symbol* symbol() const {
109 return symbol_;
110 }
111 bool has_section() const {
114 return section() != nullptr;
115 }
116 Section* section() {
120 return section_;
121 }
122 const Section* section() const {
123 return section_;
124 }
125 bool has_segment() const {
128 return segment() != nullptr;
129 }
130 SegmentCommand* segment() {
134 return segment_;
135 }
136 const SegmentCommand* segment() const {
137 return segment_;
138 }
139
140 template<class T>
141 const T* cast() const {
142 static_assert(std::is_base_of<Relocation, T>::value, "Require Relocation inheritance");
143 if (T::classof(*this)) {
144 return static_cast<const T*>(this);
145 }
146 return nullptr;
147 }
148
149 template<class T>
150 T* cast() {
151 return const_cast<T*>(static_cast<const Relocation*>(this)->cast<T>());
152 }
153
154 virtual void pc_relative(bool val) = 0;
155 virtual void type(uint8_t type);
156
157 void accept(Visitor& visitor) const override;
158
159 virtual std::ostream& print(std::ostream& os) const;
160
161 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Relocation& relocation);
162
163 protected:
164 Symbol* symbol_ = nullptr;
165 uint8_t type_ = 0;
166 Header::CPU_TYPE architecture_ = Header::CPU_TYPE::ANY;
167 Section* section_ = nullptr;
168 SegmentCommand* segment_ = nullptr;
169};
170
171
172LIEF_API const char* to_string(Relocation::ORIGIN e);
173
174}
175}
176#endif
Relocation.hpp
Header.hpp
Object.hpp
LIEF::MachO::BinaryParser
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:75
LIEF::MachO::Relocation
Class that represents a Mach-O relocation.
Definition MachO/Relocation.hpp:40
LIEF::MachO::Relocation::architecture
Header::CPU_TYPE architecture() const
Achitecture targeted by this relocation.
Definition MachO/Relocation.hpp:91
LIEF::MachO::Relocation::cast
const T * cast() const
Definition MachO/Relocation.hpp:141
LIEF::MachO::Relocation::segment
SegmentCommand * segment()
SegmentCommand associated with the relocation, if any, otherwise a nullptr.
Definition MachO/Relocation.hpp:133
LIEF::MachO::Relocation::is_pc_relative
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...
LIEF::MachO::Relocation::section
const Section * section() const
Definition MachO/Relocation.hpp:122
LIEF::MachO::Relocation::segment
const SegmentCommand * segment() const
Definition MachO/Relocation.hpp:136
LIEF::MachO::Relocation::Relocation
Relocation(uint64_t address, uint8_t type)
LIEF::MachO::Relocation::symbol
const Symbol * symbol() const
Definition MachO/Relocation.hpp:108
LIEF::MachO::Relocation::section
Section * section()
Section associated with the relocation, if any, otherwise a nullptr.
Definition MachO/Relocation.hpp:119
LIEF::MachO::Relocation::clone
virtual std::unique_ptr< Relocation > clone() const =0
LIEF::MachO::Relocation::has_symbol
bool has_symbol() const
true if the relocation has a symbol associated with
Definition MachO/Relocation.hpp:99
LIEF::MachO::Relocation::swap
void swap(Relocation &other) noexcept
LIEF::MachO::Relocation::origin
virtual ORIGIN origin() const =0
Origin of the relocation.
LIEF::MachO::Relocation::ORIGIN
ORIGIN
Definition MachO/Relocation.hpp:48
LIEF::MachO::Relocation::~Relocation
~Relocation() override=default
LIEF::MachO::Relocation::print
virtual std::ostream & print(std::ostream &os) const
LIEF::MachO::Relocation::has_segment
bool has_segment() const
true if the relocation has a SegmentCommand associated with
Definition MachO/Relocation.hpp:127
LIEF::MachO::Relocation::accept
void accept(Visitor &visitor) const override
LIEF::MachO::Relocation::has_section
bool has_section() const
true if the relocation has a section associated with
Definition MachO/Relocation.hpp:113
LIEF::MachO::Relocation::cast
T * cast()
Definition MachO/Relocation.hpp:150
LIEF::MachO::Relocation::Relocation
Relocation()=default
LIEF::MachO::Relocation::Relocation
Relocation(const Relocation &other)
LIEF::MachO::Relocation::pc_relative
virtual void pc_relative(bool val)=0
LIEF::MachO::Relocation::operator=
Relocation & operator=(const Relocation &other)
LIEF::MachO::Relocation::operator<<
friend std::ostream & operator<<(std::ostream &os, const Relocation &relocation)
LIEF::MachO::Relocation::type
virtual uint8_t type() const
Type of the relocation according to the Relocation::architecture and/or the Relocation::origin.
Definition MachO/Relocation.hpp:86
LIEF::MachO::Relocation::symbol
Symbol * symbol()
Symbol associated with the relocation, if any, otherwise a nullptr.
Definition MachO/Relocation.hpp:105
LIEF::MachO::Relocation::type
virtual void type(uint8_t type)
LIEF::MachO::Section
Class that represents a Mach-O section.
Definition MachO/Section.hpp:46
LIEF::MachO::SegmentCommand
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:50
LIEF::MachO::Symbol
Class that represents a Symbol in a Mach-O file.
Definition MachO/Symbol.hpp:47
LIEF::Relocation::size
virtual size_t size() const
Relocation size in bits
Definition Abstract/Relocation.hpp:53
LIEF::Relocation::address
virtual uint64_t address() const
Relocation's address.
Definition Abstract/Relocation.hpp:48
LIEF::MachO
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF::MachO::MACHO_TYPES::UNKNOWN
@ UNKNOWN
Definition MachO/enums.hpp:25
LIEF::MachO::to_string
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41