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