LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
runtime/utils.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_UTILS_H
16#define LIEF_RUNTIME_UTILS_H
17#include "LIEF/config.h"
18#include <cstdint>
19
20#if defined(LIEF_RUNTIME_SUPPORT)
21 #include "LIEF/runtime/config.h"
22#endif
23
24
25namespace LIEF::runtime {
26
27enum class PLATFORMS : uint32_t {
28 NONE = 0,
34};
35
36enum class ARCH : uint32_t {
37 NONE,
38 X86_64,
41};
42
44static constexpr bool is_enabled() {
45 return lief_runtime_support;
46}
47
49static constexpr PLATFORMS platform() {
50#if !defined(LIEF_RUNTIME_SUPPORT)
51 return PLATFORMS::NONE;
52#else
53 #if defined(LIEF_RUNTIME_PLATFORM_LINUX)
54 return PLATFORMS::LINUX;
55 #elif defined(LIEF_RUNTIME_PLATFORM_WINDOWS)
56 return PLATFORMS::WINDOWS;
57 #elif defined(LIEF_RUNTIME_PLATFORM_ANDROID)
59 #elif defined(LIEF_RUNTIME_PLATFORM_OSX)
60 return PLATFORMS::OSX;
61 #elif defined(LIEF_RUNTIME_PLATFORM_IOS)
62 return PLATFORMS::IOS;
63 #else
64 return PLATFORMS::NONE;
65 #endif
66#endif
67}
68
70static constexpr ARCH arch() {
71#if !defined(LIEF_RUNTIME_SUPPORT)
72 return ARCH::NONE;
73#else
74 #if defined(LIEF_RUNTIME_ARCH_ARM64)
75 return ARCH::ARM64;
76 #elif defined(LIEF_RUNTIME_ARCH_X86_64)
77 return ARCH::X86_64;
78 #elif defined(LIEF_RUNTIME_ARCH_RISCV64)
79 return ARCH::RISCV64;
80 #else
81 return ARCH::NONE;
82 #endif
83
84#endif
85}
86
87static inline constexpr uintptr_t page_mask(uintptr_t page_size) {
88 return ~(page_size - 1);
89}
90
91static inline constexpr uintptr_t page_start(uintptr_t address,
92 uintptr_t page_size) {
93 return address & page_mask(page_size);
94}
95
96static inline constexpr uintptr_t page_align(uintptr_t address,
97 uintptr_t page_size) {
98 return (address + (page_size - 1)) & page_mask(page_size);
99}
100
101static inline constexpr uintptr_t page_offset(uintptr_t address,
102 uintptr_t page_size) {
103 return (address & ~page_mask(page_size));
104}
105
106static inline constexpr uintptr_t page_end(uintptr_t address,
107 uintptr_t page_size) {
108 return page_start(address + (page_size - 1), page_size);
109}
110
111}
112
113#endif
Definition android/Host.hpp:23
ARCH
Definition runtime/utils.hpp:36
@ X86_64
Definition runtime/utils.hpp:38
@ RISCV64
Definition runtime/utils.hpp:40
@ NONE
Definition runtime/utils.hpp:37
@ ARM64
Definition runtime/utils.hpp:39
PLATFORMS
Definition runtime/utils.hpp:27
@ OSX
Definition runtime/utils.hpp:32
@ NONE
Definition runtime/utils.hpp:28
@ IOS
Definition runtime/utils.hpp:33
@ WINDOWS
Definition runtime/utils.hpp:30
@ LINUX
Definition runtime/utils.hpp:29
@ ANDROID_
Definition runtime/utils.hpp:31