LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
MemoryLayout.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_RUNTIME_MEMORY_LAYOUT_H
16#define LIEF_RUNTIME_MEMORY_LAYOUT_H
17
18#include <string_view>
19#include <cstdint>
20#include <memory>
21#include <optional>
22#include <ostream>
23#include <string>
24#include <utility>
25
27#include "LIEF/iterators.hpp"
29#include "LIEF/visibility.h"
30
31namespace LIEF::runtime {
32
33namespace details {
34class MemoryLayoutIt;
35}
36
39 public:
41 class Region {
42 public:
43 Region() = default;
44 Region(std::string name, uint64_t addr, uint64_t size,
45 uint32_t permissions = Memory::P_NONE) :
46 name_(std::move(name)),
47 addr_(addr),
48 size_(size),
49 permissions_(permissions) {}
50
51 Region(const Region&) = default;
52 Region& operator=(const Region&) = default;
53
54 Region(Region&&) noexcept = default;
55 Region& operator=(Region&&) noexcept = default;
56
57 ~Region() = default;
58
64 std::string_view name() const LIEF_LIFETIMEBOUND {
65 return name_;
66 }
67
69 uint64_t addr() const {
70 return addr_;
71 }
72
74 uint64_t size() const {
75 return size_;
76 }
77
79 uint64_t end_addr() const {
80 return addr() + size();
81 }
82
84 uint32_t permissions() const {
85 return permissions_;
86 }
87
89 bool contains(uint64_t addr) const {
90 return addr_ <= addr && addr < end_addr();
91 }
92
93 std::string to_string() const;
94
95 LIEF_API friend std::ostream& operator<<(std::ostream& os,
96 const Region& region) {
97 os << region.to_string();
98 return os;
99 }
100
101 private:
102 std::string name_;
103 uint64_t addr_ = 0;
104 uint64_t size_ = 0;
105 uint32_t permissions_ = Memory::P_NONE;
106 };
107
109 class Iterator final
110 : public iterator_facade_base<Iterator, std::forward_iterator_tag, Region,
111 std::ptrdiff_t, const Region*, const Region&> {
112 public:
113 using implementation = details::MemoryLayoutIt;
114 using iterator_facade_base::operator++;
115
117
120
122 LIEF_API Iterator& operator=(Iterator&&) noexcept;
123
124 LIEF_API Iterator(std::unique_ptr<details::MemoryLayoutIt> impl);
126
127 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
128 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
129 return !(LHS == RHS);
130 }
131
132 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
134
136
137 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
138 LIEF_API const Region* operator->() const LIEF_LIFETIMEBOUND;
139
142 LIEF_API std::unique_ptr<Region> yield();
143
144 private:
145 void load() const;
146
147 std::unique_ptr<details::MemoryLayoutIt> impl_;
148 mutable std::unique_ptr<Region> cached_;
149 };
150};
151
153
156
157inline std::optional<MemoryLayout::Region> find_region(uint64_t addr) {
158 for (const MemoryLayout::Region& region : memory_layout()) {
159 if (region.contains(addr)) {
160 return region;
161 }
162 }
163 return std::nullopt;
164}
165
166}
167#endif
Definition iterators.hpp:680
details::MemoryLayoutIt implementation
Definition MemoryLayout.hpp:113
Iterator & operator=(const Iterator &)
const Region & operator*() const
std::unique_ptr< Region > yield()
Transfer ownership of the region at the current position to the caller. Returns nullptr if the iterat...
A contiguous range of memory mapped in the current process.
Definition MemoryLayout.hpp:41
uint64_t addr() const
Address at which the region starts.
Definition MemoryLayout.hpp:69
bool contains(uint64_t addr) const
Whether the given address is within this region.
Definition MemoryLayout.hpp:89
friend std::ostream & operator<<(std::ostream &os, const Region &region)
Definition MemoryLayout.hpp:95
uint64_t size() const
Size of the region.
Definition MemoryLayout.hpp:74
uint32_t permissions() const
Permissions of the region as a combination of Memory::PERM.
Definition MemoryLayout.hpp:84
std::string_view name() const
Name associated with the region: name/path of the module mapped at this address (e....
Definition MemoryLayout.hpp:64
Region & operator=(const Region &)=default
Region(Region &&) noexcept=default
uint64_t end_addr() const
Address at which the region ends.
Definition MemoryLayout.hpp:79
Region(const Region &)=default
Region(std::string name, uint64_t addr, uint64_t size, uint32_t permissions=Memory::P_NONE)
Definition MemoryLayout.hpp:44
This class exposes the memory layout of the current process.
Definition MemoryLayout.hpp:38
@ P_NONE
Definition runtime/Memory.hpp:59
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition MemoryLayout.hpp:33
Definition android/Host.hpp:23
iterator_range< MemoryLayout::Iterator > memory_layout_it
Definition MemoryLayout.hpp:152
std::optional< MemoryLayout::Region > find_region(uint64_t addr)
Definition MemoryLayout.hpp:157
memory_layout_it memory_layout()
Return an iterator over the memory layout of the current process.
#define LIEF_API
Definition visibility.h:44