16#ifndef LIEF_RUNTIME_MODULE_H
17#define LIEF_RUNTIME_MODULE_H
45 :
public iterator_facade_base<Iterator, std::forward_iterator_tag, Module,
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()),
142 static_cast<std::streamsize
>(out.size()));
148 std::vector<uint8_t>
dump(std::ostream& os)
const {
149 std::vector<uint8_t> out =
dump();
150 os.write(
reinterpret_cast<const char*
>(out.data()),
152 static_cast<std::streamsize
>(out.size()));
161 static_assert(std::is_base_of_v<Module, T>,
"Require Module inheritance");
162 if (T::classof(
this)) {
163 return static_cast<const T*
>(
this);
170 return const_cast<T*
>(
static_cast<const Module*
>(
this)->as<T>());
181 std::unique_ptr<details::Module> impl_;
193 for (
auto it = range.begin(); it != range.end(); ++it) {
194 if (it->name() == name) {
204 for (
auto it = range.begin(); it != range.end(); ++it) {
205 if (it->path() == path) {
215 for (
auto it = range.begin(); it != range.end(); ++it) {
216 if (it->contains(addr)) {
Generic interface representing a binary executable.
Definition Abstract/Binary.hpp:60
Definition iterators.hpp:595
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...
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:148
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:173
T * as()
Definition Module.hpp:169
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:160
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition MemoryLayout.hpp:32
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:191
modules_t modules()
Return an iterator over the different modules loaded in the current process.
iterator_range< Module::Iterator > modules_t
Definition Module.hpp:184
std::unique_ptr< Module > module_from_path(const std::string &path)
Find the module with the given path.
Definition Module.hpp:202
std::unique_ptr< Module > module_from_addr(uintptr_t addr)
Find the module that encompasses the given virtual address (absolute).
Definition Module.hpp:213
LIEF namespace.
Definition Abstract/Binary.hpp:41
#define LIEF_API
Definition visibility.h:45