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
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
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
29extern "C" const char* __attribute__((weak)) internal_extern_string();
30
31bool 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
45extern "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
53extern "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
59  const char** result = static_cast<const char**>(dlsym(handle, "g_private_dlopened_string"));
60  if (result == nullptr) {
61    return nullptr;
62  } else {
63    g_dlopened = true;
64  }
65
66  return *result;
67}
68