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