• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 extern const char linker_offset;
32 
33 // This will be replaced by host_bionic_inject, but must be non-zero
34 // here so that it's placed in the data section.
35 uintptr_t original_start = 42;
36 
37 /* Find the load bias and base address of an executable or shared object loaded
38  * by the kernel. The ELF file's PHDR table must have a PT_PHDR entry.
39  *
40  * A VDSO doesn't have a PT_PHDR entry in its PHDR table.
41  */
get_elf_base_from_phdr(const ElfW (Phdr)* phdr_table,size_t phdr_count,ElfW (Addr)* base,ElfW (Addr)* load_bias)42 static void get_elf_base_from_phdr(const ElfW(Phdr)* phdr_table, size_t phdr_count,
43                                    ElfW(Addr)* base, ElfW(Addr)* load_bias) {
44   for (size_t i = 0; i < phdr_count; ++i) {
45     if (phdr_table[i].p_type == PT_PHDR) {
46       *load_bias = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_vaddr;
47       *base = reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_offset;
48       return;
49     }
50   }
51 }
52 
53 /*
54  * This is the entry point for the linker wrapper, which finds
55  * the real linker, then bootstraps into it.
56  */
__linker_init(void * raw_args)57 extern "C" ElfW(Addr) __linker_init(void* raw_args) {
58   KernelArgumentBlock args(raw_args);
59 
60   ElfW(Addr) base_addr = 0;
61   ElfW(Addr) load_bias = 0;
62   get_elf_base_from_phdr(
63     reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR)), args.getauxval(AT_PHNUM),
64     &base_addr, &load_bias);
65 
66   ElfW(Addr) linker_addr = base_addr + reinterpret_cast<uintptr_t>(&linker_offset);
67   ElfW(Addr) linker_entry_offset = reinterpret_cast<ElfW(Ehdr)*>(linker_addr)->e_entry;
68 
69   for (ElfW(auxv_t)* v = args.auxv; v->a_type != AT_NULL; ++v) {
70     if (v->a_type == AT_BASE) {
71       // Set AT_BASE to the embedded linker
72       v->a_un.a_val = linker_addr;
73     }
74     if (v->a_type == AT_ENTRY) {
75       // Set AT_ENTRY to the proper entry point
76       v->a_un.a_val = base_addr + original_start;
77     }
78   }
79 
80   // Return address of linker entry point
81   return linker_addr + linker_entry_offset;
82 }
83