16#ifndef LIEF_RUNTIME_MODULE_H
17#define LIEF_RUNTIME_MODULE_H
46 :
public iterator_facade_base<Iterator, std::forward_iterator_tag, Module,
47 std::ptrdiff_t, const Module*, const Module&> {
50 using iterator_facade_base::operator++;
83 std::unique_ptr<
details::ModuleIt> impl_;
84 mutable std::unique_ptr<
Module> cached_;
117 return base <= addr && addr < (base +
size());
124 std::vector<uint8_t>
dump()
const {
126 const uint64_t nbytes =
size();
127 if (base == 0 || nbytes == 0) {
130 std::vector<uint8_t> out(nbytes);
131 std::memcpy(out.data(),
reinterpret_cast<const void*
>(base), nbytes);
137 std::vector<uint8_t>
dump(
const std::string& filepath)
const {
138 std::vector<uint8_t> out =
dump();
139 std::ofstream ofs(filepath, std::ios::out | std::ios::binary);
141 ofs.write(
reinterpret_cast<const char*
>(out.data()),
143 static_cast<std::streamsize
>(out.size()));
149 std::vector<uint8_t>
dump(std::ostream& os)
const {
150 std::vector<uint8_t> out =
dump();
151 os.write(
reinterpret_cast<const char*
>(out.data()),
153 static_cast<std::streamsize
>(out.size()));
162 static_assert(std::is_base_of_v<Module, T>,
"Require Module inheritance");
163 if (T::classof(
this)) {
164 return static_cast<const T*
>(
this);
171 return const_cast<T*
>(
static_cast<const Module*
>(
this)->as<T>());
182 std::unique_ptr<details::Module> impl_;
199 for (
auto it = range.begin(); it != range.end(); ++it) {
200 if (it->name() == name) {
210 for (
auto it = range.begin(); it != range.end(); ++it) {
211 if (it->path() == path) {
221 for (
auto it = range.begin(); it != range.end(); ++it) {
222 if (it->contains(addr)) {
Generic interface representing a binary executable.
Definition Abstract/Binary.hpp:61
Definition iterators.hpp:681
Iterator(Iterator &&) noexcept
Iterator & operator=(const Iterator &)
details::ModuleIt implementation
Definition Module.hpp:49
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...
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:149
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:174
T * as()
Definition Module.hpp:170
bool contains(uintptr_t addr) const
Check if the current module contains the given address.
Definition Module.hpp:115
uint64_t end() const
End address of the module.
Definition Module.hpp:104
std::vector< uint8_t > dump() const
Return the content of the module as it is currently mapped in memory.
Definition Module.hpp:124
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:137
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:161
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition MemoryLayout.hpp:34
Definition android/Host.hpp:24
modules_t modules()
Return an iterator over the different modules loaded in the current process.
iterator_range< Module::Iterator > modules_t
Definition Module.hpp:185
std::unique_ptr< Module > module_from_path(std::string_view path)
Find the module with the given path.
Definition Module.hpp:208
std::unique_ptr< Module > module_from_name(std::string_view name)
Find the module with the given name.
Definition Module.hpp:197
std::unique_ptr< Module > current_module()
Return the current module: the module that contains the code calling this function (e....
std::unique_ptr< Module > module_from_addr(uintptr_t addr)
Find the module that encompasses the given virtual address (absolute).
Definition Module.hpp:219
LIEF namespace.
Definition Abstract/Binary.hpp:42
#define LIEF_API
Definition visibility.h:45