blob: 24c44f4f3dc5b7bfc37581a712ab80eebf034f68 [file] [log] [blame]
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughescbc80ba2018-02-13 14:26:29 -080029#pragma once
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080030
31#include <android/api-level.h>
32
33#include <stdlib.h>
34#include <limits.h>
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080035
36#include <memory>
37#include <string>
38#include <vector>
39#include <unordered_map>
40
Elliott Hughes5e62b342018-10-25 11:00:00 -070041#include <android-base/macros.h>
42
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080043class NamespaceLinkConfig {
44 public:
45 NamespaceLinkConfig() = default;
Logan Chien9ee45912018-01-18 12:05:09 +080046 NamespaceLinkConfig(const std::string& ns_name, const std::string& shared_libs,
47 bool allow_all_shared_libs)
48 : ns_name_(ns_name), shared_libs_(shared_libs),
49 allow_all_shared_libs_(allow_all_shared_libs) {}
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080050
51 const std::string& ns_name() const {
52 return ns_name_;
53 }
54
55 const std::string& shared_libs() const {
56 return shared_libs_;
57 }
58
Logan Chien9ee45912018-01-18 12:05:09 +080059 bool allow_all_shared_libs() const {
60 return allow_all_shared_libs_;
61 }
62
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080063 private:
64 std::string ns_name_;
65 std::string shared_libs_;
Logan Chien9ee45912018-01-18 12:05:09 +080066 bool allow_all_shared_libs_;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080067};
68
69class NamespaceConfig {
70 public:
71 explicit NamespaceConfig(const std::string& name)
Jiyong Parkd7c48322017-04-03 23:10:37 +090072 : name_(name), isolated_(false), visible_(false)
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080073 {}
74
75 const char* name() const {
76 return name_.c_str();
77 }
78
79 bool isolated() const {
80 return isolated_;
81 }
82
Jiyong Parkd7c48322017-04-03 23:10:37 +090083 bool visible() const {
84 return visible_;
85 }
86
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080087 const std::vector<std::string>& search_paths() const {
88 return search_paths_;
89 }
90
91 const std::vector<std::string>& permitted_paths() const {
92 return permitted_paths_;
93 }
94
95 const std::vector<NamespaceLinkConfig>& links() const {
96 return namespace_links_;
97 }
98
Logan Chien9ee45912018-01-18 12:05:09 +080099 void add_namespace_link(const std::string& ns_name, const std::string& shared_libs,
100 bool allow_all_shared_libs) {
101 namespace_links_.push_back(NamespaceLinkConfig(ns_name, shared_libs, allow_all_shared_libs));
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800102 }
103
104 void set_isolated(bool isolated) {
105 isolated_ = isolated;
106 }
107
Jiyong Parkd7c48322017-04-03 23:10:37 +0900108 void set_visible(bool visible) {
109 visible_ = visible;
110 }
111
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800112 void set_search_paths(std::vector<std::string>&& search_paths) {
113 search_paths_ = search_paths;
114 }
115
116 void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
117 permitted_paths_ = permitted_paths;
118 }
119 private:
120 const std::string name_;
121 bool isolated_;
Jiyong Parkd7c48322017-04-03 23:10:37 +0900122 bool visible_;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800123 std::vector<std::string> search_paths_;
124 std::vector<std::string> permitted_paths_;
125 std::vector<NamespaceLinkConfig> namespace_links_;
126
127 DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceConfig);
128};
129
130class Config {
131 public:
132 Config() : target_sdk_version_(__ANDROID_API__) {}
133
134 const std::vector<std::unique_ptr<NamespaceConfig>>& namespace_configs() const {
135 return namespace_configs_;
136 }
137
138 const NamespaceConfig* default_namespace_config() const {
139 auto it = namespace_configs_map_.find("default");
140 return it == namespace_configs_map_.end() ? nullptr : it->second;
141 }
142
143 uint32_t target_sdk_version() const {
144 return target_sdk_version_;
145 }
146
147 // note that this is one time event and therefore there is no need to
148 // read every section of the config. Every linker instance needs at
149 // most one configuration.
150 // Returns false in case of an error. If binary config was not found
151 // sets *config = nullptr.
152 static bool read_binary_config(const char* ld_config_file_path,
153 const char* binary_realpath,
154 bool is_asan,
155 const Config** config,
156 std::string* error_msg);
Justin Yun53ce7422017-11-27 16:28:07 +0900157
158 static std::string get_vndk_version_string(const char delimiter);
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800159 private:
160 void clear();
161
162 void set_target_sdk_version(uint32_t target_sdk_version) {
163 target_sdk_version_ = target_sdk_version;
164 }
165
166 NamespaceConfig* create_namespace_config(const std::string& name);
167
168 std::vector<std::unique_ptr<NamespaceConfig>> namespace_configs_;
169 std::unordered_map<std::string, NamespaceConfig*> namespace_configs_map_;
170 uint32_t target_sdk_version_;
171
172 DISALLOW_COPY_AND_ASSIGN(Config);
173};