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