xref: /aosp_15_r20/external/musl/android/ldso_trampoline.cpp (revision c9945492fdd68bbe62686c5b452b4dc1be3f8453)
1 /*
2  * Copyright (C) 2021 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 #define START "__dlwrap__start"
30 
31 #include "crt_arch.h"
32 
33 #include <elf.h>
34 #include <link.h>
35 #include "reloc.h"
36 
37 #define AUX_CNT 32
38 
39 #include "ldso_trampoline_phdr.h"
40 
41 // The location of the embedded linker in the executable.
42 // Hidden visibility is used to get a pc-relative reference instead
43 // of a GOT reference, which isn't available when this code runs.
44 __attribute__((visibility("hidden"))) extern const char __dlwrap_linker;
45 __attribute__((visibility("hidden"))) extern const char __dlwrap_linker_end;
46 
47 // The real entry point of the binary to use after linker bootstrapping.
48 __attribute__((visibility("hidden"))) extern "C" void _start();
49 
50 // Allocate some R/W memory to store a copy of the program headers.
51 static ElfW(Phdr) phdr_copy[64];
52 
get_auxv(size_t * auxv,size_t entry)53 static size_t get_auxv(size_t* auxv, size_t entry) {
54   for (size_t i = 0; auxv[i]; i += 2)
55     if (auxv[i] == entry) return auxv[i + 1];
56   return 0;
57 }
58 
set_auxv(size_t * auxv,size_t entry,size_t value)59 static void set_auxv(size_t* auxv, size_t entry, size_t value) {
60   for (size_t i = 0; auxv[i]; i += 2) {
61     if (auxv[i] == entry) {
62       auxv[i + 1] = value;
63       return;
64     }
65   }
66   __builtin_trap();
67 }
68 
69 /*
70  * This is the entry point for the linker wrapper, which finds
71  * the real linker, then bootstraps into it.
72  */
__dlwrap__start_c(size_t * sp)73 extern "C" void __dlwrap__start_c(size_t* sp) {
74   size_t i, aux[AUX_CNT];
75 
76   int argc = *sp;
77   char** argv = reinterpret_cast<char**>(sp + 1);
78 
79   for (i = argc + 1; argv[i]; i++)
80     ;
81   size_t* auxv = reinterpret_cast<size_t*>(argv + i + 1);
82 
83   for (i = 0; i < AUX_CNT; i++) aux[i] = 0;
84   for (i = 0; auxv[i]; i += 2)
85     if (auxv[i] < AUX_CNT) aux[auxv[i]] = auxv[i + 1];
86 
87   ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(get_auxv(auxv, AT_PHDR));
88   size_t phdr_count = get_auxv(auxv, AT_PHNUM);
89   ElfW(Addr) load_bias = get_elf_load_bias_from_phdr(phdr, phdr_count);
90 
91   ElfW(Addr) linker_addr = reinterpret_cast<ElfW(Addr)>(&__dlwrap_linker);
92   ElfW(Addr) linker_size = static_cast<ElfW(Addr)>(&__dlwrap_linker_end - &__dlwrap_linker);
93   ElfW(Addr) linker_vaddr = linker_addr - load_bias;
94   ElfW(Addr) linker_entry_offset = reinterpret_cast<ElfW(Ehdr)*>(linker_addr)->e_entry;
95 
96   // Make a copy of the ELF program headers that does not contain the load
97   // segments for the embedded linker.  The embedded linker contains its
98   // own copy of its load segments, which causes problems if musl uses
99   // both sets of load segments when donating unused space to the heap.
100   if (phdr_count > sizeof(phdr_copy) / sizeof(phdr_copy[0])) __builtin_trap();
101   copy_phdr(phdr, phdr_copy, phdr_count, load_bias);
102   phdr_trim_embedded_linker(phdr_copy, phdr_count, linker_vaddr, linker_vaddr + linker_size);
103 
104   // Set AT_BASE to the embedded linker
105   set_auxv(auxv, AT_BASE, linker_addr);
106   // Set AT_ENTRY to the proper entry point
107   set_auxv(auxv, AT_ENTRY, reinterpret_cast<ElfW(Addr)>(&_start));
108   // Set AT_PHDR to the copied program headers
109   set_auxv(auxv, AT_PHDR, reinterpret_cast<ElfW(Addr)>(&phdr_copy));
110 
111   // Jump to linker entry point
112   CRTJMP(linker_addr + linker_entry_offset, sp);
113 }
114