LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
PE/Symbol.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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_PE_SYMBOLS_H
17#define LIEF_PE_SYMBOLS_H
18
19#include <ostream>
20#include <memory>
21#include <vector>
22
23#include "LIEF/iterators.hpp"
24#include "LIEF/Object.hpp"
25#include "LIEF/visibility.h"
26
28
29namespace LIEF {
30class BinaryStream;
31
32namespace PE {
33class AuxiliarySymbol;
34class COFFString;
35class Parser;
36class LIEF_API Symbol : public LIEF::Symbol {
51 public:
52 using auxiliary_symbols_t = std::vector<std::unique_ptr<AuxiliarySymbol>>;
53 using it_auxiliary_symbols_t = ref_iterator<auxiliary_symbols_t&, AuxiliarySymbol*>;
54 using it_const_auxiliary_symbols_t = const_ref_iterator<const auxiliary_symbols_t&, AuxiliarySymbol*>;
55
56 static std::unique_ptr<Symbol> parse(Parser& ctx, BinaryStream& stream, size_t* idx);
57
59 Symbol(const Symbol&);
60 Symbol& operator=(const Symbol&);
61
62 Symbol(Symbol&&);
63 Symbol& operator=(Symbol&&);
64 static constexpr auto SYM_SEC_IDX_DEBUG = -2; static constexpr auto SYM_SEC_IDX_ABS = -1; static constexpr auto SYM_SEC_IDX_UNDEF = 0;
75
76 static constexpr auto SYM_COMPLEX_TYPE_SHIFT = 4;
77 enum class STORAGE_CLASS : int32_t {
80 INVALID = 0xFF,
81
82 END_OF_FUNCTION = -1,
83 NONE = 0,
84 AUTOMATIC = 1,
85 EXTERNAL = 2,
86 STATIC = 3,
87 REGISTER = 4,
88 EXTERNAL_DEF = 5,
89 LABEL = 6,
90 UNDEFINED_LABEL = 7,
91 MEMBER_OF_STRUCT = 8,
92 ARGUMENT = 9,
93 STRUCT_TAG = 10,
94 MEMBER_OF_UNION = 11,
95 UNION_TAG = 12,
96 TYPE_DEFINITION = 13,
97 UNDEFINED_STATIC = 14,
98 ENUM_TAG = 15,
99 MEMBER_OF_ENUM = 16,
100 REGISTER_PARAM = 17,
101 BIT_FIELD = 18,
102 BLOCK = 100,
103 FUNCTION = 101,
104 END_OF_STRUCT = 102,
105 FILE = 103,
106 SECTION = 104,
107 WEAK_EXTERNAL = 105,
108 CLR_TOKEN = 107
109 };
110
111 enum class BASE_TYPE : uint32_t {
112 TY_NULL = 0,
113 TY_VOID = 1,
114 TY_CHAR = 2,
115 TY_SHORT = 3,
116 TY_INT = 4,
117 TY_LONG = 5,
118 TY_FLOAT = 6,
119 TY_DOUBLE = 7,
120 TY_STRUCT = 8,
121 TY_UNION = 9,
122 TY_ENUM = 10,
123 TY_MOE = 11,
124 TY_BYTE = 12,
125 TY_WORD = 13,
126 TY_UINT = 14,
127 TY_DWORD = 15
128 };
129
130 enum class COMPLEX_TYPE : uint32_t {
131 TY_NULL = 0,
132 TY_POINTER = 1,
133 TY_FUNCTION = 2,
134 TY_ARRAY = 3,
135 };
136 static constexpr bool is_reversed_sec_idx(int16_t idx) {
139 return idx <= 0;
140 }
141 uint16_t type() const {
146 return type_;
147 }
148 STORAGE_CLASS storage_class() const {
152 return (STORAGE_CLASS)storage_class_;
153 }
154 BASE_TYPE base_type() const {
157 return (BASE_TYPE)(type_ & 0x0F);
158 }
159 COMPLEX_TYPE complex_type() const {
162 return (COMPLEX_TYPE)((type_ & 0xF0) >> SYM_COMPLEX_TYPE_SHIFT);
163 }
164 int16_t section_idx() const {
178 return section_idx_;
179 }
180
181 bool is_external() const {
182 return storage_class() == STORAGE_CLASS::EXTERNAL;
183 }
184
185 bool is_weak_external() const {
186 return storage_class() == STORAGE_CLASS::WEAK_EXTERNAL;
187 }
188
189 bool is_undefined() const {
190 return is_external() && section_idx() == SYM_SEC_IDX_UNDEF &&
191 value() == 0;
192 }
193
194 bool is_function_line_info() const {
195 return storage_class() == STORAGE_CLASS::FUNCTION;
196 }
197
198 bool is_file_record() const {
199 return storage_class() == STORAGE_CLASS::FILE;
200 }
201 it_auxiliary_symbols_t auxiliary_symbols() {
204 return auxiliary_symbols_;
205 }
206
207 it_const_auxiliary_symbols_t auxiliary_symbols() const {
208 return auxiliary_symbols_;
209 }
210 const std::string& name() const override;
214
215 std::string& name() override;
216 const COFFString* coff_name() const {
219 return coff_name_;
220 }
221
222 COFFString* coff_name() {
223 return coff_name_;
224 }
225
226 Symbol& type(uint16_t ty) {
227 type_ = ty;
228 return *this;
229 }
230
231 Symbol& storage_class(uint8_t value) {
232 storage_class_ = value;
233 return *this;
234 }
235
236 Symbol& section_idx(int16_t idx) {
237 section_idx_ = idx;
238 return *this;
239 }
240 AuxiliarySymbol& add_aux(std::unique_ptr<AuxiliarySymbol> sym);
243
244 void accept(Visitor& visitor) const override;
245
246 LIEF_API friend
247 std::ostream& operator<<(std::ostream& os, const Symbol& entry);
248
249 ~Symbol() override;
250
251 private:
252 COFFString* coff_name_ = nullptr;
253 uint16_t type_ = 0;
254 uint8_t storage_class_ = 0;
255 int16_t section_idx_ = 0;
256 auxiliary_symbols_t auxiliary_symbols_;
257};
258
259LIEF_API const char* to_string(Symbol::STORAGE_CLASS e);
260LIEF_API const char* to_string(Symbol::BASE_TYPE e);
261LIEF_API const char* to_string(Symbol::COMPLEX_TYPE e);
262
263
264} // namespace PE
265} // namespace LIEF
266#endif /* SYMBOLS_H */
Symbol.hpp
Object.hpp
LIEF::BinaryStream
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
LIEF::PE::AuxiliarySymbol
Class that represents an auxiliary symbol.
Definition AuxiliarySymbol.hpp:36
LIEF::PE::COFFString
This class represents a string located in the COFF string table.
Definition COFFString.hpp:33
LIEF::PE::Parser
Main interface to parse PE binaries. In particular the static functions: Parser::parse should be used...
Definition PE/Parser.hpp:52
LIEF::PE::Symbol
Class that represents a PE-COFF symbol.
Definition PE/Symbol.hpp:50
LIEF::PE::Symbol::storage_class
Symbol & storage_class(uint8_t value)
Definition PE/Symbol.hpp:231
LIEF::PE::Symbol::auxiliary_symbols
it_auxiliary_symbols_t auxiliary_symbols()
Auxiliary symbols associated with this symbol.
Definition PE/Symbol.hpp:203
LIEF::PE::Symbol::operator=
Symbol & operator=(const Symbol &)
LIEF::PE::Symbol::STORAGE_CLASS
STORAGE_CLASS
Reference: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#storage-class.
Definition PE/Symbol.hpp:79
LIEF::PE::Symbol::is_undefined
bool is_undefined() const
Definition PE/Symbol.hpp:189
LIEF::PE::Symbol::is_reversed_sec_idx
static constexpr bool is_reversed_sec_idx(int16_t idx)
Check if the given section index is a reserved value.
Definition PE/Symbol.hpp:138
LIEF::PE::Symbol::Symbol
Symbol(const Symbol &)
LIEF::PE::Symbol::COMPLEX_TYPE
COMPLEX_TYPE
Definition PE/Symbol.hpp:130
LIEF::PE::Symbol::type
Symbol & type(uint16_t ty)
Definition PE/Symbol.hpp:226
LIEF::PE::Symbol::parse
static std::unique_ptr< Symbol > parse(Parser &ctx, BinaryStream &stream, size_t *idx)
LIEF::PE::Symbol::complex_type
COMPLEX_TYPE complex_type() const
The complex type (if any)
Definition PE/Symbol.hpp:161
LIEF::PE::Symbol::is_weak_external
bool is_weak_external() const
Definition PE/Symbol.hpp:185
LIEF::PE::Symbol::section_idx
int16_t section_idx() const
The signed integer that identifies the section, using a one-based index into the section table....
Definition PE/Symbol.hpp:177
LIEF::PE::Symbol::is_external
bool is_external() const
Definition PE/Symbol.hpp:181
LIEF::PE::Symbol::add_aux
AuxiliarySymbol & add_aux(std::unique_ptr< AuxiliarySymbol > sym)
Add a new auxiliary record.
LIEF::PE::Symbol::type
uint16_t type() const
The symbol type. The first byte represents the base type (see: base_type()) while the upper byte repr...
Definition PE/Symbol.hpp:145
LIEF::PE::Symbol::coff_name
COFFString * coff_name()
Definition PE/Symbol.hpp:222
LIEF::PE::Symbol::is_function_line_info
bool is_function_line_info() const
Definition PE/Symbol.hpp:194
LIEF::PE::Symbol::is_file_record
bool is_file_record() const
Definition PE/Symbol.hpp:198
LIEF::PE::Symbol::section_idx
Symbol & section_idx(int16_t idx)
Definition PE/Symbol.hpp:236
LIEF::PE::Symbol::base_type
BASE_TYPE base_type() const
The simple (base) data type.
Definition PE/Symbol.hpp:156
LIEF::PE::Symbol::storage_class
STORAGE_CLASS storage_class() const
Storage class of the symbol which indicates what kind of definition a symbol represents.
Definition PE/Symbol.hpp:151
LIEF::PE::Symbol::name
std::string & name() override
LIEF::PE::Symbol::operator=
Symbol & operator=(Symbol &&)
LIEF::PE::Symbol::accept
void accept(Visitor &visitor) const override
LIEF::PE::Symbol::Symbol
Symbol()
LIEF::PE::Symbol::~Symbol
~Symbol() override
LIEF::PE::Symbol::BASE_TYPE
BASE_TYPE
Definition PE/Symbol.hpp:111
LIEF::PE::Symbol::auxiliary_symbols
it_const_auxiliary_symbols_t auxiliary_symbols() const
Definition PE/Symbol.hpp:207
LIEF::PE::Symbol::Symbol
Symbol(Symbol &&)
LIEF::PE::Symbol::name
const std::string & name() const override
Name of the symbol. If the symbol does not use a short name, it returns the string pointed by the COF...
LIEF::PE::Symbol::operator<<
friend std::ostream & operator<<(std::ostream &os, const Symbol &entry)
LIEF::PE::Symbol::coff_name
const COFFString * coff_name() const
COFF string used to represents the (long) symbol name.
Definition PE/Symbol.hpp:218
iterators.hpp
LIEF::PE
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF::PE::to_string
const char * to_string(AuxiliaryWeakExternal::CHARACTERISTICS e)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41