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