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/baseconfig.h"
18 #include "linkerconfig/environment.h"
19 #include "linkerconfig/sectionbuilder.h"
20 
21 using android::linkerconfig::modules::DirToSection;
22 using android::linkerconfig::modules::Section;
23 
24 namespace {
25 void RedirectSection(std::vector<DirToSection>& dir_to_section,
26                      const std::string& from, const std::string& to) {
27   for (auto& [dir, section] : dir_to_section) {
28     if (section == from) {
29       section = to;
30     }
31   }
32 }
33 void RemoveSection(std::vector<DirToSection>& dir_to_section,
34                    const std::string& to_be_removed) {
35   dir_to_section.erase(
36       std::remove_if(dir_to_section.begin(),
37                      dir_to_section.end(),
38                      [&](auto pair) { return (pair.second == to_be_removed); }),
39       dir_to_section.end());
40 }
41 }  // namespace
42 
43 namespace android {
44 namespace linkerconfig {
45 namespace contents {
46 android::linkerconfig::modules::Configuration CreateBaseConfiguration(
47     Context& ctx) {
48   std::vector<Section> sections;
49 
50   if (android::linkerconfig::modules::IsVndkLiteDevice()) {
51     ctx.SetCurrentLinkerConfigType(LinkerConfigType::Vndklite);
52   } else {
53     ctx.SetCurrentLinkerConfigType(LinkerConfigType::Default);
54   }
55 
56   // Don't change the order here. The first pattern that matches with the
57   // absolute path of an executable is selected.
58   std::vector<DirToSection> dirToSection = {
59       {"/system/bin/", "system"},
60       {"/system/xbin/", "system"},
61       {Var("SYSTEM_EXT") + "/bin/", "system"},
62 
63       // Processes from the product partition will have a separate section if
64       // PRODUCT_PRODUCT_VNDK_VERSION is defined. Otherwise, they are run from
65       // the "system" section.
66       {Var("PRODUCT") + "/bin/", "product"},
67 
68       {"/odm/bin/", "vendor"},
69       {"/vendor/bin/", "vendor"},
70       {"/data/nativetest/odm", "vendor"},
71       {"/data/nativetest64/odm", "vendor"},
72       {"/data/benchmarktest/odm", "vendor"},
73       {"/data/benchmarktest64/odm", "vendor"},
74       {"/data/nativetest/vendor", "vendor"},
75       {"/data/nativetest64/vendor", "vendor"},
76       {"/data/benchmarktest/vendor", "vendor"},
77       {"/data/benchmarktest64/vendor", "vendor"},
78 
79       {"/data/nativetest/unrestricted", "unrestricted"},
80       {"/data/nativetest64/unrestricted", "unrestricted"},
81 
82       // TODO(b/123864775): Ensure tests are run from /data/nativetest{,64} or
83       // (if necessary) the unrestricted subdirs above. Then clean this up.
84       {"/data/local/tmp", "unrestricted"},
85 
86       {"/postinstall", "postinstall"},
87       // Fallback entry to provide APEX namespace lookups for binaries anywhere
88       // else. This must be last.
89       {"/data", "system"},
90   };
91 
92   sections.emplace_back(BuildSystemSection(ctx));
93   if (ctx.IsVndkAvailable()) {
94     sections.emplace_back(BuildVendorSection(ctx));
95     if (android::linkerconfig::modules::IsProductVndkVersionDefined() &&
96         !android::linkerconfig::modules::IsVndkLiteDevice()) {
97       sections.emplace_back(BuildProductSection(ctx));
98     } else {
99       RedirectSection(dirToSection, "product", "system");
100     }
101   } else {
102     RemoveSection(dirToSection, "product");
103     RemoveSection(dirToSection, "vendor");
104   }
105 
106   sections.emplace_back(BuildUnrestrictedSection(ctx));
107   sections.emplace_back(BuildPostInstallSection(ctx));
108 
109   return android::linkerconfig::modules::Configuration(std::move(sections),
110                                                        dirToSection);
111 }
112 }  // namespace contents
113 }  // namespace linkerconfig
114 }  // namespace android
115