LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
ThreadLocalVariables.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_MACHO_THREAD_LOCAL_VARIABLES_H
16#define LIEF_MACHO_THREAD_LOCAL_VARIABLES_H
17
18#include "LIEF/iterators.hpp"
19#include "LIEF/visibility.h"
20#include <optional>
21
23
24#include <cassert>
25#include <cstddef>
26#include <cstdint>
27#include <iterator>
28#include <memory>
29#include <ostream>
30#include <string>
31
32
33namespace LIEF::MachO {
34
45 public:
46 friend class Section;
48
50
53
54 ThreadLocalVariables& operator=(const ThreadLocalVariables&) = default;
55 ThreadLocalVariables& operator=(ThreadLocalVariables&&) noexcept = default;
56
61 struct LIEF_API Thunk {
63 uint64_t func = 0;
64
66 uint64_t key = 0;
67
69 uint64_t offset = 0;
70
71 std::string to_string() const;
72
73 friend std::ostream& operator<<(std::ostream& os, const Thunk& thunk) {
74 os << thunk.to_string();
75 return os;
76 }
77 };
78
82 : public iterator_facade_base<Iterator, std::random_access_iterator_tag,
83 const Thunk> {
84 public:
85 Iterator() = default;
86
87 Iterator(const ThreadLocalVariables& parent, size_t pos) :
88 parent_(&parent),
89 pos_(pos) {}
90
91 Iterator(const Iterator&) = default;
92 Iterator& operator=(const Iterator&) = default;
93
94 Iterator(Iterator&&) noexcept = default;
95 Iterator& operator=(Iterator&&) noexcept = default;
96
97 ~Iterator() = default;
98
99 bool operator<(const Iterator& rhs) const {
100 return pos_ < rhs.pos_;
101 }
102
103 std::ptrdiff_t operator-(const Iterator& R) const {
104 return pos_ - R.pos_;
105 }
106
107 Iterator& operator+=(std::ptrdiff_t n) {
108 pos_ += n;
109 return *this;
110 }
111
112 Iterator& operator-=(std::ptrdiff_t n) {
113 pos_ -= n;
114 return *this;
115 }
116
117 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS) {
118 assert(LHS.parent_ == RHS.parent_);
119 return LHS.pos_ == RHS.pos_;
120 }
121
122 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
123 return !(LHS == RHS);
124 }
125
126 Thunk operator*() const {
127 auto value = parent_->get(pos_);
128 assert(value);
129 return *value; // NOLINT
130 }
131
132 private:
133 const ThreadLocalVariables* parent_ = nullptr;
134 size_t pos_ = 0;
135 };
136
138
139 std::unique_ptr<Section> clone() const override {
140 return std::unique_ptr<Section>(new ThreadLocalVariables(*this));
141 }
142
146 auto B = Iterator(*this, 0);
147 auto E = Iterator(*this, nb_thunks());
148 return make_range(std::move(B), std::move(E)); // NOLINT
149 }
150
152 size_t nb_thunks() const;
153
156 std::optional<Thunk> get(size_t idx) const;
157
159 void set(size_t idx, const Thunk& thunk);
160
162 std::optional<Thunk> operator[](size_t idx) const {
163 return get(idx);
164 }
165
166 ~ThreadLocalVariables() override = default;
167
168 static bool classof(const Section* section) {
169 return section->type() == Section::TYPE::THREAD_LOCAL_VARIABLES;
170 }
171};
172
173}
174
175
176#endif
Class that represents a Mach-O section.
Definition MachO/Section.hpp:48
Section(const details::section_32 &sec)
TYPE type() const
Type of the section. This value can help to determine the purpose of the section (e....
Definition MachO/Section.hpp:218
@ THREAD_LOCAL_VARIABLES
Section with thread local variable structure data.
Definition MachO/Section.hpp:110
Random-access iterator that materializes Thunk values on the fly from the raw section content.
Definition ThreadLocalVariables.hpp:83
Thunk operator*() const
Definition ThreadLocalVariables.hpp:126
std::ptrdiff_t operator-(const Iterator &R) const
Definition ThreadLocalVariables.hpp:103
friend bool operator==(const Iterator &LHS, const Iterator &RHS)
Definition ThreadLocalVariables.hpp:117
Iterator & operator=(const Iterator &)=default
Iterator(const ThreadLocalVariables &parent, size_t pos)
Definition ThreadLocalVariables.hpp:87
Iterator & operator+=(std::ptrdiff_t n)
Definition ThreadLocalVariables.hpp:107
Iterator(Iterator &&) noexcept=default
friend bool operator!=(const Iterator &LHS, const Iterator &RHS)
Definition ThreadLocalVariables.hpp:122
Iterator & operator-=(std::ptrdiff_t n)
Definition ThreadLocalVariables.hpp:112
std::unique_ptr< Section > clone() const override
Definition ThreadLocalVariables.hpp:139
std::optional< Thunk > get(size_t idx) const
Access the Thunk at the given idx, or return an empty optional if the index is out of range.
friend class Section
Definition ThreadLocalVariables.hpp:46
static bool classof(const Section *section)
Definition ThreadLocalVariables.hpp:168
iterator_range< Iterator > thunks_it
Definition ThreadLocalVariables.hpp:137
ThreadLocalVariables(const ThreadLocalVariables &)=default
size_t nb_thunks() const
Number of Thunk descriptors in this section.
ThreadLocalVariables(ThreadLocalVariables &&) noexcept=default
void set(size_t idx, const Thunk &thunk)
Change the Thunk at the given idx.
~ThreadLocalVariables() override=default
std::optional< Thunk > operator[](size_t idx) const
Access the Thunk at the given idx.
Definition ThreadLocalVariables.hpp:162
thunks_it thunks() const
Return an iterator range over the Thunk descriptors stored in this section.
Definition ThreadLocalVariables.hpp:145
Definition iterators.hpp:680
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:35
iterator_range< T > make_range(T &&x, T &&y)
Definition iterators.hpp:752
Descriptor for a single thread-local variable.
Definition ThreadLocalVariables.hpp:61
uint64_t key
pthread_key_t key used by the runtime
Definition ThreadLocalVariables.hpp:66
uint64_t offset
Offset of the variable in the TLS block.
Definition ThreadLocalVariables.hpp:69
uint64_t func
Address of the initializer function (tlv_thunk).
Definition ThreadLocalVariables.hpp:63
friend std::ostream & operator<<(std::ostream &os, const Thunk &thunk)
Definition ThreadLocalVariables.hpp:73
#define LIEF_API
Definition visibility.h:44