| The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2007 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 17 | #include <dlfcn.h> | 
| Elliott Hughes | 7ac9751 | 2014-01-14 17:25:13 -0800 | [diff] [blame] | 18 | #include <link.h> | 
| Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 19 | #include <stdlib.h> | 
| Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 20 | #include <android/dlext.h> | 
| Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 21 |  | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 22 | // These functions are exported by the loader | 
|  | 23 | // TODO(dimitry): replace these with reference to libc.so | 
| The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 24 |  | 
| Josh Gao | dfcc6e4 | 2017-10-13 11:56:55 -0700 | [diff] [blame] | 25 | extern "C" { | 
|  | 26 |  | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 27 | __attribute__((__weak__, visibility("default"))) | 
| dimitry | ce584e4 | 2019-03-29 05:34:33 +0100 | [diff] [blame] | 28 | void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size); | 
|  | 29 |  | 
|  | 30 | __attribute__((__weak__, visibility("default"))) | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 31 | void* __loader_dlopen(const char* filename, int flags, const void* caller_addr); | 
| Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 32 |  | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 33 | __attribute__((__weak__, visibility("default"))) | 
| Josh Gao | dfcc6e4 | 2017-10-13 11:56:55 -0700 | [diff] [blame] | 34 | char* __loader_dlerror(); | 
| Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 35 |  | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 36 | __attribute__((__weak__, visibility("default"))) | 
|  | 37 | void* __loader_dlsym(void* handle, const char* symbol, const void* caller_addr); | 
| Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 38 |  | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 39 | __attribute__((__weak__, visibility("default"))) | 
|  | 40 | void* __loader_dlvsym(void* handle, | 
|  | 41 | const char* symbol, | 
|  | 42 | const char* version, | 
|  | 43 | const void* caller_addr); | 
| Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 44 |  | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 45 | __attribute__((__weak__, visibility("default"))) | 
|  | 46 | int __loader_dladdr(const void* addr, Dl_info* info); | 
| Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 47 |  | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 48 | __attribute__((__weak__, visibility("default"))) | 
|  | 49 | int __loader_dlclose(void* handle); | 
| Elliott Hughes | 7ac9751 | 2014-01-14 17:25:13 -0800 | [diff] [blame] | 50 |  | 
| Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 51 | #if defined(__arm__) | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 52 | __attribute__((__weak__, visibility("default"))) | 
|  | 53 | _Unwind_Ptr __loader_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount); | 
| Christopher Ferris | 24053a4 | 2013-08-19 17:45:09 -0700 | [diff] [blame] | 54 | #endif | 
| Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 55 |  | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 56 | __attribute__((__weak__, visibility("default"))) | 
|  | 57 | int __loader_dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data), | 
|  | 58 | void* data); | 
|  | 59 |  | 
|  | 60 | __attribute__((__weak__, visibility("default"))) | 
|  | 61 | void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size); | 
|  | 62 |  | 
|  | 63 | __attribute__((__weak__, visibility("default"))) | 
|  | 64 | void __loader_android_update_LD_LIBRARY_PATH(const char* ld_library_path); | 
|  | 65 |  | 
|  | 66 | __attribute__((__weak__, visibility("default"))) | 
|  | 67 | void* __loader_android_dlopen_ext(const char* filename, | 
|  | 68 | int flag, | 
|  | 69 | const android_dlextinfo* extinfo, | 
|  | 70 | const void* caller_addr); | 
|  | 71 |  | 
|  | 72 | __attribute__((__weak__, visibility("default"))) | 
| Elliott Hughes | ff1428a | 2018-11-12 16:01:37 -0800 | [diff] [blame] | 73 | int __loader_android_get_application_target_sdk_version(); | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 74 |  | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 75 | // Proxy calls to bionic loader | 
| dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 76 | __attribute__((__weak__)) | 
| dimitry | ce584e4 | 2019-03-29 05:34:33 +0100 | [diff] [blame] | 77 | void android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) { | 
|  | 78 | __loader_android_get_LD_LIBRARY_PATH(buffer, buffer_size); | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | __attribute__((__weak__)) | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 82 | void* dlopen(const char* filename, int flag) { | 
|  | 83 | const void* caller_addr = __builtin_return_address(0); | 
|  | 84 | return __loader_dlopen(filename, flag, caller_addr); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 85 | } | 
| Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 86 |  | 
| dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 87 | __attribute__((__weak__)) | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 88 | char* dlerror() { | 
|  | 89 | return __loader_dlerror(); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 90 | } | 
| Dmitriy Ivanov | 79fd668 | 2015-05-21 17:43:49 -0700 | [diff] [blame] | 91 |  | 
| dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 92 | __attribute__((__weak__)) | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 93 | void* dlsym(void* handle, const char* symbol) { | 
|  | 94 | const void* caller_addr = __builtin_return_address(0); | 
|  | 95 | return __loader_dlsym(handle, symbol, caller_addr); | 
| Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 98 | __attribute__((__weak__)) | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 99 | void* dlvsym(void* handle, const char* symbol, const char* version) { | 
|  | 100 | const void* caller_addr = __builtin_return_address(0); | 
|  | 101 | return __loader_dlvsym(handle, symbol, version, caller_addr); | 
|  | 102 | } | 
|  | 103 |  | 
| dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 104 | __attribute__((__weak__)) | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 105 | int dladdr(const void* addr, Dl_info* info) { | 
|  | 106 | return __loader_dladdr(addr, info); | 
|  | 107 | } | 
|  | 108 |  | 
| dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 109 | __attribute__((__weak__)) | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 110 | int dlclose(void* handle) { | 
|  | 111 | return __loader_dlclose(handle); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | #if defined(__arm__) | 
| dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 115 | __attribute__((__weak__)) | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 116 | _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) { | 
|  | 117 | return __loader_dl_unwind_find_exidx(pc, pcount); | 
|  | 118 | } | 
|  | 119 | #endif | 
|  | 120 |  | 
| Dan Albert | db98fed | 2017-04-25 15:42:42 -0700 | [diff] [blame] | 121 | /* | 
|  | 122 | * This needs to be defined as weak because it is also defined in libc.a. | 
|  | 123 | * Without this, static executables will have a multiple definition error. | 
|  | 124 | */ | 
|  | 125 | __attribute__((__weak__)) | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 126 | int dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data), void* data) { | 
|  | 127 | return __loader_dl_iterate_phdr(cb, data); | 
|  | 128 | } | 
|  | 129 |  | 
| dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 130 | __attribute__((__weak__)) | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 131 | void* android_dlopen_ext(const char* filename, int flag, const android_dlextinfo* extinfo) { | 
|  | 132 | const void* caller_addr = __builtin_return_address(0); | 
|  | 133 | return __loader_android_dlopen_ext(filename, flag, extinfo, caller_addr); | 
|  | 134 | } | 
|  | 135 |  | 
| dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 136 | __attribute__((__weak__)) | 
| Elliott Hughes | ff1428a | 2018-11-12 16:01:37 -0800 | [diff] [blame] | 137 | int android_get_application_target_sdk_version() { | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 138 | return __loader_android_get_application_target_sdk_version(); | 
|  | 139 | } | 
|  | 140 |  | 
| Ryan Prichard | 470b666 | 2018-03-27 22:10:55 -0700 | [diff] [blame] | 141 | #if defined(__arm__) | 
|  | 142 | // An arm32 unwinding table has an R_ARM_NONE relocation to | 
|  | 143 | // __aeabi_unwind_cpp_pr0. This shared library will never invoke the unwinder, | 
|  | 144 | // so it doesn't actually need the routine. Define a dummy version here, | 
|  | 145 | // because the real version calls libc functions (e.g. memcpy, abort), which | 
|  | 146 | // would create a dependency cycle with libc.so. | 
|  | 147 | __attribute__((visibility("hidden"))) | 
|  | 148 | void __aeabi_unwind_cpp_pr0() { | 
|  | 149 | __builtin_trap(); | 
|  | 150 | } | 
|  | 151 | #endif | 
|  | 152 |  | 
| Josh Gao | dfcc6e4 | 2017-10-13 11:56:55 -0700 | [diff] [blame] | 153 | } // extern "C" |