LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
ELF/Symbol.hpp
Go to the documentation of this file.
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"
25#include "LIEF/ELF/enums.hpp"
26
27namespace LIEF {
28namespace ELF {
29class Parser;
30class Binary;
31class SymbolVersion;
32class Section;
33class 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 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 enum class VISIBILITY {
63 DEFAULT = 0,
64 INTERNAL = 1,
65 HIDDEN = 2,
66 PROTECTED = 3
67 };
68 enum SECTION_INDEX {
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 TYPE type() const {
106 return type_;
107 }
108 BINDING binding() const {
111 return binding_;
112 }
113 uint8_t information() const;
116 uint8_t other() const {
119 return other_;
120 }
121 uint16_t section_idx() const {
124 return shndx();
125 }
126 VISIBILITY visibility() const {
129 return VISIBILITY(other_);
130 }
131 Section* section() {
134 return section_;
135 }
136
137 const Section* section() const {
138 return section_;
139 }
140 uint64_t value() const override {
150 return value_;
151 }
152 uint64_t size() const override {
159 return size_;
160 }
161 uint16_t shndx() const {
164 return shndx_;
165 }
166 bool has_version() const {
169 return symbol_version_ != nullptr;
170 }
171 SymbolVersion* symbol_version() {
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 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 bool is_exported() const;
229 void set_exported(bool flag = true);
232 bool is_imported() const;
235 void set_imported(bool flag = true);
238 bool is_static() const {
241 return this->binding() == BINDING::GLOBAL;
242 }
243 bool is_function() const {
246 return this->type() == TYPE::FUNC;
247 }
248 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 */
Symbol.hpp
enums.hpp
LIEF::ELF::Binary
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
LIEF::ELF::Parser
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
LIEF::ELF::Section
Class wich represents an ELF Section.
Definition ELF/Section.hpp:48
LIEF::ELF::SymbolVersion
Class which represents an entry defined in the DT_VERSYM dynamic entry.
Definition SymbolVersion.hpp:32
LIEF::ELF::Symbol
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:35
LIEF::ELF::Symbol::accept
void accept(Visitor &visitor) const override
LIEF::ELF::Symbol::Symbol
Symbol(const Symbol &other)
LIEF::ELF::Symbol::symbol_version
const SymbolVersion * symbol_version() const
Definition ELF/Symbol.hpp:178
LIEF::ELF::Symbol::shndx
void shndx(uint16_t idx)
Definition ELF/Symbol.hpp:215
LIEF::ELF::Symbol::set_imported
void set_imported(bool flag=true)
Set whether or not the symbol is imported.
LIEF::ELF::Symbol::other
uint8_t other() const
Alias for visibility()
Definition ELF/Symbol.hpp:118
LIEF::ELF::Symbol::~Symbol
~Symbol() override=default
LIEF::ELF::Symbol::type
void type(TYPE type)
Definition ELF/Symbol.hpp:197
LIEF::ELF::Symbol::demangled_name
std::string demangled_name() const
Symbol's unmangled name. If not available, it returns an empty string.
LIEF::ELF::Symbol::is_exported
bool is_exported() const
Check if the current symbol is exported.
LIEF::ELF::Symbol::binding
void binding(BINDING binding)
Definition ELF/Symbol.hpp:201
LIEF::ELF::Symbol::information
uint8_t information() const
This member specifies the symbol's type and binding attributes.
LIEF::ELF::Symbol::type
TYPE type() const
The symbol's type provides a general classification for the associated entity.
Definition ELF/Symbol.hpp:105
LIEF::ELF::Symbol::size
uint64_t size() const override
Symbol size.
Definition ELF/Symbol.hpp:158
LIEF::ELF::Symbol::value
void value(uint64_t value) override
Definition ELF/Symbol.hpp:219
LIEF::ELF::Symbol::BINDING
BINDING
Definition ELF/Symbol.hpp:40
LIEF::ELF::Symbol::is_variable
bool is_variable() const
True if the symbol represent a variable.
Definition ELF/Symbol.hpp:250
LIEF::ELF::Symbol::section
const Section * section() const
Definition ELF/Symbol.hpp:137
LIEF::ELF::Symbol::binding
BINDING binding() const
The symbol's binding determines the linkage visibility and behavior.
Definition ELF/Symbol.hpp:110
LIEF::ELF::Symbol::swap
void swap(Symbol &other)
LIEF::ELF::Symbol::Symbol
Symbol(std::string name)
Definition ELF/Symbol.hpp:77
LIEF::ELF::Symbol::Symbol
Symbol()=default
LIEF::ELF::Symbol::shndx
uint16_t shndx() const
Definition ELF/Symbol.hpp:163
LIEF::ELF::Symbol::is_local
bool is_local() const
Definition ELF/Symbol.hpp:182
LIEF::ELF::Symbol::is_global
bool is_global() const
Definition ELF/Symbol.hpp:186
LIEF::ELF::Symbol::size
void size(uint64_t size) override
Definition ELF/Symbol.hpp:223
LIEF::ELF::Symbol::operator=
Symbol & operator=(Symbol other)
LIEF::ELF::Symbol::section_idx
uint16_t section_idx() const
ELF::Section index associated with the symbol.
Definition ELF/Symbol.hpp:123
LIEF::ELF::Symbol::TYPE
TYPE
Type of the symbol. This enum matches the STT_xxx values of the ELF specs.
Definition ELF/Symbol.hpp:49
LIEF::ELF::Symbol::visibility
void visibility(VISIBILITY visibility)
Definition ELF/Symbol.hpp:209
LIEF::ELF::Symbol::set_exported
void set_exported(bool flag=true)
Set whether or not the symbol is exported.
LIEF::ELF::Symbol::is_imported
bool is_imported() const
Check if the current symbol is imported.
LIEF::ELF::Symbol::binding_from
static BINDING binding_from(uint32_t value, ARCH)
Definition ELF/Symbol.hpp:81
LIEF::ELF::Symbol::section
Section * section()
Section associated with the symbol or a nullptr if it does not exist.
Definition ELF/Symbol.hpp:133
LIEF::ELF::Symbol::is_weak
bool is_weak() const
Definition ELF/Symbol.hpp:190
LIEF::ELF::Symbol::has_version
bool has_version() const
Check if this symbols has a symbol version .
Definition ELF/Symbol.hpp:168
LIEF::ELF::Symbol::type_from
static TYPE type_from(uint32_t value, ARCH)
Definition ELF/Symbol.hpp:85
LIEF::ELF::Symbol::other
void other(uint8_t other)
Definition ELF/Symbol.hpp:205
LIEF::ELF::Symbol::information
void information(uint8_t info)
LIEF::ELF::Symbol::visibility
VISIBILITY visibility() const
Symbol visibility.
Definition ELF/Symbol.hpp:128
LIEF::ELF::Symbol::symbol_version
SymbolVersion * symbol_version()
Return the SymbolVersion associated with this symbol. If there is no symbol version,...
Definition ELF/Symbol.hpp:174
LIEF::ELF::Symbol::to_value
static uint8_t to_value(TYPE type)
Definition ELF/Symbol.hpp:93
LIEF::ELF::Symbol::value
uint64_t value() const override
This member has slightly different interpretations:
Definition ELF/Symbol.hpp:149
LIEF::ELF::Symbol::is_static
bool is_static() const
True if the symbol is a static one.
Definition ELF/Symbol.hpp:240
LIEF::ELF::Symbol::is_function
bool is_function() const
True if the symbol represent a function.
Definition ELF/Symbol.hpp:245
LIEF::ELF::Symbol::to_value
static uint8_t to_value(BINDING binding)
Definition ELF/Symbol.hpp:89
LIEF::ELF::Symbol::VISIBILITY
VISIBILITY
Visibility of the symbol. This enum matches the STV_xxx values of the official ELF specs.
Definition ELF/Symbol.hpp:62
LIEF::ELF::Symbol::operator<<
friend std::ostream & operator<<(std::ostream &os, const Symbol &entry)
LIEF::ELF
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
LIEF::ELF::to_string
const char * to_string(DynamicEntry::TAG e)
LIEF::ELF::ARCH
ARCH
Definition ELF/enums.hpp:30
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41