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