LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
Module.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_RUNTIME_MODULE_H
17#define LIEF_RUNTIME_MODULE_H
18#include <ostream>
19#include <fstream>
20#include <cstdint>
21#include <cstring>
22
23#include "LIEF/visibility.h"
24#include "LIEF/iterators.hpp"
26
27#include <memory>
28#include <string>
29#include <vector>
30
31namespace LIEF {
32class Binary;
33namespace runtime {
34
35namespace details {
36class ModuleIt;
37class Module;
38}
39
43 public:
44 class Iterator final
45 : public iterator_facade_base<Iterator, std::forward_iterator_tag, Module,
46 std::ptrdiff_t, const Module*, const Module&> {
47 public:
48 using implementation = details::ModuleIt;
49 using iterator_facade_base::operator++;
50
52
55
57 LIEF_API Iterator& operator=(Iterator&&) noexcept;
58
59 LIEF_API Iterator(std::unique_ptr<details::ModuleIt> impl);
61
62 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
63 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
64 return !(LHS == RHS);
65 }
66
67 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
69
71
72 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
73 LIEF_API const Module* operator->() const LIEF_LIFETIMEBOUND;
74
77 LIEF_API std::unique_ptr<Module> yield();
78
79 private:
80 void load() const;
81
82 std::unique_ptr<details::ModuleIt> impl_;
83 mutable std::unique_ptr<Module> cached_;
84 };
85 Module() = delete;
86 Module(std::unique_ptr<details::Module> impl);
87
88 Module(const Module&) = delete;
89 Module& operator=(const Module&) = delete;
90
91 Module(Module&&) noexcept;
92 Module& operator=(Module&&) noexcept;
93
94 virtual std::unique_ptr<Module> clone() const;
95
97 uint64_t imagebase() const;
98
100 uint64_t size() const;
101
103 uint64_t end() const {
104 return imagebase() + size();
105 }
106
108 std::string name() const;
109
111 std::string path() const;
112
114 bool contains(uintptr_t addr) const {
115 const uint64_t base = imagebase();
116 return base <= addr && addr < (base + size());
117 }
118
123 std::vector<uint8_t> dump() const {
124 const uint64_t base = imagebase();
125 const uint64_t nbytes = size();
126 if (base == 0 || nbytes == 0) {
127 return {};
128 }
129 std::vector<uint8_t> out(nbytes);
130 std::memcpy(out.data(), reinterpret_cast<const void*>(base), nbytes);
131 return out;
132 }
133
136 std::vector<uint8_t> dump(const std::string& filepath) const {
137 std::vector<uint8_t> out = dump();
138 std::ofstream ofs(filepath, std::ios::out | std::ios::binary);
139 if (ofs) {
140 ofs.write(reinterpret_cast<const char*>(out.data()),
141 static_cast<std::streamsize>(out.size()));
142 }
143 return out;
144 }
145
147 std::vector<uint8_t> dump(std::ostream& os) const {
148 std::vector<uint8_t> out = dump();
149 os.write(reinterpret_cast<const char*>(out.data()),
150 static_cast<std::streamsize>(out.size()));
151 return out;
152 }
153
154 std::string to_string() const;
155
157 template<class T>
158 const T* as() const LIEF_LIFETIMEBOUND {
159 static_assert(std::is_base_of<Module, T>::value, "Require Module inheritance");
160 if (T::classof(this)) {
161 return static_cast<const T*>(this);
162 }
163 return nullptr;
164 }
165
166 template<class T>
168 return const_cast<T*>(static_cast<const Module*>(this)->as<T>());
169 }
170
171 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Module& M) {
172 os << M.to_string();
173 return os;
174 }
175
176 virtual ~Module();
177
178 protected:
179 std::unique_ptr<details::Module> impl_;
180};
181
183
187
189inline std::unique_ptr<Module> module_from_name(const std::string& name) {
190 auto range = modules();
191 for (auto it = range.begin(); it != range.end(); ++it) {
192 if (it->name() == name) {
193 return it.yield();
194 }
195 }
196 return nullptr;
197}
198
200inline std::unique_ptr<Module> module_from_path(const std::string& path) {
201 auto range = modules();
202 for (auto it = range.begin(); it != range.end(); ++it) {
203 if (it->path() == path) {
204 return it.yield();
205 }
206 }
207 return nullptr;
208}
209
211inline std::unique_ptr<Module> module_from_addr(uintptr_t addr) {
212 auto range = modules();
213 for (auto it = range.begin(); it != range.end(); ++it) {
214 if (it->contains(addr)) {
215 return it.yield();
216 }
217 }
218 return nullptr;
219}
220
221}
222}
223#endif
Generic interface representing a binary executable.
Definition Abstract/Binary.hpp:60
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterators.hpp:750
Definition iterators.hpp:603
Iterator(Iterator &&) noexcept
Iterator & operator=(const Iterator &)
details::ModuleIt implementation
Definition Module.hpp:48
const Module & operator*() const
std::unique_ptr< Module > yield()
Transfer ownership of the module at the current position to the caller. Returns nullptr if the iterat...
This class represents an in-memory module which can be an executable or a library.
Definition Module.hpp:42
virtual std::unique_ptr< Module > clone() const
std::vector< uint8_t > dump(std::ostream &os) const
Same as dump() but also writes the content into the given output stream.
Definition Module.hpp:147
uint64_t imagebase() const
Base address where the module is loaded in memory.
friend std::ostream & operator<<(std::ostream &os, const Module &M)
Definition Module.hpp:171
T * as()
Definition Module.hpp:167
bool contains(uintptr_t addr) const
Check if the current module contains the given address.
Definition Module.hpp:114
uint64_t end() const
End address of the module.
Definition Module.hpp:103
std::vector< uint8_t > dump() const
Return the content of the module as it is currently mapped in memory.
Definition Module.hpp:123
std::string to_string() const
std::vector< uint8_t > dump(const std::string &filepath) const
Same as dump() but also writes the content into the file located at filepath.
Definition Module.hpp:136
std::string name() const
Name of the module (e.g. libc.so.6, kernel32.dll, libsystem_c.dylib).
std::string path() const
Path of the module.
uint64_t size() const
Virtual size of the current module.
const T * as() const
This function can be used to downcast a Module instance.
Definition Module.hpp:158
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition Module.hpp:35
Definition android/Host.hpp:24
std::unique_ptr< Module > module_from_name(const std::string &name)
Find the module with the given name.
Definition Module.hpp:189
modules_t modules()
Return an iterator over the different modules loaded in the current process.
iterator_range< Module::Iterator > modules_t
Definition Module.hpp:182
std::unique_ptr< Module > module_from_path(const std::string &path)
Find the module with the given path.
Definition Module.hpp:200
std::unique_ptr< Module > module_from_addr(uintptr_t addr)
Find the module that encompasses the given virtual address (absolute).
Definition Module.hpp:211
LIEF namespace.
Definition Abstract/Binary.hpp:41
#define LIEF_API
Definition visibility.h:45