1/*- 2 * Copyright (c) 2014 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Andrew Turner under 6 * sponsorship from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#include <machine/asm.h> 31ENTRY(.rtld_start) 32 .cfi_undefined x30 33 mov x19, x0 /* Put ps_strings in a callee-saved register */ 34 35 sub sp, sp, #16 /* Make room for obj_main & exit proc */ 36 .cfi_adjust_cfa_offset 16 37 38 mov x1, sp /* exit_proc */ 39 add x2, x1, #8 /* obj_main */ 40 bl _rtld /* Call the loader */ 41 mov x8, x0 /* Backup the entry point */ 42 ldp x2, x1, [sp], #16 /* Load cleanup, obj_main */ 43 .cfi_adjust_cfa_offset 0 44 45 mov x0, x19 /* Restore ps_strings */ 46 br x8 /* Jump to the entry point */ 47END(.rtld_start) 48 49/* 50 * sp + 0 = &GOT[x + 3] 51 * sp + 8 = RA 52 * x16 = &GOT[2] 53 * x17 = &_rtld_bind_start 54 */ 55ENTRY(_rtld_bind_start) 56 mov x17, sp 57 58 /* Save frame pointer and SP */ 59 stp x29, x30, [sp, #-16]! 60 mov x29, sp 61 .cfi_def_cfa x29, 16 62 .cfi_offset x30, -8 63 .cfi_offset x29, -16 64 65 /* Save the arguments */ 66 stp x0, x1, [sp, #-16]! 67 stp x2, x3, [sp, #-16]! 68 stp x4, x5, [sp, #-16]! 69 stp x6, x7, [sp, #-16]! 70 stp x8, xzr, [sp, #-16]! 71 72 /* Save any floating-point arguments */ 73 stp q0, q1, [sp, #-32]! 74 stp q2, q3, [sp, #-32]! 75 stp q4, q5, [sp, #-32]! 76 stp q6, q7, [sp, #-32]! 77 78 /* Calculate reloff */ 79 ldr x2, [x17, #0] /* Get the address of the entry */ 80 sub x1, x2, x16 /* Find its offset */ 81 sub x1, x1, #8 /* Adjust for x16 not being at offset 0 */ 82 /* Each rela item has 3 entriesso we need reloff = 3 * index */ 83 lsl x3, x1, #1 /* x3 = 2 * offset */ 84 add x1, x1, x3 /* x1 = x3 + offset = 3 * offset */ 85 86 /* Load obj */ 87 ldr x0, [x16, #-8] 88 89 /* Call into rtld */ 90 bl _rtld_bind 91 92 /* Backup the address to branch to */ 93 mov x16, x0 94 95 /* restore the arguments */ 96 ldp q6, q7, [sp], #32 97 ldp q4, q5, [sp], #32 98 ldp q2, q3, [sp], #32 99 ldp q0, q1, [sp], #32 100 ldp x8, xzr, [sp], #16 101 ldp x6, x7, [sp], #16 102 ldp x4, x5, [sp], #16 103 ldp x2, x3, [sp], #16 104 ldp x0, x1, [sp], #16 105 106 /* Restore frame pointer */ 107 ldp x29, xzr, [sp], #16 108 109 /* Restore link register saved by the plt code */ 110 ldp xzr, x30, [sp], #16 111 112 /* Call into the correct function */ 113 br x16 114END(_rtld_bind_start) 115 116/* 117 * struct rel_tlsdesc { 118 * uint64_t resolver_fnc; 119 * uint64_t resolver_arg; 120 * 121 * 122 * uint64_t _rtld_tlsdesc_static(struct rel_tlsdesc *); 123 * 124 * Resolver function for TLS symbols resolved at load time 125 */ 126ENTRY(_rtld_tlsdesc_static) 127 ldr x0, [x0, #8] 128 ret 129END(_rtld_tlsdesc_static) 130 131/* 132 * uint64_t _rtld_tlsdesc_undef(void); 133 * 134 * Resolver function for weak and undefined TLS symbols 135 */ 136ENTRY(_rtld_tlsdesc_undef) 137 str x1, [sp, #-16]! 138 .cfi_adjust_cfa_offset 16 139 140 mrs x1, tpidr_el0 141 ldr x0, [x0, #8] 142 sub x0, x0, x1 143 144 ldr x1, [sp], #16 145 .cfi_adjust_cfa_offset -16 146 ret 147END(_rtld_tlsdesc_undef) 148 149/* 150 * uint64_t _rtld_tlsdesc_dynamic(struct rel_tlsdesc *); 151 * 152 * Resolver function for TLS symbols from dlopen() 153 */ 154ENTRY(_rtld_tlsdesc_dynamic) 155 /* Save registers used in fast path */ 156 stp x1, x2, [sp, #(-2 * 16)]! 157 stp x3, x4, [sp, #(1 * 16)] 158 .cfi_adjust_cfa_offset 2 * 16 159 .cfi_rel_offset x1, 0 160 .cfi_rel_offset x2, 8 161 .cfi_rel_offset x3, 16 162 .cfi_rel_offset x4, 24 163 164 /* Test fastpath - inlined version of tls_get_addr_common(). */ 165 ldr x1, [x0, #8] /* tlsdesc ptr */ 166 mrs x4, tpidr_el0 167 ldr x0, [x4] /* DTV pointer */ 168 ldr x2, [x0] /* dtv[0] (generation count) */ 169 ldr x3, [x1] /* tlsdec->dtv_gen */ 170 cmp x2, x3 171 b.ne 1f /* dtv[0] != tlsdec->dtv_gen */ 172 173 ldr w2, [x1, #8] /* tlsdec->tls_index */ 174 add w2, w2, #1 175 ldr x3, [x0, w2, sxtw #3] /* dtv[tlsdesc->tls_index + 1] */ 176 cbz x3, 1f 177 178 /* Return (dtv[tlsdesc->tls_index + 1] + tlsdesc->tls_offs - tp) */ 179 ldr x2, [x1, #16] /* tlsdec->tls_offs */ 180 add x2, x2, x3 181 sub x0, x2, x4 182 /* Restore registers and return */ 183 ldp x3, x4, [sp, #(1 * 16)] 184 ldp x1, x2, [sp], #(2 * 16) 185 .cfi_adjust_cfa_offset -2 * 16 186 ret 187 188 /* 189 * Slow path 190 * return( 191 * tls_get_addr_common(tp, tlsdesc->tls_index, tlsdesc->tls_offs)); 192 * 193 */ 1941: 195 /* Save all integer registers */ 196 stp x29, x30, [sp, #-(8 * 16)]! 197 .cfi_adjust_cfa_offset 8 * 16 198 .cfi_rel_offset x29, 0 199 .cfi_rel_offset x30, 8 200 201 mov x29, sp 202 stp x5, x6, [sp, #(1 * 16)] 203 stp x7, x8, [sp, #(2 * 16)] 204 stp x9, x10, [sp, #(3 * 16)] 205 stp x11, x12, [sp, #(4 * 16)] 206 stp x13, x14, [sp, #(5 * 16)] 207 stp x15, x16, [sp, #(6 * 16)] 208 stp x17, x18, [sp, #(7 * 16)] 209 .cfi_rel_offset x5, 16 210 .cfi_rel_offset x6, 24 211 .cfi_rel_offset x7, 32 212 .cfi_rel_offset x8, 40 213 .cfi_rel_offset x9, 48 214 .cfi_rel_offset x10, 56 215 .cfi_rel_offset x11, 64 216 .cfi_rel_offset x12, 72 217 .cfi_rel_offset x13, 80 218 .cfi_rel_offset x14, 88 219 .cfi_rel_offset x15, 96 220 .cfi_rel_offset x16, 104 221 .cfi_rel_offset x17, 112 222 .cfi_rel_offset x18, 120 223 224 /* Find the tls offset */ 225 mov x0, x4 /* tp */ 226 mov x3, x1 /* tlsdesc ptr */ 227 ldr w1, [x3, #8] /* tlsdec->tls_index */ 228 ldr x2, [x3, #16] /* tlsdec->tls_offs */ 229 bl tls_get_addr_common 230 mrs x1, tpidr_el0 231 sub x0, x0, x1 232 233 /* Restore slow patch registers */ 234 ldp x17, x18, [sp, #(7 * 16)] 235 ldp x15, x16, [sp, #(6 * 16)] 236 ldp x13, x14, [sp, #(5 * 16)] 237 ldp x11, x12, [sp, #(4 * 16)] 238 ldp x9, x10, [sp, #(3 * 16)] 239 ldp x7, x8, [sp, #(2 * 16)] 240 ldp x5, x6, [sp, #(1 * 16)] 241 ldp x29, x30, [sp], #(8 * 16) 242 .cfi_adjust_cfa_offset -8 * 16 243 .cfi_restore x29 244 .cfi_restore x30 245 246 /* Restore fast path registers and return */ 247 ldp x3, x4, [sp, #16] 248 ldp x1, x2, [sp], #(2 * 16) 249 .cfi_adjust_cfa_offset -2 * 16 250 ret 251END(_rtld_tlsdesc_dynamic) 252