xref: /aosp_15_r20/external/musl/android/ldso_trampoline_phdr.h (revision c9945492fdd68bbe62686c5b452b4dc1be3f8453)
1*c9945492SAndroid Build Coastguard Worker /*
2*c9945492SAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*c9945492SAndroid Build Coastguard Worker  * All rights reserved.
4*c9945492SAndroid Build Coastguard Worker  *
5*c9945492SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*c9945492SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
7*c9945492SAndroid Build Coastguard Worker  * are met:
8*c9945492SAndroid Build Coastguard Worker  *  * Redistributions of source code must retain the above copyright
9*c9945492SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*c9945492SAndroid Build Coastguard Worker  *  * Redistributions in binary form must reproduce the above copyright
11*c9945492SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in
12*c9945492SAndroid Build Coastguard Worker  *    the documentation and/or other materials provided with the
13*c9945492SAndroid Build Coastguard Worker  *    distribution.
14*c9945492SAndroid Build Coastguard Worker  *
15*c9945492SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*c9945492SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*c9945492SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*c9945492SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*c9945492SAndroid Build Coastguard Worker  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*c9945492SAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*c9945492SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*c9945492SAndroid Build Coastguard Worker  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*c9945492SAndroid Build Coastguard Worker  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*c9945492SAndroid Build Coastguard Worker  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*c9945492SAndroid Build Coastguard Worker  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*c9945492SAndroid Build Coastguard Worker  * SUCH DAMAGE.
27*c9945492SAndroid Build Coastguard Worker  */
28*c9945492SAndroid Build Coastguard Worker 
29*c9945492SAndroid Build Coastguard Worker /* Find the load bias (difference between address and p_vaddr) of an
30*c9945492SAndroid Build Coastguard Worker  * executable or shared object loaded by the kernel. The ELF file's
31*c9945492SAndroid Build Coastguard Worker  * PHDR table must have a PT_PHDR entry.  A VDSO doesn't have a PT_PHDR
32*c9945492SAndroid Build Coastguard Worker  * entry in its PHDR table.
33*c9945492SAndroid Build Coastguard Worker  */
34*c9945492SAndroid Build Coastguard Worker static inline ElfW(Addr)
get_elf_load_bias_from_phdr(const ElfW (Phdr)* phdr_table,size_t phdr_count)35*c9945492SAndroid Build Coastguard Worker     get_elf_load_bias_from_phdr(const ElfW(Phdr) * phdr_table, size_t phdr_count) {
36*c9945492SAndroid Build Coastguard Worker   for (size_t i = 0; i < phdr_count; ++i) {
37*c9945492SAndroid Build Coastguard Worker     if (phdr_table[i].p_type == PT_PHDR) {
38*c9945492SAndroid Build Coastguard Worker       return reinterpret_cast<ElfW(Addr)>(phdr_table) - phdr_table[i].p_vaddr;
39*c9945492SAndroid Build Coastguard Worker     }
40*c9945492SAndroid Build Coastguard Worker   }
41*c9945492SAndroid Build Coastguard Worker   return 0;
42*c9945492SAndroid Build Coastguard Worker }
43*c9945492SAndroid Build Coastguard Worker 
44*c9945492SAndroid Build Coastguard Worker /* Copy the phdr to a new location.  Update the PT_PHDR section to point to the
45*c9945492SAndroid Build Coastguard Worker  * new location.
46*c9945492SAndroid Build Coastguard Worker  */
copy_phdr(ElfW (Phdr)* phdr_from,ElfW (Phdr)* phdr_to,size_t count,ElfW (Addr)load_bias)47*c9945492SAndroid Build Coastguard Worker static inline void copy_phdr(ElfW(Phdr) * phdr_from, ElfW(Phdr) * phdr_to, size_t count,
48*c9945492SAndroid Build Coastguard Worker                              ElfW(Addr) load_bias) {
49*c9945492SAndroid Build Coastguard Worker   ElfW(Phdr)* pt_phdr = nullptr;       // The phdr entry with type PT_PHDR.
50*c9945492SAndroid Build Coastguard Worker   ElfW(Phdr)* phdr_to_phdr = nullptr;  // The phdr entry for the load segment that contains phdr_to.
51*c9945492SAndroid Build Coastguard Worker   ElfW(Phdr)* p = phdr_to;
52*c9945492SAndroid Build Coastguard Worker 
53*c9945492SAndroid Build Coastguard Worker   // The ELF vaddr of phdr_to.
54*c9945492SAndroid Build Coastguard Worker   ElfW(Addr) phdr_to_vaddr = reinterpret_cast<ElfW(Addr)>(phdr_to) - load_bias;
55*c9945492SAndroid Build Coastguard Worker 
56*c9945492SAndroid Build Coastguard Worker   for (size_t i = 0; i < count; ++i, ++p) {
57*c9945492SAndroid Build Coastguard Worker     // Assign each member to avoid the struct assignment being turned into a memcpy.
58*c9945492SAndroid Build Coastguard Worker     p->p_type = phdr_from[i].p_type;
59*c9945492SAndroid Build Coastguard Worker     p->p_offset = phdr_from[i].p_offset;
60*c9945492SAndroid Build Coastguard Worker     p->p_vaddr = phdr_from[i].p_vaddr;
61*c9945492SAndroid Build Coastguard Worker     p->p_paddr = phdr_from[i].p_paddr;
62*c9945492SAndroid Build Coastguard Worker     p->p_filesz = phdr_from[i].p_filesz;
63*c9945492SAndroid Build Coastguard Worker     p->p_memsz = phdr_from[i].p_memsz;
64*c9945492SAndroid Build Coastguard Worker     p->p_flags = phdr_from[i].p_flags;
65*c9945492SAndroid Build Coastguard Worker     p->p_align = phdr_from[i].p_align;
66*c9945492SAndroid Build Coastguard Worker 
67*c9945492SAndroid Build Coastguard Worker     if (p->p_type == PT_PHDR) pt_phdr = p;
68*c9945492SAndroid Build Coastguard Worker     if (p->p_vaddr <= phdr_to_vaddr && p->p_vaddr + p->p_memsz > phdr_to_vaddr) phdr_to_phdr = p;
69*c9945492SAndroid Build Coastguard Worker   }
70*c9945492SAndroid Build Coastguard Worker 
71*c9945492SAndroid Build Coastguard Worker   if (pt_phdr != nullptr && phdr_to_phdr != nullptr) {
72*c9945492SAndroid Build Coastguard Worker     pt_phdr->p_vaddr = reinterpret_cast<ElfW(Addr)>(phdr_to) - load_bias;
73*c9945492SAndroid Build Coastguard Worker     pt_phdr->p_paddr = pt_phdr->p_vaddr;
74*c9945492SAndroid Build Coastguard Worker     pt_phdr->p_offset = phdr_to_phdr->p_offset + (pt_phdr->p_vaddr - phdr_to_phdr->p_vaddr);
75*c9945492SAndroid Build Coastguard Worker   }
76*c9945492SAndroid Build Coastguard Worker }
77*c9945492SAndroid Build Coastguard Worker 
78*c9945492SAndroid Build Coastguard Worker /* Trim a section to the given start and end.
79*c9945492SAndroid Build Coastguard Worker  */
phdr_trim_segment(ElfW (Phdr)* phdr,ElfW (Addr)start,ElfW (Addr)end)80*c9945492SAndroid Build Coastguard Worker static inline void phdr_trim_segment(ElfW(Phdr) * phdr, ElfW(Addr) start, ElfW(Addr) end) {
81*c9945492SAndroid Build Coastguard Worker   const ElfW(Addr) shift = start - phdr->p_vaddr;
82*c9945492SAndroid Build Coastguard Worker   phdr->p_vaddr = start;
83*c9945492SAndroid Build Coastguard Worker   phdr->p_paddr = start;
84*c9945492SAndroid Build Coastguard Worker   phdr->p_memsz = end - start;
85*c9945492SAndroid Build Coastguard Worker   if (shift > 0) {
86*c9945492SAndroid Build Coastguard Worker     phdr->p_offset += shift;
87*c9945492SAndroid Build Coastguard Worker     phdr->p_filesz = (shift > phdr->p_filesz) ? 0 : (phdr->p_filesz - shift);
88*c9945492SAndroid Build Coastguard Worker   }
89*c9945492SAndroid Build Coastguard Worker   if (phdr->p_filesz > end - start) {
90*c9945492SAndroid Build Coastguard Worker     phdr->p_filesz = end - start;
91*c9945492SAndroid Build Coastguard Worker   }
92*c9945492SAndroid Build Coastguard Worker }
93*c9945492SAndroid Build Coastguard Worker 
94*c9945492SAndroid Build Coastguard Worker /* Trim load sections that overlap with the embedded linker, and replace load sections
95*c9945492SAndroid Build Coastguard Worker  * that are entirely contained within the embedded linker with PT_NULL.
96*c9945492SAndroid Build Coastguard Worker  */
phdr_trim_embedded_linker(ElfW (Phdr)* phdr,size_t phdr_count,ElfW (Off)linker_start,ElfW (Off)linker_end)97*c9945492SAndroid Build Coastguard Worker static inline void phdr_trim_embedded_linker(ElfW(Phdr) * phdr, size_t phdr_count,
98*c9945492SAndroid Build Coastguard Worker                                              ElfW(Off) linker_start, ElfW(Off) linker_end) {
99*c9945492SAndroid Build Coastguard Worker   for (size_t i = 0; i < phdr_count; ++i, ++phdr) {
100*c9945492SAndroid Build Coastguard Worker     if (phdr->p_type != PT_LOAD) continue;
101*c9945492SAndroid Build Coastguard Worker 
102*c9945492SAndroid Build Coastguard Worker     ElfW(Addr) start = phdr->p_vaddr;
103*c9945492SAndroid Build Coastguard Worker     ElfW(Addr) end = phdr->p_vaddr + phdr->p_memsz;
104*c9945492SAndroid Build Coastguard Worker 
105*c9945492SAndroid Build Coastguard Worker     // A segment that surrounds an embedded linker segment is not supported;
106*c9945492SAndroid Build Coastguard Worker     if (start < linker_start && end > linker_end) __builtin_trap();
107*c9945492SAndroid Build Coastguard Worker 
108*c9945492SAndroid Build Coastguard Worker     // Handle a segment that overlaps the beginning of the embedded linker;
109*c9945492SAndroid Build Coastguard Worker     if (start < linker_start && end > linker_start) end = linker_start;
110*c9945492SAndroid Build Coastguard Worker 
111*c9945492SAndroid Build Coastguard Worker     // Handle a segment that overlaps the end of the embedded linker;
112*c9945492SAndroid Build Coastguard Worker     if (start < linker_end && end > linker_end) start = linker_end;
113*c9945492SAndroid Build Coastguard Worker 
114*c9945492SAndroid Build Coastguard Worker     if (start < end && (start < linker_start || end > linker_end)) {
115*c9945492SAndroid Build Coastguard Worker       // The segment is still needed, trim it.
116*c9945492SAndroid Build Coastguard Worker       phdr_trim_segment(phdr, start, end);
117*c9945492SAndroid Build Coastguard Worker     } else {
118*c9945492SAndroid Build Coastguard Worker       // The segment is not needed, replace it with PT_NULL to avoid having
119*c9945492SAndroid Build Coastguard Worker       // to move the following segments in the phdr.
120*c9945492SAndroid Build Coastguard Worker       phdr->p_type = PT_NULL;
121*c9945492SAndroid Build Coastguard Worker     }
122*c9945492SAndroid Build Coastguard Worker   }
123*c9945492SAndroid Build Coastguard Worker }
124