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