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"
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 name_(std::move(name)),
46 addr_(addr),
47 size_(size) {}
48
49 Region(const Region&) = default;
50 Region& operator=(const Region&) = default;
51
52 Region(Region&&) noexcept = default;
53 Region& operator=(Region&&) noexcept = default;
54
55 ~Region() = default;
56
62 std::string_view name() const LIEF_LIFETIMEBOUND {
63 return name_;
64 }
65
67 uint64_t addr() const {
68 return addr_;
69 }
70
72 uint64_t size() const {
73 return size_;
74 }
75
77 uint64_t end_addr() const {
78 return addr() + size();
79 }
80
82 bool contains(uint64_t addr) const {
83 return addr_ <= addr && addr < end_addr();
84 }
85
86 std::string to_string() const;
87
88 LIEF_API friend std::ostream& operator<<(std::ostream& os,
89 const Region& region) {
90 os << region.to_string();
91 return os;
92 }
93
94 private:
95 std::string name_;
96 uint64_t addr_ = 0;
97 uint64_t size_ = 0;
98 };
99
101 class Iterator final
102 : public iterator_facade_base<Iterator, std::forward_iterator_tag, Region,
103 std::ptrdiff_t, const Region*, const Region&> {
104 public:
105 using implementation = details::MemoryLayoutIt;
106 using iterator_facade_base::operator++;
107
109
112
114 LIEF_API Iterator& operator=(Iterator&&) noexcept;
115
116 LIEF_API Iterator(std::unique_ptr<details::MemoryLayoutIt> impl);
118
119 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
120 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
121 return !(LHS == RHS);
122 }
123
124 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
126
128
129 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
130 LIEF_API const Region* operator->() const LIEF_LIFETIMEBOUND;
131
134 LIEF_API std::unique_ptr<Region> yield();
135
136 private:
137 void load() const;
138
139 std::unique_ptr<details::MemoryLayoutIt> impl_;
140 mutable std::unique_ptr<Region> cached_;
141 };
142};
143
145
148
149inline std::optional<MemoryLayout::Region> find_region(uint64_t addr) {
150 for (const MemoryLayout::Region& region : memory_layout()) {
151 if (region.contains(addr)) {
152 return region;
153 }
154 }
155 return std::nullopt;
156}
157
158}
159#endif
Definition iterators.hpp:595
details::MemoryLayoutIt implementation
Definition MemoryLayout.hpp:105
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:67
bool contains(uint64_t addr) const
Whether the given address is within this region.
Definition MemoryLayout.hpp:82
friend std::ostream & operator<<(std::ostream &os, const Region &region)
Definition MemoryLayout.hpp:88
uint64_t size() const
Size of the region.
Definition MemoryLayout.hpp:72
std::string_view name() const
Name associated with the region: name/path of the module mapped at this address (e....
Definition MemoryLayout.hpp:62
Region & operator=(const Region &)=default
Region(Region &&) noexcept=default
uint64_t end_addr() const
Address at which the region ends.
Definition MemoryLayout.hpp:77
Region(const Region &)=default
Region(std::string name, uint64_t addr, uint64_t size)
Definition MemoryLayout.hpp:44
This class exposes the memory layout of the current process.
Definition MemoryLayout.hpp:38
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition MemoryLayout.hpp:33
Definition android/Host.hpp:24
iterator_range< MemoryLayout::Iterator > memory_layout_it
Definition MemoryLayout.hpp:144
std::optional< MemoryLayout::Region > find_region(uint64_t addr)
Definition MemoryLayout.hpp:149
memory_layout_it memory_layout()
Return an iterator over the memory layout of the current process.
#define LIEF_API
Definition visibility.h:45