LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
COFF/Symbol.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_COFF_SYMBOL_H
16#define LIEF_COFF_SYMBOL_H
17
20#include "LIEF/iterators.hpp"
21#include "LIEF/visibility.h"
22#include <string_view>
23
24#include <memory>
25#include <vector>
26
27namespace LIEF {
28class BinaryStream;
29namespace COFF {
30class Parser;
31class AuxiliarySymbol;
32class String;
33class Section;
34
37 public:
38 friend class Parser;
39
40 using auxiliary_symbols_t = std::vector<std::unique_ptr<AuxiliarySymbol>>;
41
44
47
49 std::function<String*(uint32_t)> find_string;
51 };
52 static std::unique_ptr<Symbol> parse(parsing_context_t& ctx,
53 BinaryStream& stream, size_t* idx);
54
56 Symbol(const Symbol&);
58
59 Symbol(Symbol&&) noexcept;
60 Symbol& operator=(Symbol&&) noexcept;
61
65 static constexpr auto SYM_SEC_IDX_DEBUG = -2;
66
68 static constexpr auto SYM_SEC_IDX_ABS = -1;
69
73 static constexpr auto SYM_SEC_IDX_UNDEF = 0;
74
75 static constexpr auto SYM_COMPLEX_TYPE_SHIFT = 4;
76
79 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
138 static constexpr bool is_reversed_sec_idx(int16_t idx) {
139 return idx <= 0;
140 }
141
145 uint16_t type() const {
146 return type_;
147 }
148
152 return (STORAGE_CLASS)storage_class_;
153 }
154
157 return (BASE_TYPE)(type_ & 0x0F);
158 }
159
162 return (COMPLEX_TYPE)((type_ & 0xF0) >> SYM_COMPLEX_TYPE_SHIFT);
163 }
164
177 int16_t section_idx() const {
178 return section_idx_;
179 }
180
183 return section_;
184 }
185
187 return section_;
188 }
189
190 bool is_external() const {
192 }
193
194 bool is_weak_external() const {
196 }
197
198 bool is_absolute() const {
199 return section_idx() == SYM_SEC_IDX_ABS;
200 }
201
202 bool is_undefined() const {
203 return is_external() && section_idx() == SYM_SEC_IDX_UNDEF && value() == 0;
204 }
205
208 }
209
210 bool is_function() const {
212 }
213
214 bool is_file_record() const {
216 }
217
220 return auxiliary_symbols_;
221 }
222
224 return auxiliary_symbols_;
225 }
226
229 std::string_view name() const LIEF_LIFETIMEBOUND override;
230
231 std::string& name() override;
232
235 return coff_name_;
236 }
237
239 return coff_name_;
240 }
241
244 std::string demangled_name() const;
245
247 type_ = ty;
248 return *this;
249 }
250
252 storage_class_ = value;
253 return *this;
254 }
255
257 section_idx_ = idx;
258 return *this;
259 }
260
263 add_aux(std::unique_ptr<AuxiliarySymbol> sym) LIEF_LIFETIMEBOUND;
264
265 std::string to_string() const;
266
267 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Symbol& entry) {
268 os << entry.to_string();
269 return os;
270 }
271
272 ~Symbol() override;
273
274 private:
275 template<class T>
276 static std::unique_ptr<Symbol> parse_impl(parsing_context_t& ctx,
277 BinaryStream& stream, size_t* idx);
278 String* coff_name_ = nullptr;
279 uint16_t type_ = 0;
280 uint8_t storage_class_ = 0;
281 int16_t section_idx_ = 0;
282 auxiliary_symbols_t auxiliary_symbols_;
283 Section* section_ = nullptr;
284};
285
289
290}
291}
292#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
Class that represents an auxiliary symbol.
Definition AuxiliarySymbol.hpp:36
Definition COFF/Parser.hpp:36
This class represents a COFF section.
Definition COFF/Section.hpp:40
This class represents a string located in the COFF string table.
Definition String.hpp:34
Symbol(Symbol &&) noexcept
uint16_t type() const
The symbol type. The first byte represents the base type (see: base_type()) while the upper byte repr...
Definition COFF/Symbol.hpp:145
bool is_absolute() const
Definition COFF/Symbol.hpp:198
~Symbol() override
ref_iterator< auxiliary_symbols_t &, AuxiliarySymbol * > it_auxiliary_symbols_t
Definition COFF/Symbol.hpp:42
COMPLEX_TYPE
Definition COFF/Symbol.hpp:130
@ TY_FUNCTION
A function that returns a base type.
Definition COFF/Symbol.hpp:133
Symbol & storage_class(uint8_t value)
Definition COFF/Symbol.hpp:251
const Section * section() const
Definition COFF/Symbol.hpp:186
COMPLEX_TYPE complex_type() const
The complex type (if any).
Definition COFF/Symbol.hpp:161
bool is_function_line_info() const
Definition COFF/Symbol.hpp:206
Symbol & section_idx(int16_t idx)
Definition COFF/Symbol.hpp:256
bool is_file_record() const
Definition COFF/Symbol.hpp:214
Section * section()
Section associated with this symbol (if any).
Definition COFF/Symbol.hpp:182
const_ref_iterator< const auxiliary_symbols_t &, AuxiliarySymbol * > it_const_auxiliary_symbols_t
Definition COFF/Symbol.hpp:45
bool is_function() const
Definition COFF/Symbol.hpp:210
BASE_TYPE base_type() const
The simple (base) data type.
Definition COFF/Symbol.hpp:156
std::vector< std::unique_ptr< AuxiliarySymbol > > auxiliary_symbols_t
Definition COFF/Symbol.hpp:40
bool is_external() const
Definition COFF/Symbol.hpp:190
BASE_TYPE
Definition COFF/Symbol.hpp:111
std::string_view name() const override
Name of the symbol. If the symbol does not use a short name, it returns the string pointed by the COF...
STORAGE_CLASS
Reference: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#storage-class.
Definition COFF/Symbol.hpp:79
@ EXTERNAL
External symbol.
Definition COFF/Symbol.hpp:85
@ FUNCTION
Definition COFF/Symbol.hpp:103
@ WEAK_EXTERNAL
Duplicate tag.
Definition COFF/Symbol.hpp:107
@ FILE
File name.
Definition COFF/Symbol.hpp:105
Symbol & type(uint16_t ty)
Definition COFF/Symbol.hpp:246
static constexpr auto SYM_SEC_IDX_ABS
The symbol has an absolute (non-relocatable) value and is not an address.
Definition COFF/Symbol.hpp:68
static constexpr auto SYM_SEC_IDX_DEBUG
The symbol provides general type or debugging information but does not correspond to a section....
Definition COFF/Symbol.hpp:65
const String * coff_name() const
COFF string used to represent the (long) symbol name.
Definition COFF/Symbol.hpp:234
it_auxiliary_symbols_t auxiliary_symbols()
Auxiliary symbols associated with this symbol.
Definition COFF/Symbol.hpp:219
static constexpr auto SYM_SEC_IDX_UNDEF
The symbol record is not yet assigned a section. A value of zero indicates that a reference to an ext...
Definition COFF/Symbol.hpp:73
std::string to_string() const
String * coff_name()
Definition COFF/Symbol.hpp:238
static std::unique_ptr< Symbol > parse(parsing_context_t &ctx, BinaryStream &stream, size_t *idx)
int16_t section_idx() const
The signed integer that identifies the section, using a one-based index into the section table....
Definition COFF/Symbol.hpp:177
friend class Parser
Definition COFF/Symbol.hpp:38
static constexpr bool is_reversed_sec_idx(int16_t idx)
Check if the given section index is a reserved value.
Definition COFF/Symbol.hpp:138
bool is_undefined() const
Definition COFF/Symbol.hpp:202
AuxiliarySymbol & add_aux(std::unique_ptr< AuxiliarySymbol > sym)
Add a new auxiliary record.
bool is_weak_external() const
Definition COFF/Symbol.hpp:194
it_const_auxiliary_symbols_t auxiliary_symbols() const
Definition COFF/Symbol.hpp:223
Symbol(const Symbol &)
Symbol & operator=(const Symbol &)
std::string demangled_name() const
Demangled representation of the symbol or an empty string if it can't be demangled.
static constexpr auto SYM_COMPLEX_TYPE_SHIFT
Definition COFF/Symbol.hpp:75
STORAGE_CLASS storage_class() const
Storage class of the symbol which indicates what kind of definition a symbol represents.
Definition COFF/Symbol.hpp:151
friend std::ostream & operator<<(std::ostream &os, const Symbol &entry)
Definition COFF/Symbol.hpp:267
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:29
virtual uint64_t value() const
Symbol's value which is usually the address of the symbol.
Definition Abstract/Symbol.hpp:69
Iterator which returns reference on container's values.
Definition iterators.hpp:120
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition AuxiliarySymbol.hpp:29
const char * to_string(AuxiliarySectionDefinition::COMDAT_SELECTION e)
LIEF namespace.
Definition Abstract/Binary.hpp:41
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which returns a const ref on container's values.
Definition iterators.hpp:385
Definition COFF/Symbol.hpp:48
bool is_bigobj
Definition COFF/Symbol.hpp:50
std::function< String *(uint32_t)> find_string
Definition COFF/Symbol.hpp:49
#define LIEF_API
Definition visibility.h:44