blob: f2e0025025590ba70ac8a399b5a73b8a6199d4ec [file] [log] [blame]
Brigid Smithc5a13ef2014-07-23 11:22:25 -07001/*
2 * Copyright (C) 2014 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
Dimitry Ivanov21975b22017-05-02 16:31:56 -070017#include <dlfcn.h>
18#include <stdint.h>
Brigid Smithc5a13ef2014-07-23 11:22:25 -070019#include <stdio.h>
20#include <stdlib.h>
21
Dimitry Ivanov21975b22017-05-02 16:31:56 -070022static uintptr_t g_flag = 0;
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -070023
24static void __attribute__((constructor)) init_flag() {
Dimitry Ivanov21975b22017-05-02 16:31:56 -070025 g_flag = reinterpret_cast<uintptr_t>(dlsym(RTLD_DEFAULT, "dlsym"));
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -070026}
27
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -070028static const char* is_ctor_called() __attribute__ ((ifunc("is_ctor_called_ifun")));
29
Dimitry Ivanov21975b22017-05-02 16:31:56 -070030extern "C" const char* foo() __attribute__ ((ifunc ("foo_ifunc")));
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -070031
32// Static linker creates GLOBAL/IFUNC symbol and JUMP_SLOT relocation type for plt segment
Dimitry Ivanov21975b22017-05-02 16:31:56 -070033extern "C" const char* is_ctor_called_jump_slot() __attribute__ ((ifunc("is_ctor_called_ifun")));
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -070034
Dimitry Ivanov21975b22017-05-02 16:31:56 -070035extern "C" const char* is_ctor_called_irelative() {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -070036 // Call internal ifunc-resolved function with IRELATIVE reloc
37 return is_ctor_called();
38}
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -070039
Dimitry Ivanov21975b22017-05-02 16:31:56 -070040extern "C" const char* return_true() {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -070041 return "true";
42}
43
Dimitry Ivanov21975b22017-05-02 16:31:56 -070044extern "C" const char* return_false() {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -070045 return "false";
46}
Brigid Smithc5a13ef2014-07-23 11:22:25 -070047
Dimitry Ivanov21975b22017-05-02 16:31:56 -070048extern "C" const char* f1() {
Brigid Smithc5a13ef2014-07-23 11:22:25 -070049 return "unset";
50}
51
Dimitry Ivanov21975b22017-05-02 16:31:56 -070052extern "C" const char* f2() {
Brigid Smithc5a13ef2014-07-23 11:22:25 -070053 return "set";
54}
55
Dimitry Ivanov21975b22017-05-02 16:31:56 -070056typedef const char* (*fn_ptr)();
57
58extern "C" fn_ptr is_ctor_called_ifun() {
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -070059 return g_flag == 0 ? return_false : return_true;
60}
61
Dimitry Ivanov21975b22017-05-02 16:31:56 -070062extern "C" fn_ptr foo_ifunc() {
Brigid Smithc5a13ef2014-07-23 11:22:25 -070063 char* choice = getenv("IFUNC_CHOICE");
Yi Kong32bc0fc2018-08-02 17:31:13 -070064 return choice == nullptr ? f1 : f2;
Brigid Smithc5a13ef2014-07-23 11:22:25 -070065}
66
Dimitry Ivanov21975b22017-05-02 16:31:56 -070067extern "C" const char* foo_library() {
Brigid Smithc5a13ef2014-07-23 11:22:25 -070068 return foo();
Dmitriy Ivanov9598b8c2014-08-21 13:54:03 -070069}