LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
Stub.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_STUB_H
17#define LIEF_MACHO_STUB_H
18#include "LIEF/visibility.h"
19#include "LIEF/span.hpp"
20#include "LIEF/iterators.hpp"
21#include "LIEF/errors.hpp"
22
23#include "LIEF/MachO/Header.hpp"
24
25#include <vector>
26#include <cstdint>
27#include <ostream>
28
29
30namespace LIEF {
31namespace MachO {
32
33class Binary;
34class Section;
35
51 public:
54 uint32_t subtype = 0;
55 friend bool operator==(const Stub::target_info_t& lhs,
56 const Stub::target_info_t& rhs)
57 {
58 return lhs.arch == rhs.arch && lhs.subtype == rhs.subtype;
59 }
60 };
62 public iterator_facade_base<Iterator, std::random_access_iterator_tag, const Stub>
63 {
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 }
74
75 Iterator(const Iterator&) = default;
76 Iterator& operator=(const Iterator&) = default;
77
78 Iterator(Iterator&&) noexcept = default;
79 Iterator& operator=(Iterator&&) noexcept = default;
80
81 ~Iterator() = default;
82
83 bool operator<(const Iterator& rhs) const {
84 return pos_ < rhs.pos_;
85 }
86
87 std::ptrdiff_t operator-(const Iterator& R) const {
88 return pos_ - R.pos_;
89 }
90
91 Iterator& operator+=(std::ptrdiff_t n) {
92 pos_ += n;
93 return *this;
94 }
95
96 Iterator& operator-=(std::ptrdiff_t n) {
97 pos_ -= n;
98 return *this;
99 }
100
101 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS) {
102 return LHS.pos_ == RHS.pos_;
103 }
104
105 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
106 return !(LHS == RHS);
107 }
108
110
111 private:
112 const Section* find_section_offset(size_t pos, uint64_t& offset) const;
113 target_info_t target_info_;
114 std::vector<const Section*> stubs_;
115 size_t pos_ = 0;
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 {}
131
134 return raw_;
135 }
136
138 uint64_t address() const {
139 return address_;
140 }
141
156
157 friend LIEF_API std::ostream& operator<<(std::ostream& os, const Stub& stub);
158
159 private:
160 target_info_t target_info_;
161 uint64_t address_ = 0;
162 mutable uint64_t target_addr_ = 0;
163 std::vector<uint8_t> raw_;
164};
165
166}
167}
168#endif
CPU_TYPE
Definition MachO/Header.hpp:97
Class that represents a Mach-O section.
Definition MachO/Section.hpp:46
Definition Stub.hpp:63
Iterator & operator-=(std::ptrdiff_t n)
Definition Stub.hpp:96
Iterator(const Iterator &)=default
friend bool operator==(const Iterator &LHS, const Iterator &RHS)
Definition Stub.hpp:101
Iterator(Iterator &&) noexcept=default
std::ptrdiff_t operator-(const Iterator &R) const
Definition Stub.hpp:87
Iterator & operator+=(std::ptrdiff_t n)
Definition Stub.hpp:91
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:105
This class represents a stub entry in sections like __stubs,__auth_stubs.
Definition Stub.hpp:50
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:133
uint64_t address() const
The virtual address where the stub is located.
Definition Stub.hpp:138
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:590
LIEF namespace.
Definition Abstract/Binary.hpp:32
tcb::span< ElementType, Extent > span
Definition span.hpp:22
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:75
Definition Stub.hpp:52
friend bool operator==(const Stub::target_info_t &lhs, const Stub::target_info_t &rhs)
Definition Stub.hpp:55
uint32_t subtype
Definition Stub.hpp:54
Header::CPU_TYPE arch
Definition Stub.hpp:53
#define LIEF_API
Definition visibility.h:41