LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
ELF/Symbol.hpp
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_ELF_SYMBOL_H
17#define LIEF_ELF_SYMBOL_H
18
19#include <string>
20#include <vector>
21#include <ostream>
22
23#include "LIEF/visibility.h"
24#include "LIEF/Abstract/Symbol.hpp"
25#include "LIEF/ELF/enums.hpp"
26
27namespace LIEF {
28namespace ELF {
29class Parser;
30class Binary;
31class SymbolVersion;
32class Section;
33
35class LIEF_API Symbol : public LIEF::Symbol {
36 friend class Parser;
37 friend class Binary;
38 public:
39
40 enum class BINDING {
41 LOCAL = 0,
42 GLOBAL = 1,
43 WEAK = 2,
44 GNU_UNIQUE = 10,
45 };
46
49 enum class TYPE {
50 NOTYPE = 0,
51 OBJECT = 1,
52 FUNC = 2,
53 SECTION = 3,
54 FILE = 4,
55 COMMON = 5,
56 TLS = 6,
57 GNU_IFUNC = 10,
58 };
59
62 enum class VISIBILITY {
63 DEFAULT = 0,
64 INTERNAL = 1,
65 HIDDEN = 2,
66 PROTECTED = 3
67 };
68
71 UNDEF = 0,
72 ABS = 0xfff1,
73 COMMON = 0xfff2,
74 };
75
76 public:
77 Symbol(std::string name):
78 LIEF::Symbol(std::move(name), 0, 0)
79 {}
80
81 static BINDING binding_from(uint32_t value, ARCH) {
82 return BINDING(value);
83 }
84
85 static TYPE type_from(uint32_t value, ARCH) {
86 return TYPE(value);
87 }
88
89 static uint8_t to_value(BINDING binding) {
90 return static_cast<uint8_t>(binding);
91 }
92
93 static uint8_t to_value(TYPE type) {
94 return static_cast<uint8_t>(type);
95 }
96
97 Symbol() = default;
98 ~Symbol() override = default;
99
100 Symbol& operator=(Symbol other);
101 Symbol(const Symbol& other);
102 void swap(Symbol& other);
103
105 TYPE type() const {
106 return type_;
107 }
108
110 BINDING binding() const {
111 return binding_;
112 }
113
115 uint8_t information() const;
116
118 uint8_t other() const {
119 return other_;
120 }
121
123 uint16_t section_idx() const {
124 return shndx();
125 }
126
129 return VISIBILITY(other_);
130 }
131
134 return section_;
135 }
136
137 const Section* section() const {
138 return section_;
139 }
140
149 uint64_t value() const override {
150 return value_;
151 }
152
158 uint64_t size() const override {
159 return size_;
160 }
161
163 uint16_t shndx() const {
164 return shndx_;
165 }
166
168 bool has_version() const {
169 return symbol_version_ != nullptr;
170 }
171
175 return symbol_version_;
176 }
177
178 const SymbolVersion* symbol_version() const {
179 return symbol_version_;
180 }
181
182 bool is_local() const {
183 return binding() == BINDING::LOCAL;
184 }
185
186 bool is_global() const {
187 return binding() == BINDING::GLOBAL;
188 }
189
190 bool is_weak() const {
191 return binding() == BINDING::WEAK;
192 }
193
195 std::string demangled_name() const;
196
197 void type(TYPE type) {
198 type_ = type;
199 }
200
201 void binding(BINDING binding) {
202 binding_ = binding;
203 }
204
205 void other(uint8_t other) {
206 other_ = other;
207 }
208
209 void visibility(VISIBILITY visibility) {
210 other_ = static_cast<uint8_t>(visibility);
211 }
212
213 void information(uint8_t info);
214
215 void shndx(uint16_t idx) {
216 shndx_ = idx;
217 }
218
219 void value(uint64_t value) override {
220 value_ = value;
221 }
222
223 void size(uint64_t size) override {
224 size_ = size;
225 }
226
228 bool is_exported() const;
229
231 void set_exported(bool flag = true);
232
234 bool is_imported() const;
235
237 void set_imported(bool flag = true);
238
240 bool is_static() const {
241 return this->binding() == BINDING::GLOBAL;
242 }
243
245 bool is_function() const {
246 return this->type() == TYPE::FUNC;
247 }
248
250 bool is_variable() const {
251 return this->type() == TYPE::OBJECT;
252 }
253
254 void accept(Visitor& visitor) const override;
255
256 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Symbol& entry);
257
258 private:
259 template<class T>
260 LIEF_API Symbol(const T& header, ARCH arch);
261
262 TYPE type_ = TYPE::NOTYPE;
263 BINDING binding_ = BINDING::LOCAL;
264 uint8_t other_ = 0;
265 uint16_t shndx_ = 0;
266 Section* section_ = nullptr;
267 SymbolVersion* symbol_version_ = nullptr;
268 ARCH arch_ = ARCH::NONE;
269};
270
271LIEF_API const char* to_string(Symbol::BINDING binding);
272LIEF_API const char* to_string(Symbol::TYPE type);
273LIEF_API const char* to_string(Symbol::VISIBILITY viz);
274}
275}
276#endif /* _ELF_SYMBOL_H */
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
Class wich represents an ELF Section.
Definition ELF/Section.hpp:46
Class which represents an entry defined in the DT_VERSYM dynamic entry.
Definition SymbolVersion.hpp:32
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:35
void set_imported(bool flag=true)
Set whether or not the symbol is imported.
uint8_t other() const
Alias for visibility()
Definition ELF/Symbol.hpp:118
std::string demangled_name() const
Symbol's unmangled name. If not available, it returns an empty string.
bool is_exported() const
Check if the current symbol is exported.
uint8_t information() const
This member specifies the symbol's type and binding attributes.
TYPE type() const
The symbol's type provides a general classification for the associated entity.
Definition ELF/Symbol.hpp:105
uint64_t size() const override
Symbol size.
Definition ELF/Symbol.hpp:158
BINDING
Definition ELF/Symbol.hpp:40
bool is_variable() const
True if the symbol represent a variable.
Definition ELF/Symbol.hpp:250
BINDING binding() const
The symbol's binding determines the linkage visibility and behavior.
Definition ELF/Symbol.hpp:110
SECTION_INDEX
Special section indices.
Definition ELF/Symbol.hpp:70
uint16_t shndx() const
Definition ELF/Symbol.hpp:163
uint16_t section_idx() const
ELF::Section index associated with the symbol.
Definition ELF/Symbol.hpp:123
TYPE
Type of the symbol. This enum matches the STT_xxx values of the ELF specs.
Definition ELF/Symbol.hpp:49
void set_exported(bool flag=true)
Set whether or not the symbol is exported.
bool is_imported() const
Check if the current symbol is imported.
Section * section()
Section associated with the symbol or a nullptr if it does not exist.
Definition ELF/Symbol.hpp:133
bool has_version() const
Check if this symbols has a symbol version .
Definition ELF/Symbol.hpp:168
VISIBILITY visibility() const
Symbol visibility.
Definition ELF/Symbol.hpp:128
SymbolVersion * symbol_version()
Return the SymbolVersion associated with this symbol. If there is no symbol version,...
Definition ELF/Symbol.hpp:174
uint64_t value() const override
This member has slightly different interpretations:
Definition ELF/Symbol.hpp:149
bool is_static() const
True if the symbol is a static one.
Definition ELF/Symbol.hpp:240
bool is_function() const
True if the symbol represent a function.
Definition ELF/Symbol.hpp:245
VISIBILITY
Visibility of the symbol. This enum matches the STV_xxx values of the official ELF specs.
Definition ELF/Symbol.hpp:62
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:28
Definition Visitor.hpp:219
ARCH
Machine architectures See current registered ELF machine architectures at: http://www....
Definition ELF/enums.hpp:30
LIEF namespace.
Definition Abstract/Binary.hpp:32