1*8d67ca89SAndroid Build Coastguard Worker /* 2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project 3*8d67ca89SAndroid Build Coastguard Worker * All rights reserved. 4*8d67ca89SAndroid Build Coastguard Worker * 5*8d67ca89SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without 6*8d67ca89SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions 7*8d67ca89SAndroid Build Coastguard Worker * are met: 8*8d67ca89SAndroid Build Coastguard Worker * * Redistributions of source code must retain the above copyright 9*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer. 10*8d67ca89SAndroid Build Coastguard Worker * * Redistributions in binary form must reproduce the above copyright 11*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in 12*8d67ca89SAndroid Build Coastguard Worker * the documentation and/or other materials provided with the 13*8d67ca89SAndroid Build Coastguard Worker * distribution. 14*8d67ca89SAndroid Build Coastguard Worker * 15*8d67ca89SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16*8d67ca89SAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17*8d67ca89SAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18*8d67ca89SAndroid Build Coastguard Worker * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19*8d67ca89SAndroid Build Coastguard Worker * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20*8d67ca89SAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21*8d67ca89SAndroid Build Coastguard Worker * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22*8d67ca89SAndroid Build Coastguard Worker * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23*8d67ca89SAndroid Build Coastguard Worker * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24*8d67ca89SAndroid Build Coastguard Worker * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25*8d67ca89SAndroid Build Coastguard Worker * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*8d67ca89SAndroid Build Coastguard Worker * SUCH DAMAGE. 27*8d67ca89SAndroid Build Coastguard Worker */ 28*8d67ca89SAndroid Build Coastguard Worker 29*8d67ca89SAndroid Build Coastguard Worker #pragma once 30*8d67ca89SAndroid Build Coastguard Worker 31*8d67ca89SAndroid Build Coastguard Worker #include <android/api-level.h> 32*8d67ca89SAndroid Build Coastguard Worker 33*8d67ca89SAndroid Build Coastguard Worker #include <stdlib.h> 34*8d67ca89SAndroid Build Coastguard Worker #include <limits.h> 35*8d67ca89SAndroid Build Coastguard Worker 36*8d67ca89SAndroid Build Coastguard Worker #include <memory> 37*8d67ca89SAndroid Build Coastguard Worker #include <string> 38*8d67ca89SAndroid Build Coastguard Worker #include <vector> 39*8d67ca89SAndroid Build Coastguard Worker #include <unordered_map> 40*8d67ca89SAndroid Build Coastguard Worker 41*8d67ca89SAndroid Build Coastguard Worker #include <android-base/macros.h> 42*8d67ca89SAndroid Build Coastguard Worker 43*8d67ca89SAndroid Build Coastguard Worker #if defined(__LP64__) 44*8d67ca89SAndroid Build Coastguard Worker static constexpr const char* kLibPath = "lib64"; 45*8d67ca89SAndroid Build Coastguard Worker #else 46*8d67ca89SAndroid Build Coastguard Worker static constexpr const char* kLibPath = "lib"; 47*8d67ca89SAndroid Build Coastguard Worker #endif 48*8d67ca89SAndroid Build Coastguard Worker 49*8d67ca89SAndroid Build Coastguard Worker class NamespaceLinkConfig { 50*8d67ca89SAndroid Build Coastguard Worker public: 51*8d67ca89SAndroid Build Coastguard Worker NamespaceLinkConfig() = default; NamespaceLinkConfig(const std::string & ns_name,const std::string & shared_libs,bool allow_all_shared_libs)52*8d67ca89SAndroid Build Coastguard Worker NamespaceLinkConfig(const std::string& ns_name, const std::string& shared_libs, 53*8d67ca89SAndroid Build Coastguard Worker bool allow_all_shared_libs) 54*8d67ca89SAndroid Build Coastguard Worker : ns_name_(ns_name), shared_libs_(shared_libs), 55*8d67ca89SAndroid Build Coastguard Worker allow_all_shared_libs_(allow_all_shared_libs) {} 56*8d67ca89SAndroid Build Coastguard Worker ns_name()57*8d67ca89SAndroid Build Coastguard Worker const std::string& ns_name() const { 58*8d67ca89SAndroid Build Coastguard Worker return ns_name_; 59*8d67ca89SAndroid Build Coastguard Worker } 60*8d67ca89SAndroid Build Coastguard Worker shared_libs()61*8d67ca89SAndroid Build Coastguard Worker const std::string& shared_libs() const { 62*8d67ca89SAndroid Build Coastguard Worker return shared_libs_; 63*8d67ca89SAndroid Build Coastguard Worker } 64*8d67ca89SAndroid Build Coastguard Worker allow_all_shared_libs()65*8d67ca89SAndroid Build Coastguard Worker bool allow_all_shared_libs() const { 66*8d67ca89SAndroid Build Coastguard Worker return allow_all_shared_libs_; 67*8d67ca89SAndroid Build Coastguard Worker } 68*8d67ca89SAndroid Build Coastguard Worker 69*8d67ca89SAndroid Build Coastguard Worker private: 70*8d67ca89SAndroid Build Coastguard Worker std::string ns_name_; 71*8d67ca89SAndroid Build Coastguard Worker std::string shared_libs_; 72*8d67ca89SAndroid Build Coastguard Worker bool allow_all_shared_libs_; 73*8d67ca89SAndroid Build Coastguard Worker }; 74*8d67ca89SAndroid Build Coastguard Worker 75*8d67ca89SAndroid Build Coastguard Worker class NamespaceConfig { 76*8d67ca89SAndroid Build Coastguard Worker public: NamespaceConfig(const std::string & name)77*8d67ca89SAndroid Build Coastguard Worker explicit NamespaceConfig(const std::string& name) 78*8d67ca89SAndroid Build Coastguard Worker : name_(name), isolated_(false), visible_(false) 79*8d67ca89SAndroid Build Coastguard Worker {} 80*8d67ca89SAndroid Build Coastguard Worker name()81*8d67ca89SAndroid Build Coastguard Worker const char* name() const { 82*8d67ca89SAndroid Build Coastguard Worker return name_.c_str(); 83*8d67ca89SAndroid Build Coastguard Worker } 84*8d67ca89SAndroid Build Coastguard Worker isolated()85*8d67ca89SAndroid Build Coastguard Worker bool isolated() const { 86*8d67ca89SAndroid Build Coastguard Worker return isolated_; 87*8d67ca89SAndroid Build Coastguard Worker } 88*8d67ca89SAndroid Build Coastguard Worker visible()89*8d67ca89SAndroid Build Coastguard Worker bool visible() const { 90*8d67ca89SAndroid Build Coastguard Worker return visible_; 91*8d67ca89SAndroid Build Coastguard Worker } 92*8d67ca89SAndroid Build Coastguard Worker search_paths()93*8d67ca89SAndroid Build Coastguard Worker const std::vector<std::string>& search_paths() const { 94*8d67ca89SAndroid Build Coastguard Worker return search_paths_; 95*8d67ca89SAndroid Build Coastguard Worker } 96*8d67ca89SAndroid Build Coastguard Worker permitted_paths()97*8d67ca89SAndroid Build Coastguard Worker const std::vector<std::string>& permitted_paths() const { 98*8d67ca89SAndroid Build Coastguard Worker return permitted_paths_; 99*8d67ca89SAndroid Build Coastguard Worker } 100*8d67ca89SAndroid Build Coastguard Worker allowed_libs()101*8d67ca89SAndroid Build Coastguard Worker const std::vector<std::string>& allowed_libs() const { return allowed_libs_; } 102*8d67ca89SAndroid Build Coastguard Worker links()103*8d67ca89SAndroid Build Coastguard Worker const std::vector<NamespaceLinkConfig>& links() const { 104*8d67ca89SAndroid Build Coastguard Worker return namespace_links_; 105*8d67ca89SAndroid Build Coastguard Worker } 106*8d67ca89SAndroid Build Coastguard Worker add_namespace_link(const std::string & ns_name,const std::string & shared_libs,bool allow_all_shared_libs)107*8d67ca89SAndroid Build Coastguard Worker void add_namespace_link(const std::string& ns_name, const std::string& shared_libs, 108*8d67ca89SAndroid Build Coastguard Worker bool allow_all_shared_libs) { 109*8d67ca89SAndroid Build Coastguard Worker namespace_links_.push_back(NamespaceLinkConfig(ns_name, shared_libs, allow_all_shared_libs)); 110*8d67ca89SAndroid Build Coastguard Worker } 111*8d67ca89SAndroid Build Coastguard Worker set_isolated(bool isolated)112*8d67ca89SAndroid Build Coastguard Worker void set_isolated(bool isolated) { 113*8d67ca89SAndroid Build Coastguard Worker isolated_ = isolated; 114*8d67ca89SAndroid Build Coastguard Worker } 115*8d67ca89SAndroid Build Coastguard Worker set_visible(bool visible)116*8d67ca89SAndroid Build Coastguard Worker void set_visible(bool visible) { 117*8d67ca89SAndroid Build Coastguard Worker visible_ = visible; 118*8d67ca89SAndroid Build Coastguard Worker } 119*8d67ca89SAndroid Build Coastguard Worker set_search_paths(std::vector<std::string> && search_paths)120*8d67ca89SAndroid Build Coastguard Worker void set_search_paths(std::vector<std::string>&& search_paths) { 121*8d67ca89SAndroid Build Coastguard Worker search_paths_ = std::move(search_paths); 122*8d67ca89SAndroid Build Coastguard Worker } 123*8d67ca89SAndroid Build Coastguard Worker set_permitted_paths(std::vector<std::string> && permitted_paths)124*8d67ca89SAndroid Build Coastguard Worker void set_permitted_paths(std::vector<std::string>&& permitted_paths) { 125*8d67ca89SAndroid Build Coastguard Worker permitted_paths_ = std::move(permitted_paths); 126*8d67ca89SAndroid Build Coastguard Worker } 127*8d67ca89SAndroid Build Coastguard Worker set_allowed_libs(std::vector<std::string> && allowed_libs)128*8d67ca89SAndroid Build Coastguard Worker void set_allowed_libs(std::vector<std::string>&& allowed_libs) { 129*8d67ca89SAndroid Build Coastguard Worker allowed_libs_ = std::move(allowed_libs); 130*8d67ca89SAndroid Build Coastguard Worker } 131*8d67ca89SAndroid Build Coastguard Worker 132*8d67ca89SAndroid Build Coastguard Worker private: 133*8d67ca89SAndroid Build Coastguard Worker const std::string name_; 134*8d67ca89SAndroid Build Coastguard Worker bool isolated_; 135*8d67ca89SAndroid Build Coastguard Worker bool visible_; 136*8d67ca89SAndroid Build Coastguard Worker std::vector<std::string> search_paths_; 137*8d67ca89SAndroid Build Coastguard Worker std::vector<std::string> permitted_paths_; 138*8d67ca89SAndroid Build Coastguard Worker std::vector<std::string> allowed_libs_; 139*8d67ca89SAndroid Build Coastguard Worker std::vector<NamespaceLinkConfig> namespace_links_; 140*8d67ca89SAndroid Build Coastguard Worker 141*8d67ca89SAndroid Build Coastguard Worker DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceConfig); 142*8d67ca89SAndroid Build Coastguard Worker }; 143*8d67ca89SAndroid Build Coastguard Worker 144*8d67ca89SAndroid Build Coastguard Worker class Config { 145*8d67ca89SAndroid Build Coastguard Worker public: Config()146*8d67ca89SAndroid Build Coastguard Worker Config() : target_sdk_version_(__ANDROID_API__) {} 147*8d67ca89SAndroid Build Coastguard Worker namespace_configs()148*8d67ca89SAndroid Build Coastguard Worker const std::vector<std::unique_ptr<NamespaceConfig>>& namespace_configs() const { 149*8d67ca89SAndroid Build Coastguard Worker return namespace_configs_; 150*8d67ca89SAndroid Build Coastguard Worker } 151*8d67ca89SAndroid Build Coastguard Worker default_namespace_config()152*8d67ca89SAndroid Build Coastguard Worker const NamespaceConfig* default_namespace_config() const { 153*8d67ca89SAndroid Build Coastguard Worker auto it = namespace_configs_map_.find("default"); 154*8d67ca89SAndroid Build Coastguard Worker return it == namespace_configs_map_.end() ? nullptr : it->second; 155*8d67ca89SAndroid Build Coastguard Worker } 156*8d67ca89SAndroid Build Coastguard Worker target_sdk_version()157*8d67ca89SAndroid Build Coastguard Worker int target_sdk_version() const { 158*8d67ca89SAndroid Build Coastguard Worker return target_sdk_version_; 159*8d67ca89SAndroid Build Coastguard Worker } 160*8d67ca89SAndroid Build Coastguard Worker 161*8d67ca89SAndroid Build Coastguard Worker // note that this is one time event and therefore there is no need to 162*8d67ca89SAndroid Build Coastguard Worker // read every section of the config. Every linker instance needs at 163*8d67ca89SAndroid Build Coastguard Worker // most one configuration. 164*8d67ca89SAndroid Build Coastguard Worker // Returns false in case of an error. If binary config was not found 165*8d67ca89SAndroid Build Coastguard Worker // sets *config = nullptr. 166*8d67ca89SAndroid Build Coastguard Worker static bool read_binary_config(const char* ld_config_file_path, 167*8d67ca89SAndroid Build Coastguard Worker const char* binary_realpath, 168*8d67ca89SAndroid Build Coastguard Worker bool is_asan, 169*8d67ca89SAndroid Build Coastguard Worker bool is_hwasan, 170*8d67ca89SAndroid Build Coastguard Worker const Config** config, 171*8d67ca89SAndroid Build Coastguard Worker std::string* error_msg); 172*8d67ca89SAndroid Build Coastguard Worker 173*8d67ca89SAndroid Build Coastguard Worker static std::string get_vndk_version_string(const char delimiter); 174*8d67ca89SAndroid Build Coastguard Worker private: 175*8d67ca89SAndroid Build Coastguard Worker void clear(); 176*8d67ca89SAndroid Build Coastguard Worker set_target_sdk_version(int target_sdk_version)177*8d67ca89SAndroid Build Coastguard Worker void set_target_sdk_version(int target_sdk_version) { 178*8d67ca89SAndroid Build Coastguard Worker target_sdk_version_ = target_sdk_version; 179*8d67ca89SAndroid Build Coastguard Worker } 180*8d67ca89SAndroid Build Coastguard Worker 181*8d67ca89SAndroid Build Coastguard Worker NamespaceConfig* create_namespace_config(const std::string& name); 182*8d67ca89SAndroid Build Coastguard Worker 183*8d67ca89SAndroid Build Coastguard Worker std::vector<std::unique_ptr<NamespaceConfig>> namespace_configs_; 184*8d67ca89SAndroid Build Coastguard Worker std::unordered_map<std::string, NamespaceConfig*> namespace_configs_map_; 185*8d67ca89SAndroid Build Coastguard Worker int target_sdk_version_; 186*8d67ca89SAndroid Build Coastguard Worker 187*8d67ca89SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(Config); 188*8d67ca89SAndroid Build Coastguard Worker }; 189