LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
Stub.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_STUB_H
17#define LIEF_MACHO_STUB_H
19#include "LIEF/visibility.h"
20
21#include "LIEF/errors.hpp"
22#include "LIEF/iterators.hpp"
23#include "LIEF/span.hpp"
24
25#include "LIEF/MachO/Header.hpp"
26
27#include <cstdint>
28#include <ostream>
29#include <vector>
30
31
32namespace LIEF::MachO {
33
34class Binary;
35class Section;
36
52 public:
55 uint32_t subtype = 0;
57 return lhs.arch == rhs.arch && lhs.subtype == rhs.subtype;
58 }
59 };
61 : public iterator_facade_base<Iterator, std::random_access_iterator_tag,
62 const Stub> {
63 public:
64 Iterator() = default;
65
66 Iterator(target_info_t target_info, std::vector<const Section*> sections,
67 size_t pos) :
68 target_info_(target_info),
69 stubs_(std::move(sections)),
70 pos_(pos) {}
71
72 Iterator(const Iterator&) = default;
73 Iterator& operator=(const Iterator&) = default;
74
75 Iterator(Iterator&&) noexcept = default;
76 Iterator& operator=(Iterator&&) noexcept = default;
77
78 ~Iterator() = default;
79
80 bool operator<(const Iterator& rhs) const {
81 return pos_ < rhs.pos_;
82 }
83
84 std::ptrdiff_t operator-(const Iterator& R) const {
85 return pos_ - R.pos_;
86 }
87
88 Iterator& operator+=(std::ptrdiff_t n) {
89 pos_ += n;
90 return *this;
91 }
92
93 Iterator& operator-=(std::ptrdiff_t n) {
94 pos_ -= n;
95 return *this;
96 }
97
98 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS) {
99 return LHS.pos_ == RHS.pos_;
100 }
101
102 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
103 return !(LHS == RHS);
104 }
105
107
108 private:
109 const Section* find_section_offset(size_t pos, uint64_t& offset) const;
110 target_info_t target_info_;
111 std::vector<const Section*> stubs_;
112 size_t pos_ = 0;
113 };
114
115 public:
116 Stub() = delete;
117 Stub(const Stub&) = default;
118 Stub& operator=(const Stub&) = default;
119
120 Stub(Stub&&) noexcept = default;
121 Stub& operator=(Stub&&) noexcept = default;
122 ~Stub() = default;
123
124 Stub(target_info_t target_info, uint64_t addr, std::vector<uint8_t> raw) :
125 target_info_(target_info),
126 address_(addr),
127 raw_(std::move(raw)) {}
128
131 return raw_;
132 }
133
135 uint64_t address() const {
136 return address_;
137 }
138
153
154 friend LIEF_API std::ostream& operator<<(std::ostream& os, const Stub& stub);
155
156 private:
157 target_info_t target_info_;
158 uint64_t address_ = 0;
159 LIEF_MAYBE_UNUSED mutable uint64_t target_addr_ = 0;
160 std::vector<uint8_t> raw_;
161};
162
163}
164
165#endif
Class which represents a MachO binary.
Definition MachO/Binary.hpp:94
CPU_TYPE
Definition MachO/Header.hpp:107
@ ANY
Definition MachO/Header.hpp:108
Class that represents a Mach-O section.
Definition MachO/Section.hpp:49
Iterator & operator-=(std::ptrdiff_t n)
Definition Stub.hpp:93
Iterator(const Iterator &)=default
friend bool operator==(const Iterator &LHS, const Iterator &RHS)
Definition Stub.hpp:98
Iterator(Iterator &&) noexcept=default
std::ptrdiff_t operator-(const Iterator &R) const
Definition Stub.hpp:84
Iterator & operator+=(std::ptrdiff_t n)
Definition Stub.hpp:88
Iterator(target_info_t target_info, std::vector< const Section * > sections, size_t pos)
Definition Stub.hpp:66
Iterator & operator=(const Iterator &)=default
friend bool operator!=(const Iterator &LHS, const Iterator &RHS)
Definition Stub.hpp:102
Stub(Stub &&) noexcept=default
Stub(const Stub &)=default
friend std::ostream & operator<<(std::ostream &os, const Stub &stub)
result< uint64_t > target() const
The address resolved by this stub.
span< const uint8_t > raw() const
The (raw) instructions of this entry as a slice of bytes.
Definition Stub.hpp:130
uint64_t address() const
The virtual address where the stub is located.
Definition Stub.hpp:135
Stub & operator=(const Stub &)=default
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
#define LIEF_MAYBE_UNUSED
Definition compiler_attributes.hpp:62
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
tcb::span< ElementType, Extent > span
Definition span.hpp:22
Definition Stub.hpp:53
uint32_t subtype
Definition Stub.hpp:55
Header::CPU_TYPE arch
Definition Stub.hpp:54
friend bool operator==(Stub::target_info_t lhs, Stub::target_info_t rhs)
Definition Stub.hpp:56
#define LIEF_API
Definition visibility.h:45