LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
COFF/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_COFF_SYMBOL_H
17#define LIEF_COFF_SYMBOL_H
18
20#include "LIEF/visibility.h"
21#include "LIEF/iterators.hpp"
22
23#include <memory>
24#include <vector>
25
26namespace LIEF {
27class BinaryStream;
28namespace COFF {
29class Parser;
30class AuxiliarySymbol;
31class String;
32class Section;
33
36 public:
37 friend class Parser;
38
39 using auxiliary_symbols_t = std::vector<std::unique_ptr<AuxiliarySymbol>>;
42
44 std::function<String*(uint32_t)> find_string;
46 };
47 static std::unique_ptr<Symbol> parse(
48 parsing_context_t& ctx, BinaryStream& stream, size_t* idx);
49
51 Symbol(const Symbol&);
53
56
60 static constexpr auto SYM_SEC_IDX_DEBUG = -2;
62 static constexpr auto SYM_SEC_IDX_ABS = -1;
66 static constexpr auto SYM_SEC_IDX_UNDEF = 0;
67
68 static constexpr auto SYM_COMPLEX_TYPE_SHIFT = 4;
69
102
103 enum class BASE_TYPE : uint32_t {
108 TY_INT = 4,
114 TY_ENUM = 10,
115 TY_MOE = 11,
116 TY_BYTE = 12,
117 TY_WORD = 13,
118 TY_UINT = 14,
120 };
121
122 enum class COMPLEX_TYPE : uint32_t {
127 };
128
130 static constexpr bool is_reversed_sec_idx(int16_t idx) {
131 return idx <= 0;
132 }
133
137 uint16_t type() const {
138 return type_;
139 }
140
144 return (STORAGE_CLASS)storage_class_;
145 }
146
149 return (BASE_TYPE)(type_ & 0x0F);
150 }
151
154 return (COMPLEX_TYPE)((type_ & 0xF0) >> SYM_COMPLEX_TYPE_SHIFT);
155 }
156
169 int16_t section_idx() const {
170 return section_idx_;
171 }
172
175 return section_;
176 }
177
178 const Section* section() const {
179 return section_;
180 }
181
182 bool is_external() const {
184 }
185
186 bool is_weak_external() const {
188 }
189
190 bool is_absolute() const {
191 return section_idx() == SYM_SEC_IDX_ABS;
192 }
193
194 bool is_undefined() const {
195 return is_external() && section_idx() == SYM_SEC_IDX_UNDEF &&
196 value() == 0;
197 }
198
201 }
202
203 bool is_function() const {
205 }
206
207 bool is_file_record() const {
209 }
210
213 return auxiliary_symbols_;
214 }
215
217 return auxiliary_symbols_;
218 }
219
222 const std::string& name() const override;
223
224 std::string& name() override;
225
227 const String* coff_name() const {
228 return coff_name_;
229 }
230
232 return coff_name_;
233 }
234
237 std::string demangled_name() const;
238
239 Symbol& type(uint16_t ty) {
240 type_ = ty;
241 return *this;
242 }
243
245 storage_class_ = value;
246 return *this;
247 }
248
249 Symbol& section_idx(int16_t idx) {
250 section_idx_ = idx;
251 return *this;
252 }
253
255 AuxiliarySymbol& add_aux(std::unique_ptr<AuxiliarySymbol> sym);
256
257 std::string to_string() const;
258
259 LIEF_API friend
260 std::ostream& operator<<(std::ostream& os, const Symbol& entry)
261 {
262 os << entry.to_string();
263 return os;
264 }
265
266 ~Symbol() override;
267
268 private:
269 template<class T>
270 static std::unique_ptr<Symbol> parse_impl(
271 parsing_context_t& ctx, BinaryStream& stream, size_t* idx);
272 String* coff_name_ = nullptr;
273 uint16_t type_ = 0;
274 uint8_t storage_class_ = 0;
275 int16_t section_idx_ = 0;
276 auxiliary_symbols_t auxiliary_symbols_;
277 Section* section_ = nullptr;
278};
279
283
284}
285}
286#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
Class that represents an auxiliary symbol.
Definition AuxiliarySymbol.hpp:36
Definition COFF/Parser.hpp:34
This class represents a COFF section.
Definition COFF/Section.hpp:39
This class represents a string located in the COFF string table.
Definition String.hpp:33
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:137
bool is_absolute() const
Definition COFF/Symbol.hpp:190
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...
~Symbol() override
COMPLEX_TYPE
Definition COFF/Symbol.hpp:122
@ TY_ARRAY
An array of base type.
Definition COFF/Symbol.hpp:126
@ TY_POINTER
A pointer to base type.
Definition COFF/Symbol.hpp:124
@ TY_FUNCTION
A function that returns a base type.
Definition COFF/Symbol.hpp:125
Symbol & storage_class(uint8_t value)
Definition COFF/Symbol.hpp:244
const Section * section() const
Definition COFF/Symbol.hpp:178
COMPLEX_TYPE complex_type() const
The complex type (if any).
Definition COFF/Symbol.hpp:153
bool is_function_line_info() const
Definition COFF/Symbol.hpp:199
Symbol & section_idx(int16_t idx)
Definition COFF/Symbol.hpp:249
bool is_file_record() const
Definition COFF/Symbol.hpp:207
Section * section()
Section associated with this symbol (if any).
Definition COFF/Symbol.hpp:174
bool is_function() const
Definition COFF/Symbol.hpp:203
BASE_TYPE base_type() const
The simple (base) data type.
Definition COFF/Symbol.hpp:148
std::vector< std::unique_ptr< AuxiliarySymbol > > auxiliary_symbols_t
Definition COFF/Symbol.hpp:39
bool is_external() const
Definition COFF/Symbol.hpp:182
const_ref_iterator< const auxiliary_symbols_t &, AuxiliarySymbol * > it_const_auxiliary_symbols_t
Definition COFF/Symbol.hpp:41
BASE_TYPE
Definition COFF/Symbol.hpp:103
@ TY_NULL
No type information or unknown base type.
Definition COFF/Symbol.hpp:104
@ TY_UINT
An unsigned integer of natural size.
Definition COFF/Symbol.hpp:118
@ TY_MOE
A member of enumeration (a specific value).
Definition COFF/Symbol.hpp:115
@ TY_ENUM
An enumerated type.
Definition COFF/Symbol.hpp:114
@ TY_WORD
A word; unsigned 2-byte integer.
Definition COFF/Symbol.hpp:117
@ TY_LONG
A 4-byte signed integer.
Definition COFF/Symbol.hpp:109
@ TY_VOID
Used with void pointers and functions.
Definition COFF/Symbol.hpp:105
@ TY_UNION
An union.
Definition COFF/Symbol.hpp:113
@ TY_DWORD
An unsigned 4-byte integer.
Definition COFF/Symbol.hpp:119
@ TY_INT
A natural integer type on the target.
Definition COFF/Symbol.hpp:108
@ TY_STRUCT
A structure.
Definition COFF/Symbol.hpp:112
@ TY_BYTE
A byte; unsigned 1-byte integer.
Definition COFF/Symbol.hpp:116
@ TY_CHAR
A character (signed byte).
Definition COFF/Symbol.hpp:106
@ TY_SHORT
A 2-byte signed integer.
Definition COFF/Symbol.hpp:107
@ TY_FLOAT
A 4-byte floating-point number.
Definition COFF/Symbol.hpp:110
@ TY_DOUBLE
An 8-byte floating-point number.
Definition COFF/Symbol.hpp:111
STORAGE_CLASS
Reference: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#storage-class.
Definition COFF/Symbol.hpp:71
@ AUTOMATIC
Stack variable.
Definition COFF/Symbol.hpp:76
@ EXTERNAL_DEF
External definition.
Definition COFF/Symbol.hpp:80
@ TYPE_DEFINITION
Type definition.
Definition COFF/Symbol.hpp:88
@ EXTERNAL
External symbol.
Definition COFF/Symbol.hpp:77
@ ENUM_TAG
Enumeration tag.
Definition COFF/Symbol.hpp:90
@ SECTION
Definition COFF/Symbol.hpp:98
@ BLOCK
Definition COFF/Symbol.hpp:94
@ END_OF_STRUCT
End of structure.
Definition COFF/Symbol.hpp:96
@ FUNCTION
Definition COFF/Symbol.hpp:95
@ LABEL
Label.
Definition COFF/Symbol.hpp:81
@ MEMBER_OF_UNION
Member of union.
Definition COFF/Symbol.hpp:86
@ UNDEFINED_LABEL
Undefined label.
Definition COFF/Symbol.hpp:82
@ ARGUMENT
Function argument.
Definition COFF/Symbol.hpp:84
@ WEAK_EXTERNAL
Duplicate tag.
Definition COFF/Symbol.hpp:99
@ UNDEFINED_STATIC
Undefined static.
Definition COFF/Symbol.hpp:89
@ FILE
File name.
Definition COFF/Symbol.hpp:97
@ MEMBER_OF_ENUM
Member of enumeration.
Definition COFF/Symbol.hpp:91
@ REGISTER_PARAM
Register parameter.
Definition COFF/Symbol.hpp:92
@ END_OF_FUNCTION
Physical end of function.
Definition COFF/Symbol.hpp:74
@ STRUCT_TAG
Structure tag.
Definition COFF/Symbol.hpp:85
@ NONE
No symbol.
Definition COFF/Symbol.hpp:75
@ BIT_FIELD
Bit field.
Definition COFF/Symbol.hpp:93
@ UNION_TAG
Union tag.
Definition COFF/Symbol.hpp:87
@ INVALID
Definition COFF/Symbol.hpp:72
@ REGISTER
Register variable.
Definition COFF/Symbol.hpp:79
@ CLR_TOKEN
Definition COFF/Symbol.hpp:100
@ MEMBER_OF_STRUCT
Member of structure.
Definition COFF/Symbol.hpp:83
@ STATIC
Static.
Definition COFF/Symbol.hpp:78
Symbol & type(uint16_t ty)
Definition COFF/Symbol.hpp:239
static constexpr auto SYM_SEC_IDX_ABS
The symbol has an absolute (non-relocatable) value and is not an address.
Definition COFF/Symbol.hpp:62
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:60
const String * coff_name() const
COFF string used to represents the (long) symbol name.
Definition COFF/Symbol.hpp:227
it_auxiliary_symbols_t auxiliary_symbols()
Auxiliary symbols associated with this symbol.
Definition COFF/Symbol.hpp:212
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:66
std::string to_string() const
String * coff_name()
Definition COFF/Symbol.hpp:231
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:169
std::string & name() override
friend class Parser
Definition COFF/Symbol.hpp:37
static constexpr bool is_reversed_sec_idx(int16_t idx)
Check if the given section index is a reserved value.
Definition COFF/Symbol.hpp:130
bool is_undefined() const
Definition COFF/Symbol.hpp:194
ref_iterator< auxiliary_symbols_t &, AuxiliarySymbol * > it_auxiliary_symbols_t
Definition COFF/Symbol.hpp:40
AuxiliarySymbol & add_aux(std::unique_ptr< AuxiliarySymbol > sym)
Add a new auxiliary record.
bool is_weak_external() const
Definition COFF/Symbol.hpp:186
Symbol & operator=(Symbol &&)
it_const_auxiliary_symbols_t auxiliary_symbols() const
Definition COFF/Symbol.hpp:216
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:68
STORAGE_CLASS storage_class() const
Storage class of the symbol which indicates what kind of definition a symbol represents.
Definition COFF/Symbol.hpp:143
friend std::ostream & operator<<(std::ostream &os, const Symbol &entry)
Definition COFF/Symbol.hpp:260
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:28
virtual uint64_t value() const
Definition Abstract/Symbol.hpp:68
Iterator which returns reference on container's values.
Definition iterators.hpp:46
Definition AuxiliarySymbol.hpp:29
const char * to_string(AuxiliarySectionDefinition::COMDAT_SELECTION e)
LIEF namespace.
Definition Abstract/Binary.hpp:40
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which return const ref on container's values.
Definition iterators.hpp:257
Definition COFF/Symbol.hpp:43
bool is_bigobj
Definition COFF/Symbol.hpp:45
std::function< String *(uint32_t)> find_string
Definition COFF/Symbol.hpp:44
#define LIEF_API
Definition visibility.h:41