| Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 1 | /* |
| 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 Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <variant> |
| 21 | #include <vector> |
| Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 22 | |
| Jiyong Park | 16a9896 | 2019-05-04 03:10:48 +0900 | [diff] [blame] | 23 | #include <android-base/logging.h> |
| 24 | #include <android/dlext.h> |
| 25 | #include <log/log.h> |
| 26 | #include <nativebridge/native_bridge.h> |
| Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 27 | |
| 28 | namespace 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. |
| 33 | struct NativeLoaderNamespace { |
| 34 | public: |
| Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 35 | // 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 Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 40 | |
| 41 | NativeLoaderNamespace(NativeLoaderNamespace&&) = default; |
| 42 | NativeLoaderNamespace(const NativeLoaderNamespace&) = default; |
| 43 | NativeLoaderNamespace& operator=(const NativeLoaderNamespace&) = default; |
| 44 | |
| Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 45 | 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 Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 52 | } |
| 53 | |
| Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 54 | bool Link(const NativeLoaderNamespace& target, const std::string& shared_libs) const; |
| Jiyong Park | d970ccb | 2019-05-17 18:48:32 +0900 | [diff] [blame] | 55 | void* Load(const char* lib_name) const; |
| Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 56 | char* GetError() const; |
| Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 57 | |
| Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 58 | static NativeLoaderNamespace GetExportedNamespace(const std::string& name, bool is_bridged); |
| 59 | static NativeLoaderNamespace GetPlatformNamespace(bool is_bridged); |
| Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 60 | |
| 61 | private: |
| Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame] | 62 | 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 Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | } // namespace android |
| 72 | #endif // #if defined(__ANDROID__) |