Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame^] | 1 | /* |
| 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 | #ifndef _LINKER_CONFIG_H_ |
| 30 | #define _LINKER_CONFIG_H_ |
| 31 | |
| 32 | #include <android/api-level.h> |
| 33 | |
| 34 | #include <stdlib.h> |
| 35 | #include <limits.h> |
| 36 | #include "private/bionic_macros.h" |
| 37 | |
| 38 | #include <memory> |
| 39 | #include <string> |
| 40 | #include <vector> |
| 41 | #include <unordered_map> |
| 42 | |
| 43 | class NamespaceLinkConfig { |
| 44 | public: |
| 45 | NamespaceLinkConfig() = default; |
| 46 | NamespaceLinkConfig(const std::string& ns_name, const std::string& shared_libs) |
| 47 | : ns_name_(ns_name), shared_libs_(shared_libs) {} |
| 48 | |
| 49 | const std::string& ns_name() const { |
| 50 | return ns_name_; |
| 51 | } |
| 52 | |
| 53 | const std::string& shared_libs() const { |
| 54 | return shared_libs_; |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | std::string ns_name_; |
| 59 | std::string shared_libs_; |
| 60 | }; |
| 61 | |
| 62 | class NamespaceConfig { |
| 63 | public: |
| 64 | explicit NamespaceConfig(const std::string& name) |
| 65 | : name_(name), isolated_(false) |
| 66 | {} |
| 67 | |
| 68 | const char* name() const { |
| 69 | return name_.c_str(); |
| 70 | } |
| 71 | |
| 72 | bool isolated() const { |
| 73 | return isolated_; |
| 74 | } |
| 75 | |
| 76 | const std::vector<std::string>& search_paths() const { |
| 77 | return search_paths_; |
| 78 | } |
| 79 | |
| 80 | const std::vector<std::string>& permitted_paths() const { |
| 81 | return permitted_paths_; |
| 82 | } |
| 83 | |
| 84 | const std::vector<NamespaceLinkConfig>& links() const { |
| 85 | return namespace_links_; |
| 86 | } |
| 87 | |
| 88 | void add_namespace_link(const std::string& ns_name, const std::string& shared_libs) { |
| 89 | namespace_links_.push_back(NamespaceLinkConfig(ns_name, shared_libs)); |
| 90 | } |
| 91 | |
| 92 | void set_isolated(bool isolated) { |
| 93 | isolated_ = isolated; |
| 94 | } |
| 95 | |
| 96 | void set_search_paths(std::vector<std::string>&& search_paths) { |
| 97 | search_paths_ = search_paths; |
| 98 | } |
| 99 | |
| 100 | void set_permitted_paths(std::vector<std::string>&& permitted_paths) { |
| 101 | permitted_paths_ = permitted_paths; |
| 102 | } |
| 103 | private: |
| 104 | const std::string name_; |
| 105 | bool isolated_; |
| 106 | std::vector<std::string> search_paths_; |
| 107 | std::vector<std::string> permitted_paths_; |
| 108 | std::vector<NamespaceLinkConfig> namespace_links_; |
| 109 | |
| 110 | DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceConfig); |
| 111 | }; |
| 112 | |
| 113 | class Config { |
| 114 | public: |
| 115 | Config() : target_sdk_version_(__ANDROID_API__) {} |
| 116 | |
| 117 | const std::vector<std::unique_ptr<NamespaceConfig>>& namespace_configs() const { |
| 118 | return namespace_configs_; |
| 119 | } |
| 120 | |
| 121 | const NamespaceConfig* default_namespace_config() const { |
| 122 | auto it = namespace_configs_map_.find("default"); |
| 123 | return it == namespace_configs_map_.end() ? nullptr : it->second; |
| 124 | } |
| 125 | |
| 126 | uint32_t target_sdk_version() const { |
| 127 | return target_sdk_version_; |
| 128 | } |
| 129 | |
| 130 | // note that this is one time event and therefore there is no need to |
| 131 | // read every section of the config. Every linker instance needs at |
| 132 | // most one configuration. |
| 133 | // Returns false in case of an error. If binary config was not found |
| 134 | // sets *config = nullptr. |
| 135 | static bool read_binary_config(const char* ld_config_file_path, |
| 136 | const char* binary_realpath, |
| 137 | bool is_asan, |
| 138 | const Config** config, |
| 139 | std::string* error_msg); |
| 140 | private: |
| 141 | void clear(); |
| 142 | |
| 143 | void set_target_sdk_version(uint32_t target_sdk_version) { |
| 144 | target_sdk_version_ = target_sdk_version; |
| 145 | } |
| 146 | |
| 147 | NamespaceConfig* create_namespace_config(const std::string& name); |
| 148 | |
| 149 | std::vector<std::unique_ptr<NamespaceConfig>> namespace_configs_; |
| 150 | std::unordered_map<std::string, NamespaceConfig*> namespace_configs_map_; |
| 151 | uint32_t target_sdk_version_; |
| 152 | |
| 153 | DISALLOW_COPY_AND_ASSIGN(Config); |
| 154 | }; |
| 155 | |
| 156 | #endif /* _LINKER_CONFIG_H_ */ |