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" |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 43 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 44 | #include <string> |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 45 | #include <vector> |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 46 | |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 47 | #define DL_ERR(fmt, x...) \ |
| 48 | do { \ |
| 49 | __libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \ |
| 50 | /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \ |
| 51 | DEBUG("%s\n", linker_get_error_buffer()); \ |
Elliott Hughes | 7e5a8cc | 2013-06-18 13:15:00 -0700 | [diff] [blame] | 52 | } while (false) |
| 53 | |
| 54 | #define DL_WARN(fmt, x...) \ |
| 55 | do { \ |
| 56 | __libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \ |
| 57 | __libc_format_fd(2, "WARNING: linker: "); \ |
| 58 | __libc_format_fd(2, fmt, ##x); \ |
| 59 | __libc_format_fd(2, "\n"); \ |
| 60 | } while (false) |
| 61 | |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 62 | #if defined(__LP64__) |
| 63 | #define ELFW(what) ELF64_ ## what |
| 64 | #else |
| 65 | #define ELFW(what) ELF32_ ## what |
| 66 | #endif |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 67 | |
Chris Dearman | 9918665 | 2014-02-06 20:36:51 -0800 | [diff] [blame] | 68 | // mips64 interprets Elf64_Rel structures' r_info field differently. |
| 69 | // bionic (like other C libraries) has macros that assume regular ELF files, |
| 70 | // but the dynamic linker needs to be able to load mips64 ELF files. |
| 71 | #if defined(__mips__) && defined(__LP64__) |
| 72 | #undef ELF64_R_SYM |
| 73 | #undef ELF64_R_TYPE |
| 74 | #undef ELF64_R_INFO |
| 75 | #define ELF64_R_SYM(info) (((info) >> 0) & 0xffffffff) |
| 76 | #define ELF64_R_SSYM(info) (((info) >> 32) & 0xff) |
| 77 | #define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff) |
| 78 | #define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff) |
| 79 | #define ELF64_R_TYPE(info) (((info) >> 56) & 0xff) |
| 80 | #endif |
| 81 | |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 82 | #define FLAG_LINKED 0x00000001 |
| 83 | #define FLAG_EXE 0x00000004 // The main executable |
| 84 | #define FLAG_LINKER 0x00000010 // The linker itself |
| 85 | #define FLAG_GNU_HASH 0x00000040 // uses gnu hash |
| 86 | #define FLAG_MAPPED_BY_CALLER 0x00000080 // the map is reserved by the caller |
| 87 | // and should not be unmapped |
| 88 | #define FLAG_NEW_SOINFO 0x40000000 // new soinfo format |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 89 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 90 | #define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE) |
| 91 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 92 | #define SOINFO_VERSION 3 |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 93 | |
Dmitriy Ivanov | 280d546 | 2015-09-28 10:14:17 -0700 | [diff] [blame] | 94 | #if defined(__work_around_b_24465209__) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 95 | #define SOINFO_NAME_LEN 128 |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 96 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 97 | |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 98 | typedef void (*linker_function_t)(); |
| 99 | |
Chris Dearman | 9918665 | 2014-02-06 20:36:51 -0800 | [diff] [blame] | 100 | // Android uses RELA for aarch64 and x86_64. mips64 still uses REL. |
| 101 | #if defined(__aarch64__) || defined(__x86_64__) |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 102 | #define USE_RELA 1 |
| 103 | #endif |
| 104 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 105 | struct soinfo; |
| 106 | |
| 107 | class SoinfoListAllocator { |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 108 | public: |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 109 | static LinkedListEntry<soinfo>* alloc(); |
| 110 | static void free(LinkedListEntry<soinfo>* entry); |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 111 | |
| 112 | private: |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 113 | // unconstructable |
| 114 | DISALLOW_IMPLICIT_CONSTRUCTORS(SoinfoListAllocator); |
| 115 | }; |
| 116 | |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 117 | class NamespaceListAllocator { |
| 118 | public: |
| 119 | static LinkedListEntry<android_namespace_t>* alloc(); |
| 120 | static void free(LinkedListEntry<android_namespace_t>* entry); |
| 121 | |
| 122 | private: |
| 123 | // unconstructable |
| 124 | DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceListAllocator); |
| 125 | }; |
| 126 | |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 127 | class SymbolName { |
| 128 | public: |
| 129 | explicit SymbolName(const char* name) |
| 130 | : name_(name), has_elf_hash_(false), has_gnu_hash_(false), |
| 131 | elf_hash_(0), gnu_hash_(0) { } |
| 132 | |
| 133 | const char* get_name() { |
| 134 | return name_; |
| 135 | } |
| 136 | |
| 137 | uint32_t elf_hash(); |
| 138 | uint32_t gnu_hash(); |
| 139 | |
| 140 | private: |
| 141 | const char* name_; |
| 142 | bool has_elf_hash_; |
| 143 | bool has_gnu_hash_; |
| 144 | uint32_t elf_hash_; |
| 145 | uint32_t gnu_hash_; |
| 146 | |
| 147 | DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolName); |
| 148 | }; |
| 149 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 150 | struct version_info { |
Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 151 | constexpr version_info() : elf_hash(0), name(nullptr), target_si(nullptr) {} |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 152 | |
| 153 | uint32_t elf_hash; |
| 154 | const char* name; |
| 155 | const soinfo* target_si; |
| 156 | }; |
| 157 | |
| 158 | // Class used construct version dependency graph. |
| 159 | class VersionTracker { |
| 160 | public: |
| 161 | VersionTracker() = default; |
| 162 | bool init(const soinfo* si_from); |
| 163 | |
| 164 | const version_info* get_version_info(ElfW(Versym) source_symver) const; |
| 165 | private: |
| 166 | bool init_verneed(const soinfo* si_from); |
| 167 | bool init_verdef(const soinfo* si_from); |
| 168 | void add_version_info(size_t source_index, ElfW(Word) elf_hash, |
| 169 | const char* ver_name, const soinfo* target_si); |
| 170 | |
| 171 | std::vector<version_info> version_infos; |
| 172 | |
| 173 | DISALLOW_COPY_AND_ASSIGN(VersionTracker); |
| 174 | }; |
| 175 | |
Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 176 | struct soinfo { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 177 | public: |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 178 | typedef LinkedList<soinfo, SoinfoListAllocator> soinfo_list_t; |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 179 | typedef LinkedList<android_namespace_t, NamespaceListAllocator> android_namespace_list_t; |
Dmitriy Ivanov | 280d546 | 2015-09-28 10:14:17 -0700 | [diff] [blame] | 180 | #if defined(__work_around_b_24465209__) |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 181 | private: |
| 182 | char old_name_[SOINFO_NAME_LEN]; |
| 183 | #endif |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 184 | public: |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 185 | const ElfW(Phdr)* phdr; |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 186 | size_t phnum; |
Dimitry Ivanov | e687d06 | 2016-02-16 13:25:29 -0800 | [diff] [blame] | 187 | #if defined(__work_around_b_24465209__) |
| 188 | ElfW(Addr) unused0; // DO NOT USE, maintained for compatibility. |
| 189 | #endif |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 190 | ElfW(Addr) base; |
Weiwu Chen | 5ceb889 | 2013-12-03 19:47:34 +0800 | [diff] [blame] | 191 | size_t size; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 192 | |
Dmitriy Ivanov | 280d546 | 2015-09-28 10:14:17 -0700 | [diff] [blame] | 193 | #if defined(__work_around_b_24465209__) |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 194 | uint32_t unused1; // DO NOT USE, maintained for compatibility. |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 195 | #endif |
Nick Kralevich | 38bccb2 | 2011-08-29 13:49:22 -0700 | [diff] [blame] | 196 | |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 197 | ElfW(Dyn)* dynamic; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 198 | |
Dmitriy Ivanov | 280d546 | 2015-09-28 10:14:17 -0700 | [diff] [blame] | 199 | #if defined(__work_around_b_24465209__) |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 200 | uint32_t unused2; // DO NOT USE, maintained for compatibility |
| 201 | uint32_t unused3; // DO NOT USE, maintained for compatibility |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 202 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 203 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 204 | soinfo* next; |
Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 205 | private: |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 206 | uint32_t flags_; |
| 207 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 208 | const char* strtab_; |
| 209 | ElfW(Sym)* symtab_; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 210 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 211 | size_t nbucket_; |
| 212 | size_t nchain_; |
| 213 | uint32_t* bucket_; |
| 214 | uint32_t* chain_; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 215 | |
Chris Dearman | 9918665 | 2014-02-06 20:36:51 -0800 | [diff] [blame] | 216 | #if defined(__mips__) || !defined(__LP64__) |
| 217 | // This is only used by mips and mips64, but needs to be here for |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 218 | // all 32-bit architectures to preserve binary compatibility. |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 219 | ElfW(Addr)** plt_got_; |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 220 | #endif |
| 221 | |
| 222 | #if defined(USE_RELA) |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 223 | ElfW(Rela)* plt_rela_; |
| 224 | size_t plt_rela_count_; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 225 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 226 | ElfW(Rela)* rela_; |
| 227 | size_t rela_count_; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 228 | #else |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 229 | ElfW(Rel)* plt_rel_; |
| 230 | size_t plt_rel_count_; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 231 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 232 | ElfW(Rel)* rel_; |
| 233 | size_t rel_count_; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 234 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 235 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 236 | linker_function_t* preinit_array_; |
| 237 | size_t preinit_array_count_; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 238 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 239 | linker_function_t* init_array_; |
| 240 | size_t init_array_count_; |
| 241 | linker_function_t* fini_array_; |
| 242 | size_t fini_array_count_; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 243 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 244 | linker_function_t init_func_; |
| 245 | linker_function_t fini_func_; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 246 | |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 247 | #if defined(__arm__) |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 248 | public: |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 249 | // ARM EABI section used for stack unwinding. |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 250 | uint32_t* ARM_exidx; |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 251 | size_t ARM_exidx_count; |
Dmitriy Ivanov | 8894091 | 2014-11-12 18:20:39 -0800 | [diff] [blame] | 252 | private: |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 253 | #elif defined(__mips__) |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 254 | uint32_t mips_symtabno_; |
| 255 | uint32_t mips_local_gotno_; |
| 256 | uint32_t mips_gotsym_; |
Dmitriy Ivanov | f39cb63 | 2015-04-30 20:17:03 -0700 | [diff] [blame] | 257 | bool mips_relocate_got(const VersionTracker& version_tracker, |
| 258 | const soinfo_list_t& global_group, |
| 259 | const soinfo_list_t& local_group); |
Duane Sand | bc425c7 | 2015-06-01 16:29:14 -0700 | [diff] [blame] | 260 | #if !defined(__LP64__) |
| 261 | bool mips_check_and_adjust_fp_modes(); |
| 262 | #endif |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 263 | #endif |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 264 | size_t ref_count_; |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 265 | public: |
Elliott Hughes | 3a9c5d6 | 2014-02-10 13:31:13 -0800 | [diff] [blame] | 266 | link_map link_map_head; |
Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 267 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 268 | bool constructors_called; |
Nick Kralevich | 9ec0f03 | 2012-02-28 10:40:00 -0800 | [diff] [blame] | 269 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 270 | // When you read a virtual address from the ELF file, add this |
| 271 | // value to get the corresponding address in the process' address space. |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 272 | ElfW(Addr) load_bias; |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 273 | |
Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 274 | #if !defined(__LP64__) |
| 275 | bool has_text_relocations; |
Elliott Hughes | e4d792a | 2013-10-28 14:19:05 -0700 | [diff] [blame] | 276 | #endif |
Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 277 | bool has_DT_SYMBOLIC; |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 278 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 279 | public: |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 280 | soinfo(android_namespace_t* ns, const char* name, const struct stat* file_stat, |
| 281 | off64_t file_offset, int rtld_flags); |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 282 | ~soinfo(); |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 283 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 284 | void call_constructors(); |
| 285 | void call_destructors(); |
| 286 | void call_pre_init_constructors(); |
| 287 | bool prelink_image(); |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 288 | bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group, |
| 289 | const android_dlextinfo* extinfo); |
Mingwei Shi | be91052 | 2015-11-12 07:02:14 +0000 | [diff] [blame] | 290 | bool protect_relro(); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 291 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 292 | void add_child(soinfo* child); |
| 293 | void remove_all_links(); |
| 294 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 295 | ino_t get_st_ino() const; |
| 296 | dev_t get_st_dev() const; |
| 297 | off64_t get_file_offset() const; |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 298 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 299 | uint32_t get_rtld_flags() const; |
| 300 | uint32_t get_dt_flags_1() const; |
| 301 | void set_dt_flags_1(uint32_t dt_flags_1); |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 302 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 303 | soinfo_list_t& get_children(); |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 304 | const soinfo_list_t& get_children() const; |
| 305 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 306 | soinfo_list_t& get_parents(); |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 307 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 308 | bool find_symbol_by_name(SymbolName& symbol_name, |
| 309 | const version_info* vi, |
| 310 | const ElfW(Sym)** symbol) const; |
| 311 | |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 312 | ElfW(Sym)* find_symbol_by_address(const void* addr); |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 313 | ElfW(Addr) resolve_symbol_address(const ElfW(Sym)* s) const; |
Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 314 | |
Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 315 | const char* get_string(ElfW(Word) index) const; |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 316 | bool can_unload() const; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 317 | bool is_gnu_hash() const; |
Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 318 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 319 | bool inline has_min_version(uint32_t min_version __unused) const { |
Dmitriy Ivanov | 280d546 | 2015-09-28 10:14:17 -0700 | [diff] [blame] | 320 | #if defined(__work_around_b_24465209__) |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 321 | return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version; |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 322 | #else |
| 323 | return true; |
| 324 | #endif |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 325 | } |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 326 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 327 | bool is_linked() const; |
Dimitry Ivanov | e97d8ed | 2016-03-01 15:55:56 -0800 | [diff] [blame] | 328 | bool is_linker() const; |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 329 | bool is_main_executable() const; |
| 330 | |
| 331 | void set_linked(); |
| 332 | void set_linker_flag(); |
| 333 | void set_main_executable(); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 334 | void set_nodelete(); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 335 | |
| 336 | void increment_ref_count(); |
| 337 | size_t decrement_ref_count(); |
| 338 | |
| 339 | soinfo* get_local_group_root() const; |
| 340 | |
Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 341 | void set_soname(const char* soname); |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 342 | const char* get_soname() const; |
| 343 | const char* get_realpath() const; |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 344 | const ElfW(Versym)* get_versym(size_t n) const; |
| 345 | ElfW(Addr) get_verneed_ptr() const; |
| 346 | size_t get_verneed_cnt() const; |
| 347 | ElfW(Addr) get_verdef_ptr() const; |
| 348 | size_t get_verdef_cnt() const; |
| 349 | |
| 350 | bool find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const; |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 351 | |
Dmitriy Ivanov | 1913352 | 2015-06-02 17:36:54 -0700 | [diff] [blame] | 352 | uint32_t get_target_sdk_version() const; |
| 353 | |
Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 354 | void set_dt_runpath(const char *); |
Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 355 | const std::vector<std::string>& get_dt_runpath() const; |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 356 | android_namespace_t* get_primary_namespace(); |
| 357 | void add_secondary_namespace(android_namespace_t* secondary_ns); |
Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 358 | |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 359 | void set_mapped_by_caller(bool reserved_map); |
| 360 | bool is_mapped_by_caller() const; |
| 361 | |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 362 | uintptr_t get_handle() const; |
| 363 | void generate_handle(); |
| 364 | void* to_handle(); |
| 365 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 366 | private: |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 367 | bool elf_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 368 | ElfW(Sym)* elf_addr_lookup(const void* addr); |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 369 | bool gnu_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 370 | ElfW(Sym)* gnu_addr_lookup(const void* addr); |
| 371 | |
Dmitriy Ivanov | 31b408d | 2015-04-30 16:11:48 -0700 | [diff] [blame] | 372 | bool lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym, |
| 373 | const char* sym_name, const version_info** vi); |
| 374 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 375 | void call_array(const char* array_name, linker_function_t* functions, size_t count, bool reverse); |
| 376 | void call_function(const char* function_name, linker_function_t function); |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 377 | template<typename ElfRelIteratorT> |
Dmitriy Ivanov | 7e4bbba | 2015-04-30 19:49:19 -0700 | [diff] [blame] | 378 | bool relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator, |
| 379 | const soinfo_list_t& global_group, const soinfo_list_t& local_group); |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 380 | |
| 381 | private: |
| 382 | // This part of the structure is only available |
| 383 | // when FLAG_NEW_SOINFO is set in this->flags. |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 384 | uint32_t version_; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 385 | |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 386 | // version >= 0 |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 387 | dev_t st_dev_; |
| 388 | ino_t st_ino_; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 389 | |
| 390 | // dependency graph |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 391 | soinfo_list_t children_; |
| 392 | soinfo_list_t parents_; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 393 | |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 394 | // version >= 1 |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 395 | off64_t file_offset_; |
| 396 | uint32_t rtld_flags_; |
| 397 | uint32_t dt_flags_1_; |
| 398 | size_t strtab_size_; |
Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 399 | |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 400 | // version >= 2 |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 401 | |
| 402 | size_t gnu_nbucket_; |
| 403 | uint32_t* gnu_bucket_; |
| 404 | uint32_t* gnu_chain_; |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 405 | uint32_t gnu_maskwords_; |
| 406 | uint32_t gnu_shift2_; |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 407 | ElfW(Addr)* gnu_bloom_filter_; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 408 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 409 | soinfo* local_group_root_; |
| 410 | |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 411 | uint8_t* android_relocs_; |
| 412 | size_t android_relocs_size_; |
| 413 | |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 414 | const char* soname_; |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 415 | std::string realpath_; |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 416 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 417 | const ElfW(Versym)* versym_; |
| 418 | |
| 419 | ElfW(Addr) verdef_ptr_; |
| 420 | size_t verdef_cnt_; |
| 421 | |
| 422 | ElfW(Addr) verneed_ptr_; |
| 423 | size_t verneed_cnt_; |
| 424 | |
Dmitriy Ivanov | 1913352 | 2015-06-02 17:36:54 -0700 | [diff] [blame] | 425 | uint32_t target_sdk_version_; |
| 426 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 427 | // version >= 3 |
Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 428 | std::vector<std::string> dt_runpath_; |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 429 | android_namespace_t* primary_namespace_; |
| 430 | android_namespace_list_t secondary_namespaces_; |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 431 | uintptr_t handle_; |
Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 432 | |
Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 433 | friend soinfo* get_libdl_info(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 434 | }; |
| 435 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 436 | bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi, |
| 437 | soinfo** si_found_in, const soinfo::soinfo_list_t& global_group, |
| 438 | const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol); |
Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 439 | |
| 440 | enum RelocationKind { |
| 441 | kRelocAbsolute = 0, |
| 442 | kRelocRelative, |
| 443 | kRelocCopy, |
| 444 | kRelocSymbol, |
| 445 | kRelocMax |
| 446 | }; |
| 447 | |
| 448 | void count_relocation(RelocationKind kind); |
| 449 | |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 450 | soinfo* get_libdl_info(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 451 | |
Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 452 | void do_android_get_LD_LIBRARY_PATH(char*, size_t); |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 453 | void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path); |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 454 | void* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo, void* caller_addr); |
| 455 | int do_dlclose(void* handle); |
David 'Digit' Turner | 1608416 | 2012-06-12 16:25:37 +0200 | [diff] [blame] | 456 | |
Dmitriy Ivanov | 7271caf | 2015-06-29 14:48:25 -0700 | [diff] [blame] | 457 | int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data); |
| 458 | |
Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 459 | bool do_dlsym(void* handle, const char* sym_name, const char* sym_ver, |
| 460 | void* caller_addr, void** symbol); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 461 | |
Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 462 | int do_dladdr(const void* addr, Dl_info* info); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 463 | |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 464 | char* linker_get_error_buffer(); |
| 465 | size_t linker_get_error_buffer_size(); |
| 466 | |
Dmitriy Ivanov | 79fd668 | 2015-05-21 17:43:49 -0700 | [diff] [blame] | 467 | void set_application_target_sdk_version(uint32_t target); |
| 468 | uint32_t get_application_target_sdk_version(); |
| 469 | |
Dimitry Ivanov | 41fd295 | 2016-05-09 17:37:39 -0700 | [diff] [blame] | 470 | enum { |
| 471 | /* A regular namespace is the namespace with a custom search path that does |
| 472 | * not impose any restrictions on the location of native libraries. |
| 473 | */ |
| 474 | ANDROID_NAMESPACE_TYPE_REGULAR = 0, |
| 475 | |
| 476 | /* An isolated namespace requires all the libraries to be on the search path |
| 477 | * or under permitted_when_isolated_path. The search path is the union of |
| 478 | * ld_library_path and default_library_path. |
| 479 | */ |
| 480 | ANDROID_NAMESPACE_TYPE_ISOLATED = 1, |
| 481 | |
| 482 | /* The shared namespace clones the list of libraries of the caller namespace upon creation |
| 483 | * which means that they are shared between namespaces - the caller namespace and the new one |
| 484 | * will use the same copy of a library if it was loaded prior to android_create_namespace call. |
| 485 | * |
| 486 | * Note that libraries loaded after the namespace is created will not be shared. |
| 487 | * |
| 488 | * Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor |
| 489 | * permitted_path from the caller's namespace. |
| 490 | */ |
| 491 | ANDROID_NAMESPACE_TYPE_SHARED = 2, |
| 492 | ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED | |
| 493 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 494 | }; |
| 495 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 496 | 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] | 497 | android_namespace_t* create_namespace(const void* caller_addr, |
| 498 | const char* name, |
| 499 | const char* ld_library_path, |
| 500 | const char* default_library_path, |
| 501 | uint64_t type, |
| 502 | const char* permitted_when_isolated_path, |
| 503 | android_namespace_t* parent_namespace); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 504 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 505 | #endif |