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 <ostream>
23#include <string>
24#include <utility>
25
27#include "LIEF/iterators.hpp"
28#include "LIEF/visibility.h"
29
30namespace LIEF::runtime {
31
32namespace details {
33class MemoryLayoutIt;
34}
35
38 public:
40 class Region {
41 public:
42 Region() = default;
43 Region(std::string name, uint64_t addr, uint64_t size) :
44 name_(std::move(name)),
45 addr_(addr),
46 size_(size) {}
47
48 Region(const Region&) = default;
49 Region& operator=(const Region&) = default;
50
51 Region(Region&&) noexcept = default;
52 Region& operator=(Region&&) noexcept = default;
53
54 ~Region() = default;
55
61 std::string_view name() const LIEF_LIFETIMEBOUND {
62 return name_;
63 }
64
66 uint64_t addr() const {
67 return addr_;
68 }
69
71 uint64_t size() const {
72 return size_;
73 }
74
76 uint64_t end_addr() const {
77 return addr() + size();
78 }
79
81 bool contains(uint64_t addr) const {
82 return addr_ <= addr && addr < end_addr();
83 }
84
85 std::string to_string() const;
86
87 LIEF_API friend std::ostream& operator<<(std::ostream& os,
88 const Region& region) {
89 os << region.to_string();
90 return os;
91 }
92
93 private:
94 std::string name_;
95 uint64_t addr_ = 0;
96 uint64_t size_ = 0;
97 };
98
100 class Iterator final
101 : public iterator_facade_base<Iterator, std::forward_iterator_tag, Region,
102 std::ptrdiff_t, const Region*, const Region&> {
103 public:
104 using implementation = details::MemoryLayoutIt;
105 using iterator_facade_base::operator++;
106
108
111
113 LIEF_API Iterator& operator=(Iterator&&) noexcept;
114
115 LIEF_API Iterator(std::unique_ptr<details::MemoryLayoutIt> impl);
117
118 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
119 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
120 return !(LHS == RHS);
121 }
122
123 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
125
127
128 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
129 LIEF_API const Region* operator->() const LIEF_LIFETIMEBOUND;
130
133 LIEF_API std::unique_ptr<Region> yield();
134
135 private:
136 void load() const;
137
138 std::unique_ptr<details::MemoryLayoutIt> impl_;
139 mutable std::unique_ptr<Region> cached_;
140 };
141};
142
144
147
148}
149#endif
Definition iterators.hpp:595
details::MemoryLayoutIt implementation
Definition MemoryLayout.hpp:104
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:40
uint64_t addr() const
Address at which the region starts.
Definition MemoryLayout.hpp:66
bool contains(uint64_t addr) const
Whether the given address is within this region.
Definition MemoryLayout.hpp:81
friend std::ostream & operator<<(std::ostream &os, const Region &region)
Definition MemoryLayout.hpp:87
uint64_t size() const
Size of the region.
Definition MemoryLayout.hpp:71
std::string_view name() const
Name associated with the region: name/path of the module mapped at this address (e....
Definition MemoryLayout.hpp:61
Region & operator=(const Region &)=default
Region(Region &&) noexcept=default
uint64_t end_addr() const
Address at which the region ends.
Definition MemoryLayout.hpp:76
Region(const Region &)=default
Region(std::string name, uint64_t addr, uint64_t size)
Definition MemoryLayout.hpp:43
This class exposes the memory layout of the current process.
Definition MemoryLayout.hpp:37
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition MemoryLayout.hpp:32
Definition android/Host.hpp:24
iterator_range< MemoryLayout::Iterator > memory_layout_it
Definition MemoryLayout.hpp:143
memory_layout_it memory_layout()
Return an iterator over the memory layout of the current process.
#define LIEF_API
Definition visibility.h:45