LIEF: Library to Instrument Executable Formats Version 0.17.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;
35class LIEF_API Stub {
51 public:
52 struct target_info_t {
53 Header::CPU_TYPE arch;
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 };
61 class LIEF_API Iterator :
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
109 Stub operator*() const;
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 span<const uint8_t> raw() const {
134 return raw_;
135 }
136 uint64_t address() const {
139 return address_;
140 }
141 result<uint64_t> target() const;
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
Header.hpp
LIEF::MachO::Binary
Class which represents a MachO binary.
Definition MachO/Binary.hpp:86
LIEF::MachO::Section
Class that represents a Mach-O section.
Definition MachO/Section.hpp:46
LIEF::MachO::Stub::Iterator::operator-=
Iterator & operator-=(std::ptrdiff_t n)
Definition Stub.hpp:96
LIEF::MachO::Stub::Iterator::Iterator
Iterator(const Iterator &)=default
LIEF::MachO::Stub::Iterator::operator==
friend bool operator==(const Iterator &LHS, const Iterator &RHS)
Definition Stub.hpp:101
LIEF::MachO::Stub::Iterator::Iterator
Iterator(Iterator &&) noexcept=default
LIEF::MachO::Stub::Iterator::operator-
std::ptrdiff_t operator-(const Iterator &R) const
Definition Stub.hpp:87
LIEF::MachO::Stub::Iterator::operator+=
Iterator & operator+=(std::ptrdiff_t n)
Definition Stub.hpp:91
LIEF::MachO::Stub::Iterator::Iterator
Iterator(target_info_t target_info, std::vector< const Section * > sections, size_t pos)
Definition Stub.hpp:67
LIEF::MachO::Stub::Iterator::operator*
Stub operator*() const
LIEF::MachO::Stub::Iterator::operator=
Iterator & operator=(const Iterator &)=default
LIEF::MachO::Stub::Iterator::Iterator
Iterator()=default
LIEF::MachO::Stub::Iterator::operator!=
friend bool operator!=(const Iterator &LHS, const Iterator &RHS)
Definition Stub.hpp:105
LIEF::MachO::Stub
This class represents a stub entry in sections like __stubs,__auth_stubs.
Definition Stub.hpp:50
LIEF::MachO::Stub::Stub
Stub()=delete
LIEF::MachO::Stub::Stub
Stub(Stub &&) noexcept=default
LIEF::MachO::Stub::Stub
Stub(const Stub &)=default
LIEF::MachO::Stub::operator<<
friend std::ostream & operator<<(std::ostream &os, const Stub &stub)
LIEF::MachO::Stub::target
result< uint64_t > target() const
The address resolved by this stub.
LIEF::MachO::Stub::raw
span< const uint8_t > raw() const
The (raw) instructions of this entry as a slice of bytes.
Definition Stub.hpp:133
LIEF::MachO::Stub::address
uint64_t address() const
The virtual address where the stub is located.
Definition Stub.hpp:138
LIEF::MachO::Stub::operator=
Stub & operator=(const Stub &)=default
errors.hpp
iterators.hpp
LIEF::MachO
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
span.hpp
LIEF::MachO::Stub::target_info_t::operator==
friend bool operator==(const Stub::target_info_t &lhs, const Stub::target_info_t &rhs)
Definition Stub.hpp:55
LIEF::MachO::Stub::target_info_t::subtype
uint32_t subtype
Definition Stub.hpp:54
LIEF::MachO::Stub::target_info_t::arch
Header::CPU_TYPE arch
Definition Stub.hpp:53
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41