LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
MachO/Symbol.hpp
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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
23#include "LIEF/Abstract/Symbol.hpp"
24
25#include "LIEF/MachO/LoadCommand.hpp"
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
47class LIEF_API Symbol : public LIEF::Symbol {
48
49 friend class BinaryParser;
50 friend class Binary;
51
52 public:
53
56 enum class CATEGORY : uint32_t {
57 NONE = 0,
58 LOCAL,
59 EXTERNAL,
60 UNDEFINED,
61
62 INDIRECT_ABS,
63 INDIRECT_LOCAL,
64 };
65
66 enum class ORIGIN : uint32_t {
67 UNKNOWN = 0,
68 DYLD_EXPORT = 1,
69 DYLD_BIND = 2,
70 LC_SYMTAB = 3,
71 };
72
73 enum class TYPE : uint32_t{
74 UNDEFINED = 0x0u,
75 ABSOLUTE_SYM = 0x2u,
76 SECTION = 0xeu,
77 PREBOUND = 0xcu,
78 INDIRECT = 0xau
79 };
80
82 static constexpr uint32_t TYPE_MASK = 0x0e;
83
84 Symbol() = default;
85
86 Symbol(const details::nlist_32& cmd);
87 Symbol(const details::nlist_64& cmd);
88
89 Symbol& operator=(Symbol other);
90 Symbol(const Symbol& other);
91 void swap(Symbol& other) noexcept;
92
93 ~Symbol() override = default;
94
96 uint8_t raw_type() const {
97 return type_;
98 }
99
101 TYPE type() const {
102 return TYPE(type_ & TYPE_MASK);
103 }
104
107 uint8_t numberof_sections() const {
108 return numberof_sections_;
109 }
110
112 uint16_t description() const {
113 return description_;
114 }
115
118 bool has_export_info() const {
119 return export_info() != nullptr;
120 }
121
124 const ExportInfo* export_info() const {
125 return export_info_;
126 }
127 ExportInfo* export_info() {
128 return export_info_;
129 }
130
133 bool has_binding_info() const {
134 return binding_info() != nullptr;
135 }
136
139 const BindingInfo* binding_info() const {
140 return binding_info_;
141 }
142
143 BindingInfo* binding_info() {
144 return binding_info_;
145 }
146
148 std::string demangled_name() const;
149
151 bool is_external() const {
152 return type() == TYPE::UNDEFINED;
153 }
154
157 const DylibCommand* library() const {
158 return library_;
159 }
160
161 DylibCommand* library() {
162 return library_;
163 }
164
166 ORIGIN origin() const {
167 return origin_;
168 }
169
172 return category_;
173 }
174
175 void raw_type(uint8_t type) {
176 type_ = type;
177 }
178 void numberof_sections(uint8_t nbsections) {
179 numberof_sections_ = nbsections;
180 }
181 void description(uint16_t desc) {
182 description_ = desc;
183 }
184
185 void accept(Visitor& visitor) const override;
186
187 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Symbol& symbol);
188
189 static const Symbol& indirect_abs();
190 static const Symbol& indirect_local();
191
192 private:
193 Symbol(CATEGORY cat) :
194 category_(cat)
195 {}
196 void library(DylibCommand& library) {
197 this->library_ = &library;
198 }
199
200 uint8_t type_ = 0;
201 uint8_t numberof_sections_ = 0;
202 uint16_t description_ = 0;
203
204 BindingInfo* binding_info_ = nullptr;
205 ExportInfo* export_info_ = nullptr;
206
207 DylibCommand* library_ = nullptr;
208
209 ORIGIN origin_ = ORIGIN::UNKNOWN;
210 CATEGORY category_ = CATEGORY::NONE;
211};
212
213LIEF_API const char* to_string(Symbol::ORIGIN e);
214LIEF_API const char* to_string(Symbol::CATEGORY e);
215LIEF_API const char* to_string(Symbol::TYPE e);
216
217}
218}
219#endif
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:74
Class which represents a MachO binary.
Definition MachO/Binary.hpp:73
Class that provides an interface over a binding operation.
Definition BindingInfo.hpp:38
Class which represents a library dependency.
Definition DylibCommand.hpp:34
Class that provides an interface over the Dyld export info.
Definition ExportInfo.hpp:38
Class that represents a Symbol in a Mach-O file.
Definition MachO/Symbol.hpp:47
ORIGIN
Definition MachO/Symbol.hpp:66
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:107
bool is_external() const
True if the symbol is defined as an external symbol.
Definition MachO/Symbol.hpp:151
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:157
std::string demangled_name() const
Try to demangle the symbol or return an empty string if it is not possible.
CATEGORY
Category of the symbol when the symbol comes from the LC_SYMTAB command. The category is defined acco...
Definition MachO/Symbol.hpp:56
uint16_t description() const
Return information about the symbol (SYMBOL_DESCRIPTIONS)
Definition MachO/Symbol.hpp:112
TYPE
Definition MachO/Symbol.hpp:73
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:118
ORIGIN origin() const
Return the origin of the symbol: from LC_SYMTAB command or from the Dyld information.
Definition MachO/Symbol.hpp:166
uint8_t raw_type() const
Raw value of nlist_xx.n_type
Definition MachO/Symbol.hpp:96
CATEGORY category() const
Category of the symbol according to the LC_DYSYMTAB command.
Definition MachO/Symbol.hpp:171
const ExportInfo * export_info() const
Return the ExportInfo associated with this symbol (or nullptr if not present)
Definition MachO/Symbol.hpp:124
const BindingInfo * binding_info() const
Return the BindingInfo associated with this symbol (or nullptr if not present)
Definition MachO/Symbol.hpp:139
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:133
TYPE type() const
Type as defined by nlist_xx.n_type & N_TYPE
Definition MachO/Symbol.hpp:101
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:28
LIEF namespace.
Definition Abstract/Binary.hpp:31