• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
RedirectSection(std::vector<DirToSection> & dir_to_section,const std::string & from,const std::string & to)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 }
RemoveSection(std::vector<DirToSection> & dir_to_section,const std::string & to_be_removed)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 {
CreateBaseConfiguration(Context & ctx)46 android::linkerconfig::modules::Configuration CreateBaseConfiguration(
47     Context& ctx) {
48   std::vector<Section> sections;
49 
50   ctx.SetCurrentLinkerConfigType(LinkerConfigType::Default);
51 
52   // Don't change the order here. The first pattern that matches with the
53   // absolute path of an executable is selected.
54   std::vector<DirToSection> dirToSection = {
55       {"/system/bin/", "system"},
56       {"/system/xbin/", "system"},
57       {Var("SYSTEM_EXT") + "/bin/", "system"},
58 
59       // Processes from the product partition will have a separate section if
60       // PRODUCT_PRODUCT_VNDK_VERSION is defined. Otherwise, they are run from
61       // the "system" section.
62       {Var("PRODUCT") + "/bin/", "product"},
63 
64       {"/odm/bin/", "vendor"},
65       {"/vendor/bin/", "vendor"},
66       {"/data/nativetest/odm", "vendor"},
67       {"/data/nativetest64/odm", "vendor"},
68       {"/data/benchmarktest/odm", "vendor"},
69       {"/data/benchmarktest64/odm", "vendor"},
70       {"/data/nativetest/vendor", "vendor"},
71       {"/data/nativetest64/vendor", "vendor"},
72       {"/data/benchmarktest/vendor", "vendor"},
73       {"/data/benchmarktest64/vendor", "vendor"},
74 
75       {"/data/nativetest/unrestricted", "unrestricted"},
76       {"/data/nativetest64/unrestricted", "unrestricted"},
77 
78       // Create isolated namespace for development purpose.
79       // This isolates binary from the system so binaries and libraries from
80       // this location can be separated from system libraries.
81       {"/data/local/tmp/isolated", "isolated"},
82 
83       // Create directories under shell-writable /data/local/tests for
84       // each namespace in order to run tests.
85       {"/data/local/tests/product", "product"},
86       {"/data/local/tests/system", "system"},
87       {"/data/local/tests/unrestricted", "unrestricted"},
88       {"/data/local/tests/vendor", "vendor"},
89 
90       // TODO(b/123864775): Ensure tests are run from one of the subdirectories
91       // above.  Then clean this up.
92       {"/data/local/tmp", "unrestricted"},
93 
94       {"/postinstall", "postinstall"},
95       // Fallback entry to provide APEX namespace lookups for binaries anywhere
96       // else. This must be last.
97       {"/data", "system"},
98       // TODO(b/168556887): Remove this when we have a dedicated section for
99       // binaries in APKs
100       {Var("PRODUCT") + "/app/", "system"},
101   };
102 
103   sections.emplace_back(BuildSystemSection(ctx));
104   if (ctx.IsVndkAvailable()) {
105     sections.emplace_back(BuildVendorSection(ctx));
106     if (android::linkerconfig::modules::IsProductVndkVersionDefined()) {
107       sections.emplace_back(BuildProductSection(ctx));
108     } else {
109       RedirectSection(dirToSection, "product", "system");
110     }
111   } else {
112     RemoveSection(dirToSection, "product");
113     RemoveSection(dirToSection, "vendor");
114   }
115 
116   sections.emplace_back(BuildUnrestrictedSection(ctx));
117   sections.emplace_back(BuildPostInstallSection(ctx));
118 
119   sections.emplace_back(BuildIsolatedSection(ctx));
120 
121   return android::linkerconfig::modules::Configuration(std::move(sections),
122                                                        dirToSection);
123 }
124 }  // namespace contents
125 }  // namespace linkerconfig
126 }  // namespace android
127