LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
MachO/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_MACHO_SYMBOL_H
17#define LIEF_MACHO_SYMBOL_H
18
19#include <ostream>
20
21#include "LIEF/visibility.h"
22
24
26
27namespace LIEF {
28namespace MachO {
29
30class BinaryParser;
31class BindingInfo;
32class ExportInfo;
33class DylibCommand;
34class Binary;
35
36namespace details {
37struct nlist_32;
38struct nlist_64;
39}
40
48
49 friend class BinaryParser;
50 friend class Binary;
51
52 public:
53 static constexpr int SELF_LIBRARY_ORD = 0x0; // Mirror SELF_LIBRARY_ORDINAL
54 static constexpr int MAIN_EXECUTABLE_ORD = 0xff; // Mirror DYNAMIC_LOOKUP_ORDINAL
55 static constexpr int DYNAMIC_LOOKUP_ORD = 0xfe; // EXECUTABLE_ORDINAL
56
60 enum class CATEGORY : uint32_t {
61 NONE = 0,
62 LOCAL,
63 EXTERNAL,
64 UNDEFINED,
65
66 INDIRECT_ABS,
67 INDIRECT_LOCAL,
68 INDIRECT_ABS_LOCAL,
69 };
70
71 enum class ORIGIN : uint32_t {
73 DYLD_EXPORT = 1,
75 DYLD_BIND = 2,
77 SYMTAB = 3,
78 };
79
80 enum class TYPE : uint32_t {
82 UNDEFINED = 0x0u,
84 ABSOLUTE_SYM = 0x2u,
86 SECTION = 0xeu,
89 PREBOUND = 0xcu,
94 INDIRECT = 0xau,
95 };
96
97
99 static constexpr uint32_t TYPE_MASK = 0x0e;
100
101 Symbol() = default;
102
103 Symbol(const details::nlist_32& cmd);
104 Symbol(const details::nlist_64& cmd);
105 Symbol(uint8_t n_type, uint8_t n_sect, uint8_t n_desc, uint64_t value) :
106 type_{n_type},
107 numberof_sections_{n_sect},
108 description_{n_desc},
109 origin_{ORIGIN::SYMTAB} {
110 value_ = value;
111 }
112
114 Symbol(const Symbol& other);
115 void swap(Symbol& other) noexcept;
116
117 ~Symbol() override = default;
118
119
120 static bool is_valid_index_ordinal(int idx) {
121 return idx != SELF_LIBRARY_ORD && idx != MAIN_EXECUTABLE_ORD &&
122 idx != DYNAMIC_LOOKUP_ORD;
123 }
124
125 int library_ordinal() const {
126 return (description() >> 8) & 0xff;
127 }
128
130 uint8_t raw_type() const {
131 return type_;
132 }
133
135 TYPE type() const {
136 return TYPE(type_ & TYPE_MASK);
137 }
138
141 uint8_t numberof_sections() const {
142 return numberof_sections_;
143 }
144
146 uint16_t description() const {
147 return description_;
148 }
149
152 bool has_export_info() const {
153 return export_info() != nullptr;
154 }
155
158 const ExportInfo* export_info() const {
159 return export_info_;
160 }
162 return export_info_;
163 }
164
167 bool has_binding_info() const {
168 return binding_info() != nullptr;
169 }
170
174 const BindingInfo* binding_info() const {
175 return binding_info_;
176 }
177
179 return binding_info_;
180 }
181
183 std::string demangled_name() const;
184
186 bool is_external() const {
187 return type() == TYPE::UNDEFINED;
188 }
189
192 const DylibCommand* library() const {
193 return library_;
194 }
195
197 return library_;
198 }
199
202 ORIGIN origin() const {
203 return origin_;
204 }
205
208 return category_;
209 }
210
211 void raw_type(uint8_t type) {
212 type_ = type;
213 }
214 void numberof_sections(uint8_t nbsections) {
215 numberof_sections_ = nbsections;
216 }
217 void description(uint16_t desc) {
218 description_ = desc;
219 }
220
221 void accept(Visitor& visitor) const override;
222
223 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Symbol& symbol);
224
225 static const Symbol& indirect_abs();
226 static const Symbol& indirect_local();
227 static const Symbol& indirect_abs_local();
228
229 private:
230 Symbol(CATEGORY cat) :
231 category_(cat) {}
232 void library(DylibCommand& library) {
233 this->library_ = &library;
234 }
235
236 uint8_t type_ = 0;
237 uint8_t numberof_sections_ = 0;
238 uint16_t description_ = 0;
239
240 BindingInfo* binding_info_ = nullptr;
241 ExportInfo* export_info_ = nullptr;
242
243 DylibCommand* library_ = nullptr;
244
245 ORIGIN origin_ = ORIGIN::UNKNOWN;
246 CATEGORY category_ = CATEGORY::NONE;
247};
248
252
253}
254}
255#endif
Class used to parse a single binary (i.e. non-FAT).
Definition BinaryParser.hpp:78
Class which represents a MachO binary.
Definition MachO/Binary.hpp:88
Class that provides an interface over a binding operation.
Definition BindingInfo.hpp:40
Class which represents a library dependency.
Definition DylibCommand.hpp:34
Class that provides an interface over the Dyld export info.
Definition ExportInfo.hpp:38
ORIGIN
Definition MachO/Symbol.hpp:71
void description(uint16_t desc)
Definition MachO/Symbol.hpp:217
uint8_t numberof_sections() const
It returns the number of sections in which this symbol can be found. If the symbol can't be found in ...
Definition MachO/Symbol.hpp:141
bool is_external() const
True if the symbol is defined as an external symbol.
Definition MachO/Symbol.hpp:186
const DylibCommand * library() const
Return the library in which the symbol is defined. It returns a null pointer if the library can't be ...
Definition MachO/Symbol.hpp:192
static const Symbol & indirect_local()
std::string demangled_name() const
Try to demangle the symbol or return an empty string if it is not possible.
static constexpr int DYNAMIC_LOOKUP_ORD
Definition MachO/Symbol.hpp:55
void raw_type(uint8_t type)
Definition MachO/Symbol.hpp:211
static constexpr int MAIN_EXECUTABLE_ORD
Definition MachO/Symbol.hpp:54
static bool is_valid_index_ordinal(int idx)
Definition MachO/Symbol.hpp:120
void swap(Symbol &other) noexcept
friend class BinaryParser
Definition MachO/Symbol.hpp:49
Symbol(const details::nlist_32 &cmd)
~Symbol() override=default
void accept(Visitor &visitor) const override
CATEGORY
Category of the symbol when the symbol comes from the LC_SYMTAB command. The category is defined acco...
Definition MachO/Symbol.hpp:60
uint16_t description() const
Return information about the symbol (SYMBOL_DESCRIPTIONS).
Definition MachO/Symbol.hpp:146
TYPE
Definition MachO/Symbol.hpp:80
@ UNDEFINED
The symbol is undefined. It is referenced in a different module.
Definition MachO/Symbol.hpp:82
Symbol(const details::nlist_64 &cmd)
void numberof_sections(uint8_t nbsections)
Definition MachO/Symbol.hpp:214
bool has_export_info() const
True if the symbol is associated with an ExportInfo This value is set when the symbol comes from the ...
Definition MachO/Symbol.hpp:152
BindingInfo * binding_info()
Definition MachO/Symbol.hpp:178
friend std::ostream & operator<<(std::ostream &os, const Symbol &symbol)
ORIGIN origin() const
Return the origin of the symbol: from LC_SYMTAB command or from the Dyld information.
Definition MachO/Symbol.hpp:202
uint8_t raw_type() const
Raw value of nlist_xx.n_type.
Definition MachO/Symbol.hpp:130
friend class Binary
Definition MachO/Symbol.hpp:50
int library_ordinal() const
Definition MachO/Symbol.hpp:125
static const Symbol & indirect_abs()
ExportInfo * export_info()
Definition MachO/Symbol.hpp:161
Symbol & operator=(Symbol other)
static constexpr int SELF_LIBRARY_ORD
Definition MachO/Symbol.hpp:53
static const Symbol & indirect_abs_local()
CATEGORY category() const
Category of the symbol according to the LC_DYSYMTAB command.
Definition MachO/Symbol.hpp:207
Symbol(const Symbol &other)
const ExportInfo * export_info() const
Return the ExportInfo associated with this symbol (or nullptr if not present).
Definition MachO/Symbol.hpp:158
const BindingInfo * binding_info() const
Return the BindingInfo associated with this symbol (or nullptr if not present).
Definition MachO/Symbol.hpp:174
bool has_binding_info() const
True if the symbol is associated with a BindingInfo This value is set when the symbol comes from the ...
Definition MachO/Symbol.hpp:167
DylibCommand * library()
Definition MachO/Symbol.hpp:196
Symbol(uint8_t n_type, uint8_t n_sect, uint8_t n_desc, uint64_t value)
Definition MachO/Symbol.hpp:105
static constexpr uint32_t TYPE_MASK
Same as N_TYPE.
Definition MachO/Symbol.hpp:99
TYPE type() const
Type as defined by nlist_xx.n_type & N_TYPE.
Definition MachO/Symbol.hpp:135
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:28
virtual uint64_t value() const
Definition Abstract/Symbol.hpp:68
Definition Visitor.hpp:212
Definition endianness_support.hpp:60
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
@ UNKNOWN
Definition MachO/enums.hpp:24
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF namespace.
Definition Abstract/Binary.hpp:40
#define LIEF_API
Definition visibility.h:43