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