blob: 71e42477a31b5dcf15eed80b603aa1b5fa285f33 [file] [log] [blame]
Jiyong Park6291da22019-04-26 18:55:48 +09001/*
2 * Copyright (C) 2019 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#pragma once
17#if defined(__ANDROID__)
18
Jiyong Park85377812019-05-04 00:30:23 +090019#include <string>
20#include <variant>
21#include <vector>
Jiyong Park6291da22019-04-26 18:55:48 +090022
Jiyong Park16a98962019-05-04 03:10:48 +090023#include <android-base/logging.h>
24#include <android/dlext.h>
25#include <log/log.h>
26#include <nativebridge/native_bridge.h>
Jiyong Park6291da22019-04-26 18:55:48 +090027
28namespace android {
29
30// NativeLoaderNamespace abstracts a linker namespace for the native
31// architecture (ex: arm on arm) or the translated architecture (ex: arm on
32// x86). Instances of this class are managed by LibraryNamespaces object.
33struct NativeLoaderNamespace {
34 public:
Jiyong Park85377812019-05-04 00:30:23 +090035 // TODO(return with errors)
36 static NativeLoaderNamespace Create(const std::string& name, const std::string& search_paths,
37 const std::string& permitted_paths,
38 const NativeLoaderNamespace* parent, bool is_shared,
39 bool is_greylist_enabled);
Jiyong Park6291da22019-04-26 18:55:48 +090040
41 NativeLoaderNamespace(NativeLoaderNamespace&&) = default;
42 NativeLoaderNamespace(const NativeLoaderNamespace&) = default;
43 NativeLoaderNamespace& operator=(const NativeLoaderNamespace&) = default;
44
Jiyong Park85377812019-05-04 00:30:23 +090045 android_namespace_t* ToRawAndroidNamespace() const { return std::get<0>(raw_); }
46 native_bridge_namespace_t* ToRawNativeBridgeNamespace() const { return std::get<1>(raw_); }
47
48 std::string name() const { return name_; }
49 bool IsBridged() const { return raw_.index() == 1; }
50 bool IsNil() const {
51 return IsBridged() ? std::get<1>(raw_) == nullptr : std::get<0>(raw_) == nullptr;
Jiyong Park6291da22019-04-26 18:55:48 +090052 }
53
Jiyong Park85377812019-05-04 00:30:23 +090054 bool Link(const NativeLoaderNamespace& target, const std::string& shared_libs) const;
Jiyong Parkd970ccb2019-05-17 18:48:32 +090055 void* Load(const char* lib_name) const;
Jiyong Park85377812019-05-04 00:30:23 +090056 char* GetError() const;
Jiyong Park6291da22019-04-26 18:55:48 +090057
Jiyong Park85377812019-05-04 00:30:23 +090058 static NativeLoaderNamespace GetExportedNamespace(const std::string& name, bool is_bridged);
59 static NativeLoaderNamespace GetPlatformNamespace(bool is_bridged);
Jiyong Park6291da22019-04-26 18:55:48 +090060
61 private:
Jiyong Park85377812019-05-04 00:30:23 +090062 explicit NativeLoaderNamespace(const std::string& name, android_namespace_t* ns)
63 : name_(name), raw_(ns) {}
64 explicit NativeLoaderNamespace(const std::string& name, native_bridge_namespace_t* ns)
65 : name_(name), raw_(ns) {}
66
67 std::string name_;
68 std::variant<android_namespace_t*, native_bridge_namespace_t*> raw_;
Jiyong Park6291da22019-04-26 18:55:48 +090069};
70
71} // namespace android
72#endif // #if defined(__ANDROID__)