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;
63
65 static constexpr auto SYM_SEC_IDX_ABS = -1;
66
70 static constexpr auto SYM_SEC_IDX_UNDEF = 0;
71
72 static constexpr auto SYM_COMPLEX_TYPE_SHIFT = 4;
73
76 enum class STORAGE_CLASS : int32_t {
77 INVALID = 0xFF,
78
79 END_OF_FUNCTION = -1,
80 NONE = 0,
81 AUTOMATIC = 1,
82 EXTERNAL = 2,
83 STATIC = 3,
84 REGISTER = 4,
85 EXTERNAL_DEF = 5,
86 LABEL = 6,
87 UNDEFINED_LABEL = 7,
88 MEMBER_OF_STRUCT = 8,
89 ARGUMENT = 9,
90 STRUCT_TAG = 10,
91 MEMBER_OF_UNION = 11,
92 UNION_TAG = 12,
93 TYPE_DEFINITION = 13,
94 UNDEFINED_STATIC = 14,
95 ENUM_TAG = 15,
96 MEMBER_OF_ENUM = 16,
97 REGISTER_PARAM = 17,
98 BIT_FIELD = 18,
99 BLOCK = 100,
100 FUNCTION = 101,
101 END_OF_STRUCT = 102,
102 FILE = 103,
103 SECTION = 104,
104 WEAK_EXTERNAL = 105,
105 CLR_TOKEN = 107,
106 };
107
108 enum class BASE_TYPE : uint32_t {
109 TY_NULL = 0,
110 TY_VOID = 1,
111 TY_CHAR = 2,
112 TY_SHORT = 3,
113 TY_INT = 4,
114 TY_LONG = 5,
115 TY_FLOAT = 6,
116 TY_DOUBLE = 7,
117 TY_STRUCT = 8,
118 TY_UNION = 9,
119 TY_ENUM = 10,
120 TY_MOE = 11,
121 TY_BYTE = 12,
122 TY_WORD = 13,
123 TY_UINT = 14,
124 TY_DWORD = 15,
125 };
126
127 enum class COMPLEX_TYPE : uint32_t {
128 TY_NULL = 0,
129 TY_POINTER = 1,
130 TY_FUNCTION = 2,
131 TY_ARRAY = 3,
132 };
133
135 static constexpr bool is_reversed_sec_idx(int16_t idx) {
136 return idx <= 0;
137 }
138
142 uint16_t type() const {
143 return type_;
144 }
145
149 return (STORAGE_CLASS)storage_class_;
150 }
151
154 return (BASE_TYPE)(type_ & 0x0F);
155 }
156
159 return (COMPLEX_TYPE)((type_ & 0xF0) >> SYM_COMPLEX_TYPE_SHIFT);
160 }
161
174 int16_t section_idx() const {
175 return section_idx_;
176 }
177
180 return section_;
181 }
182
183 const Section* section() const {
184 return section_;
185 }
186
187 bool is_external() const {
189 }
190
191 bool is_weak_external() const {
193 }
194
195 bool is_absolute() const {
196 return section_idx() == SYM_SEC_IDX_ABS;
197 }
198
199 bool is_undefined() const {
200 return is_external() && section_idx() == SYM_SEC_IDX_UNDEF && value() == 0;
201 }
202
205 }
206
207 bool is_function() const {
209 }
210
211 bool is_file_record() const {
213 }
214
217 return auxiliary_symbols_;
218 }
219
221 return auxiliary_symbols_;
222 }
223
226 const std::string& name() const override;
227
228 std::string& name() override;
229
231 const String* coff_name() const {
232 return coff_name_;
233 }
234
236 return coff_name_;
237 }
238
241 std::string demangled_name() const;
242
243 Symbol& type(uint16_t ty) {
244 type_ = ty;
245 return *this;
246 }
247
249 storage_class_ = value;
250 return *this;
251 }
252
253 Symbol& section_idx(int16_t idx) {
254 section_idx_ = idx;
255 return *this;
256 }
257
259 AuxiliarySymbol& add_aux(std::unique_ptr<AuxiliarySymbol> sym);
260
261 std::string to_string() const;
262
263 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Symbol& entry) {
264 os << entry.to_string();
265 return os;
266 }
267
268 ~Symbol() override;
269
270 private:
271 template<class T>
272 static std::unique_ptr<Symbol> parse_impl(parsing_context_t& ctx,
273 BinaryStream& stream, size_t* idx);
274 String* coff_name_ = nullptr;
275 uint16_t type_ = 0;
276 uint8_t storage_class_ = 0;
277 int16_t section_idx_ = 0;
278 auxiliary_symbols_t auxiliary_symbols_;
279 Section* section_ = nullptr;
280};
281
285
286}
287}
288#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:40
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:142
bool is_absolute() const
Definition COFF/Symbol.hpp:195
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:127
@ TY_FUNCTION
A function that returns a base type.
Definition COFF/Symbol.hpp:130
Symbol & storage_class(uint8_t value)
Definition COFF/Symbol.hpp:248
const Section * section() const
Definition COFF/Symbol.hpp:183
COMPLEX_TYPE complex_type() const
The complex type (if any).
Definition COFF/Symbol.hpp:158
bool is_function_line_info() const
Definition COFF/Symbol.hpp:203
Symbol & section_idx(int16_t idx)
Definition COFF/Symbol.hpp:253
bool is_file_record() const
Definition COFF/Symbol.hpp:211
Section * section()
Section associated with this symbol (if any).
Definition COFF/Symbol.hpp:179
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:207
BASE_TYPE base_type() const
The simple (base) data type.
Definition COFF/Symbol.hpp:153
std::vector< std::unique_ptr< AuxiliarySymbol > > auxiliary_symbols_t
Definition COFF/Symbol.hpp:39
bool is_external() const
Definition COFF/Symbol.hpp:187
BASE_TYPE
Definition COFF/Symbol.hpp:108
STORAGE_CLASS
Reference: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#storage-class.
Definition COFF/Symbol.hpp:76
@ EXTERNAL
External symbol.
Definition COFF/Symbol.hpp:82
@ FUNCTION
Definition COFF/Symbol.hpp:100
@ WEAK_EXTERNAL
Duplicate tag.
Definition COFF/Symbol.hpp:104
@ FILE
File name.
Definition COFF/Symbol.hpp:102
Symbol & type(uint16_t ty)
Definition COFF/Symbol.hpp:243
static constexpr auto SYM_SEC_IDX_ABS
The symbol has an absolute (non-relocatable) value and is not an address.
Definition COFF/Symbol.hpp:65
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 represent the (long) symbol name.
Definition COFF/Symbol.hpp:231
it_auxiliary_symbols_t auxiliary_symbols()
Auxiliary symbols associated with this symbol.
Definition COFF/Symbol.hpp:216
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:70
std::string to_string() const
String * coff_name()
Definition COFF/Symbol.hpp:235
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:174
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:135
bool is_undefined() const
Definition COFF/Symbol.hpp:199
AuxiliarySymbol & add_aux(std::unique_ptr< AuxiliarySymbol > sym)
Add a new auxiliary record.
bool is_weak_external() const
Definition COFF/Symbol.hpp:191
Symbol & operator=(Symbol &&)
it_const_auxiliary_symbols_t auxiliary_symbols() const
Definition COFF/Symbol.hpp:220
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:72
STORAGE_CLASS storage_class() const
Storage class of the symbol which indicates what kind of definition a symbol represents.
Definition COFF/Symbol.hpp:148
friend std::ostream & operator<<(std::ostream &os, const Symbol &entry)
Definition COFF/Symbol.hpp:263
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:28
virtual uint64_t value() const
Symbol's value which is usually the address of the symbol.
Definition Abstract/Symbol.hpp:68
Iterator which returns reference on container's values.
Definition iterators.hpp:47
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:322
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:45