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;
33class LIEF_API Symbol : public LIEF::Symbol {
36 public:
37 friend class Parser;
38
39 using auxiliary_symbols_t = std::vector<std::unique_ptr<AuxiliarySymbol>>;
40 using it_auxiliary_symbols_t = ref_iterator<auxiliary_symbols_t&, AuxiliarySymbol*>;
41 using it_const_auxiliary_symbols_t = const_ref_iterator<const auxiliary_symbols_t&, AuxiliarySymbol*>;
42
43 struct parsing_context_t {
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&);
52 Symbol& operator=(const Symbol&);
53
54 Symbol(Symbol&&);
55 Symbol& operator=(Symbol&&);
56 static constexpr auto SYM_SEC_IDX_DEBUG = -2; static constexpr auto SYM_SEC_IDX_ABS = -1; static constexpr auto SYM_SEC_IDX_UNDEF = 0;
67
68 static constexpr auto SYM_COMPLEX_TYPE_SHIFT = 4;
69 enum class STORAGE_CLASS : int32_t {
72 INVALID = 0xFF,
73
74 END_OF_FUNCTION = -1,
75 NONE = 0,
76 AUTOMATIC = 1,
77 EXTERNAL = 2,
78 STATIC = 3,
79 REGISTER = 4,
80 EXTERNAL_DEF = 5,
81 LABEL = 6,
82 UNDEFINED_LABEL = 7,
83 MEMBER_OF_STRUCT = 8,
84 ARGUMENT = 9,
85 STRUCT_TAG = 10,
86 MEMBER_OF_UNION = 11,
87 UNION_TAG = 12,
88 TYPE_DEFINITION = 13,
89 UNDEFINED_STATIC = 14,
90 ENUM_TAG = 15,
91 MEMBER_OF_ENUM = 16,
92 REGISTER_PARAM = 17,
93 BIT_FIELD = 18,
94 BLOCK = 100,
95 FUNCTION = 101,
96 END_OF_STRUCT = 102,
97 FILE = 103,
98 SECTION = 104,
99 WEAK_EXTERNAL = 105,
100 CLR_TOKEN = 107
101 };
102
103 enum class BASE_TYPE : uint32_t {
104 TY_NULL = 0,
105 TY_VOID = 1,
106 TY_CHAR = 2,
107 TY_SHORT = 3,
108 TY_INT = 4,
109 TY_LONG = 5,
110 TY_FLOAT = 6,
111 TY_DOUBLE = 7,
112 TY_STRUCT = 8,
113 TY_UNION = 9,
114 TY_ENUM = 10,
115 TY_MOE = 11,
116 TY_BYTE = 12,
117 TY_WORD = 13,
118 TY_UINT = 14,
119 TY_DWORD = 15
120 };
121
122 enum class COMPLEX_TYPE : uint32_t {
123 TY_NULL = 0,
124 TY_POINTER = 1,
125 TY_FUNCTION = 2,
126 TY_ARRAY = 3,
127 };
128 static constexpr bool is_reversed_sec_idx(int16_t idx) {
131 return idx <= 0;
132 }
133 uint16_t type() const {
138 return type_;
139 }
140 STORAGE_CLASS storage_class() const {
144 return (STORAGE_CLASS)storage_class_;
145 }
146 BASE_TYPE base_type() const {
149 return (BASE_TYPE)(type_ & 0x0F);
150 }
151 COMPLEX_TYPE complex_type() const {
154 return (COMPLEX_TYPE)((type_ & 0xF0) >> SYM_COMPLEX_TYPE_SHIFT);
155 }
156 int16_t section_idx() const {
170 return section_idx_;
171 }
172 Section* section() {
175 return section_;
176 }
177
178 const Section* section() const {
179 return section_;
180 }
181
182 bool is_external() const {
183 return storage_class() == STORAGE_CLASS::EXTERNAL;
184 }
185
186 bool is_weak_external() const {
187 return storage_class() == STORAGE_CLASS::WEAK_EXTERNAL;
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
199 bool is_function_line_info() const {
200 return storage_class() == STORAGE_CLASS::FUNCTION;
201 }
202
203 bool is_function() const {
204 return complex_type() == COMPLEX_TYPE::TY_FUNCTION;
205 }
206
207 bool is_file_record() const {
208 return storage_class() == STORAGE_CLASS::FILE;
209 }
210 it_auxiliary_symbols_t auxiliary_symbols() {
213 return auxiliary_symbols_;
214 }
215
216 it_const_auxiliary_symbols_t auxiliary_symbols() const {
217 return auxiliary_symbols_;
218 }
219 const std::string& name() const override;
223
224 std::string& name() override;
225 const String* coff_name() const {
228 return coff_name_;
229 }
230
231 String* coff_name() {
232 return coff_name_;
233 }
234 std::string demangled_name() const;
238
239 Symbol& type(uint16_t ty) {
240 type_ = ty;
241 return *this;
242 }
243
244 Symbol& storage_class(uint8_t value) {
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 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
280LIEF_API const char* to_string(Symbol::STORAGE_CLASS e);
281LIEF_API const char* to_string(Symbol::BASE_TYPE e);
282LIEF_API const char* to_string(Symbol::COMPLEX_TYPE e);
283
284}
285}
286#endif
Symbol.hpp
LIEF::BinaryStream
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
LIEF::COFF::AuxiliarySymbol
Class that represents an auxiliary symbol.
Definition AuxiliarySymbol.hpp:36
LIEF::COFF::Parser
Definition COFF/Parser.hpp:34
LIEF::COFF::Section
This class represents a COFF section.
Definition COFF/Section.hpp:39
LIEF::COFF::String
This class represents a string located in the COFF string table.
Definition String.hpp:33
LIEF::COFF::Symbol
This class represents a COFF symbol.
Definition COFF/Symbol.hpp:35
LIEF::COFF::Symbol::type
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
LIEF::COFF::Symbol::is_absolute
bool is_absolute() const
Definition COFF/Symbol.hpp:190
LIEF::COFF::Symbol::name
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...
LIEF::COFF::Symbol::~Symbol
~Symbol() override
LIEF::COFF::Symbol::COMPLEX_TYPE
COMPLEX_TYPE
Definition COFF/Symbol.hpp:122
LIEF::COFF::Symbol::storage_class
Symbol & storage_class(uint8_t value)
Definition COFF/Symbol.hpp:244
LIEF::COFF::Symbol::section
const Section * section() const
Definition COFF/Symbol.hpp:178
LIEF::COFF::Symbol::complex_type
COMPLEX_TYPE complex_type() const
The complex type (if any)
Definition COFF/Symbol.hpp:153
LIEF::COFF::Symbol::is_function_line_info
bool is_function_line_info() const
Definition COFF/Symbol.hpp:199
LIEF::COFF::Symbol::section_idx
Symbol & section_idx(int16_t idx)
Definition COFF/Symbol.hpp:249
LIEF::COFF::Symbol::is_file_record
bool is_file_record() const
Definition COFF/Symbol.hpp:207
LIEF::COFF::Symbol::section
Section * section()
Section associated with this symbol (if any)
Definition COFF/Symbol.hpp:174
LIEF::COFF::Symbol::is_function
bool is_function() const
Definition COFF/Symbol.hpp:203
LIEF::COFF::Symbol::base_type
BASE_TYPE base_type() const
The simple (base) data type.
Definition COFF/Symbol.hpp:148
LIEF::COFF::Symbol::is_external
bool is_external() const
Definition COFF/Symbol.hpp:182
LIEF::COFF::Symbol::BASE_TYPE
BASE_TYPE
Definition COFF/Symbol.hpp:103
LIEF::COFF::Symbol::STORAGE_CLASS
STORAGE_CLASS
Reference: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#storage-class.
Definition COFF/Symbol.hpp:71
LIEF::COFF::Symbol::type
Symbol & type(uint16_t ty)
Definition COFF/Symbol.hpp:239
LIEF::COFF::Symbol::coff_name
const String * coff_name() const
COFF string used to represents the (long) symbol name.
Definition COFF/Symbol.hpp:227
LIEF::COFF::Symbol::auxiliary_symbols
it_auxiliary_symbols_t auxiliary_symbols()
Auxiliary symbols associated with this symbol.
Definition COFF/Symbol.hpp:212
LIEF::COFF::Symbol::Symbol
Symbol()
LIEF::COFF::Symbol::to_string
std::string to_string() const
LIEF::COFF::Symbol::coff_name
String * coff_name()
Definition COFF/Symbol.hpp:231
LIEF::COFF::Symbol::parse
static std::unique_ptr< Symbol > parse(parsing_context_t &ctx, BinaryStream &stream, size_t *idx)
LIEF::COFF::Symbol::section_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
LIEF::COFF::Symbol::name
std::string & name() override
LIEF::COFF::Symbol::is_reversed_sec_idx
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
LIEF::COFF::Symbol::is_undefined
bool is_undefined() const
Definition COFF/Symbol.hpp:194
LIEF::COFF::Symbol::Symbol
Symbol(Symbol &&)
LIEF::COFF::Symbol::add_aux
AuxiliarySymbol & add_aux(std::unique_ptr< AuxiliarySymbol > sym)
Add a new auxiliary record.
LIEF::COFF::Symbol::is_weak_external
bool is_weak_external() const
Definition COFF/Symbol.hpp:186
LIEF::COFF::Symbol::operator=
Symbol & operator=(Symbol &&)
LIEF::COFF::Symbol::auxiliary_symbols
it_const_auxiliary_symbols_t auxiliary_symbols() const
Definition COFF/Symbol.hpp:216
LIEF::COFF::Symbol::Symbol
Symbol(const Symbol &)
LIEF::COFF::Symbol::operator=
Symbol & operator=(const Symbol &)
LIEF::COFF::Symbol::demangled_name
std::string demangled_name() const
Demangled representation of the symbol or an empty string if it can't be demangled.
LIEF::COFF::Symbol::storage_class
STORAGE_CLASS storage_class() const
Storage class of the symbol which indicates what kind of definition a symbol represents.
Definition COFF/Symbol.hpp:143
LIEF::COFF::Symbol::operator<<
friend std::ostream & operator<<(std::ostream &os, const Symbol &entry)
Definition COFF/Symbol.hpp:260
iterators.hpp
LIEF::COFF
Definition AuxiliarySymbol.hpp:29
LIEF::COFF::to_string
const char * to_string(AuxiliarySectionDefinition::COMDAT_SELECTION e)
LIEF::COFF::is_bigobj
bool is_bigobj(BinaryStream &stream)
Check if the COFF file wrapped by the given stream is a bigobj
Definition COFF/utils.hpp:51
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:39
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41