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 | |
| 31 | #include "linker_debug.h" |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 32 | #include "linker_cfi.h" |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 33 | #include "linker_gdb_support.h" |
| 34 | #include "linker_globals.h" |
| 35 | #include "linker_phdr.h" |
| 36 | #include "linker_utils.h" |
| 37 | |
| 38 | #include "private/bionic_globals.h" |
| 39 | #include "private/bionic_tls.h" |
| 40 | #include "private/KernelArgumentBlock.h" |
| 41 | |
| 42 | #include "android-base/strings.h" |
| 43 | #include "android-base/stringprintf.h" |
Dan Willemsen | 7ec52b1 | 2016-11-28 17:02:25 -0800 | [diff] [blame] | 44 | #ifdef __ANDROID__ |
Josh Gao | 2a3b4fa | 2016-10-26 17:55:49 -0700 | [diff] [blame] | 45 | #include "debuggerd/handler.h" |
Dan Willemsen | 7ec52b1 | 2016-11-28 17:02:25 -0800 | [diff] [blame] | 46 | #endif |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 47 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 48 | #include <async_safe/log.h> |
| 49 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 50 | #include <vector> |
| 51 | |
| 52 | extern void __libc_init_globals(KernelArgumentBlock&); |
| 53 | extern void __libc_init_AT_SECURE(KernelArgumentBlock&); |
| 54 | |
| 55 | extern "C" void _start(); |
| 56 | |
| 57 | static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf); |
| 58 | |
| 59 | // These should be preserved static to avoid emitting |
| 60 | // RELATIVE relocations for the part of the code running |
| 61 | // before linker links itself. |
| 62 | |
| 63 | // TODO (dimtiry): remove somain, rename solist to solist_head |
| 64 | static soinfo* solist; |
| 65 | static soinfo* sonext; |
| 66 | static soinfo* somain; // main process, always the one after libdl_info |
dimitry | 8b14256 | 2018-05-09 15:22:38 +0200 | [diff] [blame] | 67 | static soinfo* vdso; // vdso if present |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 68 | |
| 69 | void solist_add_soinfo(soinfo* si) { |
| 70 | sonext->next = si; |
| 71 | sonext = si; |
| 72 | } |
| 73 | |
| 74 | bool solist_remove_soinfo(soinfo* si) { |
| 75 | soinfo *prev = nullptr, *trav; |
| 76 | for (trav = solist; trav != nullptr; trav = trav->next) { |
| 77 | if (trav == si) { |
| 78 | break; |
| 79 | } |
| 80 | prev = trav; |
| 81 | } |
| 82 | |
| 83 | if (trav == nullptr) { |
| 84 | // si was not in solist |
| 85 | PRINT("name \"%s\"@%p is not in solist!", si->get_realpath(), si); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | // prev will never be null, because the first entry in solist is |
| 90 | // always the static libdl_info. |
George Burgess IV | 7059100 | 2017-06-27 16:23:45 -0700 | [diff] [blame] | 91 | CHECK(prev != nullptr); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 92 | prev->next = si->next; |
| 93 | if (si == sonext) { |
| 94 | sonext = prev; |
| 95 | } |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | soinfo* solist_get_head() { |
| 101 | return solist; |
| 102 | } |
| 103 | |
| 104 | soinfo* solist_get_somain() { |
| 105 | return somain; |
| 106 | } |
| 107 | |
dimitry | 8b14256 | 2018-05-09 15:22:38 +0200 | [diff] [blame] | 108 | soinfo* solist_get_vdso() { |
| 109 | return vdso; |
| 110 | } |
| 111 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 112 | int g_ld_debug_verbosity; |
| 113 | abort_msg_t* g_abort_message = nullptr; // For debuggerd. |
| 114 | |
| 115 | static std::vector<std::string> g_ld_preload_names; |
| 116 | |
| 117 | static std::vector<soinfo*> g_ld_preloads; |
| 118 | |
| 119 | static void parse_path(const char* path, const char* delimiters, |
| 120 | std::vector<std::string>* resolved_paths) { |
| 121 | std::vector<std::string> paths; |
| 122 | split_path(path, delimiters, &paths); |
| 123 | resolve_paths(paths, resolved_paths); |
| 124 | } |
| 125 | |
| 126 | static void parse_LD_LIBRARY_PATH(const char* path) { |
| 127 | std::vector<std::string> ld_libary_paths; |
| 128 | parse_path(path, ":", &ld_libary_paths); |
| 129 | g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths)); |
| 130 | } |
| 131 | |
| 132 | static void parse_LD_PRELOAD(const char* path) { |
| 133 | g_ld_preload_names.clear(); |
| 134 | if (path != nullptr) { |
| 135 | // We have historically supported ':' as well as ' ' in LD_PRELOAD. |
| 136 | g_ld_preload_names = android::base::Split(path, " :"); |
Josh Gao | 44f6e18 | 2017-10-18 17:25:24 -0700 | [diff] [blame] | 137 | 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] | 138 | [](const std::string& s) { return s.empty(); }), |
| 139 | g_ld_preload_names.end()); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | // An empty list of soinfos |
| 144 | static soinfo_list_t g_empty_list; |
| 145 | |
dimitry | f9abbf6 | 2017-07-06 12:17:14 +0200 | [diff] [blame] | 146 | static void add_vdso(KernelArgumentBlock& args) { |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 147 | ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR)); |
| 148 | if (ehdr_vdso == nullptr) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0); |
| 153 | |
| 154 | si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff); |
| 155 | si->phnum = ehdr_vdso->e_phnum; |
| 156 | si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso); |
| 157 | si->size = phdr_table_get_load_size(si->phdr, si->phnum); |
| 158 | si->load_bias = get_elf_exec_load_bias(ehdr_vdso); |
| 159 | |
| 160 | si->prelink_image(); |
| 161 | si->link_image(g_empty_list, soinfo_list_t::make_list(si), nullptr); |
dimitry | c18de1b | 2017-09-26 14:31:35 +0200 | [diff] [blame] | 162 | // prevents accidental unloads... |
| 163 | si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_NODELETE); |
| 164 | si->set_linked(); |
| 165 | si->call_constructors(); |
dimitry | 8b14256 | 2018-05-09 15:22:38 +0200 | [diff] [blame] | 166 | |
| 167 | vdso = si; |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* gdb expects the linker to be in the debug shared object list. |
| 171 | * Without this, gdb has trouble locating the linker's ".text" |
| 172 | * and ".plt" sections. Gdb could also potentially use this to |
| 173 | * relocate the offset of our exported 'rtld_db_dlactivity' symbol. |
| 174 | * Note that the linker shouldn't be on the soinfo list. |
| 175 | */ |
Dimitry Ivanov | cd510cb | 2017-05-31 15:07:41 -0700 | [diff] [blame] | 176 | static link_map linker_link_map; |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 177 | |
Dimitry Ivanov | cd510cb | 2017-05-31 15:07:41 -0700 | [diff] [blame] | 178 | static void init_linker_info_for_gdb(ElfW(Addr) linker_base, char* linker_path) { |
| 179 | linker_link_map.l_addr = linker_base; |
| 180 | linker_link_map.l_name = linker_path; |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 181 | |
| 182 | /* |
| 183 | * Set the dynamic field in the link map otherwise gdb will complain with |
| 184 | * the following: |
| 185 | * warning: .dynamic section for "/system/bin/linker" is not at the |
| 186 | * expected address (wrong library or version mismatch?) |
| 187 | */ |
| 188 | ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base); |
| 189 | ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff); |
| 190 | phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base, |
Dimitry Ivanov | cd510cb | 2017-05-31 15:07:41 -0700 | [diff] [blame] | 191 | &linker_link_map.l_ld, nullptr); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 192 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | extern "C" int __system_properties_init(void); |
| 196 | |
| 197 | static const char* get_executable_path() { |
| 198 | static std::string executable_path; |
| 199 | if (executable_path.empty()) { |
Jiyong Park | 31cd08f | 2018-06-01 19:18:56 +0900 | [diff] [blame^] | 200 | if (!is_init()) { |
| 201 | char path[PATH_MAX]; |
| 202 | ssize_t path_len = readlink("/proc/self/exe", path, sizeof(path)); |
| 203 | if (path_len == -1 || path_len >= static_cast<ssize_t>(sizeof(path))) { |
| 204 | async_safe_fatal("readlink('/proc/self/exe') failed: %s", strerror(errno)); |
| 205 | } |
| 206 | executable_path = std::string(path, path_len); |
| 207 | } else { |
| 208 | executable_path = "/init"; |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 209 | } |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | return executable_path.c_str(); |
| 213 | } |
| 214 | |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 215 | #if defined(__LP64__) |
| 216 | static char kLinkerPath[] = "/system/bin/linker64"; |
| 217 | #else |
| 218 | static char kLinkerPath[] = "/system/bin/linker"; |
| 219 | #endif |
| 220 | |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 221 | static void __linker_cannot_link(const char* argv0) { |
dimitry | 04f7a79 | 2017-09-29 11:52:17 +0200 | [diff] [blame] | 222 | async_safe_format_fd(STDERR_FILENO, |
| 223 | "CANNOT LINK EXECUTABLE \"%s\": %s\n", |
| 224 | argv0, |
| 225 | linker_get_error_buffer()); |
| 226 | |
| 227 | async_safe_format_log(ANDROID_LOG_FATAL, |
| 228 | "linker", |
| 229 | "CANNOT LINK EXECUTABLE \"%s\": %s", |
| 230 | argv0, |
| 231 | linker_get_error_buffer()); |
| 232 | _exit(EXIT_FAILURE); |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Ryan Prichard | 742982d | 2018-05-30 22:32:17 -0700 | [diff] [blame] | 235 | static ElfW(Addr) linker_main(KernelArgumentBlock& args) { |
Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 236 | ProtectedDataGuard guard; |
| 237 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 238 | #if TIMING |
| 239 | struct timeval t0, t1; |
| 240 | gettimeofday(&t0, 0); |
| 241 | #endif |
| 242 | |
| 243 | // Sanitize the environment. |
| 244 | __libc_init_AT_SECURE(args); |
| 245 | |
| 246 | // Initialize system properties |
| 247 | __system_properties_init(); // may use 'environ' |
| 248 | |
| 249 | // Register the debuggerd signal handler. |
Dan Willemsen | 7ec52b1 | 2016-11-28 17:02:25 -0800 | [diff] [blame] | 250 | #ifdef __ANDROID__ |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 251 | debuggerd_callbacks_t callbacks = { |
| 252 | .get_abort_message = []() { |
| 253 | return g_abort_message; |
| 254 | }, |
| 255 | .post_dump = ¬ify_gdb_of_libraries, |
| 256 | }; |
| 257 | debuggerd_init(&callbacks); |
Dan Willemsen | 7ec52b1 | 2016-11-28 17:02:25 -0800 | [diff] [blame] | 258 | #endif |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 259 | |
| 260 | g_linker_logger.ResetState(); |
| 261 | |
| 262 | // Get a few environment variables. |
| 263 | const char* LD_DEBUG = getenv("LD_DEBUG"); |
| 264 | if (LD_DEBUG != nullptr) { |
| 265 | g_ld_debug_verbosity = atoi(LD_DEBUG); |
| 266 | } |
| 267 | |
| 268 | #if defined(__LP64__) |
| 269 | INFO("[ Android dynamic linker (64-bit) ]"); |
| 270 | #else |
| 271 | INFO("[ Android dynamic linker (32-bit) ]"); |
| 272 | #endif |
| 273 | |
| 274 | // These should have been sanitized by __libc_init_AT_SECURE, but the test |
| 275 | // doesn't cost us anything. |
| 276 | const char* ldpath_env = nullptr; |
| 277 | const char* ldpreload_env = nullptr; |
| 278 | if (!getauxval(AT_SECURE)) { |
| 279 | ldpath_env = getenv("LD_LIBRARY_PATH"); |
| 280 | if (ldpath_env != nullptr) { |
| 281 | INFO("[ LD_LIBRARY_PATH set to \"%s\" ]", ldpath_env); |
| 282 | } |
| 283 | ldpreload_env = getenv("LD_PRELOAD"); |
| 284 | if (ldpreload_env != nullptr) { |
| 285 | INFO("[ LD_PRELOAD set to \"%s\" ]", ldpreload_env); |
| 286 | } |
| 287 | } |
| 288 | |
dimitry | 8b14256 | 2018-05-09 15:22:38 +0200 | [diff] [blame] | 289 | add_vdso(args); |
| 290 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 291 | struct stat file_stat; |
| 292 | // Stat "/proc/self/exe" instead of executable_path because |
| 293 | // the executable could be unlinked by this point and it should |
| 294 | // not cause a crash (see http://b/31084669) |
Jiyong Park | 31cd08f | 2018-06-01 19:18:56 +0900 | [diff] [blame^] | 295 | if (!is_init()) { |
| 296 | if (TEMP_FAILURE_RETRY(stat("/proc/self/exe", &file_stat)) != 0) { |
| 297 | async_safe_fatal("unable to stat \"/proc/self/exe\": %s", strerror(errno)); |
| 298 | } |
| 299 | } else { |
| 300 | // /proc fs is not mounted when init starts. Therefore we can't use |
| 301 | // /proc/self/exe for init. |
| 302 | stat("/init", &file_stat); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | const char* executable_path = get_executable_path(); |
| 306 | soinfo* si = soinfo_alloc(&g_default_namespace, executable_path, &file_stat, 0, RTLD_GLOBAL); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 307 | |
Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 308 | // Bootstrap the link map, the main exe always needs to be first. |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 309 | si->set_main_executable(); |
| 310 | link_map* map = &(si->link_map_head); |
| 311 | |
| 312 | // Register the main executable and the linker upfront to have |
| 313 | // gdb aware of them before loading the rest of the dependency |
| 314 | // tree. |
| 315 | map->l_addr = 0; |
| 316 | map->l_name = const_cast<char*>(executable_path); |
| 317 | insert_link_map_into_debug_map(map); |
Dimitry Ivanov | cd510cb | 2017-05-31 15:07:41 -0700 | [diff] [blame] | 318 | insert_link_map_into_debug_map(&linker_link_map); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 319 | |
| 320 | // Extract information passed from the kernel. |
| 321 | si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR)); |
| 322 | si->phnum = args.getauxval(AT_PHNUM); |
| 323 | |
| 324 | /* Compute the value of si->base. We can't rely on the fact that |
| 325 | * the first entry is the PHDR because this will not be true |
| 326 | * for certain executables (e.g. some in the NDK unit test suite) |
| 327 | */ |
| 328 | si->base = 0; |
| 329 | si->size = phdr_table_get_load_size(si->phdr, si->phnum); |
| 330 | si->load_bias = 0; |
| 331 | for (size_t i = 0; i < si->phnum; ++i) { |
| 332 | if (si->phdr[i].p_type == PT_PHDR) { |
| 333 | si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr; |
| 334 | si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset; |
| 335 | break; |
| 336 | } |
| 337 | } |
George Burgess IV | 7059100 | 2017-06-27 16:23:45 -0700 | [diff] [blame] | 338 | |
| 339 | if (si->base == 0) { |
| 340 | async_safe_fatal("Could not find a PHDR: broken executable?"); |
| 341 | } |
| 342 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 343 | si->dynamic = nullptr; |
| 344 | |
| 345 | ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base); |
Elliott Hughes | 3bdb31b | 2017-01-07 10:38:20 -0800 | [diff] [blame] | 346 | |
| 347 | // We haven't supported non-PIE since Lollipop for security reasons. |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 348 | if (elf_hdr->e_type != ET_DYN) { |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 349 | // We don't use async_safe_fatal here because we don't want a tombstone: |
| 350 | // even after several years we still find ourselves on app compatibility |
Elliott Hughes | 3bdb31b | 2017-01-07 10:38:20 -0800 | [diff] [blame] | 351 | // investigations because some app's trying to launch an executable that |
| 352 | // hasn't worked in at least three years, and we've "helpfully" dropped a |
| 353 | // tombstone for them. The tombstone never provided any detail relevant to |
| 354 | // fixing the problem anyway, and the utility of drawing extra attention |
| 355 | // to the problem is non-existent at this late date. |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 356 | async_safe_format_fd(STDERR_FILENO, |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 357 | "\"%s\": error: Android 5.0 and later only support " |
| 358 | "position-independent executables (-fPIE).\n", |
| 359 | g_argv[0]); |
Dan Albert | 95e2e6f | 2017-01-31 16:02:43 -0800 | [diff] [blame] | 360 | exit(EXIT_FAILURE); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid). |
| 364 | parse_LD_LIBRARY_PATH(ldpath_env); |
| 365 | parse_LD_PRELOAD(ldpreload_env); |
| 366 | |
| 367 | somain = si; |
| 368 | |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 369 | std::vector<android_namespace_t*> namespaces = init_default_namespaces(executable_path); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 370 | |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 371 | if (!si->prelink_image()) __linker_cannot_link(g_argv[0]); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 372 | |
| 373 | // add somain to global group |
| 374 | 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] | 375 | // ... and add it to all other linked namespaces |
| 376 | for (auto linked_ns : namespaces) { |
| 377 | if (linked_ns != &g_default_namespace) { |
| 378 | linked_ns->add_soinfo(somain); |
| 379 | somain->add_secondary_namespace(linked_ns); |
| 380 | } |
| 381 | } |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 382 | |
| 383 | // Load ld_preloads and dependencies. |
| 384 | std::vector<const char*> needed_library_name_list; |
| 385 | size_t ld_preloads_count = 0; |
| 386 | |
| 387 | for (const auto& ld_preload_name : g_ld_preload_names) { |
| 388 | needed_library_name_list.push_back(ld_preload_name.c_str()); |
| 389 | ++ld_preloads_count; |
| 390 | } |
| 391 | |
| 392 | for_each_dt_needed(si, [&](const char* name) { |
| 393 | needed_library_name_list.push_back(name); |
| 394 | }); |
| 395 | |
| 396 | const char** needed_library_names = &needed_library_name_list[0]; |
| 397 | size_t needed_libraries_count = needed_library_name_list.size(); |
| 398 | |
| 399 | if (needed_libraries_count > 0 && |
Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 400 | !find_libraries(&g_default_namespace, |
| 401 | si, |
| 402 | needed_library_names, |
| 403 | needed_libraries_count, |
| 404 | nullptr, |
| 405 | &g_ld_preloads, |
| 406 | ld_preloads_count, |
| 407 | RTLD_GLOBAL, |
| 408 | nullptr, |
| 409 | true /* add_as_children */, |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 410 | true /* search_linked_namespaces */, |
Jiyong Park | 02586a2 | 2017-05-20 01:01:24 +0900 | [diff] [blame] | 411 | &namespaces)) { |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 412 | __linker_cannot_link(g_argv[0]); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 413 | } else if (needed_libraries_count == 0) { |
| 414 | if (!si->link_image(g_empty_list, soinfo_list_t::make_list(si), nullptr)) { |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 415 | __linker_cannot_link(g_argv[0]); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 416 | } |
| 417 | si->increment_ref_count(); |
| 418 | } |
| 419 | |
Elliott Hughes | ad2d038 | 2017-07-31 11:43:34 -0700 | [diff] [blame] | 420 | if (!get_cfi_shadow()->InitialLinkDone(solist)) __linker_cannot_link(g_argv[0]); |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 421 | |
Ryan Prichard | 6631f9b | 2018-04-30 18:29:32 -0700 | [diff] [blame] | 422 | // Store a pointer to the kernel argument block in a TLS slot to be |
| 423 | // picked up by the libc constructor. |
| 424 | __get_tls()[TLS_SLOT_BIONIC_PREINIT] = &args; |
| 425 | |
Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 426 | si->call_pre_init_constructors(); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 427 | |
Dimitry Ivanov | 4cabfaa | 2017-03-07 11:19:05 -0800 | [diff] [blame] | 428 | /* After the prelink_image, the si->load_bias is initialized. |
| 429 | * For so lib, the map->l_addr will be updated in notify_gdb_of_load. |
| 430 | * We need to update this value for so exe here. So Unwind_Backtrace |
| 431 | * for some arch like x86 could work correctly within so exe. |
| 432 | */ |
| 433 | map->l_addr = si->load_bias; |
| 434 | si->call_constructors(); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 435 | |
| 436 | #if TIMING |
| 437 | gettimeofday(&t1, nullptr); |
| 438 | PRINT("LINKER TIME: %s: %d microseconds", g_argv[0], (int) ( |
| 439 | (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) - |
| 440 | (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec))); |
| 441 | #endif |
| 442 | #if STATS |
| 443 | PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", g_argv[0], |
| 444 | linker_stats.count[kRelocAbsolute], |
| 445 | linker_stats.count[kRelocRelative], |
| 446 | linker_stats.count[kRelocCopy], |
| 447 | linker_stats.count[kRelocSymbol]); |
| 448 | #endif |
| 449 | #if COUNT_PAGES |
| 450 | { |
| 451 | unsigned n; |
| 452 | unsigned i; |
| 453 | unsigned count = 0; |
| 454 | for (n = 0; n < 4096; n++) { |
| 455 | if (bitmask[n]) { |
| 456 | unsigned x = bitmask[n]; |
| 457 | #if defined(__LP64__) |
| 458 | for (i = 0; i < 32; i++) { |
| 459 | #else |
| 460 | for (i = 0; i < 8; i++) { |
| 461 | #endif |
| 462 | if (x & 1) { |
| 463 | count++; |
| 464 | } |
| 465 | x >>= 1; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | PRINT("PAGES MODIFIED: %s: %d (%dKB)", g_argv[0], count, count * 4); |
| 470 | } |
| 471 | #endif |
| 472 | |
| 473 | #if TIMING || STATS || COUNT_PAGES |
| 474 | fflush(stdout); |
| 475 | #endif |
| 476 | |
| 477 | ElfW(Addr) entry = args.getauxval(AT_ENTRY); |
| 478 | TRACE("[ Ready to execute \"%s\" @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry)); |
| 479 | return entry; |
| 480 | } |
| 481 | |
| 482 | /* Compute the load-bias of an existing executable. This shall only |
| 483 | * be used to compute the load bias of an executable or shared library |
| 484 | * that was loaded by the kernel itself. |
| 485 | * |
| 486 | * Input: |
| 487 | * elf -> address of ELF header, assumed to be at the start of the file. |
| 488 | * Return: |
| 489 | * load bias, i.e. add the value of any p_vaddr in the file to get |
| 490 | * the corresponding address in memory. |
| 491 | */ |
| 492 | static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) { |
| 493 | ElfW(Addr) offset = elf->e_phoff; |
| 494 | const ElfW(Phdr)* phdr_table = |
| 495 | reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset); |
| 496 | const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum; |
| 497 | |
| 498 | for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) { |
| 499 | if (phdr->p_type == PT_LOAD) { |
| 500 | return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr; |
| 501 | } |
| 502 | } |
| 503 | return 0; |
| 504 | } |
| 505 | |
Ryan Prichard | 742982d | 2018-05-30 22:32:17 -0700 | [diff] [blame] | 506 | static ElfW(Addr) __attribute__((noinline)) |
| 507 | __linker_init_post_relocation(KernelArgumentBlock& args, |
| 508 | ElfW(Addr) linker_addr, |
| 509 | soinfo& linker_so); |
| 510 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 511 | /* |
| 512 | * This is the entry point for the linker, called from begin.S. This |
| 513 | * method is responsible for fixing the linker's own relocations, and |
| 514 | * then calling __linker_init_post_relocation(). |
| 515 | * |
| 516 | * Because this method is called before the linker has fixed it's own |
| 517 | * relocations, any attempt to reference an extern variable, extern |
| 518 | * function, or other GOT reference will generate a segfault. |
| 519 | */ |
| 520 | extern "C" ElfW(Addr) __linker_init(void* raw_args) { |
| 521 | KernelArgumentBlock args(raw_args); |
| 522 | |
Ryan Prichard | 27475b5 | 2018-05-17 17:14:18 -0700 | [diff] [blame] | 523 | #if defined(__i386__) |
| 524 | __libc_init_sysinfo(args); |
| 525 | #endif |
| 526 | |
Dimitry Ivanov | 9b1cc4b | 2017-03-23 16:17:15 -0700 | [diff] [blame] | 527 | // AT_BASE is set to 0 in the case when linker is run by iself |
| 528 | // so in order to link the linker it needs to calcuate AT_BASE |
| 529 | // using information at hand. The trick below takes advantage |
| 530 | // of the fact that the value of linktime_addr before relocations |
| 531 | // are run is an offset and this can be used to calculate AT_BASE. |
| 532 | static uintptr_t linktime_addr = reinterpret_cast<uintptr_t>(&linktime_addr); |
| 533 | ElfW(Addr) linker_addr = reinterpret_cast<uintptr_t>(&linktime_addr) - linktime_addr; |
| 534 | |
George Burgess IV | 7059100 | 2017-06-27 16:23:45 -0700 | [diff] [blame] | 535 | #if defined(__clang_analyzer__) |
| 536 | // The analyzer assumes that linker_addr will always be null. Make it an |
| 537 | // unknown value so we don't have to mark N places with NOLINTs. |
| 538 | // |
| 539 | // (`+=`, rather than `=`, allows us to sidestep a potential "unused store" |
| 540 | // complaint) |
| 541 | linker_addr += reinterpret_cast<uintptr_t>(raw_args); |
| 542 | #endif |
| 543 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 544 | ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr); |
| 545 | ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff); |
| 546 | |
| 547 | soinfo linker_so(nullptr, nullptr, nullptr, 0, 0); |
| 548 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 549 | linker_so.base = linker_addr; |
| 550 | linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum); |
| 551 | linker_so.load_bias = get_elf_exec_load_bias(elf_hdr); |
| 552 | linker_so.dynamic = nullptr; |
| 553 | linker_so.phdr = phdr; |
| 554 | linker_so.phnum = elf_hdr->e_phnum; |
| 555 | linker_so.set_linker_flag(); |
| 556 | |
| 557 | // Prelink the linker so we can access linker globals. |
| 558 | if (!linker_so.prelink_image()) __linker_cannot_link(args.argv[0]); |
| 559 | |
| 560 | // This might not be obvious... The reasons why we pass g_empty_list |
| 561 | // in place of local_group here are (1) we do not really need it, because |
| 562 | // linker is built with DT_SYMBOLIC and therefore relocates its symbols against |
| 563 | // itself without having to look into local_group and (2) allocators |
| 564 | // are not yet initialized, and therefore we cannot use linked_list.push_* |
| 565 | // functions at this point. |
| 566 | if (!linker_so.link_image(g_empty_list, g_empty_list, nullptr)) __linker_cannot_link(args.argv[0]); |
| 567 | |
Ryan Prichard | 742982d | 2018-05-30 22:32:17 -0700 | [diff] [blame] | 568 | return __linker_init_post_relocation(args, linker_addr, linker_so); |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * This code is called after the linker has linked itself and fixed its own |
| 573 | * GOT. It is safe to make references to externs and other non-local data at |
| 574 | * this point. The compiler sometimes moves GOT references earlier in a |
| 575 | * function, so avoid inlining this function (http://b/80503879). |
| 576 | */ |
| 577 | static ElfW(Addr) __attribute__((noinline)) |
| 578 | __linker_init_post_relocation(KernelArgumentBlock& args, |
| 579 | ElfW(Addr) linker_addr, |
| 580 | soinfo& linker_so) { |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 581 | // Initialize the main thread (including TLS, so system calls really work). |
| 582 | __libc_init_main_thread(args); |
| 583 | |
| 584 | // We didn't protect the linker's RELRO pages in link_image because we |
| 585 | // couldn't make system calls on x86 at that point, but we can now... |
| 586 | if (!linker_so.protect_relro()) __linker_cannot_link(args.argv[0]); |
| 587 | |
| 588 | // Initialize the linker's static libc's globals |
| 589 | __libc_init_globals(args); |
| 590 | |
| 591 | // store argc/argv/envp to use them for calling constructors |
| 592 | g_argc = args.argc; |
| 593 | g_argv = args.argv; |
| 594 | g_envp = args.envp; |
| 595 | |
| 596 | // Initialize the linker's own global variables |
| 597 | linker_so.call_constructors(); |
| 598 | |
Dimitry Ivanov | 9b1cc4b | 2017-03-23 16:17:15 -0700 | [diff] [blame] | 599 | // If the linker is not acting as PT_INTERP entry_point is equal to |
| 600 | // _start. Which means that the linker is running as an executable and |
| 601 | // already linked by PT_INTERP. |
| 602 | // |
| 603 | // This happens when user tries to run 'adb shell /system/bin/linker' |
| 604 | // see also https://code.google.com/p/android/issues/detail?id=63174 |
Ryan Prichard | 742982d | 2018-05-30 22:32:17 -0700 | [diff] [blame] | 605 | ElfW(Addr) entry_point = args.getauxval(AT_ENTRY); |
Dimitry Ivanov | 9b1cc4b | 2017-03-23 16:17:15 -0700 | [diff] [blame] | 606 | if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 607 | async_safe_format_fd(STDOUT_FILENO, |
Dimitry Ivanov | 9b1cc4b | 2017-03-23 16:17:15 -0700 | [diff] [blame] | 608 | "This is %s, the helper program for dynamic executables.\n", |
| 609 | args.argv[0]); |
| 610 | exit(0); |
| 611 | } |
| 612 | |
Dimitry Ivanov | cd510cb | 2017-05-31 15:07:41 -0700 | [diff] [blame] | 613 | init_linker_info_for_gdb(linker_addr, kLinkerPath); |
| 614 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 615 | // Initialize static variables. Note that in order to |
| 616 | // get correct libdl_info we need to call constructors |
| 617 | // before get_libdl_info(). |
dimitry | 7abea57 | 2017-08-29 18:14:49 +0200 | [diff] [blame] | 618 | sonext = solist = get_libdl_info(kLinkerPath, linker_so, linker_link_map); |
Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 619 | g_default_namespace.add_soinfo(solist); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 620 | |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 621 | args.abort_message_ptr = &g_abort_message; |
Ryan Prichard | 742982d | 2018-05-30 22:32:17 -0700 | [diff] [blame] | 622 | ElfW(Addr) start_address = linker_main(args); |
Dimitry Ivanov | 3f66057 | 2016-09-09 10:00:39 -0700 | [diff] [blame] | 623 | |
| 624 | INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address)); |
| 625 | |
| 626 | // Return the address that the calling assembly stub should jump to. |
| 627 | return start_address; |
| 628 | } |