xref: /aosp_15_r20/system/linkerconfig/contents/configuration/baseconfig.cc (revision e5eeaa8e05bc25a862c0c861bda7c8a6bfb42dad)
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 #include "linkerconfig/variables.h"
21 
22 using android::linkerconfig::modules::DirToSection;
23 using android::linkerconfig::modules::Section;
24 
25 namespace {
RemoveSection(std::vector<DirToSection> & dir_to_section,const std::string & to_be_removed)26 void RemoveSection(std::vector<DirToSection>& dir_to_section,
27                    const std::string& to_be_removed) {
28   dir_to_section.erase(
29       std::remove_if(dir_to_section.begin(),
30                      dir_to_section.end(),
31                      [&](auto pair) { return (pair.second == to_be_removed); }),
32       dir_to_section.end());
33 }
34 }  // namespace
35 
36 namespace android {
37 namespace linkerconfig {
38 namespace contents {
CreateBaseConfiguration(Context & ctx)39 android::linkerconfig::modules::Configuration CreateBaseConfiguration(
40     Context& ctx) {
41   std::vector<Section> sections;
42 
43   ctx.SetCurrentLinkerConfigType(LinkerConfigType::Default);
44 
45   // Don't change the order here. The first pattern that matches with the
46   // absolute path of an executable is selected.
47   std::vector<DirToSection> dirToSection = {
48       {"/system/bin/", "system"},
49       {"/system/xbin/", "system"},
50       {Var("SYSTEM_EXT") + "/bin/", "system"},
51 
52       // Processes from the product partition will have a separate section if
53       // PRODUCT_PRODUCT_VNDK_VERSION is defined. Otherwise, they are run from
54       // the "system" section.
55       {Var("PRODUCT") + "/bin/", "product"},
56 
57       {"/odm/bin/", "vendor"},
58       {"/vendor/bin/", "vendor"},
59       {"/data/nativetest/odm", "vendor"},
60       {"/data/nativetest64/odm", "vendor"},
61       {"/data/benchmarktest/odm", "vendor"},
62       {"/data/benchmarktest64/odm", "vendor"},
63       {"/data/nativetest/vendor", "vendor"},
64       {"/data/nativetest64/vendor", "vendor"},
65       {"/data/benchmarktest/vendor", "vendor"},
66       {"/data/benchmarktest64/vendor", "vendor"},
67 
68       {"/data/nativetest/unrestricted", "unrestricted"},
69       {"/data/nativetest64/unrestricted", "unrestricted"},
70 
71       // Create isolated namespace for development purpose.
72       // This isolates binary from the system so binaries and libraries from
73       // this location can be separated from system libraries.
74       {"/data/local/tmp/isolated", "isolated"},
75 
76       // Create directories under shell-writable /data/local/tests for
77       // each namespace in order to run tests.
78       {"/data/local/tests/product", "product"},
79       {"/data/local/tests/system", "system"},
80       {"/data/local/tests/unrestricted", "unrestricted"},
81       {"/data/local/tests/vendor", "vendor"},
82 
83       // TODO(b/123864775): Ensure tests are run from one of the subdirectories
84       // above.  Then clean this up.
85       {"/data/local/tmp", "unrestricted"},
86 
87       {"/postinstall", "postinstall"},
88       // Fallback entries to provide APEX namespace lookups for binaries
89       // anywhere else. These must be last.
90       {"/data", "system"},
91       {"/tmp", "system"},
92       // TODO(b/168556887): Remove this when we have a dedicated section for
93       // binaries in APKs
94       {Var("PRODUCT") + "/app/", "system"},
95   };
96 
97   sections.emplace_back(BuildSystemSection(ctx));
98   if (android::linkerconfig::modules::IsTreblelizedDevice()) {
99     sections.emplace_back(BuildVendorSection(ctx));
100     sections.emplace_back(BuildProductSection(ctx));
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   sections.emplace_back(BuildIsolatedSection(ctx));
110 
111   return android::linkerconfig::modules::Configuration(std::move(sections),
112                                                        std::move(dirToSection));
113 }
114 }  // namespace contents
115 }  // namespace linkerconfig
116 }  // namespace android
117