Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -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 | |
| 29 | #include "linker_main.h" |
| 30 | |
Ryan Prichard | 701bd0c | 2018-11-21 16:23:03 -0800 | [diff] [blame] | 31 | #include <link.h> |
| 32 | #include <sys/auxv.h> |
| 33 | |
Collin Fijalkovich | 47d27aa | 2021-03-24 10:17:39 -0700 | [diff] [blame^] | 34 | #include "linker.h" |
| 35 | #include "linker_cfi.h" |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 36 | #include "linker_debug.h" |
Ryan Prichard | 80e40f0 | 2019-10-31 19:54:46 -0700 | [diff] [blame] | 37 | #include "linker_debuggerd.h" |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 38 | #include "linker_gdb_support.h" |
| 39 | #include "linker_globals.h" |
| 40 | #include "linker_phdr.h" |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 41 | #include "linker_relocate.h" |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 42 | #include "linker_tls.h" |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 43 | #include "linker_utils.h" |
| 44 | |
Ryan Prichard | 249757b | 2019-11-01 17:18:28 -0700 | [diff] [blame] | 45 | #include "private/bionic_auxv.h" |
| 46 | #include "private/bionic_call_ifunc_resolver.h" |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 47 | #include "private/bionic_globals.h" |
| 48 | #include "private/bionic_tls.h" |
| 49 | #include "private/KernelArgumentBlock.h" |
| 50 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 51 | #include "android-base/unique_fd.h" |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 52 | #include "android-base/strings.h" |
| 53 | #include "android-base/stringprintf.h" |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 54 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 55 | #include <async_safe/log.h> |
Ryan Prichard | 701bd0c | 2018-11-21 16:23:03 -0800 | [diff] [blame] | 56 | #include <bionic/libc_init_common.h> |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 57 | #include <bionic/pthread_internal.h> |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 58 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 59 | #include <vector> |
| 60 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 61 | __LIBC_HIDDEN__ extern "C" void _start(); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 62 | |
| 63 | static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf); |
| 64 | |
Ryan Prichard | 9729f35 | 2018-07-13 22:40:26 -0700 | [diff] [blame] | 65 | static void get_elf_base_from_phdr(const ElfW(Phdr)* phdr_table, size_t phdr_count, |
| 66 | ElfW(Addr)* base, ElfW(Addr)* load_bias); |
| 67 | |
Vic Yang | 1bf62b2 | 2019-08-13 14:53:28 -0700 | [diff] [blame] | 68 | static void set_bss_vma_name(soinfo* si); |
| 69 | |
Evgenii Stepanov | 8564b8d | 2020-12-15 13:55:32 -0800 | [diff] [blame] | 70 | void __libc_init_mte(const void* phdr_start, size_t phdr_count, uintptr_t load_bias); |
| 71 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 72 | // These should be preserved static to avoid emitting |
| 73 | // RELATIVE relocations for the part of the code running |
| 74 | // before linker links itself. |
| 75 | |
| 76 | // TODO (dimtiry): remove somain, rename solist to solist_head |
| 77 | static soinfo* solist; |
| 78 | static soinfo* sonext; |
| 79 | static soinfo* somain; // main process, always the one after libdl_info |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 80 | static soinfo* solinker; |
dimitry | 8b14256 | 2018-05-09 15:22:38 +0200 | [diff] [blame] | 81 | static soinfo* vdso; // vdso if present |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 82 | |
| 83 | void solist_add_soinfo(soinfo* si) { |
| 84 | sonext->next = si; |
| 85 | sonext = si; |
| 86 | } |
| 87 | |
| 88 | bool solist_remove_soinfo(soinfo* si) { |
| 89 | soinfo *prev = nullptr, *trav; |
| 90 | for (trav = solist; trav != nullptr; trav = trav->next) { |
| 91 | if (trav == si) { |
| 92 | break; |
| 93 | } |
| 94 | prev = trav; |
| 95 | } |
| 96 | |
| 97 | if (trav == nullptr) { |
| 98 | // si was not in solist |
| 99 | PRINT("name \"%s\"@%p is not in solist!", si->get_realpath(), si); |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | // prev will never be null, because the first entry in solist is |
| 104 | // always the static libdl_info. |
George Burgess IV | 7059100 | 2017-06-27 16:23:45 -0700 | [diff] [blame] | 105 | CHECK(prev != nullptr); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 106 | prev->next = si->next; |
| 107 | if (si == sonext) { |
| 108 | sonext = prev; |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | soinfo* solist_get_head() { |
| 115 | return solist; |
| 116 | } |
| 117 | |
| 118 | soinfo* solist_get_somain() { |
| 119 | return somain; |
| 120 | } |
| 121 | |
dimitry | 8b14256 | 2018-05-09 15:22:38 +0200 | [diff] [blame] | 122 | soinfo* solist_get_vdso() { |
| 123 | return vdso; |
| 124 | } |
| 125 | |
Elliott Hughes | 90f96b9 | 2019-05-09 15:56:39 -0700 | [diff] [blame] | 126 | bool g_is_ldd; |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 127 | int g_ld_debug_verbosity; |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 128 | |
| 129 | static std::vector<std::string> g_ld_preload_names; |
| 130 | |
| 131 | static std::vector<soinfo*> g_ld_preloads; |
| 132 | |
| 133 | static void parse_path(const char* path, const char* delimiters, |
| 134 | std::vector<std::string>* resolved_paths) { |
| 135 | std::vector<std::string> paths; |
| 136 | split_path(path, delimiters, &paths); |
| 137 | resolve_paths(paths, resolved_paths); |
| 138 | } |
| 139 | |
| 140 | static void parse_LD_LIBRARY_PATH(const char* path) { |
| 141 | std::vector<std::string> ld_libary_paths; |
| 142 | parse_path(path, ":", &ld_libary_paths); |
| 143 | g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths)); |
| 144 | } |
| 145 | |
| 146 | static void parse_LD_PRELOAD(const char* path) { |
| 147 | g_ld_preload_names.clear(); |
| 148 | if (path != nullptr) { |
| 149 | // We have historically supported ':' as well as ' ' in LD_PRELOAD. |
| 150 | g_ld_preload_names = android::base::Split(path, " :"); |
Josh Gao | 44f6e18 | 2017-10-18 17:25:24 -0700 | [diff] [blame] | 151 | g_ld_preload_names.erase(std::remove_if(g_ld_preload_names.begin(), g_ld_preload_names.end(), |
Josh Gao | 27242c6 | 2017-10-20 17:45:13 -0700 | [diff] [blame] | 152 | [](const std::string& s) { return s.empty(); }), |
| 153 | g_ld_preload_names.end()); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | // An empty list of soinfos |
| 158 | static soinfo_list_t g_empty_list; |
| 159 | |
Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 160 | static void add_vdso() { |
| 161 | ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(getauxval(AT_SYSINFO_EHDR)); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 162 | if (ehdr_vdso == nullptr) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0); |
| 167 | |
| 168 | si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff); |
| 169 | si->phnum = ehdr_vdso->e_phnum; |
| 170 | si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso); |
| 171 | si->size = phdr_table_get_load_size(si->phdr, si->phnum); |
| 172 | si->load_bias = get_elf_exec_load_bias(ehdr_vdso); |
| 173 | |
| 174 | si->prelink_image(); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 175 | si->link_image(SymbolLookupList(si), si, nullptr, nullptr); |
dimitry | c18de1b | 2017-09-26 14:31:35 +0200 | [diff] [blame] | 176 | // prevents accidental unloads... |
| 177 | si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_NODELETE); |
| 178 | si->set_linked(); |
| 179 | si->call_constructors(); |
dimitry | 8b14256 | 2018-05-09 15:22:38 +0200 | [diff] [blame] | 180 | |
| 181 | vdso = si; |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 184 | // Initializes an soinfo's link_map_head field using other fields from the |
Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 185 | // soinfo (phdr, phnum, load_bias). The soinfo's realpath must not change after |
| 186 | // this function is called. |
| 187 | static void init_link_map_head(soinfo& info) { |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 188 | auto& map = info.link_map_head; |
| 189 | map.l_addr = info.load_bias; |
Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 190 | map.l_name = const_cast<char*>(info.get_realpath()); |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 191 | phdr_table_get_dynamic_section(info.phdr, info.phnum, info.load_bias, &map.l_ld, nullptr); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | extern "C" int __system_properties_init(void); |
| 195 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 196 | struct ExecutableInfo { |
| 197 | std::string path; |
| 198 | struct stat file_stat; |
| 199 | const ElfW(Phdr)* phdr; |
| 200 | size_t phdr_count; |
| 201 | ElfW(Addr) entry_point; |
| 202 | }; |
| 203 | |
Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 204 | static ExecutableInfo get_executable_info() { |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 205 | ExecutableInfo result = {}; |
| 206 | |
Tom Cherry | 66bc428 | 2018-11-08 13:40:52 -0800 | [diff] [blame] | 207 | if (is_first_stage_init()) { |
| 208 | // /proc fs is not mounted when first stage init starts. Therefore we can't |
| 209 | // use /proc/self/exe for init. |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 210 | stat("/init", &result.file_stat); |
Tom Cherry | 66bc428 | 2018-11-08 13:40:52 -0800 | [diff] [blame] | 211 | |
| 212 | // /init may be a symlink, so try to read it as such. |
| 213 | char path[PATH_MAX]; |
| 214 | ssize_t path_len = readlink("/init", path, sizeof(path)); |
| 215 | if (path_len == -1 || path_len >= static_cast<ssize_t>(sizeof(path))) { |
| 216 | result.path = "/init"; |
| 217 | } else { |
| 218 | result.path = std::string(path, path_len); |
| 219 | } |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 220 | } else { |
| 221 | // Stat "/proc/self/exe" instead of executable_path because |
| 222 | // the executable could be unlinked by this point and it should |
| 223 | // not cause a crash (see http://b/31084669) |
| 224 | if (TEMP_FAILURE_RETRY(stat("/proc/self/exe", &result.file_stat)) != 0) { |
| 225 | async_safe_fatal("unable to stat \"/proc/self/exe\": %s", strerror(errno)); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 226 | } |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 227 | char path[PATH_MAX]; |
| 228 | ssize_t path_len = readlink("/proc/self/exe", path, sizeof(path)); |
| 229 | if (path_len == -1 || path_len >= static_cast<ssize_t>(sizeof(path))) { |
| 230 | async_safe_fatal("readlink('/proc/self/exe') failed: %s", strerror(errno)); |
| 231 | } |
| 232 | result.path = std::string(path, path_len); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 235 | result.phdr = reinterpret_cast<const ElfW(Phdr)*>(getauxval(AT_PHDR)); |
| 236 | result.phdr_count = getauxval(AT_PHNUM); |
| 237 | result.entry_point = getauxval(AT_ENTRY); |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 238 | return result; |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 241 | #if defined(__LP64__) |
Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 242 | static char kFallbackLinkerPath[] = "/system/bin/linker64"; |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 243 | #else |
Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 244 | static char kFallbackLinkerPath[] = "/system/bin/linker"; |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 245 | #endif |
| 246 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 247 | __printflike(1, 2) |
| 248 | static void __linker_error(const char* fmt, ...) { |
| 249 | va_list ap; |
dimitry | 04f7a79 | 2017-09-29 11:52:17 +0200 | [diff] [blame] | 250 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 251 | va_start(ap, fmt); |
| 252 | async_safe_format_fd_va_list(STDERR_FILENO, fmt, ap); |
| 253 | va_end(ap); |
| 254 | |
| 255 | va_start(ap, fmt); |
| 256 | async_safe_format_log_va_list(ANDROID_LOG_FATAL, "linker", fmt, ap); |
| 257 | va_end(ap); |
| 258 | |
dimitry | 04f7a79 | 2017-09-29 11:52:17 +0200 | [diff] [blame] | 259 | _exit(EXIT_FAILURE); |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 262 | static void __linker_cannot_link(const char* argv0) { |
| 263 | __linker_error("CANNOT LINK EXECUTABLE \"%s\": %s\n", |
| 264 | argv0, |
| 265 | linker_get_error_buffer()); |
| 266 | } |
| 267 | |
| 268 | // Load an executable. Normally the kernel has already loaded the executable when the linker |
| 269 | // starts. The linker can be invoked directly on an executable, though, and then the linker must |
| 270 | // load it. This function doesn't load dependencies or resolve relocations. |
| 271 | static ExecutableInfo load_executable(const char* orig_path) { |
| 272 | ExecutableInfo result = {}; |
| 273 | |
| 274 | if (orig_path[0] != '/') { |
| 275 | __linker_error("error: expected absolute path: \"%s\"\n", orig_path); |
| 276 | } |
| 277 | |
| 278 | off64_t file_offset; |
| 279 | android::base::unique_fd fd(open_executable(orig_path, &file_offset, &result.path)); |
| 280 | if (fd.get() == -1) { |
| 281 | __linker_error("error: unable to open file \"%s\"\n", orig_path); |
| 282 | } |
| 283 | |
| 284 | if (TEMP_FAILURE_RETRY(fstat(fd.get(), &result.file_stat)) == -1) { |
| 285 | __linker_error("error: unable to stat \"%s\": %s\n", result.path.c_str(), strerror(errno)); |
| 286 | } |
| 287 | |
| 288 | ElfReader elf_reader; |
| 289 | if (!elf_reader.Read(result.path.c_str(), fd.get(), file_offset, result.file_stat.st_size)) { |
| 290 | __linker_error("error: %s\n", linker_get_error_buffer()); |
| 291 | } |
Torne (Richard Coles) | efbe9a5 | 2018-10-17 15:59:38 -0400 | [diff] [blame] | 292 | address_space_params address_space; |
| 293 | if (!elf_reader.Load(&address_space)) { |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 294 | __linker_error("error: %s\n", linker_get_error_buffer()); |
| 295 | } |
| 296 | |
| 297 | result.phdr = elf_reader.loaded_phdr(); |
| 298 | result.phdr_count = elf_reader.phdr_count(); |
| 299 | result.entry_point = elf_reader.entry_point(); |
| 300 | return result; |
| 301 | } |
| 302 | |
Tamas Petz | 8d55d18 | 2020-02-24 14:15:25 +0100 | [diff] [blame] | 303 | static void platform_properties_init() { |
| 304 | #if defined(__aarch64__) |
| 305 | const unsigned long hwcap2 = getauxval(AT_HWCAP2); |
| 306 | g_platform_properties.bti_supported = (hwcap2 & HWCAP2_BTI) != 0; |
| 307 | #endif |
| 308 | } |
| 309 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 310 | static ElfW(Addr) linker_main(KernelArgumentBlock& args, const char* exe_to_load) { |
Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 311 | ProtectedDataGuard guard; |
| 312 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 313 | #if TIMING |
| 314 | struct timeval t0, t1; |
| 315 | gettimeofday(&t0, 0); |
| 316 | #endif |
| 317 | |
| 318 | // Sanitize the environment. |
Ryan Prichard | 48b1159 | 2018-11-22 02:41:36 -0800 | [diff] [blame] | 319 | __libc_init_AT_SECURE(args.envp); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 320 | |
| 321 | // Initialize system properties |
| 322 | __system_properties_init(); // may use 'environ' |
| 323 | |
Tamas Petz | 8d55d18 | 2020-02-24 14:15:25 +0100 | [diff] [blame] | 324 | // Initialize platform properties. |
| 325 | platform_properties_init(); |
| 326 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 327 | // Register the debuggerd signal handler. |
Ryan Prichard | 80e40f0 | 2019-10-31 19:54:46 -0700 | [diff] [blame] | 328 | linker_debuggerd_init(); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 329 | |
| 330 | g_linker_logger.ResetState(); |
| 331 | |
| 332 | // Get a few environment variables. |
| 333 | const char* LD_DEBUG = getenv("LD_DEBUG"); |
| 334 | if (LD_DEBUG != nullptr) { |
| 335 | g_ld_debug_verbosity = atoi(LD_DEBUG); |
| 336 | } |
| 337 | |
| 338 | #if defined(__LP64__) |
| 339 | INFO("[ Android dynamic linker (64-bit) ]"); |
| 340 | #else |
| 341 | INFO("[ Android dynamic linker (32-bit) ]"); |
| 342 | #endif |
| 343 | |
| 344 | // These should have been sanitized by __libc_init_AT_SECURE, but the test |
| 345 | // doesn't cost us anything. |
| 346 | const char* ldpath_env = nullptr; |
| 347 | const char* ldpreload_env = nullptr; |
| 348 | if (!getauxval(AT_SECURE)) { |
| 349 | ldpath_env = getenv("LD_LIBRARY_PATH"); |
| 350 | if (ldpath_env != nullptr) { |
| 351 | INFO("[ LD_LIBRARY_PATH set to \"%s\" ]", ldpath_env); |
| 352 | } |
| 353 | ldpreload_env = getenv("LD_PRELOAD"); |
| 354 | if (ldpreload_env != nullptr) { |
| 355 | INFO("[ LD_PRELOAD set to \"%s\" ]", ldpreload_env); |
| 356 | } |
| 357 | } |
| 358 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 359 | const ExecutableInfo exe_info = exe_to_load ? load_executable(exe_to_load) : |
Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 360 | get_executable_info(); |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 361 | |
Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 362 | INFO("[ Linking executable \"%s\" ]", exe_info.path.c_str()); |
Martin Stjernholm | 95252ee | 2019-02-22 22:48:59 +0000 | [diff] [blame] | 363 | |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 364 | // Initialize the main exe's soinfo. |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 365 | soinfo* si = soinfo_alloc(&g_default_namespace, |
Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 366 | exe_info.path.c_str(), &exe_info.file_stat, |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 367 | 0, RTLD_GLOBAL); |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 368 | somain = si; |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 369 | si->phdr = exe_info.phdr; |
| 370 | si->phnum = exe_info.phdr_count; |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 371 | get_elf_base_from_phdr(si->phdr, si->phnum, &si->base, &si->load_bias); |
| 372 | si->size = phdr_table_get_load_size(si->phdr, si->phnum); |
| 373 | si->dynamic = nullptr; |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 374 | si->set_main_executable(); |
Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 375 | init_link_map_head(*si); |
| 376 | |
Vic Yang | 1bf62b2 | 2019-08-13 14:53:28 -0700 | [diff] [blame] | 377 | set_bss_vma_name(si); |
| 378 | |
Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 379 | // Use the executable's PT_INTERP string as the solinker filename in the |
| 380 | // dynamic linker's module list. gdb reads both PT_INTERP and the module list, |
| 381 | // and if the paths for the linker are different, gdb will report that the |
| 382 | // PT_INTERP linker path was unloaded once the module list is initialized. |
| 383 | // There are three situations to handle: |
| 384 | // - the APEX linker (/system/bin/linker[64] -> /apex/.../linker[64]) |
| 385 | // - the ASAN linker (/system/bin/linker_asan[64] -> /apex/.../linker[64]) |
| 386 | // - the bootstrap linker (/system/bin/bootstrap/linker[64]) |
| 387 | const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum, |
| 388 | somain->load_bias); |
| 389 | if (interp == nullptr) { |
| 390 | // This case can happen if the linker attempts to execute itself |
| 391 | // (e.g. "linker64 /system/bin/linker64"). |
| 392 | interp = kFallbackLinkerPath; |
| 393 | } |
| 394 | solinker->set_realpath(interp); |
| 395 | init_link_map_head(*solinker); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 396 | |
Tamas Petz | 8d55d18 | 2020-02-24 14:15:25 +0100 | [diff] [blame] | 397 | #if defined(__aarch64__) |
| 398 | if (exe_to_load == nullptr) { |
| 399 | // Kernel does not add PROT_BTI to executable pages of the loaded ELF. |
| 400 | // Apply appropriate protections here if it is needed. |
| 401 | auto note_gnu_property = GnuPropertySection(somain); |
| 402 | if (note_gnu_property.IsBTICompatible() && |
| 403 | (phdr_table_protect_segments(somain->phdr, somain->phnum, somain->load_bias, |
| 404 | ¬e_gnu_property) < 0)) { |
| 405 | __linker_error("error: can't protect segments for \"%s\": %s", exe_info.path.c_str(), |
| 406 | strerror(errno)); |
| 407 | } |
| 408 | } |
Evgenii Stepanov | 8564b8d | 2020-12-15 13:55:32 -0800 | [diff] [blame] | 409 | |
| 410 | __libc_init_mte(somain->phdr, somain->phnum, somain->load_bias); |
Tamas Petz | 8d55d18 | 2020-02-24 14:15:25 +0100 | [diff] [blame] | 411 | #endif |
| 412 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 413 | // Register the main executable and the linker upfront to have |
| 414 | // gdb aware of them before loading the rest of the dependency |
| 415 | // tree. |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 416 | // |
| 417 | // gdb expects the linker to be in the debug shared object list. |
| 418 | // Without this, gdb has trouble locating the linker's ".text" |
| 419 | // and ".plt" sections. Gdb could also potentially use this to |
| 420 | // relocate the offset of our exported 'rtld_db_dlactivity' symbol. |
| 421 | // |
| 422 | insert_link_map_into_debug_map(&si->link_map_head); |
| 423 | insert_link_map_into_debug_map(&solinker->link_map_head); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 424 | |
Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 425 | add_vdso(); |
Ryan Prichard | 14dd992 | 2018-08-20 17:43:44 -0700 | [diff] [blame] | 426 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 427 | ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base); |
Elliott Hughes | 3bdb31b | 2017-01-07 10:38:20 -0800 | [diff] [blame] | 428 | |
| 429 | // We haven't supported non-PIE since Lollipop for security reasons. |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 430 | if (elf_hdr->e_type != ET_DYN) { |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 431 | // We don't use async_safe_fatal here because we don't want a tombstone: |
| 432 | // even after several years we still find ourselves on app compatibility |
Elliott Hughes | 3bdb31b | 2017-01-07 10:38:20 -0800 | [diff] [blame] | 433 | // investigations because some app's trying to launch an executable that |
| 434 | // hasn't worked in at least three years, and we've "helpfully" dropped a |
| 435 | // tombstone for them. The tombstone never provided any detail relevant to |
| 436 | // fixing the problem anyway, and the utility of drawing extra attention |
| 437 | // to the problem is non-existent at this late date. |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 438 | async_safe_format_fd(STDERR_FILENO, |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 439 | "\"%s\": error: Android 5.0 and later only support " |
| 440 | "position-independent executables (-fPIE).\n", |
| 441 | g_argv[0]); |
Elliott Hughes | 90f96b9 | 2019-05-09 15:56:39 -0700 | [diff] [blame] | 442 | _exit(EXIT_FAILURE); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid). |
| 446 | parse_LD_LIBRARY_PATH(ldpath_env); |
| 447 | parse_LD_PRELOAD(ldpreload_env); |
| 448 | |
Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 449 | std::vector<android_namespace_t*> namespaces = init_default_namespaces(exe_info.path.c_str()); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 450 | |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 451 | if (!si->prelink_image()) __linker_cannot_link(g_argv[0]); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 452 | |
| 453 | // add somain to global group |
| 454 | si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL); |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 455 | // ... and add it to all other linked namespaces |
| 456 | for (auto linked_ns : namespaces) { |
| 457 | if (linked_ns != &g_default_namespace) { |
| 458 | linked_ns->add_soinfo(somain); |
| 459 | somain->add_secondary_namespace(linked_ns); |
| 460 | } |
| 461 | } |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 462 | |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 463 | linker_setup_exe_static_tls(g_argv[0]); |
| 464 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 465 | // Load ld_preloads and dependencies. |
| 466 | std::vector<const char*> needed_library_name_list; |
| 467 | size_t ld_preloads_count = 0; |
| 468 | |
| 469 | for (const auto& ld_preload_name : g_ld_preload_names) { |
| 470 | needed_library_name_list.push_back(ld_preload_name.c_str()); |
| 471 | ++ld_preloads_count; |
| 472 | } |
| 473 | |
| 474 | for_each_dt_needed(si, [&](const char* name) { |
| 475 | needed_library_name_list.push_back(name); |
| 476 | }); |
| 477 | |
| 478 | const char** needed_library_names = &needed_library_name_list[0]; |
| 479 | size_t needed_libraries_count = needed_library_name_list.size(); |
| 480 | |
| 481 | if (needed_libraries_count > 0 && |
Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 482 | !find_libraries(&g_default_namespace, |
| 483 | si, |
| 484 | needed_library_names, |
| 485 | needed_libraries_count, |
| 486 | nullptr, |
| 487 | &g_ld_preloads, |
| 488 | ld_preloads_count, |
| 489 | RTLD_GLOBAL, |
| 490 | nullptr, |
| 491 | true /* add_as_children */, |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 492 | &namespaces)) { |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 493 | __linker_cannot_link(g_argv[0]); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 494 | } else if (needed_libraries_count == 0) { |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 495 | if (!si->link_image(SymbolLookupList(si), si, nullptr, nullptr)) { |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 496 | __linker_cannot_link(g_argv[0]); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 497 | } |
| 498 | si->increment_ref_count(); |
| 499 | } |
| 500 | |
Ryan Prichard | e5e69e0 | 2019-01-01 18:53:48 -0800 | [diff] [blame] | 501 | linker_finalize_static_tls(); |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 502 | __libc_init_main_thread_final(); |
| 503 | |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 504 | if (!get_cfi_shadow()->InitialLinkDone(solist)) __linker_cannot_link(g_argv[0]); |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 505 | |
Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 506 | si->call_pre_init_constructors(); |
Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 507 | si->call_constructors(); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 508 | |
| 509 | #if TIMING |
| 510 | gettimeofday(&t1, nullptr); |
Vic Yang | 7b9db34 | 2019-04-16 14:54:58 -0700 | [diff] [blame] | 511 | PRINT("LINKER TIME: %s: %d microseconds", g_argv[0], |
| 512 | static_cast<int>(((static_cast<long long>(t1.tv_sec) * 1000000LL) + |
| 513 | static_cast<long long>(t1.tv_usec)) - |
| 514 | ((static_cast<long long>(t0.tv_sec) * 1000000LL) + |
| 515 | static_cast<long long>(t0.tv_usec)))); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 516 | #endif |
| 517 | #if STATS |
Vic Yang | 542db79 | 2019-07-25 10:39:27 -0700 | [diff] [blame] | 518 | print_linker_stats(); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 519 | #endif |
Ryan Prichard | 78cd283 | 2019-10-25 17:46:43 -0700 | [diff] [blame] | 520 | #if TIMING || STATS |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 521 | fflush(stdout); |
| 522 | #endif |
| 523 | |
Vic Yang | bb7e123 | 2019-01-29 20:23:16 -0800 | [diff] [blame] | 524 | // We are about to hand control over to the executable loaded. We don't want |
| 525 | // to leave dirty pages behind unnecessarily. |
| 526 | purge_unused_memory(); |
| 527 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 528 | ElfW(Addr) entry = exe_info.entry_point; |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 529 | TRACE("[ Ready to execute \"%s\" @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry)); |
| 530 | return entry; |
| 531 | } |
| 532 | |
| 533 | /* Compute the load-bias of an existing executable. This shall only |
| 534 | * be used to compute the load bias of an executable or shared library |
| 535 | * that was loaded by the kernel itself. |
| 536 | * |
| 537 | * Input: |
| 538 | * elf -> address of ELF header, assumed to be at the start of the file. |
| 539 | * Return: |
| 540 | * load bias, i.e. add the value of any p_vaddr in the file to get |
| 541 | * the corresponding address in memory. |
| 542 | */ |
| 543 | static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) { |
| 544 | ElfW(Addr) offset = elf->e_phoff; |
| 545 | const ElfW(Phdr)* phdr_table = |
| 546 | reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset); |
| 547 | const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum; |
| 548 | |
| 549 | for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) { |
| 550 | if (phdr->p_type == PT_LOAD) { |
| 551 | return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr; |
| 552 | } |
| 553 | } |
| 554 | return 0; |
| 555 | } |
| 556 | |
Ryan Prichard | 9729f35 | 2018-07-13 22:40:26 -0700 | [diff] [blame] | 557 | /* Find the load bias and base address of an executable or shared object loaded |
| 558 | * by the kernel. The ELF file's PHDR table must have a PT_PHDR entry. |
| 559 | * |
| 560 | * A VDSO doesn't have a PT_PHDR entry in its PHDR table. |
| 561 | */ |
| 562 | static void get_elf_base_from_phdr(const ElfW(Phdr)* phdr_table, size_t phdr_count, |
| 563 | ElfW(Addr)* base, ElfW(Addr)* load_bias) { |
| 564 | for (size_t i = 0; i < phdr_count; ++i) { |
| 565 | if (phdr_table[i].p_type == PT_PHDR) { |
| 566 | *load_bias = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_vaddr; |
| 567 | *base = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_offset; |
| 568 | return; |
| 569 | } |
| 570 | } |
| 571 | async_safe_fatal("Could not find a PHDR: broken executable?"); |
| 572 | } |
| 573 | |
Vic Yang | 1bf62b2 | 2019-08-13 14:53:28 -0700 | [diff] [blame] | 574 | /* |
| 575 | * Set anonymous VMA name for .bss section. For DSOs loaded by the linker, this |
| 576 | * is done by ElfReader. This function is here for DSOs loaded by the kernel, |
| 577 | * namely the linker itself and the main executable. |
| 578 | */ |
| 579 | static void set_bss_vma_name(soinfo* si) { |
| 580 | for (size_t i = 0; i < si->phnum; ++i) { |
| 581 | auto phdr = &si->phdr[i]; |
| 582 | |
| 583 | if (phdr->p_type != PT_LOAD) { |
| 584 | continue; |
| 585 | } |
| 586 | |
| 587 | ElfW(Addr) seg_start = phdr->p_vaddr + si->load_bias; |
| 588 | ElfW(Addr) seg_page_end = PAGE_END(seg_start + phdr->p_memsz); |
| 589 | ElfW(Addr) seg_file_end = PAGE_END(seg_start + phdr->p_filesz); |
| 590 | |
| 591 | if (seg_page_end > seg_file_end) { |
| 592 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, |
| 593 | reinterpret_cast<void*>(seg_file_end), seg_page_end - seg_file_end, |
| 594 | ".bss"); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
Ryan Prichard | 249757b | 2019-11-01 17:18:28 -0700 | [diff] [blame] | 599 | // TODO: There is a similar ifunc resolver calling loop in libc_init_static.cpp, but that version |
| 600 | // uses weak symbols, which don't work in the linker prior to its relocation. This version also |
| 601 | // supports a load bias. When we stop supporting the gold linker in the NDK, then maybe we can use |
| 602 | // non-weak definitions and merge the two loops. |
| 603 | #if defined(USE_RELA) |
| 604 | extern __LIBC_HIDDEN__ ElfW(Rela) __rela_iplt_start[], __rela_iplt_end[]; |
| 605 | |
| 606 | static void call_ifunc_resolvers(ElfW(Addr) load_bias) { |
| 607 | for (ElfW(Rela) *r = __rela_iplt_start; r != __rela_iplt_end; ++r) { |
| 608 | ElfW(Addr)* offset = reinterpret_cast<ElfW(Addr)*>(r->r_offset + load_bias); |
| 609 | ElfW(Addr) resolver = r->r_addend + load_bias; |
| 610 | *offset = __bionic_call_ifunc_resolver(resolver); |
| 611 | } |
| 612 | } |
| 613 | #else |
| 614 | extern __LIBC_HIDDEN__ ElfW(Rel) __rel_iplt_start[], __rel_iplt_end[]; |
| 615 | |
| 616 | static void call_ifunc_resolvers(ElfW(Addr) load_bias) { |
| 617 | for (ElfW(Rel) *r = __rel_iplt_start; r != __rel_iplt_end; ++r) { |
| 618 | ElfW(Addr)* offset = reinterpret_cast<ElfW(Addr)*>(r->r_offset + load_bias); |
| 619 | ElfW(Addr) resolver = *offset + load_bias; |
| 620 | *offset = __bionic_call_ifunc_resolver(resolver); |
| 621 | } |
| 622 | } |
| 623 | #endif |
| 624 | |
Ryan Prichard | 94a8e85 | 2019-11-05 14:19:18 -0800 | [diff] [blame] | 625 | // Usable before ifunc resolvers have been called. This function is compiled with -ffreestanding. |
| 626 | static void linker_memclr(void* dst, size_t cnt) { |
| 627 | for (size_t i = 0; i < cnt; ++i) { |
| 628 | reinterpret_cast<char*>(dst)[i] = '\0'; |
| 629 | } |
| 630 | } |
| 631 | |
Ryan Prichard | 1990ba5 | 2019-02-07 21:31:31 -0800 | [diff] [blame] | 632 | // Detect an attempt to run the linker on itself. e.g.: |
| 633 | // /system/bin/linker64 /system/bin/linker64 |
| 634 | // Use priority-1 to run this constructor before other constructors. |
| 635 | __attribute__((constructor(1))) static void detect_self_exec() { |
| 636 | // Normally, the linker initializes the auxv global before calling its |
| 637 | // constructors. If the linker loads itself, though, the first loader calls |
| 638 | // the second loader's constructors before calling __linker_init. |
| 639 | if (__libc_shared_globals()->auxv != nullptr) { |
| 640 | return; |
| 641 | } |
| 642 | #if defined(__i386__) |
| 643 | // We don't have access to the auxv struct from here, so use the int 0x80 |
| 644 | // fallback. |
| 645 | __libc_sysinfo = reinterpret_cast<void*>(__libc_int0x80); |
| 646 | #endif |
| 647 | __linker_error("error: linker cannot load itself\n"); |
| 648 | } |
| 649 | |
Ryan Prichard | 742982d | 2018-05-30 22:32:17 -0700 | [diff] [blame] | 650 | static ElfW(Addr) __attribute__((noinline)) |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 651 | __linker_init_post_relocation(KernelArgumentBlock& args, soinfo& linker_so); |
Ryan Prichard | 742982d | 2018-05-30 22:32:17 -0700 | [diff] [blame] | 652 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 653 | /* |
| 654 | * This is the entry point for the linker, called from begin.S. This |
| 655 | * method is responsible for fixing the linker's own relocations, and |
| 656 | * then calling __linker_init_post_relocation(). |
| 657 | * |
| 658 | * Because this method is called before the linker has fixed it's own |
| 659 | * relocations, any attempt to reference an extern variable, extern |
| 660 | * function, or other GOT reference will generate a segfault. |
| 661 | */ |
| 662 | extern "C" ElfW(Addr) __linker_init(void* raw_args) { |
Ryan Prichard | 9cfca86 | 2018-11-22 02:44:09 -0800 | [diff] [blame] | 663 | // Initialize TLS early so system calls and errno work. |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 664 | KernelArgumentBlock args(raw_args); |
Ryan Prichard | 94a8e85 | 2019-11-05 14:19:18 -0800 | [diff] [blame] | 665 | bionic_tcb temp_tcb __attribute__((uninitialized)); |
| 666 | linker_memclr(&temp_tcb, sizeof(temp_tcb)); |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 667 | __libc_init_main_thread_early(args, &temp_tcb); |
Ryan Prichard | 27475b5 | 2018-05-17 17:14:18 -0700 | [diff] [blame] | 668 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 669 | // When the linker is run by itself (rather than as an interpreter for |
| 670 | // another program), AT_BASE is 0. |
Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 671 | ElfW(Addr) linker_addr = getauxval(AT_BASE); |
Ryan Prichard | 9729f35 | 2018-07-13 22:40:26 -0700 | [diff] [blame] | 672 | if (linker_addr == 0) { |
Ryan Prichard | 1990ba5 | 2019-02-07 21:31:31 -0800 | [diff] [blame] | 673 | // The AT_PHDR and AT_PHNUM aux values describe this linker instance, so use |
| 674 | // the phdr to find the linker's base address. |
Ryan Prichard | 9729f35 | 2018-07-13 22:40:26 -0700 | [diff] [blame] | 675 | ElfW(Addr) load_bias; |
| 676 | get_elf_base_from_phdr( |
Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 677 | reinterpret_cast<ElfW(Phdr)*>(getauxval(AT_PHDR)), getauxval(AT_PHNUM), |
Ryan Prichard | 9729f35 | 2018-07-13 22:40:26 -0700 | [diff] [blame] | 678 | &linker_addr, &load_bias); |
| 679 | } |
George Burgess IV | 7059100 | 2017-06-27 16:23:45 -0700 | [diff] [blame] | 680 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 681 | ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr); |
| 682 | ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff); |
| 683 | |
Ryan Prichard | 249757b | 2019-11-01 17:18:28 -0700 | [diff] [blame] | 684 | // string.h functions must not be used prior to calling the linker's ifunc resolvers. |
| 685 | const ElfW(Addr) load_bias = get_elf_exec_load_bias(elf_hdr); |
| 686 | call_ifunc_resolvers(load_bias); |
| 687 | |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 688 | soinfo tmp_linker_so(nullptr, nullptr, nullptr, 0, 0); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 689 | |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 690 | tmp_linker_so.base = linker_addr; |
| 691 | tmp_linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum); |
Ryan Prichard | 249757b | 2019-11-01 17:18:28 -0700 | [diff] [blame] | 692 | tmp_linker_so.load_bias = load_bias; |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 693 | tmp_linker_so.dynamic = nullptr; |
| 694 | tmp_linker_so.phdr = phdr; |
| 695 | tmp_linker_so.phnum = elf_hdr->e_phnum; |
| 696 | tmp_linker_so.set_linker_flag(); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 697 | |
| 698 | // Prelink the linker so we can access linker globals. |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 699 | if (!tmp_linker_so.prelink_image()) __linker_cannot_link(args.argv[0]); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 700 | if (!tmp_linker_so.link_image(SymbolLookupList(&tmp_linker_so), &tmp_linker_so, nullptr, nullptr)) __linker_cannot_link(args.argv[0]); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 701 | |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 702 | return __linker_init_post_relocation(args, tmp_linker_so); |
Ryan Prichard | 742982d | 2018-05-30 22:32:17 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /* |
| 706 | * This code is called after the linker has linked itself and fixed its own |
| 707 | * GOT. It is safe to make references to externs and other non-local data at |
| 708 | * this point. The compiler sometimes moves GOT references earlier in a |
| 709 | * function, so avoid inlining this function (http://b/80503879). |
| 710 | */ |
| 711 | static ElfW(Addr) __attribute__((noinline)) |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 712 | __linker_init_post_relocation(KernelArgumentBlock& args, soinfo& tmp_linker_so) { |
Ryan Prichard | 9cfca86 | 2018-11-22 02:44:09 -0800 | [diff] [blame] | 713 | // Finish initializing the main thread. |
Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 714 | __libc_init_main_thread_late(); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 715 | |
| 716 | // We didn't protect the linker's RELRO pages in link_image because we |
| 717 | // couldn't make system calls on x86 at that point, but we can now... |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 718 | if (!tmp_linker_so.protect_relro()) __linker_cannot_link(args.argv[0]); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 719 | |
Vic Yang | 1bf62b2 | 2019-08-13 14:53:28 -0700 | [diff] [blame] | 720 | // And we can set VMA name for the bss section now |
| 721 | set_bss_vma_name(&tmp_linker_so); |
| 722 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 723 | // Initialize the linker's static libc's globals |
Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 724 | __libc_init_globals(); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 725 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 726 | // Initialize the linker's own global variables |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 727 | tmp_linker_so.call_constructors(); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 728 | |
Ryan Prichard | bb1e373 | 2021-01-12 23:09:10 -0800 | [diff] [blame] | 729 | // Setting the linker soinfo's soname can allocate heap memory, so delay it until here. |
| 730 | for (const ElfW(Dyn)* d = tmp_linker_so.dynamic; d->d_tag != DT_NULL; ++d) { |
| 731 | if (d->d_tag == DT_SONAME) { |
| 732 | tmp_linker_so.set_soname(tmp_linker_so.get_string(d->d_un.d_val)); |
| 733 | } |
| 734 | } |
| 735 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 736 | // When the linker is run directly rather than acting as PT_INTERP, parse |
| 737 | // arguments and determine the executable to load. When it's instead acting |
| 738 | // as PT_INTERP, AT_ENTRY will refer to the loaded executable rather than the |
| 739 | // linker's _start. |
| 740 | const char* exe_to_load = nullptr; |
Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 741 | if (getauxval(AT_ENTRY) == reinterpret_cast<uintptr_t>(&_start)) { |
Elliott Hughes | 90f96b9 | 2019-05-09 15:56:39 -0700 | [diff] [blame] | 742 | if (args.argc == 3 && !strcmp(args.argv[1], "--list")) { |
| 743 | // We're being asked to behave like ldd(1). |
| 744 | g_is_ldd = true; |
| 745 | exe_to_load = args.argv[2]; |
| 746 | } else if (args.argc <= 1 || !strcmp(args.argv[1], "--help")) { |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 747 | async_safe_format_fd(STDOUT_FILENO, |
Elliott Hughes | 90f96b9 | 2019-05-09 15:56:39 -0700 | [diff] [blame] | 748 | "Usage: %s [--list] PROGRAM [ARGS-FOR-PROGRAM...]\n" |
| 749 | " %s [--list] path.zip!/PROGRAM [ARGS-FOR-PROGRAM...]\n" |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 750 | "\n" |
| 751 | "A helper program for linking dynamic executables. Typically, the kernel loads\n" |
| 752 | "this program because it's the PT_INTERP of a dynamic executable.\n" |
| 753 | "\n" |
| 754 | "This program can also be run directly to load and run a dynamic executable. The\n" |
| 755 | "executable can be inside a zip file if it's stored uncompressed and at a\n" |
Elliott Hughes | 90f96b9 | 2019-05-09 15:56:39 -0700 | [diff] [blame] | 756 | "page-aligned offset.\n" |
| 757 | "\n" |
| 758 | "The --list option gives behavior equivalent to ldd(1) on other systems.\n", |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 759 | args.argv[0], args.argv[0]); |
Elliott Hughes | 90f96b9 | 2019-05-09 15:56:39 -0700 | [diff] [blame] | 760 | _exit(EXIT_SUCCESS); |
| 761 | } else { |
| 762 | exe_to_load = args.argv[1]; |
| 763 | __libc_shared_globals()->initial_linker_arg_count = 1; |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 764 | } |
Dimitry Ivanov | 9b1cc4b | 2017-03-23 16:17:15 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 767 | // store argc/argv/envp to use them for calling constructors |
Ryan Prichard | abf736a | 2018-11-22 02:40:17 -0800 | [diff] [blame] | 768 | g_argc = args.argc - __libc_shared_globals()->initial_linker_arg_count; |
| 769 | g_argv = args.argv + __libc_shared_globals()->initial_linker_arg_count; |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 770 | g_envp = args.envp; |
Ryan Prichard | 48b1159 | 2018-11-22 02:41:36 -0800 | [diff] [blame] | 771 | __libc_shared_globals()->init_progname = g_argv[0]; |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 772 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 773 | // Initialize static variables. Note that in order to |
| 774 | // get correct libdl_info we need to call constructors |
| 775 | // before get_libdl_info(). |
Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 776 | sonext = solist = solinker = get_libdl_info(tmp_linker_so); |
Ryan Prichard | 0489645 | 2018-08-20 17:44:42 -0700 | [diff] [blame] | 777 | g_default_namespace.add_soinfo(solinker); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 778 | |
Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 779 | ElfW(Addr) start_address = linker_main(args, exe_to_load); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 780 | |
Elliott Hughes | 90f96b9 | 2019-05-09 15:56:39 -0700 | [diff] [blame] | 781 | if (g_is_ldd) _exit(EXIT_SUCCESS); |
| 782 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 783 | INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address)); |
| 784 | |
| 785 | // Return the address that the calling assembly stub should jump to. |
| 786 | return start_address; |
| 787 | } |