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 32 #include <dlfcn.h> 33 #include <android/dlext.h> 34 #include <elf.h> 35 #include <inttypes.h> 36 #include <link.h> 37 #include <sys/stat.h> 38 #include <unistd.h> 39 40 #include "private/bionic_page.h" 41 #include "private/libc_logging.h" 42 #include "linked_list.h" 43 44 #include <string> 45 #include <vector> 46 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()); \ 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 62 #if defined(__LP64__) 63 #define ELFW(what) ELF64_ ## what 64 #else 65 #define ELFW(what) ELF32_ ## what 66 #endif 67 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 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 89 90 #define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE) 91 92 #define SOINFO_VERSION 3 93 94 #if defined(__work_around_b_24465209__) 95 #define SOINFO_NAME_LEN 128 96 #endif 97 98 typedef void (*linker_function_t)(); 99 100 // Android uses RELA for aarch64 and x86_64. mips64 still uses REL. 101 #if defined(__aarch64__) || defined(__x86_64__) 102 #define USE_RELA 1 103 #endif 104 105 struct soinfo; 106 107 class SoinfoListAllocator { 108 public: 109 static LinkedListEntry<soinfo>* alloc(); 110 static void free(LinkedListEntry<soinfo>* entry); 111 112 private: 113 // unconstructable 114 DISALLOW_IMPLICIT_CONSTRUCTORS(SoinfoListAllocator); 115 }; 116 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 127 class SymbolName { 128 public: SymbolName(const char * name)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 get_name()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 150 struct version_info { version_infoversion_info151 constexpr version_info() : elf_hash(0), name(nullptr), target_si(nullptr) {} 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 176 struct soinfo { 177 public: 178 typedef LinkedList<soinfo, SoinfoListAllocator> soinfo_list_t; 179 typedef LinkedList<android_namespace_t, NamespaceListAllocator> android_namespace_list_t; 180 #if defined(__work_around_b_24465209__) 181 private: 182 char old_name_[SOINFO_NAME_LEN]; 183 #endif 184 public: 185 const ElfW(Phdr)* phdr; 186 size_t phnum; 187 ElfW(Addr) entry; 188 ElfW(Addr) base; 189 size_t size; 190 191 #if defined(__work_around_b_24465209__) 192 uint32_t unused1; // DO NOT USE, maintained for compatibility. 193 #endif 194 195 ElfW(Dyn)* dynamic; 196 197 #if defined(__work_around_b_24465209__) 198 uint32_t unused2; // DO NOT USE, maintained for compatibility 199 uint32_t unused3; // DO NOT USE, maintained for compatibility 200 #endif 201 202 soinfo* next; 203 private: 204 uint32_t flags_; 205 206 const char* strtab_; 207 ElfW(Sym)* symtab_; 208 209 size_t nbucket_; 210 size_t nchain_; 211 uint32_t* bucket_; 212 uint32_t* chain_; 213 214 #if defined(__mips__) || !defined(__LP64__) 215 // This is only used by mips and mips64, but needs to be here for 216 // all 32-bit architectures to preserve binary compatibility. 217 ElfW(Addr)** plt_got_; 218 #endif 219 220 #if defined(USE_RELA) 221 ElfW(Rela)* plt_rela_; 222 size_t plt_rela_count_; 223 224 ElfW(Rela)* rela_; 225 size_t rela_count_; 226 #else 227 ElfW(Rel)* plt_rel_; 228 size_t plt_rel_count_; 229 230 ElfW(Rel)* rel_; 231 size_t rel_count_; 232 #endif 233 234 linker_function_t* preinit_array_; 235 size_t preinit_array_count_; 236 237 linker_function_t* init_array_; 238 size_t init_array_count_; 239 linker_function_t* fini_array_; 240 size_t fini_array_count_; 241 242 linker_function_t init_func_; 243 linker_function_t fini_func_; 244 245 #if defined(__arm__) 246 public: 247 // ARM EABI section used for stack unwinding. 248 uint32_t* ARM_exidx; 249 size_t ARM_exidx_count; 250 private: 251 #elif defined(__mips__) 252 uint32_t mips_symtabno_; 253 uint32_t mips_local_gotno_; 254 uint32_t mips_gotsym_; 255 bool mips_relocate_got(const VersionTracker& version_tracker, 256 const soinfo_list_t& global_group, 257 const soinfo_list_t& local_group); 258 #if !defined(__LP64__) 259 bool mips_check_and_adjust_fp_modes(); 260 #endif 261 #endif 262 size_t ref_count_; 263 public: 264 link_map link_map_head; 265 266 bool constructors_called; 267 268 // When you read a virtual address from the ELF file, add this 269 // value to get the corresponding address in the process' address space. 270 ElfW(Addr) load_bias; 271 272 #if !defined(__LP64__) 273 bool has_text_relocations; 274 #endif 275 bool has_DT_SYMBOLIC; 276 277 public: 278 soinfo(android_namespace_t* ns, const char* name, const struct stat* file_stat, 279 off64_t file_offset, int rtld_flags); 280 ~soinfo(); 281 282 void call_constructors(); 283 void call_destructors(); 284 void call_pre_init_constructors(); 285 bool prelink_image(); 286 bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group, 287 const android_dlextinfo* extinfo); 288 bool protect_relro(); 289 290 void add_child(soinfo* child); 291 void remove_all_links(); 292 293 ino_t get_st_ino() const; 294 dev_t get_st_dev() const; 295 off64_t get_file_offset() const; 296 297 uint32_t get_rtld_flags() const; 298 uint32_t get_dt_flags_1() const; 299 void set_dt_flags_1(uint32_t dt_flags_1); 300 301 soinfo_list_t& get_children(); 302 const soinfo_list_t& get_children() const; 303 304 soinfo_list_t& get_parents(); 305 306 bool find_symbol_by_name(SymbolName& symbol_name, 307 const version_info* vi, 308 const ElfW(Sym)** symbol) const; 309 310 ElfW(Sym)* find_symbol_by_address(const void* addr); 311 ElfW(Addr) resolve_symbol_address(const ElfW(Sym)* s) const; 312 313 const char* get_string(ElfW(Word) index) const; 314 bool can_unload() const; 315 bool is_gnu_hash() const; 316 has_min_versionsoinfo317 bool inline has_min_version(uint32_t min_version __unused) const { 318 #if defined(__work_around_b_24465209__) 319 return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version; 320 #else 321 return true; 322 #endif 323 } 324 325 bool is_linked() const; 326 bool is_linker() const; 327 bool is_main_executable() const; 328 329 void set_linked(); 330 void set_linker_flag(); 331 void set_main_executable(); 332 void set_nodelete(); 333 334 void increment_ref_count(); 335 size_t decrement_ref_count(); 336 337 soinfo* get_local_group_root() const; 338 339 void set_soname(const char* soname); 340 const char* get_soname() const; 341 const char* get_realpath() const; 342 const ElfW(Versym)* get_versym(size_t n) const; 343 ElfW(Addr) get_verneed_ptr() const; 344 size_t get_verneed_cnt() const; 345 ElfW(Addr) get_verdef_ptr() const; 346 size_t get_verdef_cnt() const; 347 348 bool find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const; 349 350 uint32_t get_target_sdk_version() const; 351 352 void set_dt_runpath(const char *); 353 const std::vector<std::string>& get_dt_runpath() const; 354 android_namespace_t* get_primary_namespace(); 355 void add_secondary_namespace(android_namespace_t* secondary_ns); 356 357 void set_mapped_by_caller(bool reserved_map); 358 bool is_mapped_by_caller() const; 359 360 uintptr_t get_handle() const; 361 void generate_handle(); 362 void* to_handle(); 363 364 private: 365 bool elf_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const; 366 ElfW(Sym)* elf_addr_lookup(const void* addr); 367 bool gnu_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const; 368 ElfW(Sym)* gnu_addr_lookup(const void* addr); 369 370 bool lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym, 371 const char* sym_name, const version_info** vi); 372 373 void call_array(const char* array_name, linker_function_t* functions, size_t count, bool reverse); 374 void call_function(const char* function_name, linker_function_t function); 375 template<typename ElfRelIteratorT> 376 bool relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator, 377 const soinfo_list_t& global_group, const soinfo_list_t& local_group); 378 379 private: 380 // This part of the structure is only available 381 // when FLAG_NEW_SOINFO is set in this->flags. 382 uint32_t version_; 383 384 // version >= 0 385 dev_t st_dev_; 386 ino_t st_ino_; 387 388 // dependency graph 389 soinfo_list_t children_; 390 soinfo_list_t parents_; 391 392 // version >= 1 393 off64_t file_offset_; 394 uint32_t rtld_flags_; 395 uint32_t dt_flags_1_; 396 size_t strtab_size_; 397 398 // version >= 2 399 400 size_t gnu_nbucket_; 401 uint32_t* gnu_bucket_; 402 uint32_t* gnu_chain_; 403 uint32_t gnu_maskwords_; 404 uint32_t gnu_shift2_; 405 ElfW(Addr)* gnu_bloom_filter_; 406 407 soinfo* local_group_root_; 408 409 uint8_t* android_relocs_; 410 size_t android_relocs_size_; 411 412 const char* soname_; 413 std::string realpath_; 414 415 const ElfW(Versym)* versym_; 416 417 ElfW(Addr) verdef_ptr_; 418 size_t verdef_cnt_; 419 420 ElfW(Addr) verneed_ptr_; 421 size_t verneed_cnt_; 422 423 uint32_t target_sdk_version_; 424 425 // version >= 3 426 std::vector<std::string> dt_runpath_; 427 android_namespace_t* primary_namespace_; 428 android_namespace_list_t secondary_namespaces_; 429 uintptr_t handle_; 430 431 friend soinfo* get_libdl_info(); 432 }; 433 434 bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi, 435 soinfo** si_found_in, const soinfo::soinfo_list_t& global_group, 436 const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol); 437 438 enum RelocationKind { 439 kRelocAbsolute = 0, 440 kRelocRelative, 441 kRelocCopy, 442 kRelocSymbol, 443 kRelocMax 444 }; 445 446 void count_relocation(RelocationKind kind); 447 448 soinfo* get_libdl_info(); 449 450 void do_android_get_LD_LIBRARY_PATH(char*, size_t); 451 void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path); 452 void* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo, void* caller_addr); 453 int do_dlclose(void* handle); 454 455 int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data); 456 457 bool do_dlsym(void* handle, const char* sym_name, const char* sym_ver, 458 void* caller_addr, void** symbol); 459 460 int do_dladdr(const void* addr, Dl_info* info); 461 462 void debuggerd_init(); 463 extern "C" abort_msg_t* g_abort_message; 464 465 char* linker_get_error_buffer(); 466 size_t linker_get_error_buffer_size(); 467 468 void set_application_target_sdk_version(uint32_t target); 469 uint32_t get_application_target_sdk_version(); 470 471 enum { 472 /* A regular namespace is the namespace with a custom search path that does 473 * not impose any restrictions on the location of native libraries. 474 */ 475 ANDROID_NAMESPACE_TYPE_REGULAR = 0, 476 477 /* An isolated namespace requires all the libraries to be on the search path 478 * or under permitted_when_isolated_path. The search path is the union of 479 * ld_library_path and default_library_path. 480 */ 481 ANDROID_NAMESPACE_TYPE_ISOLATED = 1, 482 483 /* The shared namespace clones the list of libraries of the caller namespace upon creation 484 * which means that they are shared between namespaces - the caller namespace and the new one 485 * will use the same copy of a library if it was loaded prior to android_create_namespace call. 486 * 487 * Note that libraries loaded after the namespace is created will not be shared. 488 * 489 * Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor 490 * permitted_path from the caller's namespace. 491 */ 492 ANDROID_NAMESPACE_TYPE_SHARED = 2, 493 ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED | 494 ANDROID_NAMESPACE_TYPE_ISOLATED, 495 }; 496 497 bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path); 498 android_namespace_t* create_namespace(const void* caller_addr, 499 const char* name, 500 const char* ld_library_path, 501 const char* default_library_path, 502 uint64_t type, 503 const char* permitted_when_isolated_path, 504 android_namespace_t* parent_namespace); 505 506 #endif 507