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 <ostream>
21
22#include "LIEF/visibility.h"
24#include "LIEF/ELF/enums.hpp"
25
26namespace LIEF {
27namespace ELF {
28class Parser;
29class Binary;
30class SymbolVersion;
31class Section;
32
35 friend class Parser;
36 friend class Binary;
37
38 public:
39 enum class BINDING {
40 LOCAL = 0,
41 GLOBAL,
42 WEAK,
43 GNU_UNIQUE = 10,
44 };
45
48 enum class TYPE {
49 NOTYPE = 0,
50 OBJECT,
51 FUNC,
52 SECTION,
53 FILE,
55 TLS,
56 GNU_IFUNC = 10,
57 };
58
61 enum class VISIBILITY {
62 DEFAULT = 0,
63 INTERNAL,
64 HIDDEN,
65 PROTECTED,
66 };
67
70 UNDEF = 0,
71 ABS = 0xfff1,
72 COMMON = 0xfff2,
73 };
74
75 public:
76 Symbol(std::string name) :
77 LIEF::Symbol(std::move(name), 0, 0) {}
78
79 static BINDING binding_from(uint32_t value, ARCH) {
80 return BINDING(value);
81 }
82
83 static TYPE type_from(uint32_t value, ARCH) {
84 return TYPE(value);
85 }
86
87 static uint8_t to_value(BINDING binding) {
88 return static_cast<uint8_t>(binding);
89 }
90
91 static uint8_t to_value(TYPE type) {
92 return static_cast<uint8_t>(type);
93 }
94
95 Symbol() = default;
96 ~Symbol() override = default;
97
101
103 TYPE type() const {
104 return type_;
105 }
106
108 BINDING binding() const {
109 return binding_;
110 }
111
113 uint8_t information() const;
114
116 uint8_t other() const {
117 return other_;
118 }
119
121 uint16_t section_idx() const {
122 return shndx();
123 }
124
127 return VISIBILITY(other_);
128 }
129
132 return section_;
133 }
134
136 return section_;
137 }
138
151 uint64_t value() const override {
152 return value_;
153 }
154
160 uint64_t size() const override {
161 return size_;
162 }
163
165 uint16_t shndx() const {
166 return shndx_;
167 }
168
170 bool has_version() const {
171 return symbol_version_ != nullptr;
172 }
173
177 return symbol_version_;
178 }
179
181 return symbol_version_;
182 }
183
184 bool is_local() const {
185 return binding() == BINDING::LOCAL;
186 }
187
188 bool is_global() const {
189 return binding() == BINDING::GLOBAL;
190 }
191
192 bool is_weak() const {
193 return binding() == BINDING::WEAK;
194 }
195
197 std::string demangled_name() const;
198
199 void type(TYPE type) {
200 type_ = type;
201 }
202
204 binding_ = binding;
205 }
206
207 void other(uint8_t other) {
208 other_ = other;
209 }
210
212 other_ = static_cast<uint8_t>(visibility);
213 }
214
215 void information(uint8_t info);
216
217 void shndx(uint16_t idx) {
218 shndx_ = idx;
219 }
220
221 void value(uint64_t value) override {
222 value_ = value;
223 }
224
225 void size(uint64_t size) override {
226 size_ = size;
227 }
228
230 bool is_exported() const;
231
233 void set_exported(bool flag = true);
234
236 bool is_imported() const;
237
239 void set_imported(bool flag = true);
240
242 bool is_static() const {
243 return this->binding() == BINDING::GLOBAL;
244 }
245
247 bool is_function() const {
248 return this->type() == TYPE::FUNC;
249 }
250
252 bool is_variable() const {
253 return this->type() == TYPE::OBJECT;
254 }
255
256 void accept(Visitor& visitor) const override;
257
258 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Symbol& entry);
259
260 private:
261 template<class T>
262 LIEF_API Symbol(const T& header, ARCH arch);
263
264 TYPE type_ = TYPE::NOTYPE;
265 BINDING binding_ = BINDING::LOCAL;
266 uint8_t other_ = 0;
267 uint16_t shndx_ = 0;
268 Section* section_ = nullptr;
269 SymbolVersion* symbol_version_ = nullptr;
270 ARCH arch_ = ARCH::NONE;
271};
272
273LIEF_API const char* to_string(Symbol::BINDING binding);
276}
277}
278#endif
Class which represents an ELF binary.
Definition ELF/Binary.hpp:60
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:44
Class which represents an ELF Section.
Definition ELF/Section.hpp:49
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:180
void shndx(uint16_t idx)
Definition ELF/Symbol.hpp:217
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:116
~Symbol() override=default
void type(TYPE type)
Definition ELF/Symbol.hpp:199
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:203
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:103
uint64_t size() const override
Symbol size.
Definition ELF/Symbol.hpp:160
void value(uint64_t value) override
Definition ELF/Symbol.hpp:221
BINDING
Definition ELF/Symbol.hpp:39
@ WEAK
Weak symbol.
Definition ELF/Symbol.hpp:42
@ LOCAL
Local symbol.
Definition ELF/Symbol.hpp:40
@ GLOBAL
Global symbol.
Definition ELF/Symbol.hpp:41
bool is_variable() const
True if the symbol represents a variable.
Definition ELF/Symbol.hpp:252
const Section * section() const
Definition ELF/Symbol.hpp:135
BINDING binding() const
The symbol's binding determines the linkage visibility and behavior.
Definition ELF/Symbol.hpp:108
SECTION_INDEX
Special section indices.
Definition ELF/Symbol.hpp:69
@ COMMON
Associated symbol is common.
Definition ELF/Symbol.hpp:72
@ ABS
Associated symbol is absolute.
Definition ELF/Symbol.hpp:71
@ UNDEF
Undefined section.
Definition ELF/Symbol.hpp:70
void swap(Symbol &other)
Symbol(std::string name)
Definition ELF/Symbol.hpp:76
uint16_t shndx() const
Definition ELF/Symbol.hpp:165
bool is_local() const
Definition ELF/Symbol.hpp:184
friend class Binary
Definition ELF/Symbol.hpp:36
bool is_global() const
Definition ELF/Symbol.hpp:188
void size(uint64_t size) override
Definition ELF/Symbol.hpp:225
Symbol & operator=(Symbol other)
friend class Parser
Definition ELF/Symbol.hpp:35
uint16_t section_idx() const
ELF::Section index associated with the symbol.
Definition ELF/Symbol.hpp:121
TYPE
Type of the symbol. This enum matches the STT_xxx values of the ELF specs.
Definition ELF/Symbol.hpp:48
@ NOTYPE
Symbol's type is not specified.
Definition ELF/Symbol.hpp:49
@ FUNC
Symbol is executable code (function, etc.).
Definition ELF/Symbol.hpp:51
@ OBJECT
Symbol is a data object (variable, array, etc.).
Definition ELF/Symbol.hpp:50
void visibility(VISIBILITY visibility)
Definition ELF/Symbol.hpp:211
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:79
Section * section()
Section associated with the symbol or a nullptr if it does not exist.
Definition ELF/Symbol.hpp:131
bool is_weak() const
Definition ELF/Symbol.hpp:192
bool has_version() const
Check if this symbols has a symbol version .
Definition ELF/Symbol.hpp:170
static TYPE type_from(uint32_t value, ARCH)
Definition ELF/Symbol.hpp:83
void other(uint8_t other)
Definition ELF/Symbol.hpp:207
void information(uint8_t info)
VISIBILITY visibility() const
Symbol visibility.
Definition ELF/Symbol.hpp:126
SymbolVersion * symbol_version()
Return the SymbolVersion associated with this symbol. If there is no symbol version,...
Definition ELF/Symbol.hpp:176
static uint8_t to_value(TYPE type)
Definition ELF/Symbol.hpp:91
uint64_t value() const override
This member has slightly different interpretations:
Definition ELF/Symbol.hpp:151
bool is_static() const
True if the symbol is a static one.
Definition ELF/Symbol.hpp:242
bool is_function() const
True if the symbol represents a function.
Definition ELF/Symbol.hpp:247
static uint8_t to_value(BINDING binding)
Definition ELF/Symbol.hpp:87
VISIBILITY
Visibility of the symbol. This enum matches the STV_xxx values of the official ELF specs.
Definition ELF/Symbol.hpp:61
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
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
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:41
#define LIEF_API
Definition visibility.h:45