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 * 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_MEMORY_LAYOUT_H
17#define LIEF_RUNTIME_MEMORY_LAYOUT_H
18
19#include <string_view>
20#include <cstdint>
21#include <memory>
22#include <optional>
23#include <ostream>
24#include <string>
25#include <utility>
26
28#include "LIEF/iterators.hpp"
30#include "LIEF/visibility.h"
31
32namespace LIEF::runtime {
33
34namespace details {
35class MemoryLayoutIt;
36}
37
40 public:
42 class Region {
43 public:
44 Region() = default;
45 Region(std::string name, uint64_t addr, uint64_t size,
46 uint32_t permissions = Memory::P_NONE) :
47 name_(std::move(name)),
48 addr_(addr),
49 size_(size),
50 permissions_(permissions) {}
51
52 Region(const Region&) = default;
53 Region& operator=(const Region&) = default;
54
55 Region(Region&&) noexcept = default;
56 Region& operator=(Region&&) noexcept = default;
57
58 ~Region() = default;
59
65 std::string_view name() const LIEF_LIFETIMEBOUND {
66 return name_;
67 }
68
70 uint64_t addr() const {
71 return addr_;
72 }
73
75 uint64_t size() const {
76 return size_;
77 }
78
80 uint64_t end_addr() const {
81 return addr() + size();
82 }
83
85 uint32_t permissions() const {
86 return permissions_;
87 }
88
90 bool contains(uint64_t addr) const {
91 return addr_ <= addr && addr < end_addr();
92 }
93
94 std::string to_string() const;
95
96 LIEF_API friend std::ostream& operator<<(std::ostream& os,
97 const Region& region) {
98 os << region.to_string();
99 return os;
100 }
101
102 private:
103 std::string name_;
104 uint64_t addr_ = 0;
105 uint64_t size_ = 0;
106 uint32_t permissions_ = Memory::P_NONE;
107 };
108
110 class Iterator final
111 : public iterator_facade_base<Iterator, std::forward_iterator_tag, Region,
112 std::ptrdiff_t, const Region*, const Region&> {
113 public:
114 using implementation = details::MemoryLayoutIt;
115 using iterator_facade_base::operator++;
116
118
121
123 LIEF_API Iterator& operator=(Iterator&&) noexcept;
124
125 LIEF_API Iterator(std::unique_ptr<details::MemoryLayoutIt> impl);
127
128 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
129 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
130 return !(LHS == RHS);
131 }
132
133 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
135
137
138 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
139 LIEF_API const Region* operator->() const LIEF_LIFETIMEBOUND;
140
143 LIEF_API std::unique_ptr<Region> yield();
144
145 private:
146 void load() const;
147
148 std::unique_ptr<details::MemoryLayoutIt> impl_;
149 mutable std::unique_ptr<Region> cached_;
150 };
151};
152
154
157
158inline std::optional<MemoryLayout::Region> find_region(uint64_t addr) {
159 for (const MemoryLayout::Region& region : memory_layout()) {
160 if (region.contains(addr)) {
161 return region;
162 }
163 }
164 return std::nullopt;
165}
166
167}
168#endif
Definition iterators.hpp:681
details::MemoryLayoutIt implementation
Definition MemoryLayout.hpp:114
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:42
uint64_t addr() const
Address at which the region starts.
Definition MemoryLayout.hpp:70
bool contains(uint64_t addr) const
Whether the given address is within this region.
Definition MemoryLayout.hpp:90
friend std::ostream & operator<<(std::ostream &os, const Region &region)
Definition MemoryLayout.hpp:96
uint64_t size() const
Size of the region.
Definition MemoryLayout.hpp:75
uint32_t permissions() const
Permissions of the region as a combination of Memory::PERM.
Definition MemoryLayout.hpp:85
std::string_view name() const
Name associated with the region: name/path of the module mapped at this address (e....
Definition MemoryLayout.hpp:65
Region & operator=(const Region &)=default
Region(Region &&) noexcept=default
uint64_t end_addr() const
Address at which the region ends.
Definition MemoryLayout.hpp:80
Region(const Region &)=default
Region(std::string name, uint64_t addr, uint64_t size, uint32_t permissions=Memory::P_NONE)
Definition MemoryLayout.hpp:45
This class exposes the memory layout of the current process.
Definition MemoryLayout.hpp:39
@ P_NONE
Definition runtime/Memory.hpp:60
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition MemoryLayout.hpp:34
Definition android/Host.hpp:24
iterator_range< MemoryLayout::Iterator > memory_layout_it
Definition MemoryLayout.hpp:153
std::optional< MemoryLayout::Region > find_region(uint64_t addr)
Definition MemoryLayout.hpp:158
memory_layout_it memory_layout()
Return an iterator over the memory layout of the current process.
#define LIEF_API
Definition visibility.h:45