Index: head/lib/csu/aarch64/crt1.c =================================================================== --- head/lib/csu/aarch64/crt1.c (revision 284706) +++ head/lib/csu/aarch64/crt1.c (revision 284707) @@ -1,89 +1,93 @@ /* LINTLIBRARY */ /*- * Copyright 1996-1998 John D. Polstra. * Copyright 2014 Andrew Turner. * Copyright 2014-2015 The FreeBSD Foundation. * All rights reserved. * * Portions of this software were developed by Andrew Turner * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #ifndef lint #ifndef __GNUC__ #error "GCC is needed to compile this file" #endif #endif /* lint */ #include #include "libc_private.h" #include "crtbrand.c" #include "ignore_init.c" #ifdef GCRT extern void _mcleanup(void); extern void monstartup(void *, void *); extern int eprol; extern int etext; #endif void __start(int, char **, char **, void (*)(void)); /* The entry function. */ __asm(" .text \n" " .align 0 \n" " .globl _start \n" " _start: \n" -" mov x3, x2 \n" /* cleanup */ -" ldr x0, [sp] \n" /* Load argc */ -" add x1, sp, #8 \n" /* load argv */ +/* TODO: Remove this when the kernel correctly aligns the stack */ +" cbnz x0, 1f \n" /* Are we using a new kernel? */ +" mov x0, sp \n" /* No, load the args from sp */ +" and sp, x0, #~0xf \n" /* And align the stack */ +"1: mov x3, x2 \n" /* cleanup */ +" add x1, x0, #8 \n" /* load argv */ +" ldr x0, [x0] \n" /* load argc */ " add x2, x1, x0, lsl #3 \n" /* env is after argv */ " add x2, x2, #8 \n" /* argv is null terminated */ " b __start "); /* The entry function. */ void __start(int argc, char *argv[], char *env[], void (*cleanup)(void)) { handle_argv(argc, argv, env); if (&_DYNAMIC != NULL) atexit(cleanup); else _init_tls(); #ifdef GCRT atexit(_mcleanup); monstartup(&eprol, &etext); __asm__("eprol:"); #endif handle_static_init(argc, argv, env); exit(main(argc, argv, env)); } Index: head/libexec/rtld-elf/aarch64/rtld_start.S =================================================================== --- head/libexec/rtld-elf/aarch64/rtld_start.S (revision 284706) +++ head/libexec/rtld-elf/aarch64/rtld_start.S (revision 284707) @@ -1,152 +1,156 @@ /*- * Copyright (c) 2014 The FreeBSD Foundation * All rights reserved. * * This software was developed by Andrew Turner under * sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); ENTRY(.rtld_start) mov x19, x0 /* Put ps_strings in a callee-saved register */ mov x20, sp /* And the stack pointer */ - sub x8, x20, #16 /* Make room for obj_main & exit proc */ - mov sp, x8 /* Update the stack pointer */ + /* Handle the old style stack */ + /* TODO: Remove this when the kernel correctly aligns the stack */ + cbnz x0, 1f + mov x0, sp /* sp points to the args */ + and sp, x0, #~0xf /* Align the stack as needed */ - mov x0, x20 /* Pass the stack we were given to _rtld */ +1: sub sp, sp, #16 /* Make room for obj_main & exit proc */ + mov x1, sp /* exit_proc */ add x2, x1, #8 /* obj_main */ bl _rtld /* Call the loader */ mov x8, x0 /* Backup the entry point */ ldr x2, [sp] /* Load cleanup */ ldr x1, [sp, #8] /* Load obj_main */ mov x0, x19 /* Restore ps_strings */ mov sp, x20 /* Restore the stack pointer */ br x8 /* Jump to the entry point */ END(.rtld_start) /* * sp + 0 = &GOT[x + 3] * sp + 8 = RA * x16 = &GOT[2] * x17 = &_rtld_bind_start */ ENTRY(_rtld_bind_start) mov x17, sp /* Save the arguments */ stp x0, x1, [sp, #-16]! stp x2, x3, [sp, #-16]! stp x4, x5, [sp, #-16]! stp x6, x7, [sp, #-16]! /* Calculate reloff */ ldr x2, [x17, #0] /* Get the address of the entry */ sub x1, x2, x16 /* Find its offset */ sub x1, x1, #8 /* Adjust for x16 not being at offset 0 */ /* Each rela item has 3 entriesso we need reloff = 3 * index */ lsl x3, x1, #1 /* x3 = 2 * offset */ add x1, x1, x3 /* x1 = x3 + offset = 3 * offset */ /* Load obj */ ldr x0, [x16, #-8] /* Call into rtld */ bl _rtld_bind /* Restore the registers saved by the plt code */ ldp xzr, x30, [sp, #(4 * 16)] /* Backup the address to branch to */ mov x16, x0 /* restore the arguments */ ldp x6, x7, [sp], #16 ldp x4, x5, [sp], #16 ldp x2, x3, [sp], #16 ldp x0, x1, [sp], #16 /* And the part of the stack the plt entry handled */ add sp, sp, #16 /* Call into the correct function */ br x16 END(_rtld_bind_start) /* * uint64_t _rtld_tlsdesc(struct tlsdesc *); * * struct tlsdesc { * uint64_t ptr; * uint64_t data; * }; * * Returns the data. */ ENTRY(_rtld_tlsdesc) ldr x0, [x0, #8] ret END(_rtld_tlsdesc) /* * uint64_t _rtld_tlsdesc_dynamic(struct tlsdesc *); * * TODO: We could lookup the saved index here to skip saving the entire stack. */ ENTRY(_rtld_tlsdesc_dynamic) /* Store any registers we may use in rtld_tlsdesc_handle */ stp x29, x30, [sp, #-(10 * 16)]! mov x29, sp stp x1, x2, [sp, #(1 * 16)] stp x3, x4, [sp, #(2 * 16)] stp x5, x6, [sp, #(3 * 16)] stp x7, x8, [sp, #(4 * 16)] stp x9, x10, [sp, #(5 * 16)] stp x11, x12, [sp, #(6 * 16)] stp x13, x14, [sp, #(7 * 16)] stp x15, x16, [sp, #(8 * 16)] stp x17, x18, [sp, #(9 * 16)] /* Find the tls offset */ ldr x0, [x0, #8] mov x1, #1 bl rtld_tlsdesc_handle /* Restore the registers */ ldp x17, x18, [sp, #(9 * 16)] ldp x15, x16, [sp, #(8 * 16)] ldp x13, x14, [sp, #(7 * 16)] ldp x11, x12, [sp, #(6 * 16)] ldp x9, x10, [sp, #(5 * 16)] ldp x7, x8, [sp, #(4 * 16)] ldp x5, x6, [sp, #(3 * 16)] ldp x3, x4, [sp, #(2 * 16)] ldp x1, x2, [sp, #(1 * 16)] ldp x29, x30, [sp], #(10 * 16) ret END(_rtld_tlsdesc_dynamic)