Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 19 | static const char* g_local_string = "This string is local to root library"; |
| 20 | extern "C" const char* g_private_extern_string; |
| 21 | extern "C" const char* g_public_extern_string; |
| 22 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame^] | 23 | // This is resolved only if public library is in the same namespace as |
| 24 | // the root one. It should remain unresolved if looking up for public library |
| 25 | // crosses namespace boundary. |
| 26 | // |
| 27 | // Defined in libnstest_public_internal.so on which libnstest_public.so |
| 28 | // depends on |
| 29 | extern "C" const char* __attribute__((weak)) internal_extern_string(); |
| 30 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 31 | bool g_dlopened = false; |
| 32 | |
| 33 | extern "C" const char* ns_get_local_string() { |
| 34 | return g_local_string; |
| 35 | } |
| 36 | |
| 37 | extern "C" const char* ns_get_private_extern_string() { |
| 38 | return g_private_extern_string; |
| 39 | } |
| 40 | |
| 41 | extern "C" const char* ns_get_public_extern_string() { |
| 42 | return g_public_extern_string; |
| 43 | } |
| 44 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame^] | 45 | extern "C" const char* ns_get_internal_extern_string() { |
| 46 | if (internal_extern_string != nullptr) { |
| 47 | return internal_extern_string(); |
| 48 | } else { |
| 49 | return nullptr; |
| 50 | } |
| 51 | } |
| 52 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 53 | extern "C" const char* ns_get_dlopened_string() { |
| 54 | void* handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_GLOBAL); |
| 55 | if (handle == nullptr) { |
| 56 | return nullptr; |
| 57 | } |
| 58 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 59 | const char** result = static_cast<const char**>(dlsym(handle, "g_private_dlopened_string")); |
| 60 | if (result == nullptr) { |
| 61 | return nullptr; |
| 62 | } else { |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 63 | g_dlopened = true; |
| 64 | } |
| 65 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 66 | return *result; |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 67 | } |