| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 17 | #include <dlfcn.h> | 
|  | 18 | #include <stdint.h> | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 19 | #include <stdio.h> | 
|  | 20 | #include <stdlib.h> | 
|  | 21 |  | 
| Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 22 | static uintptr_t g_flag = 0; | 
| Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 23 |  | 
|  | 24 | static void __attribute__((constructor)) init_flag() { | 
| Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 25 | g_flag = reinterpret_cast<uintptr_t>(dlsym(RTLD_DEFAULT, "dlsym")); | 
| Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 26 | } | 
|  | 27 |  | 
| Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 28 | static const char* is_ctor_called() __attribute__ ((ifunc("is_ctor_called_ifun"))); | 
|  | 29 |  | 
| Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 30 | extern "C" const char* foo() __attribute__ ((ifunc ("foo_ifunc"))); | 
| Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | // Static linker creates GLOBAL/IFUNC symbol and JUMP_SLOT relocation type for plt segment | 
| Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 33 | extern "C" const char* is_ctor_called_jump_slot() __attribute__ ((ifunc("is_ctor_called_ifun"))); | 
| Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 34 |  | 
| Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 35 | extern "C" const char* is_ctor_called_irelative() { | 
| Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 36 | // Call internal ifunc-resolved function with IRELATIVE reloc | 
|  | 37 | return is_ctor_called(); | 
|  | 38 | } | 
| Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 39 |  | 
| Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 40 | extern "C" const char* return_true() { | 
| Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 41 | return "true"; | 
|  | 42 | } | 
|  | 43 |  | 
| Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 44 | extern "C" const char* return_false() { | 
| Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 45 | return "false"; | 
|  | 46 | } | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 47 |  | 
| Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 48 | extern "C" const char* f1() { | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 49 | return "unset"; | 
|  | 50 | } | 
|  | 51 |  | 
| Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 52 | extern "C" const char* f2() { | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 53 | return "set"; | 
|  | 54 | } | 
|  | 55 |  | 
| Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 56 | typedef const char* (*fn_ptr)(); | 
|  | 57 |  | 
|  | 58 | extern "C" fn_ptr is_ctor_called_ifun() { | 
| Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 59 | return g_flag == 0 ? return_false : return_true; | 
|  | 60 | } | 
|  | 61 |  | 
| Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 62 | extern "C" fn_ptr foo_ifunc() { | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 63 | char* choice = getenv("IFUNC_CHOICE"); | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 64 | return choice == nullptr ? f1 : f2; | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 65 | } | 
|  | 66 |  | 
| Dimitry Ivanov | 21975b2 | 2017-05-02 16:31:56 -0700 | [diff] [blame] | 67 | extern "C" const char* foo_library() { | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 68 | return foo(); | 
| Dmitriy Ivanov | 9598b8c | 2014-08-21 13:54:03 -0700 | [diff] [blame] | 69 | } |