LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
ELF/Symbol.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_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"
25#include "LIEF/ELF/enums.hpp"
26
27namespace LIEF {
28namespace ELF {
29class Parser;
30class Binary;
31class SymbolVersion;
32class Section;
33
36 friend class Parser;
37 friend class Binary;
38
39 public:
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 static BINDING binding_from(uint32_t value, ARCH) {
81 return BINDING(value);
82 }
83
84 static TYPE type_from(uint32_t value, ARCH) {
85 return TYPE(value);
86 }
87
88 static uint8_t to_value(BINDING binding) {
89 return static_cast<uint8_t>(binding);
90 }
91
92 static uint8_t to_value(TYPE type) {
93 return static_cast<uint8_t>(type);
94 }
95
96 Symbol() = default;
97 ~Symbol() override = default;
98
102
104 TYPE type() const {
105 return type_;
106 }
107
109 BINDING binding() const {
110 return binding_;
111 }
112
114 uint8_t information() const;
115
117 uint8_t other() const {
118 return other_;
119 }
120
122 uint16_t section_idx() const {
123 return shndx();
124 }
125
128 return VISIBILITY(other_);
129 }
130
133 return section_;
134 }
135
136 const Section* section() const {
137 return section_;
138 }
139
152 uint64_t value() const override {
153 return value_;
154 }
155
161 uint64_t size() const override {
162 return size_;
163 }
164
166 uint16_t shndx() const {
167 return shndx_;
168 }
169
171 bool has_version() const {
172 return symbol_version_ != nullptr;
173 }
174
178 return symbol_version_;
179 }
180
182 return symbol_version_;
183 }
184
185 bool is_local() const {
186 return binding() == BINDING::LOCAL;
187 }
188
189 bool is_global() const {
190 return binding() == BINDING::GLOBAL;
191 }
192
193 bool is_weak() const {
194 return binding() == BINDING::WEAK;
195 }
196
198 std::string demangled_name() const;
199
200 void type(TYPE type) {
201 type_ = type;
202 }
203
205 binding_ = binding;
206 }
207
208 void other(uint8_t other) {
209 other_ = other;
210 }
211
213 other_ = static_cast<uint8_t>(visibility);
214 }
215
216 void information(uint8_t info);
217
218 void shndx(uint16_t idx) {
219 shndx_ = idx;
220 }
221
222 void value(uint64_t value) override {
223 value_ = value;
224 }
225
226 void size(uint64_t size) override {
227 size_ = size;
228 }
229
231 bool is_exported() const;
232
234 void set_exported(bool flag = true);
235
237 bool is_imported() const;
238
240 void set_imported(bool flag = true);
241
243 bool is_static() const {
244 return this->binding() == BINDING::GLOBAL;
245 }
246
248 bool is_function() const {
249 return this->type() == TYPE::FUNC;
250 }
251
253 bool is_variable() const {
254 return this->type() == TYPE::OBJECT;
255 }
256
257 void accept(Visitor& visitor) const override;
258
259 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Symbol& entry);
260
261 private:
262 template<class T>
263 LIEF_API Symbol(const T& header, ARCH arch);
264
265 TYPE type_ = TYPE::NOTYPE;
266 BINDING binding_ = BINDING::LOCAL;
267 uint8_t other_ = 0;
268 uint16_t shndx_ = 0;
269 Section* section_ = nullptr;
270 SymbolVersion* symbol_version_ = nullptr;
271 ARCH arch_ = ARCH::NONE;
272};
273
274LIEF_API const char* to_string(Symbol::BINDING binding);
277}
278}
279#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 which represents an ELF Section.
Definition ELF/Section.hpp:48
Class which represents an entry defined in the DT_VERSYM dynamic entry.
Definition SymbolVersion.hpp:33
void accept(Visitor &visitor) const override
Symbol(const Symbol &other)
const SymbolVersion * symbol_version() const
Definition ELF/Symbol.hpp:181
void shndx(uint16_t idx)
Definition ELF/Symbol.hpp:218
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:117
~Symbol() override=default
void type(TYPE type)
Definition ELF/Symbol.hpp:200
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.
void binding(BINDING binding)
Definition ELF/Symbol.hpp:204
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:104
uint64_t size() const override
Symbol size.
Definition ELF/Symbol.hpp:161
void value(uint64_t value) override
Definition ELF/Symbol.hpp:222
BINDING
Definition ELF/Symbol.hpp:40
@ WEAK
Weak symbol.
Definition ELF/Symbol.hpp:43
@ LOCAL
Local symbol.
Definition ELF/Symbol.hpp:41
@ GLOBAL
Global symbol.
Definition ELF/Symbol.hpp:42
bool is_variable() const
True if the symbol represent a variable.
Definition ELF/Symbol.hpp:253
const Section * section() const
Definition ELF/Symbol.hpp:136
BINDING binding() const
The symbol's binding determines the linkage visibility and behavior.
Definition ELF/Symbol.hpp:109
SECTION_INDEX
Special section indices.
Definition ELF/Symbol.hpp:70
@ COMMON
Associated symbol is common.
Definition ELF/Symbol.hpp:73
@ ABS
Associated symbol is absolute.
Definition ELF/Symbol.hpp:72
@ UNDEF
Undefined section.
Definition ELF/Symbol.hpp:71
void swap(Symbol &other)
Symbol(std::string name)
Definition ELF/Symbol.hpp:77
uint16_t shndx() const
Definition ELF/Symbol.hpp:166
bool is_local() const
Definition ELF/Symbol.hpp:185
friend class Binary
Definition ELF/Symbol.hpp:37
bool is_global() const
Definition ELF/Symbol.hpp:189
void size(uint64_t size) override
Definition ELF/Symbol.hpp:226
Symbol & operator=(Symbol other)
friend class Parser
Definition ELF/Symbol.hpp:36
uint16_t section_idx() const
ELF::Section index associated with the symbol.
Definition ELF/Symbol.hpp:122
TYPE
Type of the symbol. This enum matches the STT_xxx values of the ELF specs.
Definition ELF/Symbol.hpp:49
@ NOTYPE
Symbol's type is not specified.
Definition ELF/Symbol.hpp:50
@ FUNC
Symbol is executable code (function, etc.).
Definition ELF/Symbol.hpp:52
@ OBJECT
Symbol is a data object (variable, array, etc.).
Definition ELF/Symbol.hpp:51
void visibility(VISIBILITY visibility)
Definition ELF/Symbol.hpp:212
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.
static BINDING binding_from(uint32_t value, ARCH)
Definition ELF/Symbol.hpp:80
Section * section()
Section associated with the symbol or a nullptr if it does not exist.
Definition ELF/Symbol.hpp:132
bool is_weak() const
Definition ELF/Symbol.hpp:193
bool has_version() const
Check if this symbols has a symbol version .
Definition ELF/Symbol.hpp:171
static TYPE type_from(uint32_t value, ARCH)
Definition ELF/Symbol.hpp:84
void other(uint8_t other)
Definition ELF/Symbol.hpp:208
void information(uint8_t info)
VISIBILITY visibility() const
Symbol visibility.
Definition ELF/Symbol.hpp:127
SymbolVersion * symbol_version()
Return the SymbolVersion associated with this symbol. If there is no symbol version,...
Definition ELF/Symbol.hpp:177
static uint8_t to_value(TYPE type)
Definition ELF/Symbol.hpp:92
uint64_t value() const override
This member has slightly different interpretations:
Definition ELF/Symbol.hpp:152
bool is_static() const
True if the symbol is a static one.
Definition ELF/Symbol.hpp:243
bool is_function() const
True if the symbol represent a function.
Definition ELF/Symbol.hpp:248
static uint8_t to_value(BINDING binding)
Definition ELF/Symbol.hpp:88
VISIBILITY
Visibility of the symbol. This enum matches the STV_xxx values of the official ELF specs.
Definition ELF/Symbol.hpp:62
friend std::ostream & operator<<(std::ostream &os, const Symbol &entry)
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:28
virtual const std::string & name() const
Return the symbol's name.
Definition Abstract/Symbol.hpp:54
Definition Visitor.hpp:212
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
const char * to_string(DynamicEntry::TAG e)
ARCH
Definition ELF/enums.hpp:30
@ NONE
Definition ELF/enums.hpp:31
LIEF namespace.
Definition Abstract/Binary.hpp:40
Definition string.h:155
#define LIEF_API
Definition visibility.h:43