• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <dlfcn.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 static uintptr_t g_flag = 0;
23 
init_flag()24 static void __attribute__((constructor)) init_flag() {
25   g_flag = reinterpret_cast<uintptr_t>(dlsym(RTLD_DEFAULT, "dlsym"));
26 }
27 
28 static const char* is_ctor_called() __attribute__ ((ifunc("is_ctor_called_ifun")));
29 
30 extern "C" const char* foo() __attribute__ ((ifunc ("foo_ifunc")));
31 
32 // Static linker creates GLOBAL/IFUNC symbol and JUMP_SLOT relocation type for plt segment
33 extern "C" const char* is_ctor_called_jump_slot() __attribute__ ((ifunc("is_ctor_called_ifun")));
34 
is_ctor_called_irelative()35 extern "C" const char* is_ctor_called_irelative() {
36   // Call internal ifunc-resolved function with IRELATIVE reloc
37   return is_ctor_called();
38 }
39 
40 extern "C" const char* var_true = "true";
41 extern "C" const char* var_false = "false";
42 
43 extern "C" const char* v1 = "unset";
44 extern "C" const char* v2 = "set";
45 
46 typedef const char* (*fn_ptr)();
47 
is_ctor_called_ifun()48 extern "C" fn_ptr is_ctor_called_ifun() {
49   return (fn_ptr)(g_flag == 0 ? &var_false : &var_true);
50 }
51 
foo_ifunc()52 extern "C" fn_ptr foo_ifunc() {
53   char* choice = getenv("IFUNC_CHOICE");
54   return (fn_ptr)(choice == nullptr ? &v1 : &v2);
55 }
56