The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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_H_ |
| 30 | #define _LINKER_H_ |
| 31 | |
Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 32 | #include <dlfcn.h> |
Elliott Hughes | afab3ff | 2015-07-28 14:58:37 -0700 | [diff] [blame] | 33 | #include <android/dlext.h> |
Nick Kralevich | 9ec0f03 | 2012-02-28 10:40:00 -0800 | [diff] [blame] | 34 | #include <elf.h> |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 35 | #include <inttypes.h> |
Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 36 | #include <link.h> |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 37 | #include <sys/stat.h> |
Elliott Hughes | afab3ff | 2015-07-28 14:58:37 -0700 | [diff] [blame] | 38 | #include <unistd.h> |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | afab3ff | 2015-07-28 14:58:37 -0700 | [diff] [blame] | 40 | #include "private/bionic_page.h" |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 41 | #include "private/libc_logging.h" |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 42 | #include "linked_list.h" |
Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 43 | #include "linker_common_types.h" |
Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 44 | #include "linker_logger.h" |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 45 | #include "linker_soinfo.h" |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 46 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 47 | #include <string> |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 48 | #include <vector> |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 50 | #if defined(__LP64__) |
| 51 | #define ELFW(what) ELF64_ ## what |
| 52 | #else |
| 53 | #define ELFW(what) ELF32_ ## what |
| 54 | #endif |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 55 | |
Chris Dearman | 9918665 | 2014-02-06 20:36:51 -0800 | [diff] [blame] | 56 | // mips64 interprets Elf64_Rel structures' r_info field differently. |
| 57 | // bionic (like other C libraries) has macros that assume regular ELF files, |
| 58 | // but the dynamic linker needs to be able to load mips64 ELF files. |
| 59 | #if defined(__mips__) && defined(__LP64__) |
| 60 | #undef ELF64_R_SYM |
| 61 | #undef ELF64_R_TYPE |
| 62 | #undef ELF64_R_INFO |
| 63 | #define ELF64_R_SYM(info) (((info) >> 0) & 0xffffffff) |
| 64 | #define ELF64_R_SSYM(info) (((info) >> 32) & 0xff) |
| 65 | #define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff) |
| 66 | #define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff) |
| 67 | #define ELF64_R_TYPE(info) (((info) >> 56) & 0xff) |
| 68 | #endif |
| 69 | |
Bernhard Rosenkränzer | 59ba65e | 2016-08-19 21:57:12 +0200 | [diff] [blame] | 70 | #define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE | DF_1_PIE) |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 71 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 72 | // Class used construct version dependency graph. |
| 73 | class VersionTracker { |
| 74 | public: |
| 75 | VersionTracker() = default; |
| 76 | bool init(const soinfo* si_from); |
| 77 | |
| 78 | const version_info* get_version_info(ElfW(Versym) source_symver) const; |
| 79 | private: |
| 80 | bool init_verneed(const soinfo* si_from); |
| 81 | bool init_verdef(const soinfo* si_from); |
| 82 | void add_version_info(size_t source_index, ElfW(Word) elf_hash, |
| 83 | const char* ver_name, const soinfo* target_si); |
| 84 | |
| 85 | std::vector<version_info> version_infos; |
| 86 | |
| 87 | DISALLOW_COPY_AND_ASSIGN(VersionTracker); |
| 88 | }; |
| 89 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 90 | bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi, |
Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 91 | soinfo** si_found_in, const soinfo_list_t& global_group, |
| 92 | const soinfo_list_t& local_group, const ElfW(Sym)** symbol); |
Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 93 | |
| 94 | enum RelocationKind { |
| 95 | kRelocAbsolute = 0, |
| 96 | kRelocRelative, |
| 97 | kRelocCopy, |
| 98 | kRelocSymbol, |
| 99 | kRelocMax |
| 100 | }; |
| 101 | |
| 102 | void count_relocation(RelocationKind kind); |
| 103 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 104 | soinfo* get_libdl_info(const char* linker_path); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 105 | |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame^] | 106 | soinfo* find_containing_library(const void* p); |
| 107 | |
Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 108 | void do_android_get_LD_LIBRARY_PATH(char*, size_t); |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 109 | void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path); |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 110 | void* do_dlopen(const char* name, |
| 111 | int flags, |
| 112 | const android_dlextinfo* extinfo, |
| 113 | const void* caller_addr); |
| 114 | |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 115 | int do_dlclose(void* handle); |
David 'Digit' Turner | 1608416 | 2012-06-12 16:25:37 +0200 | [diff] [blame] | 116 | |
Dmitriy Ivanov | 7271caf | 2015-06-29 14:48:25 -0700 | [diff] [blame] | 117 | int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data); |
| 118 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 119 | #if defined(__arm__) |
| 120 | _Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount); |
| 121 | #endif |
| 122 | |
| 123 | bool do_dlsym(void* handle, const char* sym_name, |
| 124 | const char* sym_ver, |
| 125 | const void* caller_addr, |
| 126 | void** symbol); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 127 | |
Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 128 | int do_dladdr(const void* addr, Dl_info* info); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 129 | |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame^] | 130 | // void ___cfi_slowpath(uint64_t CallSiteTypeId, void *Ptr, void *Ret); |
| 131 | // void ___cfi_slowpath_diag(uint64_t CallSiteTypeId, void *Ptr, void *DiagData, void *Ret); |
| 132 | void ___cfi_fail(uint64_t CallSiteTypeId, void* Ptr, void *DiagData, void *Ret); |
| 133 | |
Dmitriy Ivanov | 79fd668 | 2015-05-21 17:43:49 -0700 | [diff] [blame] | 134 | void set_application_target_sdk_version(uint32_t target); |
| 135 | uint32_t get_application_target_sdk_version(); |
| 136 | |
Dimitry Ivanov | 41fd295 | 2016-05-09 17:37:39 -0700 | [diff] [blame] | 137 | enum { |
| 138 | /* A regular namespace is the namespace with a custom search path that does |
| 139 | * not impose any restrictions on the location of native libraries. |
| 140 | */ |
| 141 | ANDROID_NAMESPACE_TYPE_REGULAR = 0, |
| 142 | |
| 143 | /* An isolated namespace requires all the libraries to be on the search path |
| 144 | * or under permitted_when_isolated_path. The search path is the union of |
| 145 | * ld_library_path and default_library_path. |
| 146 | */ |
| 147 | ANDROID_NAMESPACE_TYPE_ISOLATED = 1, |
| 148 | |
| 149 | /* The shared namespace clones the list of libraries of the caller namespace upon creation |
| 150 | * which means that they are shared between namespaces - the caller namespace and the new one |
| 151 | * will use the same copy of a library if it was loaded prior to android_create_namespace call. |
| 152 | * |
| 153 | * Note that libraries loaded after the namespace is created will not be shared. |
| 154 | * |
| 155 | * Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor |
| 156 | * permitted_path from the caller's namespace. |
| 157 | */ |
| 158 | ANDROID_NAMESPACE_TYPE_SHARED = 2, |
| 159 | ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED | |
| 160 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 161 | }; |
| 162 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 163 | bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path); |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 164 | android_namespace_t* create_namespace(const void* caller_addr, |
| 165 | const char* name, |
| 166 | const char* ld_library_path, |
| 167 | const char* default_library_path, |
| 168 | uint64_t type, |
| 169 | const char* permitted_when_isolated_path, |
| 170 | android_namespace_t* parent_namespace); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 171 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 172 | #endif |