blob: e69271f0244f4b02af5282f249276e43f4895d6d [file] [log] [blame]
Peter Collingbourne7a0f04c2019-01-23 17:56:24 -08001/*
2 * Copyright (C) 2019 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#include <gtest/gtest.h>
18
Peter Collingbournee9491952019-10-28 10:57:26 -070019#include <sys/auxv.h>
20#if defined(__BIONIC__)
21#include <sys/ifunc.h>
22#endif
23
24typedef int (*fn_ptr_t)();
25
Peter Collingbourne7a0f04c2019-01-23 17:56:24 -080026int ret42() {
27 return 42;
28}
29
Peter Collingbournee9491952019-10-28 10:57:26 -070030extern "C" fn_ptr_t resolver() {
31 return ret42;
Peter Collingbourne7a0f04c2019-01-23 17:56:24 -080032}
33
34int ifunc() __attribute__((ifunc("resolver")));
35
36TEST(ifunc, function) {
37 ASSERT_EQ(42, ifunc());
38}
Peter Collingbournee9491952019-10-28 10:57:26 -070039
40#if defined(__BIONIC__)
41
42#if defined(__aarch64__)
43
44static uint64_t g_hwcap;
45static __ifunc_arg_t g_arg;
46
47extern "C" fn_ptr_t hwcap_resolver(uint64_t hwcap, __ifunc_arg_t* arg) {
48 g_hwcap = hwcap;
49 g_arg = *arg;
50 return ret42;
51}
52
53#elif defined(__arm__)
54
55static unsigned long g_hwcap;
56
57extern "C" fn_ptr_t hwcap_resolver(unsigned long hwcap) {
58 g_hwcap = hwcap;
59 return ret42;
60}
61
62#else
63
64extern "C" fn_ptr_t hwcap_resolver() {
65 return ret42;
66}
67
68#endif
69
70int hwcap() __attribute__((ifunc("hwcap_resolver")));
71
72TEST(ifunc, hwcap) {
73 ASSERT_EQ(42, hwcap());
74
75#if defined(__aarch64__)
76 EXPECT_EQ(getauxval(AT_HWCAP) | _IFUNC_ARG_HWCAP, g_hwcap);
77
78 EXPECT_EQ(sizeof(__ifunc_arg_t), g_arg._size);
79 EXPECT_EQ(getauxval(AT_HWCAP), g_arg._hwcap);
80 EXPECT_EQ(getauxval(AT_HWCAP2), g_arg._hwcap2);
81#elif defined(__arm__)
82 EXPECT_EQ(getauxval(AT_HWCAP), g_hwcap);
83#endif
84}
85
86#endif // defined(__BIONIC__)