1 /*
2 * Copyright (C) 2017 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 "private/KernelArgumentBlock.h"
30
31 // The offset from the linker's original program header load addresses to
32 // the load addresses when embedded into a binary. Set by the extract_linker
33 // tool.
34 extern const char __dlwrap_linker_offset;
35
36 // The real entry point of the binary to use after linker bootstrapping.
37 __LIBC_HIDDEN__ extern "C" void _start();
38
39 /* Find the load bias and base address of an executable or shared object loaded
40 * by the kernel. The ELF file's PHDR table must have a PT_PHDR entry.
41 *
42 * A VDSO doesn't have a PT_PHDR entry in its PHDR table.
43 */
get_elf_base_from_phdr(const ElfW (Phdr)* phdr_table,size_t phdr_count,ElfW (Addr)* base,ElfW (Addr)* load_bias)44 static void get_elf_base_from_phdr(const ElfW(Phdr)* phdr_table, size_t phdr_count,
45 ElfW(Addr)* base, ElfW(Addr)* load_bias) {
46 for (size_t i = 0; i < phdr_count; ++i) {
47 if (phdr_table[i].p_type == PT_PHDR) {
48 *load_bias = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_vaddr;
49 *base = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_offset;
50 return;
51 }
52 }
53 }
54
55 /*
56 * This is the entry point for the linker wrapper, which finds
57 * the real linker, then bootstraps into it.
58 */
__linker_init(void * raw_args)59 extern "C" ElfW(Addr) __linker_init(void* raw_args) {
60 KernelArgumentBlock args(raw_args);
61
62 ElfW(Addr) base_addr = 0;
63 ElfW(Addr) load_bias = 0;
64 get_elf_base_from_phdr(
65 reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR)), args.getauxval(AT_PHNUM),
66 &base_addr, &load_bias);
67
68 ElfW(Addr) linker_addr = base_addr + reinterpret_cast<uintptr_t>(&__dlwrap_linker_offset);
69 ElfW(Addr) linker_entry_offset = reinterpret_cast<ElfW(Ehdr)*>(linker_addr)->e_entry;
70
71 for (ElfW(auxv_t)* v = args.auxv; v->a_type != AT_NULL; ++v) {
72 if (v->a_type == AT_BASE) {
73 // Set AT_BASE to the embedded linker
74 v->a_un.a_val = linker_addr;
75 }
76 if (v->a_type == AT_ENTRY) {
77 // Set AT_ENTRY to the proper entry point
78 v->a_un.a_val = reinterpret_cast<ElfW(Addr)>(&_start);
79 }
80 }
81
82 // Return address of linker entry point
83 return linker_addr + linker_entry_offset;
84 }
85