Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 29 | #pragma once |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 30 | |
| 31 | #include <link.h> |
| 32 | |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 33 | #include <memory> |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 34 | #include <string> |
Ryan Prichard | ffaae70 | 2019-01-23 17:47:10 -0800 | [diff] [blame] | 35 | #include <vector> |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 36 | |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 37 | #include "private/bionic_elf_tls.h" |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 38 | #include "linker_namespaces.h" |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 39 | #include "linker_tls.h" |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 40 | |
| 41 | #define FLAG_LINKED 0x00000001 |
| 42 | #define FLAG_EXE 0x00000004 // The main executable |
| 43 | #define FLAG_LINKER 0x00000010 // The linker itself |
| 44 | #define FLAG_GNU_HASH 0x00000040 // uses gnu hash |
| 45 | #define FLAG_MAPPED_BY_CALLER 0x00000080 // the map is reserved by the caller |
| 46 | // and should not be unmapped |
dimitry | 965d06d | 2017-11-28 16:03:07 +0100 | [diff] [blame] | 47 | #define FLAG_IMAGE_LINKED 0x00000100 // Is image linked - this is a guard on link_image. |
| 48 | // The difference between this flag and |
| 49 | // FLAG_LINKED is that FLAG_LINKED |
| 50 | // means is set when load_group is |
| 51 | // successfully loaded whereas this |
| 52 | // flag is set to avoid linking image |
| 53 | // when link_image called for the |
| 54 | // second time. This situation happens |
| 55 | // when load group is crossing |
| 56 | // namespace boundary twice and second |
| 57 | // local group depends on the same libraries. |
dimitry | 55547db | 2018-05-25 14:17:37 +0200 | [diff] [blame] | 58 | #define FLAG_RESERVED 0x00000200 // This flag was set when there is at least one |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 59 | // outstanding thread_local dtor |
| 60 | // registered with this soinfo. In such |
| 61 | // a case the actual unload is |
| 62 | // postponed until the last thread_local |
| 63 | // destructor associated with this |
| 64 | // soinfo is executed and this flag is |
| 65 | // unset. |
Ryan Prichard | ae320cd | 2019-12-10 12:39:05 -0800 | [diff] [blame^] | 66 | #define FLAG_PRELINKED 0x00000400 // prelink_image has successfully processed this soinfo |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 67 | #define FLAG_NEW_SOINFO 0x40000000 // new soinfo format |
| 68 | |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 69 | #define SOINFO_VERSION 5 |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 70 | |
| 71 | typedef void (*linker_dtor_function_t)(); |
| 72 | typedef void (*linker_ctor_function_t)(int, char**, char**); |
| 73 | |
| 74 | class SymbolName { |
| 75 | public: |
| 76 | explicit SymbolName(const char* name) |
| 77 | : name_(name), has_elf_hash_(false), has_gnu_hash_(false), |
| 78 | elf_hash_(0), gnu_hash_(0) { } |
| 79 | |
| 80 | const char* get_name() { |
| 81 | return name_; |
| 82 | } |
| 83 | |
| 84 | uint32_t elf_hash(); |
| 85 | uint32_t gnu_hash(); |
| 86 | |
| 87 | private: |
| 88 | const char* name_; |
| 89 | bool has_elf_hash_; |
| 90 | bool has_gnu_hash_; |
| 91 | uint32_t elf_hash_; |
| 92 | uint32_t gnu_hash_; |
| 93 | |
| 94 | DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolName); |
| 95 | }; |
| 96 | |
| 97 | struct version_info { |
| 98 | constexpr version_info() : elf_hash(0), name(nullptr), target_si(nullptr) {} |
| 99 | |
| 100 | uint32_t elf_hash; |
| 101 | const char* name; |
| 102 | const soinfo* target_si; |
| 103 | }; |
| 104 | |
| 105 | // TODO(dimitry): remove reference from soinfo member functions to this class. |
| 106 | class VersionTracker; |
| 107 | |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 108 | struct soinfo_tls { |
| 109 | TlsSegment segment; |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 110 | size_t module_id = kTlsUninitializedModuleId; |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 113 | #if defined(__work_around_b_24465209__) |
| 114 | #define SOINFO_NAME_LEN 128 |
| 115 | #endif |
| 116 | |
| 117 | struct soinfo { |
| 118 | #if defined(__work_around_b_24465209__) |
| 119 | private: |
| 120 | char old_name_[SOINFO_NAME_LEN]; |
| 121 | #endif |
| 122 | public: |
| 123 | const ElfW(Phdr)* phdr; |
| 124 | size_t phnum; |
| 125 | #if defined(__work_around_b_24465209__) |
| 126 | ElfW(Addr) unused0; // DO NOT USE, maintained for compatibility. |
| 127 | #endif |
| 128 | ElfW(Addr) base; |
| 129 | size_t size; |
| 130 | |
| 131 | #if defined(__work_around_b_24465209__) |
| 132 | uint32_t unused1; // DO NOT USE, maintained for compatibility. |
| 133 | #endif |
| 134 | |
| 135 | ElfW(Dyn)* dynamic; |
| 136 | |
| 137 | #if defined(__work_around_b_24465209__) |
| 138 | uint32_t unused2; // DO NOT USE, maintained for compatibility |
| 139 | uint32_t unused3; // DO NOT USE, maintained for compatibility |
| 140 | #endif |
| 141 | |
| 142 | soinfo* next; |
| 143 | private: |
| 144 | uint32_t flags_; |
| 145 | |
| 146 | const char* strtab_; |
| 147 | ElfW(Sym)* symtab_; |
| 148 | |
| 149 | size_t nbucket_; |
| 150 | size_t nchain_; |
| 151 | uint32_t* bucket_; |
| 152 | uint32_t* chain_; |
| 153 | |
| 154 | #if defined(__mips__) || !defined(__LP64__) |
| 155 | // This is only used by mips and mips64, but needs to be here for |
| 156 | // all 32-bit architectures to preserve binary compatibility. |
| 157 | ElfW(Addr)** plt_got_; |
| 158 | #endif |
| 159 | |
| 160 | #if defined(USE_RELA) |
| 161 | ElfW(Rela)* plt_rela_; |
| 162 | size_t plt_rela_count_; |
| 163 | |
| 164 | ElfW(Rela)* rela_; |
| 165 | size_t rela_count_; |
| 166 | #else |
| 167 | ElfW(Rel)* plt_rel_; |
| 168 | size_t plt_rel_count_; |
| 169 | |
| 170 | ElfW(Rel)* rel_; |
| 171 | size_t rel_count_; |
| 172 | #endif |
| 173 | |
| 174 | linker_ctor_function_t* preinit_array_; |
| 175 | size_t preinit_array_count_; |
| 176 | |
| 177 | linker_ctor_function_t* init_array_; |
| 178 | size_t init_array_count_; |
| 179 | linker_dtor_function_t* fini_array_; |
| 180 | size_t fini_array_count_; |
| 181 | |
| 182 | linker_ctor_function_t init_func_; |
| 183 | linker_dtor_function_t fini_func_; |
| 184 | |
| 185 | #if defined(__arm__) |
| 186 | public: |
| 187 | // ARM EABI section used for stack unwinding. |
| 188 | uint32_t* ARM_exidx; |
| 189 | size_t ARM_exidx_count; |
| 190 | private: |
| 191 | #elif defined(__mips__) |
| 192 | uint32_t mips_symtabno_; |
| 193 | uint32_t mips_local_gotno_; |
| 194 | uint32_t mips_gotsym_; |
| 195 | bool mips_relocate_got(const VersionTracker& version_tracker, |
| 196 | const soinfo_list_t& global_group, |
| 197 | const soinfo_list_t& local_group); |
| 198 | #if !defined(__LP64__) |
| 199 | bool mips_check_and_adjust_fp_modes(); |
| 200 | #endif |
| 201 | #endif |
| 202 | size_t ref_count_; |
| 203 | public: |
| 204 | link_map link_map_head; |
| 205 | |
| 206 | bool constructors_called; |
| 207 | |
| 208 | // When you read a virtual address from the ELF file, add this |
| 209 | // value to get the corresponding address in the process' address space. |
| 210 | ElfW(Addr) load_bias; |
| 211 | |
| 212 | #if !defined(__LP64__) |
| 213 | bool has_text_relocations; |
| 214 | #endif |
| 215 | bool has_DT_SYMBOLIC; |
| 216 | |
| 217 | public: |
| 218 | soinfo(android_namespace_t* ns, const char* name, const struct stat* file_stat, |
| 219 | off64_t file_offset, int rtld_flags); |
| 220 | ~soinfo(); |
| 221 | |
| 222 | void call_constructors(); |
| 223 | void call_destructors(); |
| 224 | void call_pre_init_constructors(); |
| 225 | bool prelink_image(); |
| 226 | bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group, |
Torne (Richard Coles) | efbe9a5 | 2018-10-17 15:59:38 -0400 | [diff] [blame] | 227 | const android_dlextinfo* extinfo, size_t* relro_fd_offset); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 228 | bool protect_relro(); |
| 229 | |
| 230 | void add_child(soinfo* child); |
| 231 | void remove_all_links(); |
| 232 | |
| 233 | ino_t get_st_ino() const; |
| 234 | dev_t get_st_dev() const; |
| 235 | off64_t get_file_offset() const; |
| 236 | |
| 237 | uint32_t get_rtld_flags() const; |
| 238 | uint32_t get_dt_flags_1() const; |
| 239 | void set_dt_flags_1(uint32_t dt_flags_1); |
| 240 | |
| 241 | soinfo_list_t& get_children(); |
| 242 | const soinfo_list_t& get_children() const; |
| 243 | |
| 244 | soinfo_list_t& get_parents(); |
| 245 | |
| 246 | bool find_symbol_by_name(SymbolName& symbol_name, |
| 247 | const version_info* vi, |
| 248 | const ElfW(Sym)** symbol) const; |
| 249 | |
| 250 | ElfW(Sym)* find_symbol_by_address(const void* addr); |
| 251 | ElfW(Addr) resolve_symbol_address(const ElfW(Sym)* s) const; |
| 252 | |
| 253 | const char* get_string(ElfW(Word) index) const; |
| 254 | bool can_unload() const; |
| 255 | bool is_gnu_hash() const; |
| 256 | |
| 257 | bool inline has_min_version(uint32_t min_version __unused) const { |
| 258 | #if defined(__work_around_b_24465209__) |
| 259 | return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version; |
| 260 | #else |
| 261 | return true; |
| 262 | #endif |
| 263 | } |
| 264 | |
| 265 | bool is_linked() const; |
| 266 | bool is_linker() const; |
| 267 | bool is_main_executable() const; |
| 268 | |
| 269 | void set_linked(); |
| 270 | void set_linker_flag(); |
| 271 | void set_main_executable(); |
| 272 | void set_nodelete(); |
| 273 | |
dimitry | 965d06d | 2017-11-28 16:03:07 +0100 | [diff] [blame] | 274 | size_t increment_ref_count(); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 275 | size_t decrement_ref_count(); |
dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 276 | size_t get_ref_count() const; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 277 | |
| 278 | soinfo* get_local_group_root() const; |
| 279 | |
| 280 | void set_soname(const char* soname); |
| 281 | const char* get_soname() const; |
Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 282 | void set_realpath(const char* path); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 283 | const char* get_realpath() const; |
| 284 | const ElfW(Versym)* get_versym(size_t n) const; |
| 285 | ElfW(Addr) get_verneed_ptr() const; |
| 286 | size_t get_verneed_cnt() const; |
| 287 | ElfW(Addr) get_verdef_ptr() const; |
| 288 | size_t get_verdef_cnt() const; |
| 289 | |
Elliott Hughes | ff1428a | 2018-11-12 16:01:37 -0800 | [diff] [blame] | 290 | int get_target_sdk_version() const; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 291 | |
| 292 | void set_dt_runpath(const char *); |
| 293 | const std::vector<std::string>& get_dt_runpath() const; |
| 294 | android_namespace_t* get_primary_namespace(); |
| 295 | void add_secondary_namespace(android_namespace_t* secondary_ns); |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 296 | android_namespace_list_t& get_secondary_namespaces(); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 297 | |
Ryan Prichard | c6bec07 | 2019-10-03 17:57:15 -0700 | [diff] [blame] | 298 | soinfo_tls* get_tls() const { |
| 299 | return has_min_version(5) ? tls_.get() : nullptr; |
| 300 | } |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 301 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 302 | void set_mapped_by_caller(bool reserved_map); |
| 303 | bool is_mapped_by_caller() const; |
| 304 | |
| 305 | uintptr_t get_handle() const; |
| 306 | void generate_handle(); |
| 307 | void* to_handle(); |
| 308 | |
| 309 | private: |
dimitry | 965d06d | 2017-11-28 16:03:07 +0100 | [diff] [blame] | 310 | bool is_image_linked() const; |
| 311 | void set_image_linked(); |
| 312 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 313 | bool elf_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const; |
| 314 | ElfW(Sym)* elf_addr_lookup(const void* addr); |
| 315 | bool gnu_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const; |
| 316 | ElfW(Sym)* gnu_addr_lookup(const void* addr); |
| 317 | |
| 318 | bool lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym, |
| 319 | const char* sym_name, const version_info** vi); |
| 320 | |
| 321 | template<typename ElfRelIteratorT> |
| 322 | bool relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator, |
| 323 | const soinfo_list_t& global_group, const soinfo_list_t& local_group); |
Rahul Chaudhry | b7feec7 | 2017-12-19 15:25:23 -0800 | [diff] [blame] | 324 | bool relocate_relr(); |
Rahul Chaudhry | f16b659 | 2018-01-25 15:34:15 -0800 | [diff] [blame] | 325 | void apply_relr_reloc(ElfW(Addr) offset); |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 326 | |
| 327 | private: |
| 328 | // This part of the structure is only available |
| 329 | // when FLAG_NEW_SOINFO is set in this->flags. |
| 330 | uint32_t version_; |
| 331 | |
| 332 | // version >= 0 |
| 333 | dev_t st_dev_; |
| 334 | ino_t st_ino_; |
| 335 | |
| 336 | // dependency graph |
| 337 | soinfo_list_t children_; |
| 338 | soinfo_list_t parents_; |
| 339 | |
| 340 | // version >= 1 |
| 341 | off64_t file_offset_; |
| 342 | uint32_t rtld_flags_; |
| 343 | uint32_t dt_flags_1_; |
| 344 | size_t strtab_size_; |
| 345 | |
| 346 | // version >= 2 |
| 347 | |
| 348 | size_t gnu_nbucket_; |
| 349 | uint32_t* gnu_bucket_; |
| 350 | uint32_t* gnu_chain_; |
| 351 | uint32_t gnu_maskwords_; |
| 352 | uint32_t gnu_shift2_; |
| 353 | ElfW(Addr)* gnu_bloom_filter_; |
| 354 | |
| 355 | soinfo* local_group_root_; |
| 356 | |
| 357 | uint8_t* android_relocs_; |
| 358 | size_t android_relocs_size_; |
| 359 | |
| 360 | const char* soname_; |
| 361 | std::string realpath_; |
| 362 | |
| 363 | const ElfW(Versym)* versym_; |
| 364 | |
| 365 | ElfW(Addr) verdef_ptr_; |
| 366 | size_t verdef_cnt_; |
| 367 | |
| 368 | ElfW(Addr) verneed_ptr_; |
| 369 | size_t verneed_cnt_; |
| 370 | |
Elliott Hughes | ff1428a | 2018-11-12 16:01:37 -0800 | [diff] [blame] | 371 | int target_sdk_version_; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 372 | |
| 373 | // version >= 3 |
| 374 | std::vector<std::string> dt_runpath_; |
| 375 | android_namespace_t* primary_namespace_; |
| 376 | android_namespace_list_t secondary_namespaces_; |
| 377 | uintptr_t handle_; |
| 378 | |
Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 379 | friend soinfo* get_libdl_info(const soinfo& linker_si); |
Rahul Chaudhry | b7feec7 | 2017-12-19 15:25:23 -0800 | [diff] [blame] | 380 | |
| 381 | // version >= 4 |
| 382 | ElfW(Relr)* relr_; |
| 383 | size_t relr_count_; |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 384 | |
| 385 | // version >= 5 |
| 386 | std::unique_ptr<soinfo_tls> tls_; |
Ryan Prichard | ffaae70 | 2019-01-23 17:47:10 -0800 | [diff] [blame] | 387 | std::vector<TlsDynamicResolverArg> tlsdesc_args_; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 388 | }; |
| 389 | |
| 390 | // This function is used by dlvsym() to calculate hash of sym_ver |
| 391 | uint32_t calculate_elf_hash(const char* name); |
| 392 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 393 | const char* fix_dt_needed(const char* dt_needed, const char* sopath); |
| 394 | |
| 395 | template<typename F> |
| 396 | void for_each_dt_needed(const soinfo* si, F action) { |
| 397 | for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) { |
| 398 | if (d->d_tag == DT_NEEDED) { |
| 399 | action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath())); |
| 400 | } |
| 401 | } |
| 402 | } |