blob: 1af5da80138ee6d72ba54e2d18e3ce180356d114 [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
29#include "linker_config.h"
30
31#include "linker_globals.h"
32#include "linker_debug.h"
33#include "linker_utils.h"
34
35#include <android-base/file.h>
Justin Yun53ce7422017-11-27 16:28:07 +090036#include <android-base/properties.h>
Tom Cherryb8ab6182017-04-05 16:20:29 -070037#include <android-base/scopeguard.h>
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080038#include <android-base/strings.h>
39
Christopher Ferris7a3681e2017-04-24 17:48:32 -070040#include <async_safe/log.h>
41
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080042#include <stdlib.h>
43
44#include <string>
45#include <unordered_map>
46
Sundong Ahn8fc50322017-10-10 14:12:40 +090047#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
48#include <sys/_system_properties.h>
49
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -080050class ConfigParser {
51 public:
52 enum {
53 kProperty,
54 kSection,
55 kEndOfFile,
56 kError,
57 };
58
59 explicit ConfigParser(std::string&& content)
60 : content_(content), p_(0), lineno_(0), was_end_of_file_(false) {}
61
62 /*
63 * Possible return values
64 * kProperty: name is set to property name and value is set to property value
65 * kSection: name is set to section name.
66 * kEndOfFile: reached end of file.
67 * kError: error_msg is set.
68 */
69 int next_token(std::string* name, std::string* value, std::string* error_msg) {
70 std::string line;
71 while(NextLine(&line)) {
72 size_t found = line.find('#');
73 line = android::base::Trim(line.substr(0, found));
74
75 if (line.empty()) {
76 continue;
77 }
78
79 if (line[0] == '[' && line[line.size() - 1] == ']') {
80 *name = line.substr(1, line.size() - 2);
81 return kSection;
82 }
83
84 found = line.find('=');
85 if (found == std::string::npos) {
86 *error_msg = std::string("invalid format: ") +
87 line +
88 ", expected \"name = property\" or \"[section]\"";
89 return kError;
90 }
91
92 *name = android::base::Trim(line.substr(0, found));
93 *value = android::base::Trim(line.substr(found + 1));
94 return kProperty;
95 }
96
97 // to avoid infinite cycles when programmer makes a mistake
98 CHECK(!was_end_of_file_);
99 was_end_of_file_ = true;
100 return kEndOfFile;
101 }
102
103 size_t lineno() const {
104 return lineno_;
105 }
106
107 private:
108 bool NextLine(std::string* line) {
109 if (p_ == std::string::npos) {
110 return false;
111 }
112
113 size_t found = content_.find('\n', p_);
114 if (found != std::string::npos) {
115 *line = content_.substr(p_, found - p_);
116 p_ = found + 1;
117 } else {
118 *line = content_.substr(p_);
119 p_ = std::string::npos;
120 }
121
122 lineno_++;
123 return true;
124 }
125
126 std::string content_;
127 size_t p_;
128 size_t lineno_;
129 bool was_end_of_file_;
130
131 DISALLOW_IMPLICIT_CONSTRUCTORS(ConfigParser);
132};
133
134class PropertyValue {
135 public:
136 PropertyValue() = default;
137
138 PropertyValue(std::string&& value, size_t lineno)
139 : value_(value), lineno_(lineno) {}
140
141 const std::string& value() const {
142 return value_;
143 }
144
145 size_t lineno() const {
146 return lineno_;
147 }
148
149 private:
150 std::string value_;
151 size_t lineno_;
152};
153
154static std::string create_error_msg(const char* file,
155 size_t lineno,
156 const std::string& msg) {
157 char buf[1024];
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700158 async_safe_format_buffer(buf, sizeof(buf), "%s:%zu: error: %s", file, lineno, msg.c_str());
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800159
160 return std::string(buf);
161}
162
163static bool parse_config_file(const char* ld_config_file_path,
164 const char* binary_realpath,
165 std::unordered_map<std::string, PropertyValue>* properties,
166 std::string* error_msg) {
167 std::string content;
168 if (!android::base::ReadFileToString(ld_config_file_path, &content)) {
169 if (errno != ENOENT) {
170 *error_msg = std::string("error reading file \"") +
171 ld_config_file_path + "\": " + strerror(errno);
172 }
173 return false;
174 }
175
176 ConfigParser cp(std::move(content));
177
178 std::string section_name;
179
180 while(true) {
181 std::string name;
182 std::string value;
183 std::string error;
184
185 int result = cp.next_token(&name, &value, &error);
186 if (result == ConfigParser::kError) {
187 DL_WARN("error parsing %s:%zd: %s (ignoring this line)",
188 ld_config_file_path,
189 cp.lineno(),
190 error.c_str());
191 continue;
192 }
193
194 if (result == ConfigParser::kSection || result == ConfigParser::kEndOfFile) {
195 return false;
196 }
197
198 if (result == ConfigParser::kProperty) {
199 if (!android::base::StartsWith(name, "dir.")) {
200 DL_WARN("error parsing %s:%zd: unexpected property name \"%s\", "
201 "expected format dir.<section_name> (ignoring this line)",
202 ld_config_file_path,
203 cp.lineno(),
204 name.c_str());
205 continue;
206 }
207
208 // remove trailing '/'
209 while (value[value.size() - 1] == '/') {
210 value = value.substr(0, value.size() - 1);
211 }
212
213 if (value.empty()) {
214 DL_WARN("error parsing %s:%zd: property value is empty (ignoring this line)",
215 ld_config_file_path,
216 cp.lineno());
217 continue;
218 }
219
220 if (file_is_under_dir(binary_realpath, value)) {
221 section_name = name.substr(4);
222 break;
223 }
224 }
225 }
226
227 // skip everything until we meet a correct section
228 while (true) {
229 std::string name;
230 std::string value;
231 std::string error;
232
233 int result = cp.next_token(&name, &value, &error);
234
235 if (result == ConfigParser::kSection && name == section_name) {
236 break;
237 }
238
239 if (result == ConfigParser::kEndOfFile) {
240 *error_msg = create_error_msg(ld_config_file_path,
241 cp.lineno(),
242 std::string("section \"") + section_name + "\" not found");
243 return false;
244 }
245 }
246
247 // found the section - parse it
248 while (true) {
249 std::string name;
250 std::string value;
251 std::string error;
252
253 int result = cp.next_token(&name, &value, &error);
254
255 if (result == ConfigParser::kEndOfFile || result == ConfigParser::kSection) {
256 break;
257 }
258
259 if (result == ConfigParser::kProperty) {
260 if (properties->find(name) != properties->end()) {
261 DL_WARN("%s:%zd: warning: property \"%s\" redefinition",
262 ld_config_file_path,
263 cp.lineno(),
264 name.c_str());
265 }
266
267 (*properties)[name] = PropertyValue(std::move(value), cp.lineno());
268 }
269
270 if (result == ConfigParser::kError) {
271 DL_WARN("error parsing %s:%zd: %s (ignoring this line)",
272 ld_config_file_path,
273 cp.lineno(),
274 error.c_str());
275 continue;
276 }
277 }
278
279 return true;
280}
281
282static Config g_config;
283
284static constexpr const char* kDefaultConfigName = "default";
285static constexpr const char* kPropertyAdditionalNamespaces = "additional.namespaces";
286#if defined(__LP64__)
287static constexpr const char* kLibParamValue = "lib64";
288#else
289static constexpr const char* kLibParamValue = "lib";
290#endif
291
292class Properties {
293 public:
294 explicit Properties(std::unordered_map<std::string, PropertyValue>&& properties)
295 : properties_(properties), target_sdk_version_(__ANDROID_API__) {}
296
297 std::vector<std::string> get_strings(const std::string& name, size_t* lineno = nullptr) const {
298 auto it = find_property(name, lineno);
299 if (it == properties_.end()) {
300 // return empty vector
301 return std::vector<std::string>();
302 }
303
304 std::vector<std::string> strings = android::base::Split(it->second.value(), ",");
305
306 for (size_t i = 0; i < strings.size(); ++i) {
307 strings[i] = android::base::Trim(strings[i]);
308 }
309
310 return strings;
311 }
312
313 bool get_bool(const std::string& name, size_t* lineno = nullptr) const {
314 auto it = find_property(name, lineno);
315 if (it == properties_.end()) {
316 return false;
317 }
318
319 return it->second.value() == "true";
320 }
321
322 std::string get_string(const std::string& name, size_t* lineno = nullptr) const {
323 auto it = find_property(name, lineno);
324 return (it == properties_.end()) ? "" : it->second.value();
325 }
326
Jiyong Park0f33f232017-09-21 10:27:36 +0900327 std::vector<std::string> get_paths(const std::string& name, bool resolve, size_t* lineno = nullptr) {
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800328 std::string paths_str = get_string(name, lineno);
329
330 std::vector<std::string> paths;
331 split_path(paths_str.c_str(), ":", &paths);
332
333 std::vector<std::pair<std::string, std::string>> params;
334 params.push_back({ "LIB", kLibParamValue });
335 if (target_sdk_version_ != 0) {
336 char buf[16];
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700337 async_safe_format_buffer(buf, sizeof(buf), "%d", target_sdk_version_);
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800338 params.push_back({ "SDK_VER", buf });
339 }
340
Justin Yun53ce7422017-11-27 16:28:07 +0900341 static std::string vndk = Config::get_vndk_version_string('-');
Sundong Ahn8fc50322017-10-10 14:12:40 +0900342 params.push_back({ "VNDK_VER", vndk });
343
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800344 for (auto&& path : paths) {
345 format_string(&path, params);
346 }
347
Jiyong Park0f33f232017-09-21 10:27:36 +0900348 if (resolve) {
349 std::vector<std::string> resolved_paths;
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800350
Jiyong Park0f33f232017-09-21 10:27:36 +0900351 // do not remove paths that do not exist
352 resolve_paths(paths, &resolved_paths);
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800353
Jiyong Park0f33f232017-09-21 10:27:36 +0900354 return resolved_paths;
355 } else {
356 return paths;
357 }
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800358 }
359
360 void set_target_sdk_version(int target_sdk_version) {
361 target_sdk_version_ = target_sdk_version;
362 }
363
364 private:
365 std::unordered_map<std::string, PropertyValue>::const_iterator
366 find_property(const std::string& name, size_t* lineno) const {
367 auto it = properties_.find(name);
368 if (it != properties_.end() && lineno != nullptr) {
369 *lineno = it->second.lineno();
370 }
371
372 return it;
373 }
374 std::unordered_map<std::string, PropertyValue> properties_;
375 int target_sdk_version_;
376
377 DISALLOW_IMPLICIT_CONSTRUCTORS(Properties);
378};
379
380bool Config::read_binary_config(const char* ld_config_file_path,
381 const char* binary_realpath,
382 bool is_asan,
383 const Config** config,
384 std::string* error_msg) {
385 g_config.clear();
386
387 std::unordered_map<std::string, PropertyValue> property_map;
388 if (!parse_config_file(ld_config_file_path, binary_realpath, &property_map, error_msg)) {
389 return false;
390 }
391
392 Properties properties(std::move(property_map));
393
Tom Cherryb8ab6182017-04-05 16:20:29 -0700394 auto failure_guard = android::base::make_scope_guard([] { g_config.clear(); });
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800395
396 std::unordered_map<std::string, NamespaceConfig*> namespace_configs;
397
398 namespace_configs[kDefaultConfigName] = g_config.create_namespace_config(kDefaultConfigName);
399
400 std::vector<std::string> additional_namespaces = properties.get_strings(kPropertyAdditionalNamespaces);
401 for (const auto& name : additional_namespaces) {
402 namespace_configs[name] = g_config.create_namespace_config(name);
403 }
404
405 bool versioning_enabled = properties.get_bool("enable.target.sdk.version");
406 int target_sdk_version = __ANDROID_API__;
407 if (versioning_enabled) {
408 std::string version_file = dirname(binary_realpath) + "/.version";
409 std::string content;
410 if (!android::base::ReadFileToString(version_file, &content)) {
411 if (errno != ENOENT) {
412 *error_msg = std::string("error reading version file \"") +
413 version_file + "\": " + strerror(errno);
414 return false;
415 }
416 } else {
417 content = android::base::Trim(content);
418 errno = 0;
419 char* end = nullptr;
420 const char* content_str = content.c_str();
421 int result = strtol(content_str, &end, 10);
422 if (errno == 0 && *end == '\0' && result > 0) {
423 target_sdk_version = result;
424 properties.set_target_sdk_version(target_sdk_version);
425 } else {
426 *error_msg = std::string("invalid version \"") + version_file + "\": \"" + content +"\"";
427 return false;
428 }
429 }
430 }
431
432 g_config.set_target_sdk_version(target_sdk_version);
433
434 for (auto ns_config_it : namespace_configs) {
435 auto& name = ns_config_it.first;
436 NamespaceConfig* ns_config = ns_config_it.second;
437
438 std::string property_name_prefix = std::string("namespace.") + name;
439
440 size_t lineno = 0;
441 std::vector<std::string> linked_namespaces =
442 properties.get_strings(property_name_prefix + ".links", &lineno);
443
444 for (const auto& linked_ns_name : linked_namespaces) {
445 if (namespace_configs.find(linked_ns_name) == namespace_configs.end()) {
446 *error_msg = create_error_msg(ld_config_file_path,
447 lineno,
448 std::string("undefined namespace: ") + linked_ns_name);
449 return false;
450 }
451
452 std::string shared_libs = properties.get_string(property_name_prefix +
453 ".link." +
454 linked_ns_name +
455 ".shared_libs", &lineno);
456
457 if (shared_libs.empty()) {
458 *error_msg = create_error_msg(ld_config_file_path,
459 lineno,
460 std::string("list of shared_libs for ") +
461 name +
462 "->" +
463 linked_ns_name +
464 " link is not specified or is empty.");
465 return false;
466 }
467
468 ns_config->add_namespace_link(linked_ns_name, shared_libs);
469 }
470
471 ns_config->set_isolated(properties.get_bool(property_name_prefix + ".isolated"));
Jiyong Park01de74e2017-04-03 23:10:37 +0900472 ns_config->set_visible(properties.get_bool(property_name_prefix + ".visible"));
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800473
474 // these are affected by is_asan flag
475 if (is_asan) {
476 property_name_prefix += ".asan";
477 }
478
Jiyong Park0f33f232017-09-21 10:27:36 +0900479 // search paths are resolved (canonicalized). This is required mainly for
480 // the case when /vendor is a symlink to /system/vendor, which is true for
481 // non Treble-ized legacy devices.
482 ns_config->set_search_paths(properties.get_paths(property_name_prefix + ".search.paths", true));
483
484 // However, for permitted paths, we are not required to resolve the paths
485 // since they are only set for isolated namespaces, which implies the device
486 // is Treble-ized (= /vendor is not a symlink to /system/vendor).
487 // In fact, the resolving is causing an unexpected side effect of selinux
488 // denials on some executables which are not allowed to access some of the
489 // permitted paths.
490 ns_config->set_permitted_paths(properties.get_paths(property_name_prefix + ".permitted.paths", false));
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800491 }
492
Tom Cherryb8ab6182017-04-05 16:20:29 -0700493 failure_guard.Disable();
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800494 *config = &g_config;
495 return true;
496}
497
Justin Yun53ce7422017-11-27 16:28:07 +0900498std::string Config::get_vndk_version_string(const char delimiter) {
499 std::string version = android::base::GetProperty("ro.vndk.version", "");
500 if (version != "" && version != "current") {
501 //add the delimiter char in front of the string and return it.
502 return version.insert(0, 1, delimiter);
503 }
504 return "";
505}
506
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800507NamespaceConfig* Config::create_namespace_config(const std::string& name) {
508 namespace_configs_.push_back(std::unique_ptr<NamespaceConfig>(new NamespaceConfig(name)));
509 NamespaceConfig* ns_config_ptr = namespace_configs_.back().get();
510 namespace_configs_map_[name] = ns_config_ptr;
511 return ns_config_ptr;
512}
513
514void Config::clear() {
515 namespace_configs_.clear();
516 namespace_configs_map_.clear();
517}