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 const std::string llndk_libraries_path =
87 vndk_path + "/etc/llndk.libraries." + vndk_version + ".txt";
88 const std::string vndksp_libraries_path =
89 vndk_path + "/etc/vndksp.libraries." + vndk_version + ".txt";
90 const std::string vndkcore_libraries_path =
91 vndk_path + "/etc/vndkcore.libraries." + vndk_version + ".txt";
92 const std::string vndkprivate_libraries_path =
93 vndk_path + "/etc/vndkprivate.libraries." + vndk_version + ".txt";
94 const std::string sanitizer_library_path =
95 root + "/system/etc/sanitizer.libraries.txt";
96
97 Variables::AddValue("LLNDK_LIBRARIES_" + partition,
98 GetPublicLibrariesString(llndk_libraries_path,
99 vndkprivate_libraries_path));
100
101 Variables::AddValue("PRIVATE_LLNDK_LIBRARIES_" + partition,
102 GetPrivateLibrariesString(llndk_libraries_path,
103 vndkprivate_libraries_path));
104
105 Variables::AddValue("VNDK_SAMEPROCESS_LIBRARIES_" + partition,
106 GetPublicLibrariesString(vndksp_libraries_path,
107 vndkprivate_libraries_path));
108
109 Variables::AddValue("VNDK_CORE_LIBRARIES_" + partition,
110 GetPublicLibrariesString(vndkcore_libraries_path,
111 vndkprivate_libraries_path));
112
113 Variables::AddValue("SANITIZER_DEFAULT_" + partition,
114 GetPublicLibrariesString(sanitizer_library_path,
115 vndkcore_libraries_path));
116
117 if (partition == "VENDOR") {
118 auto vndkcorevariant_library_path =
119 root + "/system/etc/vndkcorevariant.libraries.txt";
120 Variables::AddValue("VNDK_USING_CORE_VARIANT_LIBRARIES",
121 GetPublicLibrariesString(vndkcorevariant_library_path,
122 vndkprivate_libraries_path));
123 }
124 }
125
LoadLibraryListVariables(const std::string & root)126 void LoadLibraryListVariables(const std::string& root) {
127 LoadVndkLibraryListVariables(root, GetVendorVndkVersion(), "VENDOR");
128 LoadVndkLibraryListVariables(root, GetProductVndkVersion(), "PRODUCT");
129
130 auto sanitizer_library_path = root + "/system/etc/sanitizer.libraries.txt";
131 Variables::AddValue("SANITIZER_RUNTIME_LIBRARIES",
132 GetLibrariesString(sanitizer_library_path));
133 }
134 } // namespace
135
136 namespace android {
137 namespace linkerconfig {
138 namespace generator {
139
LoadVariables(const std::string & root)140 void LoadVariables(const std::string& root) {
141 LoadVndkVersionVariable();
142 LoadPartitionPathVariables(root);
143 LoadLibraryListVariables(root);
144 }
145
146 } // namespace generator
147 } // namespace linkerconfig
148 } // namespace android
149