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