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 | |
Mitch Phillips | a493fe4 | 2023-01-19 12:47:22 -0800 | [diff] [blame] | 17 | #include <android/dlext.h> |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 18 | #include <dlfcn.h> |
Elliott Hughes | 7ac9751 | 2014-01-14 17:25:13 -0800 | [diff] [blame] | 19 | #include <link.h> |
Mitch Phillips | a493fe4 | 2023-01-19 12:47:22 -0800 | [diff] [blame] | 20 | #include <signal.h> |
Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 23 | // These functions are exported by the loader |
| 24 | // TODO(dimitry): replace these with reference to libc.so |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 25 | |
Josh Gao | dfcc6e4 | 2017-10-13 11:56:55 -0700 | [diff] [blame] | 26 | extern "C" { |
| 27 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 28 | __attribute__((__weak__, visibility("default"))) |
dimitry | ce584e4 | 2019-03-29 05:34:33 +0100 | [diff] [blame] | 29 | void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size); |
| 30 | |
| 31 | __attribute__((__weak__, visibility("default"))) |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 32 | void* __loader_dlopen(const char* filename, int flags, const void* caller_addr); |
Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 33 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 34 | __attribute__((__weak__, visibility("default"))) |
Josh Gao | dfcc6e4 | 2017-10-13 11:56:55 -0700 | [diff] [blame] | 35 | char* __loader_dlerror(); |
Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 36 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 37 | __attribute__((__weak__, visibility("default"))) |
| 38 | void* __loader_dlsym(void* handle, const char* symbol, const void* caller_addr); |
Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 39 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 40 | __attribute__((__weak__, visibility("default"))) |
| 41 | void* __loader_dlvsym(void* handle, |
| 42 | const char* symbol, |
| 43 | const char* version, |
| 44 | const void* caller_addr); |
Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 45 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 46 | __attribute__((__weak__, visibility("default"))) |
| 47 | int __loader_dladdr(const void* addr, Dl_info* info); |
Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 48 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 49 | __attribute__((__weak__, visibility("default"))) |
| 50 | int __loader_dlclose(void* handle); |
Elliott Hughes | 7ac9751 | 2014-01-14 17:25:13 -0800 | [diff] [blame] | 51 | |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 52 | #if defined(__arm__) |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 53 | __attribute__((__weak__, visibility("default"))) |
| 54 | _Unwind_Ptr __loader_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount); |
Christopher Ferris | 24053a4 | 2013-08-19 17:45:09 -0700 | [diff] [blame] | 55 | #endif |
Elliott Hughes | 22d6292 | 2012-10-12 10:50:21 -0700 | [diff] [blame] | 56 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 57 | __attribute__((__weak__, visibility("default"))) |
| 58 | int __loader_dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data), |
| 59 | void* data); |
| 60 | |
| 61 | __attribute__((__weak__, visibility("default"))) |
| 62 | void __loader_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size); |
| 63 | |
| 64 | __attribute__((__weak__, visibility("default"))) |
| 65 | void __loader_android_update_LD_LIBRARY_PATH(const char* ld_library_path); |
| 66 | |
| 67 | __attribute__((__weak__, visibility("default"))) |
| 68 | void* __loader_android_dlopen_ext(const char* filename, |
| 69 | int flag, |
| 70 | const android_dlextinfo* extinfo, |
| 71 | const void* caller_addr); |
| 72 | |
| 73 | __attribute__((__weak__, visibility("default"))) |
Elliott Hughes | ff1428a | 2018-11-12 16:01:37 -0800 | [diff] [blame] | 74 | int __loader_android_get_application_target_sdk_version(); |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 75 | |
Mitch Phillips | a493fe4 | 2023-01-19 12:47:22 -0800 | [diff] [blame] | 76 | __attribute__((__weak__, visibility("default"))) bool __loader_android_handle_signal( |
| 77 | int signal_number, siginfo_t* info, void* context); |
| 78 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 79 | // Proxy calls to bionic loader |
dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 80 | __attribute__((__weak__)) |
dimitry | ce584e4 | 2019-03-29 05:34:33 +0100 | [diff] [blame] | 81 | void android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) { |
| 82 | __loader_android_get_LD_LIBRARY_PATH(buffer, buffer_size); |
| 83 | } |
| 84 | |
| 85 | __attribute__((__weak__)) |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 86 | void* dlopen(const char* filename, int flag) { |
| 87 | const void* caller_addr = __builtin_return_address(0); |
| 88 | return __loader_dlopen(filename, flag, caller_addr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 89 | } |
Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 90 | |
dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 91 | __attribute__((__weak__)) |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 92 | char* dlerror() { |
| 93 | return __loader_dlerror(); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 94 | } |
Dmitriy Ivanov | 79fd668 | 2015-05-21 17:43:49 -0700 | [diff] [blame] | 95 | |
dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 96 | __attribute__((__weak__)) |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 97 | void* dlsym(void* handle, const char* symbol) { |
| 98 | const void* caller_addr = __builtin_return_address(0); |
| 99 | return __loader_dlsym(handle, symbol, caller_addr); |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 100 | } |
| 101 | |
dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 102 | __attribute__((__weak__)) |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 103 | void* dlvsym(void* handle, const char* symbol, const char* version) { |
| 104 | const void* caller_addr = __builtin_return_address(0); |
| 105 | return __loader_dlvsym(handle, symbol, version, caller_addr); |
| 106 | } |
| 107 | |
dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 108 | __attribute__((__weak__)) |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 109 | int dladdr(const void* addr, Dl_info* info) { |
| 110 | return __loader_dladdr(addr, info); |
| 111 | } |
| 112 | |
dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 113 | __attribute__((__weak__)) |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 114 | int dlclose(void* handle) { |
| 115 | return __loader_dlclose(handle); |
| 116 | } |
| 117 | |
| 118 | #if defined(__arm__) |
dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 119 | __attribute__((__weak__)) |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 120 | _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) { |
| 121 | return __loader_dl_unwind_find_exidx(pc, pcount); |
| 122 | } |
| 123 | #endif |
| 124 | |
Dan Albert | db98fed | 2017-04-25 15:42:42 -0700 | [diff] [blame] | 125 | /* |
| 126 | * This needs to be defined as weak because it is also defined in libc.a. |
| 127 | * Without this, static executables will have a multiple definition error. |
| 128 | */ |
| 129 | __attribute__((__weak__)) |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 130 | int dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data), void* data) { |
| 131 | return __loader_dl_iterate_phdr(cb, data); |
| 132 | } |
| 133 | |
dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 134 | __attribute__((__weak__)) |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 135 | void* android_dlopen_ext(const char* filename, int flag, const android_dlextinfo* extinfo) { |
| 136 | const void* caller_addr = __builtin_return_address(0); |
| 137 | return __loader_android_dlopen_ext(filename, flag, extinfo, caller_addr); |
| 138 | } |
| 139 | |
dimitry | c5ea3eb | 2017-10-05 17:23:10 +0200 | [diff] [blame] | 140 | __attribute__((__weak__)) |
Elliott Hughes | ff1428a | 2018-11-12 16:01:37 -0800 | [diff] [blame] | 141 | int android_get_application_target_sdk_version() { |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 142 | return __loader_android_get_application_target_sdk_version(); |
| 143 | } |
| 144 | |
Mitch Phillips | a493fe4 | 2023-01-19 12:47:22 -0800 | [diff] [blame] | 145 | // Returns true if this function handled the signal, false if the caller should handle the signal |
| 146 | // itself. This function returns true if the sigchain handler should immediately return, which |
| 147 | // happens when the signal came from GWP-ASan, and we've dumped a debuggerd report and patched up |
| 148 | // the GWP-ASan allocator to recover from the fault, and regular execution of the program can |
| 149 | // continue. |
| 150 | __attribute__((__weak__)) bool android_handle_signal(int signal_number, siginfo_t* info, |
| 151 | void* context) { |
| 152 | return __loader_android_handle_signal(signal_number, info, context); |
| 153 | } |
| 154 | |
Josh Gao | dfcc6e4 | 2017-10-13 11:56:55 -0700 | [diff] [blame] | 155 | } // extern "C" |