LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
MachO/Symbol.hpp
Go to the documentation of this file.
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
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}
40class LIEF_API Symbol : public LIEF::Symbol {
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 enum class CATEGORY : uint32_t {
60 NONE = 0,
61 LOCAL,
62 EXTERNAL,
63 UNDEFINED,
64
65 INDIRECT_ABS,
66 INDIRECT_LOCAL,
67 INDIRECT_ABS_LOCAL,
68 };
69
70 enum class ORIGIN : uint32_t {
72 DYLD_EXPORT = 1,
73 DYLD_BIND = 2,
74 LC_SYMTAB = 3,
75 };
76
77 enum class TYPE : uint32_t{
78 UNDEFINED = 0x0u,
79 ABSOLUTE_SYM = 0x2u,
80 SECTION = 0xeu,
81 PREBOUND = 0xcu,
82 INDIRECT = 0xau
83 };
84
85 static constexpr uint32_t TYPE_MASK = 0x0e;
88
89 Symbol() = default;
90
91 Symbol(const details::nlist_32& cmd);
92 Symbol(const details::nlist_64& cmd);
93 Symbol(uint8_t n_type, uint8_t n_sect, uint8_t n_desc, uint64_t value) :
94 type_{n_type},
95 numberof_sections_{n_sect},
96 description_{n_desc},
97 origin_{ORIGIN::LC_SYMTAB}
98 {
99 value_ = value;
100 }
101
102 Symbol& operator=(Symbol other);
103 Symbol(const Symbol& other);
104 void swap(Symbol& other) noexcept;
105
106 ~Symbol() override = default;
107
108
109 static bool is_valid_index_ordinal(int idx) {
110 return idx != SELF_LIBRARY_ORD && idx != MAIN_EXECUTABLE_ORD &&
111 idx != DYNAMIC_LOOKUP_ORD;
112 }
113
114 int library_ordinal() const {
115 return (description() >> 8) & 0xff;
116 }
117 uint8_t raw_type() const {
120 return type_;
121 }
122 TYPE type() const {
125 return TYPE(type_ & TYPE_MASK);
126 }
127 uint8_t numberof_sections() const {
131 return numberof_sections_;
132 }
133 uint16_t description() const {
136 return description_;
137 }
138 bool has_export_info() const {
142 return export_info() != nullptr;
143 }
144 const ExportInfo* export_info() const {
148 return export_info_;
149 }
150 ExportInfo* export_info() {
151 return export_info_;
152 }
153 bool has_binding_info() const {
157 return binding_info() != nullptr;
158 }
159 const BindingInfo* binding_info() const {
163 return binding_info_;
164 }
165
166 BindingInfo* binding_info() {
167 return binding_info_;
168 }
169 std::string demangled_name() const;
172 bool is_external() const {
175 return type() == TYPE::UNDEFINED;
176 }
177 const DylibCommand* library() const {
181 return library_;
182 }
183
184 DylibCommand* library() {
185 return library_;
186 }
187 ORIGIN origin() const {
190 return origin_;
191 }
192 CATEGORY category() const {
195 return category_;
196 }
197
198 void raw_type(uint8_t type) {
199 type_ = type;
200 }
201 void numberof_sections(uint8_t nbsections) {
202 numberof_sections_ = nbsections;
203 }
204 void description(uint16_t desc) {
205 description_ = desc;
206 }
207
208 void accept(Visitor& visitor) const override;
209
210 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Symbol& symbol);
211
212 static const Symbol& indirect_abs();
213 static const Symbol& indirect_local();
214 static const Symbol& indirect_abs_local();
215
216 private:
217 Symbol(CATEGORY cat) :
218 category_(cat)
219 {}
220 void library(DylibCommand& library) {
221 this->library_ = &library;
222 }
223
224 uint8_t type_ = 0;
225 uint8_t numberof_sections_ = 0;
226 uint16_t description_ = 0;
227
228 BindingInfo* binding_info_ = nullptr;
229 ExportInfo* export_info_ = nullptr;
230
231 DylibCommand* library_ = nullptr;
232
233 ORIGIN origin_ = ORIGIN::UNKNOWN;
234 CATEGORY category_ = CATEGORY::NONE;
235};
236
237LIEF_API const char* to_string(Symbol::ORIGIN e);
238LIEF_API const char* to_string(Symbol::CATEGORY e);
239LIEF_API const char* to_string(Symbol::TYPE e);
240
241}
242}
243#endif
Symbol.hpp
LoadCommand.hpp
LIEF::MachO::BinaryParser
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:74
LIEF::MachO::Binary
Class which represents a MachO binary.
Definition MachO/Binary.hpp:85
LIEF::MachO::BindingInfo
Class that provides an interface over a binding operation.
Definition BindingInfo.hpp:39
LIEF::MachO::DylibCommand
Class which represents a library dependency.
Definition DylibCommand.hpp:34
LIEF::MachO::ExportInfo
Class that provides an interface over the Dyld export info.
Definition ExportInfo.hpp:38
LIEF::MachO::Symbol
Class that represents a Symbol in a Mach-O file.
Definition MachO/Symbol.hpp:47
LIEF::MachO::Symbol::ORIGIN
ORIGIN
Definition MachO/Symbol.hpp:70
LIEF::MachO::Symbol::description
void description(uint16_t desc)
Definition MachO/Symbol.hpp:204
LIEF::MachO::Symbol::numberof_sections
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:130
LIEF::MachO::Symbol::is_external
bool is_external() const
True if the symbol is defined as an external symbol.
Definition MachO/Symbol.hpp:174
LIEF::MachO::Symbol::library
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:180
LIEF::MachO::Symbol::indirect_local
static const Symbol & indirect_local()
LIEF::MachO::Symbol::demangled_name
std::string demangled_name() const
Try to demangle the symbol or return an empty string if it is not possible.
LIEF::MachO::Symbol::Symbol
Symbol()=default
LIEF::MachO::Symbol::raw_type
void raw_type(uint8_t type)
Definition MachO/Symbol.hpp:198
LIEF::MachO::Symbol::is_valid_index_ordinal
static bool is_valid_index_ordinal(int idx)
Definition MachO/Symbol.hpp:109
LIEF::MachO::Symbol::swap
void swap(Symbol &other) noexcept
LIEF::MachO::Symbol::Symbol
Symbol(const details::nlist_32 &cmd)
LIEF::MachO::Symbol::~Symbol
~Symbol() override=default
LIEF::MachO::Symbol::accept
void accept(Visitor &visitor) const override
LIEF::MachO::Symbol::CATEGORY
CATEGORY
Category of the symbol when the symbol comes from the LC_SYMTAB command. The category is defined acco...
Definition MachO/Symbol.hpp:59
LIEF::MachO::Symbol::description
uint16_t description() const
Return information about the symbol (SYMBOL_DESCRIPTIONS)
Definition MachO/Symbol.hpp:135
LIEF::MachO::Symbol::TYPE
TYPE
Definition MachO/Symbol.hpp:77
LIEF::MachO::Symbol::Symbol
Symbol(const details::nlist_64 &cmd)
LIEF::MachO::Symbol::numberof_sections
void numberof_sections(uint8_t nbsections)
Definition MachO/Symbol.hpp:201
LIEF::MachO::Symbol::has_export_info
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:141
LIEF::MachO::Symbol::binding_info
BindingInfo * binding_info()
Definition MachO/Symbol.hpp:166
LIEF::MachO::Symbol::operator<<
friend std::ostream & operator<<(std::ostream &os, const Symbol &symbol)
LIEF::MachO::Symbol::origin
ORIGIN origin() const
Return the origin of the symbol: from LC_SYMTAB command or from the Dyld information.
Definition MachO/Symbol.hpp:189
LIEF::MachO::Symbol::raw_type
uint8_t raw_type() const
Raw value of nlist_xx.n_type
Definition MachO/Symbol.hpp:119
LIEF::MachO::Symbol::library_ordinal
int library_ordinal() const
Definition MachO/Symbol.hpp:114
LIEF::MachO::Symbol::indirect_abs
static const Symbol & indirect_abs()
LIEF::MachO::Symbol::export_info
ExportInfo * export_info()
Definition MachO/Symbol.hpp:150
LIEF::MachO::Symbol::operator=
Symbol & operator=(Symbol other)
LIEF::MachO::Symbol::indirect_abs_local
static const Symbol & indirect_abs_local()
LIEF::MachO::Symbol::category
CATEGORY category() const
Category of the symbol according to the LC_DYSYMTAB command.
Definition MachO/Symbol.hpp:194
LIEF::MachO::Symbol::Symbol
Symbol(const Symbol &other)
LIEF::MachO::Symbol::export_info
const ExportInfo * export_info() const
Return the ExportInfo associated with this symbol (or nullptr if not present)
Definition MachO/Symbol.hpp:147
LIEF::MachO::Symbol::binding_info
const BindingInfo * binding_info() const
Return the BindingInfo associated with this symbol (or nullptr if not present)
Definition MachO/Symbol.hpp:162
LIEF::MachO::Symbol::has_binding_info
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:156
LIEF::MachO::Symbol::library
DylibCommand * library()
Definition MachO/Symbol.hpp:184
LIEF::MachO::Symbol::Symbol
Symbol(uint8_t n_type, uint8_t n_sect, uint8_t n_desc, uint64_t value)
Definition MachO/Symbol.hpp:93
LIEF::MachO::Symbol::type
TYPE type() const
Type as defined by nlist_xx.n_type & N_TYPE
Definition MachO/Symbol.hpp:124
LIEF::MachO::details
Definition endianness_support.hpp:59
LIEF::MachO
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF::MachO::MACHO_TYPES::UNKNOWN
@ UNKNOWN
Definition MachO/enums.hpp:25
LIEF::MachO::to_string
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41