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 * 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_COFF_SYMBOL_H
17#define LIEF_COFF_SYMBOL_H
18
21#include "LIEF/iterators.hpp"
22#include "LIEF/visibility.h"
23#include <string_view>
24
25#include <memory>
26#include <vector>
27
28namespace LIEF {
29class BinaryStream;
30namespace COFF {
31class Parser;
32class AuxiliarySymbol;
33class String;
34class Section;
35
38 public:
39 friend class Parser;
40
41 using auxiliary_symbols_t = std::vector<std::unique_ptr<AuxiliarySymbol>>;
42
45
48
50 std::function<String*(uint32_t)> find_string;
52 };
53 static std::unique_ptr<Symbol> parse(parsing_context_t& ctx,
54 BinaryStream& stream, size_t* idx);
55
57 Symbol(const Symbol&);
59
60 Symbol(Symbol&&) noexcept;
61 Symbol& operator=(Symbol&&) noexcept;
62
66 static constexpr auto SYM_SEC_IDX_DEBUG = -2;
67
69 static constexpr auto SYM_SEC_IDX_ABS = -1;
70
74 static constexpr auto SYM_SEC_IDX_UNDEF = 0;
75
76 static constexpr auto SYM_COMPLEX_TYPE_SHIFT = 4;
77
80 enum class STORAGE_CLASS : int32_t {
81 INVALID = 0xFF,
82
83 END_OF_FUNCTION = -1,
84 NONE = 0,
85 AUTOMATIC = 1,
86 EXTERNAL = 2,
87 STATIC = 3,
88 REGISTER = 4,
89 EXTERNAL_DEF = 5,
90 LABEL = 6,
91 UNDEFINED_LABEL = 7,
92 MEMBER_OF_STRUCT = 8,
93 ARGUMENT = 9,
94 STRUCT_TAG = 10,
95 MEMBER_OF_UNION = 11,
96 UNION_TAG = 12,
97 TYPE_DEFINITION = 13,
98 UNDEFINED_STATIC = 14,
99 ENUM_TAG = 15,
100 MEMBER_OF_ENUM = 16,
101 REGISTER_PARAM = 17,
102 BIT_FIELD = 18,
103 BLOCK = 100,
104 FUNCTION = 101,
105 END_OF_STRUCT = 102,
106 FILE = 103,
107 SECTION = 104,
108 WEAK_EXTERNAL = 105,
109 CLR_TOKEN = 107,
110 };
111
112 enum class BASE_TYPE : uint32_t {
113 TY_NULL = 0,
114 TY_VOID = 1,
115 TY_CHAR = 2,
116 TY_SHORT = 3,
117 TY_INT = 4,
118 TY_LONG = 5,
119 TY_FLOAT = 6,
120 TY_DOUBLE = 7,
121 TY_STRUCT = 8,
122 TY_UNION = 9,
123 TY_ENUM = 10,
124 TY_MOE = 11,
125 TY_BYTE = 12,
126 TY_WORD = 13,
127 TY_UINT = 14,
128 TY_DWORD = 15,
129 };
130
131 enum class COMPLEX_TYPE : uint32_t {
132 TY_NULL = 0,
133 TY_POINTER = 1,
134 TY_FUNCTION = 2,
135 TY_ARRAY = 3,
136 };
137
139 static constexpr bool is_reversed_sec_idx(int16_t idx) {
140 return idx <= 0;
141 }
142
146 uint16_t type() const {
147 return type_;
148 }
149
153 return (STORAGE_CLASS)storage_class_;
154 }
155
158 return (BASE_TYPE)(type_ & 0x0F);
159 }
160
163 return (COMPLEX_TYPE)((type_ & 0xF0) >> SYM_COMPLEX_TYPE_SHIFT);
164 }
165
178 int16_t section_idx() const {
179 return section_idx_;
180 }
181
184 return section_;
185 }
186
188 return section_;
189 }
190
191 bool is_external() const {
193 }
194
195 bool is_weak_external() const {
197 }
198
199 bool is_absolute() const {
200 return section_idx() == SYM_SEC_IDX_ABS;
201 }
202
203 bool is_undefined() const {
204 return is_external() && section_idx() == SYM_SEC_IDX_UNDEF && value() == 0;
205 }
206
209 }
210
211 bool is_function() const {
213 }
214
215 bool is_file_record() const {
217 }
218
221 return auxiliary_symbols_;
222 }
223
225 return auxiliary_symbols_;
226 }
227
230 std::string_view name() const LIEF_LIFETIMEBOUND override;
231
232 std::string& name() override;
233
236 return coff_name_;
237 }
238
240 return coff_name_;
241 }
242
245 std::string demangled_name() const;
246
248 type_ = ty;
249 return *this;
250 }
251
253 storage_class_ = value;
254 return *this;
255 }
256
258 section_idx_ = idx;
259 return *this;
260 }
261
264 add_aux(std::unique_ptr<AuxiliarySymbol> sym) LIEF_LIFETIMEBOUND;
265
266 std::string to_string() const;
267
268 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Symbol& entry) {
269 os << entry.to_string();
270 return os;
271 }
272
273 ~Symbol() override;
274
275 private:
276 template<class T>
277 static std::unique_ptr<Symbol> parse_impl(parsing_context_t& ctx,
278 BinaryStream& stream, size_t* idx);
279 String* coff_name_ = nullptr;
280 uint16_t type_ = 0;
281 uint8_t storage_class_ = 0;
282 int16_t section_idx_ = 0;
283 auxiliary_symbols_t auxiliary_symbols_;
284 Section* section_ = nullptr;
285};
286
290
291}
292}
293#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:35
Class that represents an auxiliary symbol.
Definition AuxiliarySymbol.hpp:37
Definition COFF/Parser.hpp:34
This class represents a COFF section.
Definition COFF/Section.hpp:41
This class represents a string located in the COFF string table.
Definition String.hpp:35
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:146
bool is_absolute() const
Definition COFF/Symbol.hpp:199
~Symbol() override
ref_iterator< auxiliary_symbols_t &, AuxiliarySymbol * > it_auxiliary_symbols_t
Definition COFF/Symbol.hpp:43
COMPLEX_TYPE
Definition COFF/Symbol.hpp:131
@ TY_FUNCTION
A function that returns a base type.
Definition COFF/Symbol.hpp:134
Symbol & storage_class(uint8_t value)
Definition COFF/Symbol.hpp:252
const Section * section() const
Definition COFF/Symbol.hpp:187
COMPLEX_TYPE complex_type() const
The complex type (if any).
Definition COFF/Symbol.hpp:162
bool is_function_line_info() const
Definition COFF/Symbol.hpp:207
Symbol & section_idx(int16_t idx)
Definition COFF/Symbol.hpp:257
bool is_file_record() const
Definition COFF/Symbol.hpp:215
Section * section()
Section associated with this symbol (if any).
Definition COFF/Symbol.hpp:183
const_ref_iterator< const auxiliary_symbols_t &, AuxiliarySymbol * > it_const_auxiliary_symbols_t
Definition COFF/Symbol.hpp:46
bool is_function() const
Definition COFF/Symbol.hpp:211
BASE_TYPE base_type() const
The simple (base) data type.
Definition COFF/Symbol.hpp:157
std::vector< std::unique_ptr< AuxiliarySymbol > > auxiliary_symbols_t
Definition COFF/Symbol.hpp:41
bool is_external() const
Definition COFF/Symbol.hpp:191
BASE_TYPE
Definition COFF/Symbol.hpp:112
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:80
@ EXTERNAL
External symbol.
Definition COFF/Symbol.hpp:86
@ FUNCTION
Definition COFF/Symbol.hpp:104
@ WEAK_EXTERNAL
Duplicate tag.
Definition COFF/Symbol.hpp:108
@ FILE
File name.
Definition COFF/Symbol.hpp:106
Symbol & type(uint16_t ty)
Definition COFF/Symbol.hpp:247
static constexpr auto SYM_SEC_IDX_ABS
The symbol has an absolute (non-relocatable) value and is not an address.
Definition COFF/Symbol.hpp:69
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:66
const String * coff_name() const
COFF string used to represent the (long) symbol name.
Definition COFF/Symbol.hpp:235
it_auxiliary_symbols_t auxiliary_symbols()
Auxiliary symbols associated with this symbol.
Definition COFF/Symbol.hpp:220
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:74
std::string to_string() const
String * coff_name()
Definition COFF/Symbol.hpp:239
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:178
friend class Parser
Definition COFF/Symbol.hpp:39
static constexpr bool is_reversed_sec_idx(int16_t idx)
Check if the given section index is a reserved value.
Definition COFF/Symbol.hpp:139
bool is_undefined() const
Definition COFF/Symbol.hpp:203
AuxiliarySymbol & add_aux(std::unique_ptr< AuxiliarySymbol > sym)
Add a new auxiliary record.
bool is_weak_external() const
Definition COFF/Symbol.hpp:195
it_const_auxiliary_symbols_t auxiliary_symbols() const
Definition COFF/Symbol.hpp:224
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:76
STORAGE_CLASS storage_class() const
Storage class of the symbol which indicates what kind of definition a symbol represents.
Definition COFF/Symbol.hpp:152
friend std::ostream & operator<<(std::ostream &os, const Symbol &entry)
Definition COFF/Symbol.hpp:268
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:30
virtual uint64_t value() const
Symbol's value which is usually the address of the symbol.
Definition Abstract/Symbol.hpp:70
Iterator which returns reference on container's values.
Definition iterators.hpp:47
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition AuxiliarySymbol.hpp:30
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:316
Definition COFF/Symbol.hpp:49
bool is_bigobj
Definition COFF/Symbol.hpp:51
std::function< String *(uint32_t)> find_string
Definition COFF/Symbol.hpp:50
#define LIEF_API
Definition visibility.h:45