LIEF: Library to Instrument Executable Formats Version 0.15.1
Loading...
Searching...
No Matches
RelocationFixup.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_FIXUP_H
17#define LIEF_MACHO_RELOCATION_FIXUP_H
18#include <ostream>
19#include <memory>
20
21#include "LIEF/visibility.h"
22
25
26namespace LIEF {
27namespace MachO {
28
29namespace details {
30struct dyld_chained_ptr_arm64e_rebase;
31struct dyld_chained_ptr_arm64e_auth_rebase;
32struct dyld_chained_ptr_64_rebase;
33struct dyld_chained_ptr_32_rebase;
34}
35
36class BinaryParser;
37class Builder;
38
50
51 friend class BinaryParser;
52 friend class Builder;
53
54 public:
55 RelocationFixup() = delete;
56 RelocationFixup(DYLD_CHAINED_PTR_FORMAT fmt, uint64_t imagebase);
57
60
62 RelocationFixup(RelocationFixup&&) noexcept = default;
63
64 ~RelocationFixup() override;
65
66 std::unique_ptr<Relocation> clone() const override {
67 return std::unique_ptr<RelocationFixup>(new RelocationFixup(*this));
68 }
69
71 bool is_pc_relative() const override {
72 return false;
73 }
74
77 ORIGIN origin() const override {
78 return ORIGIN::CHAINED_FIXUPS;
79 }
80
82 return ptr_fmt_;
83 }
84
88 uint64_t target() const;
89 void target(uint64_t target);
90
92 void pc_relative(bool) override {}
93
94 uint32_t offset() const {
95 return offset_;
96 }
97
98 void offset(uint32_t offset) {
99 offset_ = offset;
100 }
101
103 uint64_t address() const override {
104 return imagebase_ + offset_;
105 }
106
108 void address(uint64_t address) override {
109 offset_ = address - imagebase_;
110 }
111
112 void accept(Visitor& visitor) const override;
113
114 static bool classof(const Relocation& r) {
115 return r.origin() == Relocation::ORIGIN::CHAINED_FIXUPS;
116 }
117
118 std::ostream& print(std::ostream& os) const override;
119
120 private:
121 enum class REBASE_TYPES {
122 UNKNOWN = 0,
123
124 ARM64E_REBASE,
125 ARM64E_AUTH_REBASE,
126 PTR64_REBASE,
127 PTR32_REBASE,
128 };
129
130 void set(const details::dyld_chained_ptr_arm64e_rebase& fixup);
131 void set(const details::dyld_chained_ptr_arm64e_auth_rebase& fixup);
132 void set(const details::dyld_chained_ptr_64_rebase& fixup);
133 void set(const details::dyld_chained_ptr_32_rebase& fixup);
134
135 DYLD_CHAINED_PTR_FORMAT ptr_fmt_ = DYLD_CHAINED_PTR_FORMAT::PTR_32;
136 uint64_t imagebase_ = 0;
137 uint32_t offset_ = 0;
138
139 REBASE_TYPES rtypes_ = REBASE_TYPES::UNKNOWN;
140
141 union {
142 details::dyld_chained_ptr_arm64e_rebase* arm64_rebase_ = nullptr;
143 details::dyld_chained_ptr_arm64e_auth_rebase* arm64_auth_rebase_;
144 details::dyld_chained_ptr_64_rebase* p64_rebase_;
145 details::dyld_chained_ptr_32_rebase* p32_rebase_;
146 };
147};
148
149}
150}
151#endif
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:74
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:55
Class that represents a rebase relocation found in the LC_DYLD_CHAINED_FIXUPS command.
Definition RelocationFixup.hpp:49
details::dyld_chained_ptr_64_rebase * p64_rebase_
Definition RelocationFixup.hpp:144
details::dyld_chained_ptr_32_rebase * p32_rebase_
Definition RelocationFixup.hpp:145
RelocationFixup & operator=(RelocationFixup &&) noexcept=default
RelocationFixup(DYLD_CHAINED_PTR_FORMAT fmt, uint64_t imagebase)
void offset(uint32_t offset)
Definition RelocationFixup.hpp:98
std::ostream & print(std::ostream &os) const override
void target(uint64_t target)
uint64_t target() const
The value that should be set at the address pointed by LIEF::Relocation::address if the imagebase cho...
ORIGIN origin() const override
Origin of the relocation. For this concrete object, it should be Relocation::ORIGIN::CHAINED_FIXUPS.
Definition RelocationFixup.hpp:77
uint32_t offset() const
Definition RelocationFixup.hpp:94
void pc_relative(bool) override
Not relevant for this kind of relocation.
Definition RelocationFixup.hpp:92
uint64_t address() const override
The address of this relocation is bound to its offset.
Definition RelocationFixup.hpp:103
details::dyld_chained_ptr_arm64e_auth_rebase * arm64_auth_rebase_
Definition RelocationFixup.hpp:143
static bool classof(const Relocation &r)
Definition RelocationFixup.hpp:114
bool is_pc_relative() const override
Not relevant for this kind of relocation.
Definition RelocationFixup.hpp:71
RelocationFixup & operator=(const RelocationFixup &)
void address(uint64_t address) override
Changing the address means changing the offset.
Definition RelocationFixup.hpp:108
RelocationFixup(const RelocationFixup &)
DYLD_CHAINED_PTR_FORMAT ptr_format() const
Definition RelocationFixup.hpp:81
void accept(Visitor &visitor) const override
Class that represents a Mach-O relocation.
Definition MachO/Relocation.hpp:40
virtual ORIGIN origin() const =0
Origin of the relocation.
ORIGIN
Definition MachO/Relocation.hpp:48
Definition Visitor.hpp:224
DYLD_CHAINED_PTR_FORMAT
Definition DyldChainedFormat.hpp:29
LIEF namespace.
Definition Abstract/Binary.hpp:32
#define LIEF_API
Definition visibility.h:41