Home | History | Annotate | Download | only in libs
      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 
     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 
     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 extern "C" void* is_ctor_called_ifun() {
     47   return g_flag == 0 ? &var_false : &var_true;
     48 }
     49 
     50 extern "C" void* foo_ifunc() {
     51    char* choice = getenv("IFUNC_CHOICE");
     52    return choice == NULL ? &v1 : &v2;
     53 }
     54