blob: a551673ff5e5e442cf0bde98f21effe4fdaf3fb7 [file] [log] [blame]
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001/*
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
19static const char* g_local_string = "This string is local to root library";
20extern "C" const char* g_private_extern_string;
21extern "C" const char* g_public_extern_string;
22
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -080023// 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
29extern "C" const char* __attribute__((weak)) internal_extern_string();
30
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070031bool g_dlopened = false;
32
33extern "C" const char* ns_get_local_string() {
34 return g_local_string;
35}
36
37extern "C" const char* ns_get_private_extern_string() {
38 return g_private_extern_string;
39}
40
41extern "C" const char* ns_get_public_extern_string() {
42 return g_public_extern_string;
43}
44
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -080045extern "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 Ivanov42d5fcb2015-10-29 17:01:24 -070053extern "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 Ivanov1ffec1c2015-11-23 11:26:35 -080059 const char** result = static_cast<const char**>(dlsym(handle, "g_private_dlopened_string"));
60 if (result == nullptr) {
61 return nullptr;
62 } else {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070063 g_dlopened = true;
64 }
65
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -080066 return *result;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070067}