LIEF: Library to Instrument Executable Formats Version 1.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
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>>;
44
46 std::function<String*(uint32_t)> find_string;
48 };
49 static std::unique_ptr<Symbol> parse(parsing_context_t& ctx,
50 BinaryStream& stream, size_t* idx);
51
53 Symbol(const Symbol&);
55
58
62 static constexpr auto SYM_SEC_IDX_DEBUG = -2;
64 static constexpr auto SYM_SEC_IDX_ABS = -1;
68 static constexpr auto SYM_SEC_IDX_UNDEF = 0;
69
70 static constexpr auto SYM_COMPLEX_TYPE_SHIFT = 4;
71
74 enum class STORAGE_CLASS : int32_t {
75 INVALID = 0xFF,
76
77 END_OF_FUNCTION = -1,
78 NONE = 0,
79 AUTOMATIC = 1,
80 EXTERNAL = 2,
81 STATIC = 3,
82 REGISTER = 4,
83 EXTERNAL_DEF = 5,
84 LABEL = 6,
85 UNDEFINED_LABEL = 7,
86 MEMBER_OF_STRUCT = 8,
87 ARGUMENT = 9,
88 STRUCT_TAG = 10,
89 MEMBER_OF_UNION = 11,
90 UNION_TAG = 12,
91 TYPE_DEFINITION = 13,
92 UNDEFINED_STATIC = 14,
93 ENUM_TAG = 15,
94 MEMBER_OF_ENUM = 16,
95 REGISTER_PARAM = 17,
96 BIT_FIELD = 18,
97 BLOCK = 100,
98 FUNCTION = 101,
99 END_OF_STRUCT = 102,
100 FILE = 103,
101 SECTION = 104,
102 WEAK_EXTERNAL = 105,
103 CLR_TOKEN = 107,
104 };
105
106 enum class BASE_TYPE : uint32_t {
107 TY_NULL = 0,
108 TY_VOID = 1,
109 TY_CHAR = 2,
110 TY_SHORT = 3,
111 TY_INT = 4,
112 TY_LONG = 5,
113 TY_FLOAT = 6,
114 TY_DOUBLE = 7,
115 TY_STRUCT = 8,
116 TY_UNION = 9,
117 TY_ENUM = 10,
118 TY_MOE = 11,
119 TY_BYTE = 12,
120 TY_WORD = 13,
121 TY_UINT = 14,
122 TY_DWORD = 15,
123 };
124
125 enum class COMPLEX_TYPE : uint32_t {
126 TY_NULL = 0,
127 TY_POINTER = 1,
128 TY_FUNCTION = 2,
129 TY_ARRAY = 3,
130 };
131
133 static constexpr bool is_reversed_sec_idx(int16_t idx) {
134 return idx <= 0;
135 }
136
140 uint16_t type() const {
141 return type_;
142 }
143
147 return (STORAGE_CLASS)storage_class_;
148 }
149
152 return (BASE_TYPE)(type_ & 0x0F);
153 }
154
157 return (COMPLEX_TYPE)((type_ & 0xF0) >> SYM_COMPLEX_TYPE_SHIFT);
158 }
159
172 int16_t section_idx() const {
173 return section_idx_;
174 }
175
178 return section_;
179 }
180
181 const Section* section() const {
182 return section_;
183 }
184
185 bool is_external() const {
187 }
188
189 bool is_weak_external() const {
191 }
192
193 bool is_absolute() const {
194 return section_idx() == SYM_SEC_IDX_ABS;
195 }
196
197 bool is_undefined() const {
198 return is_external() && section_idx() == SYM_SEC_IDX_UNDEF && value() == 0;
199 }
200
203 }
204
205 bool is_function() const {
207 }
208
209 bool is_file_record() const {
211 }
212
215 return auxiliary_symbols_;
216 }
217
219 return auxiliary_symbols_;
220 }
221
224 const std::string& name() const override;
225
226 std::string& name() override;
227
229 const String* coff_name() const {
230 return coff_name_;
231 }
232
234 return coff_name_;
235 }
236
239 std::string demangled_name() const;
240
241 Symbol& type(uint16_t ty) {
242 type_ = ty;
243 return *this;
244 }
245
247 storage_class_ = value;
248 return *this;
249 }
250
251 Symbol& section_idx(int16_t idx) {
252 section_idx_ = idx;
253 return *this;
254 }
255
257 AuxiliarySymbol& add_aux(std::unique_ptr<AuxiliarySymbol> sym);
258
259 std::string to_string() const;
260
261 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Symbol& entry) {
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(parsing_context_t& ctx,
271 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:35
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:34
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:140
bool is_absolute() const
Definition COFF/Symbol.hpp:193
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
ref_iterator< auxiliary_symbols_t &, AuxiliarySymbol * > it_auxiliary_symbols_t
Definition COFF/Symbol.hpp:40
COMPLEX_TYPE
Definition COFF/Symbol.hpp:125
@ TY_FUNCTION
A function that returns a base type.
Definition COFF/Symbol.hpp:128
Symbol & storage_class(uint8_t value)
Definition COFF/Symbol.hpp:246
const Section * section() const
Definition COFF/Symbol.hpp:181
COMPLEX_TYPE complex_type() const
The complex type (if any).
Definition COFF/Symbol.hpp:156
bool is_function_line_info() const
Definition COFF/Symbol.hpp:201
Symbol & section_idx(int16_t idx)
Definition COFF/Symbol.hpp:251
bool is_file_record() const
Definition COFF/Symbol.hpp:209
Section * section()
Section associated with this symbol (if any).
Definition COFF/Symbol.hpp:177
const_ref_iterator< const auxiliary_symbols_t &, AuxiliarySymbol * > it_const_auxiliary_symbols_t
Definition COFF/Symbol.hpp:42
bool is_function() const
Definition COFF/Symbol.hpp:205
BASE_TYPE base_type() const
The simple (base) data type.
Definition COFF/Symbol.hpp:151
std::vector< std::unique_ptr< AuxiliarySymbol > > auxiliary_symbols_t
Definition COFF/Symbol.hpp:39
bool is_external() const
Definition COFF/Symbol.hpp:185
BASE_TYPE
Definition COFF/Symbol.hpp:106
STORAGE_CLASS
Reference: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#storage-class.
Definition COFF/Symbol.hpp:74
@ EXTERNAL
External symbol.
Definition COFF/Symbol.hpp:80
@ FUNCTION
Definition COFF/Symbol.hpp:98
@ WEAK_EXTERNAL
Duplicate tag.
Definition COFF/Symbol.hpp:102
@ FILE
File name.
Definition COFF/Symbol.hpp:100
Symbol & type(uint16_t ty)
Definition COFF/Symbol.hpp:241
static constexpr auto SYM_SEC_IDX_ABS
The symbol has an absolute (non-relocatable) value and is not an address.
Definition COFF/Symbol.hpp:64
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:62
const String * coff_name() const
COFF string used to represents the (long) symbol name.
Definition COFF/Symbol.hpp:229
it_auxiliary_symbols_t auxiliary_symbols()
Auxiliary symbols associated with this symbol.
Definition COFF/Symbol.hpp:214
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:68
std::string to_string() const
String * coff_name()
Definition COFF/Symbol.hpp:233
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:172
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:133
bool is_undefined() const
Definition COFF/Symbol.hpp:197
AuxiliarySymbol & add_aux(std::unique_ptr< AuxiliarySymbol > sym)
Add a new auxiliary record.
bool is_weak_external() const
Definition COFF/Symbol.hpp:189
Symbol & operator=(Symbol &&)
it_const_auxiliary_symbols_t auxiliary_symbols() const
Definition COFF/Symbol.hpp:218
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:70
STORAGE_CLASS storage_class() const
Storage class of the symbol which indicates what kind of definition a symbol represents.
Definition COFF/Symbol.hpp:146
friend std::ostream & operator<<(std::ostream &os, const Symbol &entry)
Definition COFF/Symbol.hpp:261
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:45
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:286
Definition COFF/Symbol.hpp:45
bool is_bigobj
Definition COFF/Symbol.hpp:47
std::function< String *(uint32_t)> find_string
Definition COFF/Symbol.hpp:46
#define LIEF_API
Definition visibility.h:43