Path: blob/main/libexec/rtld-elf/riscv/rtld_start.S
34923 views
/*-1* Copyright (c) 2015-2018 Ruslan Bukin <[email protected]>2* All rights reserved.3*4* This software was developed by SRI International and the University of5* Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-02376* ("CTSRD"), as part of the DARPA CRASH research programme.7*8* This software was developed by the University of Cambridge Computer9* Laboratory as part of the CTSRD Project, with support from the UK Higher10* Education Innovation Fund (HEIF).11*12* Redistribution and use in source and binary forms, with or without13* modification, are permitted provided that the following conditions14* are met:15* 1. Redistributions of source code must retain the above copyright16* notice, this list of conditions and the following disclaimer.17* 2. Redistributions in binary form must reproduce the above copyright18* notice, this list of conditions and the following disclaimer in the19* documentation and/or other materials provided with the distribution.20*21* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <machine/asm.h>35/*36* func_ptr_type37* _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)38*/3940ENTRY(.rtld_start)41.cfi_undefined ra /* Do not attempt to unwind any further. */42mv s0, a0 /* Put ps_strings in a callee-saved register */43mv s1, sp /* And the stack pointer */4445addi sp, sp, -16 /* Make room for obj_main & exit proc */4647mv a1, sp /* exit_proc */48addi a2, a1, 8 /* obj_main */49jal _rtld /* Call the loader */50mv t0, a0 /* Backup the entry point */5152ld a2, 0(sp) /* Load cleanup */53ld a1, 8(sp) /* Load obj_main */54mv a0, s0 /* Restore ps_strings */55mv sp, s1 /* Restore the stack pointer */56jr t0 /* Jump to the entry point */57END(.rtld_start)5859/*60* t0 = obj pointer61* t1 = reloc offset62*/63ENTRY(_rtld_bind_start)64/* Save the arguments and ra */65/* We require 17 dwords, but the stack must be aligned to 16-bytes */66addi sp, sp, -(8 * 18)67sd a0, (8 * 0)(sp)68sd a1, (8 * 1)(sp)69sd a2, (8 * 2)(sp)70sd a3, (8 * 3)(sp)71sd a4, (8 * 4)(sp)72sd a5, (8 * 5)(sp)73sd a6, (8 * 6)(sp)74sd a7, (8 * 7)(sp)75sd ra, (8 * 8)(sp)7677#ifdef __riscv_float_abi_double78/* Save any floating-point arguments */79fsd fa0, (8 * 9)(sp)80fsd fa1, (8 * 10)(sp)81fsd fa2, (8 * 11)(sp)82fsd fa3, (8 * 12)(sp)83fsd fa4, (8 * 13)(sp)84fsd fa5, (8 * 14)(sp)85fsd fa6, (8 * 15)(sp)86fsd fa7, (8 * 16)(sp)87#endif8889/* Reloc offset is 3x of the .got.plt offset */90slli a1, t1, 1 /* Mult items by 2 */91add a1, a1, t1 /* Plus item */9293/* Load obj */94mv a0, t09596/* Call into rtld */97jal _rtld_bind9899/* Backup the address to branch to */100mv t0, a0101102/* Restore the arguments and ra */103ld a0, (8 * 0)(sp)104ld a1, (8 * 1)(sp)105ld a2, (8 * 2)(sp)106ld a3, (8 * 3)(sp)107ld a4, (8 * 4)(sp)108ld a5, (8 * 5)(sp)109ld a6, (8 * 6)(sp)110ld a7, (8 * 7)(sp)111ld ra, (8 * 8)(sp)112113#ifdef __riscv_float_abi_double114/* Restore floating-point arguments */115fld fa0, (8 * 9)(sp)116fld fa1, (8 * 10)(sp)117fld fa2, (8 * 11)(sp)118fld fa3, (8 * 12)(sp)119fld fa4, (8 * 13)(sp)120fld fa5, (8 * 14)(sp)121fld fa6, (8 * 15)(sp)122fld fa7, (8 * 16)(sp)123#endif124addi sp, sp, (8 * 18)125126/* Call into the correct function */127jr t0128END(_rtld_bind_start)129130131