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;
56 friend bool operator==(const Stub::target_info_t& lhs,
57 const Stub::target_info_t& rhs) {
58 return lhs.arch == rhs.arch && lhs.subtype == rhs.subtype;
59 }
60 };
62 : public iterator_facade_base<Iterator, std::random_access_iterator_tag,
63 const Stub> {
64 public:
65 Iterator() = default;
66
67 Iterator(target_info_t target_info, std::vector<const Section*> sections,
68 size_t pos) :
69 target_info_(target_info),
70 stubs_(std::move(sections)),
71 pos_(pos) {}
72
73 Iterator(const Iterator&) = default;
74 Iterator& operator=(const Iterator&) = default;
75
76 Iterator(Iterator&&) noexcept = default;
77 Iterator& operator=(Iterator&&) noexcept = default;
78
79 ~Iterator() = default;
80
81 bool operator<(const Iterator& rhs) const {
82 return pos_ < rhs.pos_;
83 }
84
85 std::ptrdiff_t operator-(const Iterator& R) const {
86 return pos_ - R.pos_;
87 }
88
89 Iterator& operator+=(std::ptrdiff_t n) {
90 pos_ += n;
91 return *this;
92 }
93
94 Iterator& operator-=(std::ptrdiff_t n) {
95 pos_ -= n;
96 return *this;
97 }
98
99 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS) {
100 return LHS.pos_ == RHS.pos_;
101 }
102
103 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
104 return !(LHS == RHS);
105 }
106
108
109 private:
110 const Section* find_section_offset(size_t pos, uint64_t& offset) const;
111 target_info_t target_info_;
112 std::vector<const Section*> stubs_;
113 size_t pos_ = 0;
114 };
115
116 public:
117 Stub() = delete;
118 Stub(const Stub&) = default;
119 Stub& operator=(const Stub&) = default;
120
121 Stub(Stub&&) noexcept = default;
122 Stub& operator=(Stub&&) noexcept = default;
123 ~Stub() = default;
124
125 Stub(target_info_t target_info, uint64_t addr, std::vector<uint8_t> raw) :
126 target_info_(target_info),
127 address_(addr),
128 raw_(std::move(raw)) {}
129
132 return raw_;
133 }
134
136 uint64_t address() const {
137 return address_;
138 }
139
154
155 friend LIEF_API std::ostream& operator<<(std::ostream& os, const Stub& stub);
156
157 private:
158 target_info_t target_info_;
159 uint64_t address_ = 0;
160 LIEF_MAYBE_UNUSED mutable uint64_t target_addr_ = 0;
161 std::vector<uint8_t> raw_;
162};
163
164}
165
166#endif
Class which represents a MachO binary.
Definition MachO/Binary.hpp:94
CPU_TYPE
Definition MachO/Header.hpp:107
Class that represents a Mach-O section.
Definition MachO/Section.hpp:49
Iterator & operator-=(std::ptrdiff_t n)
Definition Stub.hpp:94
Iterator(const Iterator &)=default
friend bool operator==(const Iterator &LHS, const Iterator &RHS)
Definition Stub.hpp:99
Iterator(Iterator &&) noexcept=default
std::ptrdiff_t operator-(const Iterator &R) const
Definition Stub.hpp:85
Iterator & operator+=(std::ptrdiff_t n)
Definition Stub.hpp:89
Iterator(target_info_t target_info, std::vector< const Section * > sections, size_t pos)
Definition Stub.hpp:67
Iterator & operator=(const Iterator &)=default
friend bool operator!=(const Iterator &LHS, const Iterator &RHS)
Definition Stub.hpp:103
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:131
uint64_t address() const
The virtual address where the stub is located.
Definition Stub.hpp:136
Stub & operator=(const Stub &)=default
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterators.hpp:738
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
friend bool operator==(const Stub::target_info_t &lhs, const Stub::target_info_t &rhs)
Definition Stub.hpp:56
uint32_t subtype
Definition Stub.hpp:55
Header::CPU_TYPE arch
Definition Stub.hpp:54
#define LIEF_API
Definition visibility.h:45