1 //
2 // Copyright (C) 2017 The Android Open Source Project
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 
17 #ifndef NATIVE_BRIDGE_SUPPORT_VDSO_INTERCEPTABLE_FUNCTIONS_H_
18 #define NATIVE_BRIDGE_SUPPORT_VDSO_INTERCEPTABLE_FUNCTIONS_H_
19 
20 #include <assert.h>
21 #include <stdint.h>
22 
23 #include "native_bridge_support/vdso/vdso.h"
24 
25 #if defined(__arm__)
26 #define INTERCEPTABLE_STUB_ASM_FUNCTION(name)                  \
27   extern "C" void __attribute((target("arm"), naked)) name() { \
28     __asm__ __volatile__(                                      \
29         "ldr r3, =0\n"                                         \
30         "bx r3");                                              \
31   }
32 #elif defined(__aarch64__)
33 #define INTERCEPTABLE_STUB_ASM_FUNCTION(name)   \
34   extern "C" void __attribute((naked)) name() { \
35     __asm__ __volatile__(                       \
36         "ldr x3, =0\n"                          \
37         "blr x3");                              \
38   }
39 #elif defined(__riscv)
40 #define INTERCEPTABLE_STUB_ASM_FUNCTION(name)   \
41   extern "C" void __attribute((naked)) name() { \
42     __asm__ __volatile__(                       \
43         "unimp");                               \
44   }
45 #else
46 #error Unknown architecture, only riscv64, arm and aarch64 are supported.
47 #endif
48 
49 #define DEFINE_INTERCEPTABLE_STUB_VARIABLE(name) uintptr_t name;
50 
51 #define INIT_INTERCEPTABLE_STUB_VARIABLE(library_name, name) \
52   native_bridge_intercept_symbol(&name, library_name, #name)
53 
54 #define DEFINE_INTERCEPTABLE_STUB_FUNCTION(name) \
55   extern "C" void name();                        \
56   INTERCEPTABLE_STUB_ASM_FUNCTION(name)
57 
58 #define INIT_INTERCEPTABLE_STUB_FUNCTION(library_name, name) \
59   native_bridge_intercept_symbol(reinterpret_cast<void*>(name), library_name, #name)
60 
61 #endif  // NATIVE_BRIDGE_SUPPORT_VDSO_INTERCEPTABLE_FUNCTIONS_H_
62