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 #include "linkerconfig/section.h"
17
18 #include <algorithm>
19 #include <functional>
20 #include <unordered_map>
21 #include <utility>
22
23 #include <android-base/result.h>
24 #include <android-base/strings.h>
25
26 #include "linkerconfig/log.h"
27
28 using android::base::Join;
29 using android::base::Result;
30
31 namespace android {
32 namespace linkerconfig {
33 namespace modules {
34
WriteConfig(ConfigWriter & writer)35 void Section::WriteConfig(ConfigWriter& writer) {
36 writer.WriteLine("[" + name_ + "]");
37
38 std::sort(namespaces_.begin(),
39 namespaces_.end(),
40 [](const auto& lhs, const auto& rhs) -> bool {
41 // make "default" a smallest one
42 if (lhs.GetName() == "default") return true;
43 if (rhs.GetName() == "default") return false;
44 return lhs.GetName() < rhs.GetName();
45 });
46
47 if (namespaces_.size() > 1) {
48 std::vector<std::string> additional_namespaces;
49 for (const auto& ns : namespaces_) {
50 if (ns.GetName() != "default") {
51 additional_namespaces.push_back(ns.GetName());
52 }
53 }
54 writer.WriteLine("additional.namespaces = " +
55 Join(additional_namespaces, ','));
56 }
57
58 for (auto& ns : namespaces_) {
59 ns.WriteConfig(writer);
60 }
61 }
62
63 // Resolve() resolves require/provide constraints between namespaces.
64 // When foo.AddProvides({"libfoo.so"}) and bar.AddRequires({"libfoo.so"}),
65 // then Resolve() creates a linke between foo and bar:
66 // foo.GetLink("bar").AddSharedLib({"libfoo.so"}).
67 //
68 // When a referenced lib is not provided by existing namespaces,
69 // it searches the lib in available apexes <apex_providers>
70 // and available aliases <lib_providers>, If found, new namespace is added.
Resolve(const BaseContext & ctx,const LibProviders & lib_providers)71 Result<void> Section::Resolve(const BaseContext& ctx,
72 const LibProviders& lib_providers) {
73 // libs provided by existing namespaces
74 std::unordered_map<std::string, std::string> providers;
75 for (auto& ns : namespaces_) {
76 for (const auto& lib : ns.GetProvides()) {
77 if (auto iter = providers.find(lib); iter != providers.end()) {
78 return Errorf("duplicate: {} is provided by {} and {} in [{}]",
79 lib,
80 iter->second,
81 ns.GetName(),
82 name_);
83 }
84 providers[lib] = ns.GetName();
85 }
86 }
87
88 // libs provided by apexes
89 std::unordered_map<std::string, ApexInfo> apex_providers;
90 for (const auto& apex : ctx.GetApexModules()) {
91 for (const auto& lib : apex.provide_libs) {
92 apex_providers[lib] = apex;
93 }
94 }
95
96 // add a new namespace if not found
97 auto add_namespace = [&](auto name, auto builder) {
98 for (auto& ns : namespaces_) {
99 if (ns.GetName() == name) {
100 LOG(WARNING) << "Skip to create a duplicate namespace(" << name << ")";
101 return;
102 }
103 }
104 auto new_ns = builder();
105 // Update providing library map from the new namespace
106 for (const auto& new_lib : new_ns.GetProvides()) {
107 if (providers.find(new_lib) == providers.end()) {
108 providers[new_lib] = new_ns.GetName();
109 }
110 }
111 namespaces_.push_back(std::move(new_ns));
112 };
113
114 // Reserve enough space for namespace vector which can be increased maximum as
115 // much as potential providers. Appending new namespaces without reserving
116 // enough space from iteration can crash the process.
117 namespaces_.reserve(namespaces_.size() + ctx.GetApexModules().size() +
118 lib_providers.size());
119
120 auto iter = namespaces_.begin();
121 do {
122 auto& ns = *iter;
123 for (const auto& lib : ns.GetRequires()) {
124 // Search the required library in existing namespaces first <providers>,
125 // then the available apexes <apex_providers>,
126 // then the available aliases <lib_providers>
127 if (auto it = providers.find(lib); it != providers.end()) {
128 ns.GetLink(it->second).AddSharedLib(lib);
129 } else if (auto it = apex_providers.find(lib); it != apex_providers.end()) {
130 ns.GetLink(it->second.namespace_name).AddSharedLib(lib);
131 // Add a new namespace for the apex
132 add_namespace(it->second.namespace_name, [&]() {
133 return ctx.BuildApexNamespace(it->second, false);
134 });
135 } else if (auto it = lib_providers.find(lib); it != lib_providers.end()) {
136 // Alias is expanded to <shared_libs>.
137 // For example, ":vndk" is expanded to the list of VNDK-Core/VNDK-Sp libraries
138 ns.GetLink(it->second.ns).AddSharedLib(it->second.shared_libs);
139 // Add a new namespace for the alias
140 add_namespace(it->second.ns, it->second.ns_builder);
141 } else if (ctx.IsStrictMode()) {
142 return Errorf(
143 "not found: {} is required by {} in [{}]", lib, ns.GetName(), name_);
144 }
145 }
146 iter++;
147 } while (iter != namespaces_.end());
148
149 return {};
150 }
151
GetNamespace(const std::string & namespace_name)152 Namespace* Section::GetNamespace(const std::string& namespace_name) {
153 for (auto& ns : namespaces_) {
154 if (ns.GetName() == namespace_name) {
155 return &ns;
156 }
157 }
158
159 return nullptr;
160 }
161
GetName()162 std::string Section::GetName() {
163 return name_;
164 }
165 } // namespace modules
166 } // namespace linkerconfig
167 } // namespace android
168