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 {
64 uint64_t func = 0;
65 uint64_t key = 0;
66 uint64_t offset = 0;
67 std::string to_string() const;
68
69 friend std::ostream& operator<<(std::ostream& os, const Thunk& thunk) {
70 os << thunk.to_string();
71 return os;
72 }
73 };
74
78 : public iterator_facade_base<Iterator, std::random_access_iterator_tag,
79 const Thunk> {
80 public:
81 Iterator() = default;
82
83 Iterator(const ThreadLocalVariables& parent, size_t pos) :
84 parent_(&parent),
85 pos_(pos) {}
86
87 Iterator(const Iterator&) = default;
88 Iterator& operator=(const Iterator&) = default;
89
90 Iterator(Iterator&&) noexcept = default;
91 Iterator& operator=(Iterator&&) noexcept = default;
92
93 ~Iterator() = default;
94
95 bool operator<(const Iterator& rhs) const {
96 return pos_ < rhs.pos_;
97 }
98
99 std::ptrdiff_t operator-(const Iterator& R) const {
100 return pos_ - R.pos_;
101 }
102
103 Iterator& operator+=(std::ptrdiff_t n) {
104 pos_ += n;
105 return *this;
106 }
107
108 Iterator& operator-=(std::ptrdiff_t n) {
109 pos_ -= n;
110 return *this;
111 }
112
113 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS) {
114 assert(LHS.parent_ == RHS.parent_);
115 return LHS.pos_ == RHS.pos_;
116 }
117
118 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
119 return !(LHS == RHS);
120 }
121
122 Thunk operator*() const {
123 auto value = parent_->get(pos_);
124 assert(value);
125 return *value;
126 }
127
128 private:
129 const ThreadLocalVariables* parent_ = nullptr;
130 size_t pos_ = 0;
131 };
132
134
135 std::unique_ptr<Section> clone() const override {
136 return std::unique_ptr<Section>(new ThreadLocalVariables(*this));
137 }
138
142 auto B = Iterator(*this, 0);
143 auto E = Iterator(*this, nb_thunks());
144 return make_range(std::move(B), std::move(E)); // NOLINT
145 }
146
148 size_t nb_thunks() const;
149
152 optional<Thunk> get(size_t idx) const;
153
155 void set(size_t idx, const Thunk& thunk);
156
158 optional<Thunk> operator[](size_t idx) const {
159 return get(idx);
160 }
161
162 ~ThreadLocalVariables() override = default;
163
164 static bool classof(const Section* section) {
165 return section->type() == Section::TYPE::THREAD_LOCAL_VARIABLES;
166 }
167};
168
169}
170}
171
172#endif
Class that represents a Mach-O section.
Definition MachO/Section.hpp:47
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:217
@ THREAD_LOCAL_VARIABLES
Section with thread local variable structure data.
Definition MachO/Section.hpp:109
Random-access iterator that materializes Thunk values on the fly from the raw section content.
Definition ThreadLocalVariables.hpp:79
Thunk operator*() const
Definition ThreadLocalVariables.hpp:122
std::ptrdiff_t operator-(const Iterator &R) const
Definition ThreadLocalVariables.hpp:99
friend bool operator==(const Iterator &LHS, const Iterator &RHS)
Definition ThreadLocalVariables.hpp:113
Iterator & operator=(const Iterator &)=default
Iterator(const ThreadLocalVariables &parent, size_t pos)
Definition ThreadLocalVariables.hpp:83
Iterator & operator+=(std::ptrdiff_t n)
Definition ThreadLocalVariables.hpp:103
Iterator(Iterator &&) noexcept=default
friend bool operator!=(const Iterator &LHS, const Iterator &RHS)
Definition ThreadLocalVariables.hpp:118
Iterator & operator-=(std::ptrdiff_t n)
Definition ThreadLocalVariables.hpp:108
std::unique_ptr< Section > clone() const override
Definition ThreadLocalVariables.hpp:135
friend class Section
Definition ThreadLocalVariables.hpp:48
static bool classof(const Section *section)
Definition ThreadLocalVariables.hpp:164
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:133
optional< Thunk > operator[](size_t idx) const
Access the Thunk at the given idx.
Definition ThreadLocalVariables.hpp:158
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:141
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterators.hpp:685
Definition iterators.hpp:567
Definition optional.hpp:23
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF namespace.
Definition Abstract/Binary.hpp:40
iterator_range< T > make_range(T &&x, T &&y)
Definition iterators.hpp:620
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:65
uint64_t offset
Offset of the variable in the TLS block.
Definition ThreadLocalVariables.hpp:66
uint64_t func
Address of the initializer function (tlv_thunk).
Definition ThreadLocalVariables.hpp:64
friend std::ostream & operator<<(std::ostream &os, const Thunk &thunk)
Definition ThreadLocalVariables.hpp:69
#define LIEF_API
Definition visibility.h:43