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 #include "linkerconfig/variableloader.h"
18
19 #include <android-base/result.h>
20 #include <android-base/strings.h>
21
22 #include <climits>
23 #include <cstdlib>
24 #include <cstring>
25
26 #include "linkerconfig/environment.h"
27 #include "linkerconfig/librarylistloader.h"
28 #include "linkerconfig/log.h"
29 #include "linkerconfig/stringutil.h"
30 #include "linkerconfig/variables.h"
31
32 using android::base::Result;
33 using android::linkerconfig::modules::GetProductVndkVersion;
34 using android::linkerconfig::modules::GetVendorVndkVersion;
35 using android::linkerconfig::modules::TrimPrefix;
36 using android::linkerconfig::modules::Variables;
37
38 namespace {
39 using namespace android::linkerconfig::generator;
40
LoadVndkVersionVariable()41 void LoadVndkVersionVariable() {
42 Variables::AddValue("VENDOR_VNDK_VERSION", GetVendorVndkVersion());
43 Variables::AddValue("PRODUCT_VNDK_VERSION", GetProductVndkVersion());
44 }
45
GetRealPath(std::string target_path)46 Result<std::string> GetRealPath(std::string target_path) {
47 char resolved_path[PATH_MAX];
48 if (realpath(target_path.c_str(), resolved_path) != nullptr) {
49 return resolved_path;
50 }
51
52 return ErrnoErrorf("Failed to get realpath from {}", target_path);
53 }
54
LoadVariableFromPartitionPath(const std::string & root,std::string variable_name,std::string partition)55 void LoadVariableFromPartitionPath(const std::string& root,
56 std::string variable_name,
57 std::string partition) {
58 auto real_path = GetRealPath(root + partition);
59
60 if (real_path.ok()) {
61 Variables::AddValue(variable_name, TrimPrefix(*real_path, root));
62 } else {
63 LOG(WARNING) << real_path.error();
64 Variables::AddValue(variable_name, partition);
65 }
66 }
67
LoadPartitionPathVariables(const std::string & root)68 void LoadPartitionPathVariables(const std::string& root) {
69 // TODO(b/141714913): generalize path handling
70 LoadVariableFromPartitionPath(root, "PRODUCT", "/product");
71 LoadVariableFromPartitionPath(root, "SYSTEM_EXT", "/system_ext");
72 }
73
LoadVndkLibraryListVariables(const std::string & root,const std::string & vndk_version,const std::string & partition)74 void LoadVndkLibraryListVariables(const std::string& root,
75 const std::string& vndk_version,
76 const std::string& partition) {
77 if (vndk_version == "") {
78 return;
79 }
80 const std::string vndk_path = root + "/apex/com.android.vndk.v" + vndk_version;
81 // Skip loading if VNDK APEX is not available
82 if (::access(vndk_path.c_str(), F_OK) != 0) {
83 PLOG(ERROR) << "Unable to access VNDK APEX at path: " << vndk_path;
84 return;
85 }
86
87 const std::string vndksp_libraries_path =
88 vndk_path + "/etc/vndksp.libraries." + vndk_version + ".txt";
89 const std::string vndkcore_libraries_path =
90 vndk_path + "/etc/vndkcore.libraries." + vndk_version + ".txt";
91 const std::string vndkprivate_libraries_path =
92 vndk_path + "/etc/vndkprivate.libraries." + vndk_version + ".txt";
93 const std::string sanitizer_library_path =
94 root + "/system/etc/sanitizer.libraries.txt";
95
96 Variables::AddValue("VNDK_SAMEPROCESS_LIBRARIES_" + partition,
97 GetPublicLibrariesString(vndksp_libraries_path,
98 vndkprivate_libraries_path));
99
100 Variables::AddValue("VNDK_CORE_LIBRARIES_" + partition,
101 GetPublicLibrariesString(vndkcore_libraries_path,
102 vndkprivate_libraries_path));
103
104 Variables::AddValue("SANITIZER_DEFAULT_" + partition,
105 GetPublicLibrariesString(sanitizer_library_path,
106 vndkcore_libraries_path));
107
108 if (partition == "VENDOR") {
109 auto vndkcorevariant_library_path =
110 root + "/system/etc/vndkcorevariant.libraries.txt";
111 Variables::AddValue("VNDK_USING_CORE_VARIANT_LIBRARIES",
112 GetPublicLibrariesString(vndkcorevariant_library_path,
113 vndkprivate_libraries_path));
114 }
115 }
116
LoadLlndkLibraryListVariables(const std::string & root,const std::string & vndk_version,const std::string & partition)117 void LoadLlndkLibraryListVariables(const std::string& root,
118 const std::string& vndk_version,
119 const std::string& partition) {
120 if (vndk_version == "") {
121 // llndk.libraries.txt in the system image does not contain version
122 // information in the file name because system has fixed target version
123 // which is always higher or equal than vendor / product images, and llndk
124 // libraries (and its list) are backward compatible.
125 const std::string llndk_libraries_in_system_path =
126 root + "/system/etc/llndk.libraries.txt";
127 Variables::AddValue("LLNDK_LIBRARIES_" + partition,
128 GetLibrariesString(llndk_libraries_in_system_path));
129 } else {
130 const std::string vndk_path =
131 root + "/apex/com.android.vndk.v" + vndk_version;
132 // Skip loading if VNDK APEX is not available
133 if (::access(vndk_path.c_str(), F_OK) != 0) {
134 PLOG(ERROR) << "Unable to access VNDK APEX at path: " << vndk_path;
135 return;
136 }
137
138 const std::string llndk_libraries_path =
139 vndk_path + "/etc/llndk.libraries." + vndk_version + ".txt";
140 const std::string vndkprivate_libraries_path =
141 vndk_path + "/etc/vndkprivate.libraries." + vndk_version + ".txt";
142
143 Variables::AddValue("LLNDK_LIBRARIES_" + partition,
144 GetPublicLibrariesString(llndk_libraries_path,
145 vndkprivate_libraries_path));
146 Variables::AddValue("PRIVATE_LLNDK_LIBRARIES_" + partition,
147 GetPrivateLibrariesString(llndk_libraries_path,
148 vndkprivate_libraries_path));
149 }
150 }
151
LoadLibraryListVariables(const std::string & root)152 void LoadLibraryListVariables(const std::string& root) {
153 LoadVndkLibraryListVariables(root, GetVendorVndkVersion(), "VENDOR");
154 LoadVndkLibraryListVariables(root, GetProductVndkVersion(), "PRODUCT");
155
156 LoadLlndkLibraryListVariables(root, GetVendorVndkVersion(), "VENDOR");
157 LoadLlndkLibraryListVariables(root, GetProductVndkVersion(), "PRODUCT");
158
159 auto llndk_library_path = root + "/system/etc/llndk.libraries.txt";
160 auto sanitizer_library_path = root + "/system/etc/sanitizer.libraries.txt";
161 Variables::AddValue("SANITIZER_RUNTIME_LIBRARIES",
162 GetLibrariesString(sanitizer_library_path));
163 Variables::AddValue(
164 "SANITIZER_LIBRARIES_LLNDK",
165 GetPrivateLibrariesString(sanitizer_library_path, llndk_library_path));
166 }
167 } // namespace
168
169 namespace android {
170 namespace linkerconfig {
171 namespace generator {
172
LoadVariables(const std::string & root)173 void LoadVariables(const std::string& root) {
174 LoadVndkVersionVariable();
175 LoadPartitionPathVariables(root);
176 LoadLibraryListVariables(root);
177 }
178
179 } // namespace generator
180 } // namespace linkerconfig
181 } // namespace android
182