LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
ThreadLocalVariables.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_THREAD_LOCAL_VARIABLES_H
17#define LIEF_MACHO_THREAD_LOCAL_VARIABLES_H
18
19#include "LIEF/visibility.h"
20#include "LIEF/iterators.hpp"
21#include "LIEF/optional.hpp"
22
24
25#include <cassert>
26#include <cstddef>
27#include <cstdint>
28#include <iterator>
29#include <memory>
30#include <ostream>
31#include <string>
32
33namespace LIEF {
34
35namespace MachO {
36
47 public:
48 friend class Section;
50
52
55
56 ThreadLocalVariables& operator=(const ThreadLocalVariables&) = default;
57 ThreadLocalVariables& operator=(ThreadLocalVariables&&) noexcept = default;
58
63 struct LIEF_API Thunk {
65 uint64_t func = 0;
66
68 uint64_t key = 0;
69
71 uint64_t offset = 0;
72
73 std::string to_string() const;
74
75 friend std::ostream& operator<<(std::ostream& os, const Thunk& thunk) {
76 os << thunk.to_string();
77 return os;
78 }
79 };
80
84 : public iterator_facade_base<Iterator, std::random_access_iterator_tag,
85 const Thunk> {
86 public:
87 Iterator() = default;
88
89 Iterator(const ThreadLocalVariables& parent, size_t pos) :
90 parent_(&parent),
91 pos_(pos) {}
92
93 Iterator(const Iterator&) = default;
94 Iterator& operator=(const Iterator&) = default;
95
96 Iterator(Iterator&&) noexcept = default;
97 Iterator& operator=(Iterator&&) noexcept = default;
98
99 ~Iterator() = default;
100
101 bool operator<(const Iterator& rhs) const {
102 return pos_ < rhs.pos_;
103 }
104
105 std::ptrdiff_t operator-(const Iterator& R) const {
106 return pos_ - R.pos_;
107 }
108
109 Iterator& operator+=(std::ptrdiff_t n) {
110 pos_ += n;
111 return *this;
112 }
113
114 Iterator& operator-=(std::ptrdiff_t n) {
115 pos_ -= n;
116 return *this;
117 }
118
119 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS) {
120 assert(LHS.parent_ == RHS.parent_);
121 return LHS.pos_ == RHS.pos_;
122 }
123
124 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
125 return !(LHS == RHS);
126 }
127
128 Thunk operator*() const {
129 auto value = parent_->get(pos_);
130 assert(value);
131 return *value;
132 }
133
134 private:
135 const ThreadLocalVariables* parent_ = nullptr;
136 size_t pos_ = 0;
137 };
138
140
141 std::unique_ptr<Section> clone() const override {
142 return std::unique_ptr<Section>(new ThreadLocalVariables(*this));
143 }
144
148 auto B = Iterator(*this, 0);
149 auto E = Iterator(*this, nb_thunks());
150 return make_range(std::move(B), std::move(E)); // NOLINT
151 }
152
154 size_t nb_thunks() const;
155
158 optional<Thunk> get(size_t idx) const;
159
161 void set(size_t idx, const Thunk& thunk);
162
164 optional<Thunk> operator[](size_t idx) const {
165 return get(idx);
166 }
167
168 ~ThreadLocalVariables() override = default;
169
170 static bool classof(const Section* section) {
171 return section->type() == Section::TYPE::THREAD_LOCAL_VARIABLES;
172 }
173};
174
175}
176}
177
178#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:85
Thunk operator*() const
Definition ThreadLocalVariables.hpp:128
std::ptrdiff_t operator-(const Iterator &R) const
Definition ThreadLocalVariables.hpp:105
friend bool operator==(const Iterator &LHS, const Iterator &RHS)
Definition ThreadLocalVariables.hpp:119
Iterator & operator=(const Iterator &)=default
Iterator(const ThreadLocalVariables &parent, size_t pos)
Definition ThreadLocalVariables.hpp:89
Iterator & operator+=(std::ptrdiff_t n)
Definition ThreadLocalVariables.hpp:109
Iterator(Iterator &&) noexcept=default
friend bool operator!=(const Iterator &LHS, const Iterator &RHS)
Definition ThreadLocalVariables.hpp:124
Iterator & operator-=(std::ptrdiff_t n)
Definition ThreadLocalVariables.hpp:114
std::unique_ptr< Section > clone() const override
Definition ThreadLocalVariables.hpp:141
friend class Section
Definition ThreadLocalVariables.hpp:48
static bool classof(const Section *section)
Definition ThreadLocalVariables.hpp:170
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.
iterator_range< Iterator > thunks_it
Definition ThreadLocalVariables.hpp:139
optional< Thunk > operator[](size_t idx) const
Access the Thunk at the given idx.
Definition ThreadLocalVariables.hpp:164
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
thunks_it thunks() const
Return an iterator range over the Thunk descriptors stored in this section.
Definition ThreadLocalVariables.hpp:147
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterators.hpp:750
Definition iterators.hpp:603
Definition optional.hpp:23
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF namespace.
Definition Abstract/Binary.hpp:41
iterator_range< T > make_range(T &&x, T &&y)
Definition iterators.hpp:675
Descriptor for a single thread-local variable.
Definition ThreadLocalVariables.hpp:63
uint64_t key
pthread_key_t key used by the runtime
Definition ThreadLocalVariables.hpp:68
uint64_t offset
Offset of the variable in the TLS block.
Definition ThreadLocalVariables.hpp:71
uint64_t func
Address of the initializer function (tlv_thunk).
Definition ThreadLocalVariables.hpp:65
friend std::ostream & operator<<(std::ostream &os, const Thunk &thunk)
Definition ThreadLocalVariables.hpp:75
#define LIEF_API
Definition visibility.h:45