16#ifndef LIEF_RUNTIME_MODULE_H
17#define LIEF_RUNTIME_MODULE_H
46 std::ptrdiff_t, const Module*, const Module&> {
49 using iterator_facade_base::operator++;
82 std::unique_ptr<
details::ModuleIt> impl_;
83 mutable std::unique_ptr<
Module> cached_;
116 return base <= addr && addr < (base +
size());
123 std::vector<uint8_t>
dump()
const {
125 const uint64_t nbytes =
size();
126 if (base == 0 || nbytes == 0) {
129 std::vector<uint8_t> out(nbytes);
130 std::memcpy(out.data(),
reinterpret_cast<const void*
>(base), nbytes);
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);
140 ofs.write(
reinterpret_cast<const char*
>(out.data()),
141 static_cast<std::streamsize
>(out.size()));
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()));
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);
168 return const_cast<T*
>(
static_cast<const Module*
>(
this)->as<T>());
179 std::unique_ptr<details::Module> impl_;
191 for (
auto it = range.begin(); it != range.end(); ++it) {
192 if (it->name() == name) {
202 for (
auto it = range.begin(); it != range.end(); ++it) {
203 if (it->path() == path) {
213 for (
auto it = range.begin(); it != range.end(); ++it) {
214 if (it->contains(addr)) {
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
Iterator(const Iterator &)
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 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