LIEF: Library to Instrument Executable Formats Version
Loading...
Searching...
No Matches
DyldInfo.hpp
1/* Copyright 2017 - 2023 R. Thomas
2 * Copyright 2017 - 2023 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_DYLD_INFO_COMMAND_H
17#define LIEF_MACHO_DYLD_INFO_COMMAND_H
18#include <string>
19#include <set>
20#include <vector>
21#include <ostream>
22#include <memory>
23
24#include "LIEF/visibility.h"
25#include "LIEF/types.hpp"
26#include "LIEF/span.hpp"
27
28#include "LIEF/MachO/LoadCommand.hpp"
29#include "LIEF/MachO/type_traits.hpp"
30#include "LIEF/iterators.hpp"
31
32namespace LIEF {
33class vector_iostream;
34class BinaryStream;
35namespace MachO {
36
37class Binary;
38class BinaryParser;
39class Builder;
40class DyldBindingInfo;
41class ExportInfo;
42class LinkEdit;
43class RelocationDyld;
44
45namespace details {
46struct dyld_info_command;
47}
48
50class LIEF_API DyldInfo : public LoadCommand {
51
52 friend class BinaryParser;
53 friend class Binary;
54 friend class Builder;
55 friend class LinkEdit;
56
57 public:
59 using info_t = std::pair<uint32_t, uint32_t>;
60
62 using binding_info_t = std::vector<std::unique_ptr<DyldBindingInfo>>;
63
66
69
71 using export_info_t = std::vector<std::unique_ptr<ExportInfo>>;
72
75
78
79
80 enum class BINDING_ENCODING_VERSION {
81 UNKNOWN = 0,
82 V1,
83 V2
84 };
85
86 DyldInfo();
87 DyldInfo(const details::dyld_info_command& dyld_info_cmd);
88
89 DyldInfo& operator=(DyldInfo other);
90 DyldInfo(const DyldInfo& copy);
91
92 void swap(DyldInfo& other);
93
94 DyldInfo* clone() const override;
95
96 ~DyldInfo() override;
97
111 const info_t& rebase() const;
112
114 span<const uint8_t> rebase_opcodes() const;
115 span<uint8_t> rebase_opcodes();
116
118 void rebase_opcodes(buffer_t raw);
119
120
122 std::string show_rebases_opcodes() const;
123
138 const info_t& bind() const;
139
141 span<const uint8_t> bind_opcodes() const;
142 span<uint8_t> bind_opcodes();
143
145 void bind_opcodes(buffer_t raw);
146
148 std::string show_bind_opcodes() const;
149
167 const info_t& weak_bind() const;
168
170 span<const uint8_t> weak_bind_opcodes() const;
171 span<uint8_t> weak_bind_opcodes();
172
174 void weak_bind_opcodes(buffer_t raw);
175
177 std::string show_weak_bind_opcodes() const;
178
193 const info_t& lazy_bind() const;
194
196 span<const uint8_t> lazy_bind_opcodes() const;
197 span<uint8_t> lazy_bind_opcodes();
198
200 void lazy_bind_opcodes(buffer_t raw);
201
203 std::string show_lazy_bind_opcodes() const;
204
207 it_const_binding_info bindings() const;
208
235 const info_t& export_info() const;
236
239 it_const_export_info exports() const;
240
242 span<const uint8_t> export_trie() const;
243 span<uint8_t> export_trie();
244
246 void export_trie(buffer_t raw);
247
249 std::string show_export_trie() const;
250
251 void rebase(const info_t& info);
252 void bind(const info_t& info);
253 void weak_bind(const info_t& info);
254 void lazy_bind(const info_t& info);
255 void export_info(const info_t& info);
256
257 void set_rebase_offset(uint32_t offset);
258 void set_rebase_size(uint32_t size);
259
260 void set_bind_offset(uint32_t offset);
261 void set_bind_size(uint32_t size);
262
263 void set_weak_bind_offset(uint32_t offset);
264 void set_weak_bind_size(uint32_t size);
265
266 void set_lazy_bind_offset(uint32_t offset);
267 void set_lazy_bind_size(uint32_t size);
268
269 void set_export_offset(uint32_t offset);
270 void set_export_size(uint32_t size);
271
272 void add(std::unique_ptr<ExportInfo> info);
273
274
275 void accept(Visitor& visitor) const override;
276
277 std::ostream& print(std::ostream& os) const override;
278
279 static bool classof(const LoadCommand* cmd);
280
281 private:
282 using bind_container_t = std::set<DyldBindingInfo*, std::function<bool(DyldBindingInfo*, DyldBindingInfo*)>>;
283
284 void show_bindings(std::ostream& os, span<const uint8_t> buffer, bool is_lazy = false) const;
285
286 void show_trie(std::ostream& output, std::string output_prefix, BinaryStream& stream, uint64_t start, uint64_t end, const std::string& prefix) const;
287
288 LIEF_LOCAL DyldInfo& update_standard_bindings(const bind_container_t& bindings, vector_iostream& stream);
289 LIEF_LOCAL DyldInfo& update_standard_bindings_v1(const bind_container_t& bindings, vector_iostream& stream);
290 LIEF_LOCAL DyldInfo& update_standard_bindings_v2(const bind_container_t& bindings,
291 std::vector<RelocationDyld*> rebases, vector_iostream& stream);
292
293 LIEF_LOCAL DyldInfo& update_weak_bindings(const bind_container_t& bindings, vector_iostream& stream);
294 LIEF_LOCAL DyldInfo& update_lazy_bindings(const bind_container_t& bindings, vector_iostream& stream);
295
296 LIEF_LOCAL DyldInfo& update_rebase_info(vector_iostream& stream);
297 LIEF_LOCAL DyldInfo& update_binding_info(vector_iostream& stream, details::dyld_info_command& cmd);
298 LIEF_LOCAL DyldInfo& update_export_trie(vector_iostream& stream);
299
300 info_t rebase_;
301 span<uint8_t> rebase_opcodes_;
302
303 info_t bind_;
304 span<uint8_t> bind_opcodes_;
305
306 info_t weak_bind_;
307 span<uint8_t> weak_bind_opcodes_;
308
309 info_t lazy_bind_;
310 span<uint8_t> lazy_bind_opcodes_;
311
312 info_t export_;
313 span<uint8_t> export_trie_;
314
315 export_info_t export_info_;
316 binding_info_t binding_info_;
317
318 BINDING_ENCODING_VERSION binding_encoding_version_ = BINDING_ENCODING_VERSION::UNKNOWN;
319
320 Binary* binary_ = nullptr;
321};
322
323}
324}
325#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:73
Class which represents a MachO binary.
Definition MachO/Binary.hpp:74
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:58
This class represents a symbol binding operation associated with the LC_DYLD_INFO bytecode.
Definition DyldBindingInfo.hpp:35
Class that represents the LC_DYLD_INFO and LC_DYLD_INFO_ONLY commands.
Definition DyldInfo.hpp:50
span< const uint8_t > bind_opcodes() const
Return Binding's opcodes as raw data.
std::string show_lazy_bind_opcodes() const
Return the lazy opcodes in a humman-readable way.
std::vector< std::unique_ptr< DyldBindingInfo > > binding_info_t
Internal container for storing DyldBindingInfo.
Definition DyldInfo.hpp:62
span< const uint8_t > rebase_opcodes() const
Return Rebase's opcodes as raw data.
void lazy_bind_opcodes(buffer_t raw)
Set new opcodes.
const info_t & export_info() const
Export information
std::string show_export_trie() const
Return the export trie in a humman-readable way.
const info_t & bind() const
Bind information
const info_t & rebase() const
Rebase information
std::vector< std::unique_ptr< ExportInfo > > export_info_t
Internal container for storing ExportInfo.
Definition DyldInfo.hpp:71
void rebase_opcodes(buffer_t raw)
Set new opcodes.
const info_t & weak_bind() const
Weak Bind information
it_binding_info bindings()
Iterator over BindingInfo entries.
void weak_bind_opcodes(buffer_t raw)
Set new opcodes.
it_export_info exports()
Iterator over ExportInfo entries.
void bind_opcodes(buffer_t raw)
Set new opcodes.
std::pair< uint32_t, uint32_t > info_t
Tuple of offset and size
Definition DyldInfo.hpp:59
std::string show_bind_opcodes() const
Return the bind opcodes in a humman-readable way.
std::string show_rebases_opcodes() const
Return the rebase opcodes in a humman-readable way.
void export_trie(buffer_t raw)
Set new trie.
span< const uint8_t > lazy_bind_opcodes() const
Return Lazy Binding's opcodes as raw data.
const info_t & lazy_bind() const
Lazy Bind information
span< const uint8_t > export_trie() const
Return Export's trie as raw data.
span< const uint8_t > weak_bind_opcodes() const
Return Weak Binding's opcodes as raw data.
std::string show_weak_bind_opcodes() const
Return the bind opcodes in a humman-readable way.
Definition LinkEdit.hpp:48
Based class for the Mach-O load commands.
Definition LoadCommand.hpp:39
Definition Visitor.hpp:219
Iterator which returns reference on container's values.
Definition iterators.hpp:48
Definition iostream.hpp:30
LIEF namespace.
Definition Abstract/Binary.hpp:32