xref: /aosp_15_r20/system/linkerconfig/contents/namespace/vndk.cc (revision e5eeaa8e05bc25a862c0c861bda7c8a6bfb42dad)
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 
17 // This namespace is exclusively for vndk-sp libs.
18 
19 #include "linkerconfig/environment.h"
20 
21 #include <android-base/strings.h>
22 
23 #include "linkerconfig/namespacebuilder.h"
24 
25 using android::linkerconfig::modules::Namespace;
26 
27 namespace android {
28 namespace linkerconfig {
29 namespace contents {
BuildVndkNamespace(const Context & ctx,VndkUserPartition vndk_user)30 Namespace BuildVndkNamespace([[maybe_unused]] const Context& ctx,
31                              VndkUserPartition vndk_user) {
32   bool is_system_or_unrestricted_section = ctx.IsSystemSection() ||
33                                            ctx.IsUnrestrictedSection();
34   if (ctx.IsApexBinaryConfig()) {
35     is_system_or_unrestricted_section = ctx.GetCurrentApex().InSystem();
36   }
37   // In the system section, we need to have an additional vndk namespace for
38   // product apps. We must have a different name "vndk_product" for this
39   // namespace. "vndk_product" namespace is used only from the native_loader for
40   // product apps.
41   const char* name;
42   if (is_system_or_unrestricted_section &&
43       vndk_user == VndkUserPartition::Product) {
44     name = "vndk_product";
45   } else {
46     name = "vndk";
47   }
48 
49   // Isolated and visible when used in the [system] or [unrestricted] section to
50   // allow links to be created at runtime, e.g. through android_link_namespaces
51   // in libnativeloader. Otherwise namespace should be isolated but not visible
52   // so namespace itself keep strict and links would not be modified at runtime.
53   Namespace ns(name,
54                /*is_isolated=*/true,
55                /*is_visible=*/is_system_or_unrestricted_section);
56 
57   std::vector<std::string> lib_paths;
58   std::string vndk_version;
59   if (vndk_user == VndkUserPartition::Product) {
60     lib_paths = {Var("PRODUCT") + "/${LIB}"};
61     vndk_version = Var("PRODUCT_VNDK_VERSION");
62   } else {
63     // default for vendor
64     lib_paths = {"/odm/${LIB}", "/vendor/${LIB}"};
65     vndk_version = Var("VENDOR_VNDK_VERSION");
66   }
67 
68   // Search order:
69   // 1. VNDK Extensions
70   // 2. VNDK APEX
71   // 3. vendor/lib or product/lib to allow extensions to use them
72 
73   // 1. VNDK Extensions
74   for (const auto& lib_path : lib_paths) {
75     ns.AddSearchPath(lib_path + "/vndk-sp");
76     if (!is_system_or_unrestricted_section) {
77       ns.AddSearchPath(lib_path + "/vndk");
78     }
79   }
80 
81   // 2. VNDK APEX
82   ns.AddSearchPath("/apex/com.android.vndk.v" + vndk_version + "/${LIB}");
83 
84   if (vndk_user == VndkUserPartition::Vendor) {
85     // It is for vendor sp-hal
86     ns.AddPermittedPath("/odm/${LIB}/hw");
87     ns.AddPermittedPath("/odm/${LIB}/egl");
88     ns.AddPermittedPath("/vendor/${LIB}/hw");
89     ns.AddPermittedPath("/vendor/${LIB}/egl");
90     ns.AddPermittedPath("/system/vendor/${LIB}/hw");
91     ns.AddPermittedPath("/system/vendor/${LIB}/egl");
92 
93     // This is exceptionally required since [email protected] is here
94     ns.AddPermittedPath("/apex/com.android.vndk.v" +
95                         Var("VENDOR_VNDK_VERSION") + "/${LIB}/hw");
96   }
97 
98   // 3. vendor/lib or product/lib
99   if (is_system_or_unrestricted_section || ctx.IsApexBinaryConfig()) {
100     // Add (vendor|product)/lib for cases (vendor|product) namespace does not exist.
101     for (const auto& lib_path : lib_paths) {
102       ns.AddSearchPath(lib_path);
103     }
104   } else {
105     // To avoid double loading library issue, add link to the default
106     // namespace instead of adding search path.
107     ns.GetLink("default").AllowAllSharedLibs();
108   }
109 
110   AddLlndkLibraries(ctx, &ns, vndk_user);
111 
112   if (ctx.IsProductSection() || ctx.IsVendorSection()) {
113     if (android::linkerconfig::modules::IsVndkInSystemNamespace()) {
114       ns.GetLink("vndk_in_system")
115           .AddSharedLib(Var("VNDK_USING_CORE_VARIANT_LIBRARIES"));
116     }
117   }
118 
119   return ns;
120 }
121 }  // namespace contents
122 }  // namespace linkerconfig
123 }  // namespace android
124