1*abb1a6d3Sskrll/* $NetBSD: rtld_start.S,v 1.4 2022/12/04 17:04:06 skrll Exp $ */ 225195b1eSmatt 325195b1eSmatt/*- 425195b1eSmatt * Copyright (c) 2014 The NetBSD Foundation, Inc. 525195b1eSmatt * All rights reserved. 625195b1eSmatt * 725195b1eSmatt * This code is derived from software contributed to The NetBSD Foundation 825195b1eSmatt * by Matt Thomas of 3am Software Foundry. 925195b1eSmatt * 1025195b1eSmatt * Redistribution and use in source and binary forms, with or without 1125195b1eSmatt * modification, are permitted provided that the following conditions 1225195b1eSmatt * are met: 1325195b1eSmatt * 1. Redistributions of source code must retain the above copyright 1425195b1eSmatt * notice, this list of conditions and the following disclaimer. 1525195b1eSmatt * 2. Redistributions in binary form must reproduce the above copyright 1625195b1eSmatt * notice, this list of conditions and the following disclaimer in the 1725195b1eSmatt * documentation and/or other materials provided with the distribution. 1825195b1eSmatt * 1925195b1eSmatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2025195b1eSmatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2125195b1eSmatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2225195b1eSmatt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2325195b1eSmatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2425195b1eSmatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2525195b1eSmatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2625195b1eSmatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2725195b1eSmatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2825195b1eSmatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2925195b1eSmatt * POSSIBILITY OF SUCH DAMAGE. 3025195b1eSmatt */ 3125195b1eSmatt 3225195b1eSmatt#include <machine/asm.h> 3325195b1eSmatt 3425195b1eSmatt .globl _C_LABEL(_rtld_relocate_nonplt_self) 3525195b1eSmatt .globl _C_LABEL(_rtld) 3625195b1eSmatt 3725195b1eSmatt/* 3825195b1eSmatt * void 3925195b1eSmatt * ___start(void (*cleanup)(void), 4025195b1eSmatt * const Obj_Entry *obj, 4125195b1eSmatt * struct ps_strings *ps_strings) 4225195b1eSmatt */ 4325195b1eSmattENTRY(_rtld_start) 44*abb1a6d3Sskrll mv s0, sp # save stack pointer 4525195b1eSmatt addi sp, sp, -4*__SIZEOF_POINTER__ 4625195b1eSmatt # adjust stack pointer 4725195b1eSmatt # -> 2*PTR_SIZE(sp) for atexit 4825195b1eSmatt # -> 3*PTR_SIZE(sp) for obj_main 49*abb1a6d3Sskrll mv s1, a2 # save ps_strings pointer 5025195b1eSmatt 513ae76ab5Smatt.L0: auipc gp, %pcrel_hi(_GLOBAL_OFFSET_TABLE_) 523ae76ab5Smatt PTR_L t0, %pcrel_lo(.L0)(gp) # &_DYNAMIC 533ae76ab5Smatt.L1: auipc a0, %pcrel_hi(_DYNAMIC) 543ae76ab5Smatt addi a0, a0, %pcrel_lo(.L1) 5525195b1eSmatt sub s2, a0, t0 # save for _rtld 56*abb1a6d3Sskrll mv a1, s2 5725195b1eSmatt call _C_LABEL(_rtld_relocate_nonplt_self) 5825195b1eSmatt 59*abb1a6d3Sskrll mv a1, s2 # relocbase 6025195b1eSmatt addi a0, sp, 2*__SIZEOF_POINTER__ # sp 613ae76ab5Smatt call _C_LABEL(_rtld) # a0 = _rtld(sp, relocbase) 623ae76ab5Smatt mv t0, a0 6325195b1eSmatt 6425195b1eSmatt PTR_L a0, 2*__SIZEOF_POINTER__(sp) # cleanup function 6525195b1eSmatt PTR_L a1, 3*__SIZEOF_POINTER__(sp) # obj_main entry 66*abb1a6d3Sskrll mv a2, s1 # restore ps_strings 67*abb1a6d3Sskrll mv sp, s0 # readjust stack 68*abb1a6d3Sskrll mv s0, zero # break stack chain 693ae76ab5Smatt jr t0 # _start(cleanup, obj_main, ps_strings); 7025195b1eSmattEND(_rtld_start) 7125195b1eSmatt 7225195b1eSmatt#define XCALLFRAME_SIZ (12*SZREG) 7325195b1eSmatt#define XCALLFRAME_RA (8*SZREG) 7425195b1eSmatt#define XCALLFRAME_A7 (7*SZREG) 7525195b1eSmatt#define XCALLFRAME_A6 (6*SZREG) 7625195b1eSmatt#define XCALLFRAME_A5 (5*SZREG) 7725195b1eSmatt#define XCALLFRAME_A4 (4*SZREG) 7825195b1eSmatt#define XCALLFRAME_A3 (3*SZREG) 7925195b1eSmatt#define XCALLFRAME_A2 (2*SZREG) 8025195b1eSmatt#define XCALLFRAME_A1 (1*SZREG) 8125195b1eSmatt#define XCALLFRAME_A0 (0*SZREG) 8225195b1eSmatt 8325195b1eSmatt/* 8425195b1eSmatt * t0 = obj pointer 8525195b1eSmatt * t1 = reloc offset 8625195b1eSmatt */ 8725195b1eSmattENTRY_NP(_rtld_bind_start) 8825195b1eSmatt addi sp, sp, -XCALLFRAME_SIZ // save arguments on stack 8925195b1eSmatt REG_S a0, XCALLFRAME_A0(sp) 9025195b1eSmatt REG_S a1, XCALLFRAME_A1(sp) 9125195b1eSmatt REG_S a2, XCALLFRAME_A2(sp) 9225195b1eSmatt REG_S a3, XCALLFRAME_A3(sp) 9325195b1eSmatt REG_S a4, XCALLFRAME_A4(sp) 9425195b1eSmatt REG_S a5, XCALLFRAME_A5(sp) 9525195b1eSmatt REG_S a6, XCALLFRAME_A6(sp) 9625195b1eSmatt REG_S a7, XCALLFRAME_A7(sp) 9725195b1eSmatt REG_S ra, XCALLFRAME_RA(sp) 9825195b1eSmatt 993ae76ab5Smatt mv a0, t0 /* object from got.plt[1] */ 1003ae76ab5Smatt mv a1, t1 /* reloc offset */ 10125195b1eSmatt 10225195b1eSmatt call _C_LABEL(_rtld_bind) 1033ae76ab5Smatt mv t0, a0 /* save function pointer */ 10425195b1eSmatt 10525195b1eSmatt REG_L a0, XCALLFRAME_A0(sp) 10625195b1eSmatt REG_L a1, XCALLFRAME_A1(sp) 10725195b1eSmatt REG_L a2, XCALLFRAME_A2(sp) 10825195b1eSmatt REG_L a3, XCALLFRAME_A3(sp) 10925195b1eSmatt REG_L a4, XCALLFRAME_A4(sp) 11025195b1eSmatt REG_L a5, XCALLFRAME_A5(sp) 11125195b1eSmatt REG_L a6, XCALLFRAME_A6(sp) 11225195b1eSmatt REG_L a7, XCALLFRAME_A7(sp) 11325195b1eSmatt REG_L ra, XCALLFRAME_RA(sp) 11425195b1eSmatt addi sp, sp, XCALLFRAME_SIZ 1153ae76ab5Smatt jr t0 11625195b1eSmattEND(_rtld_bind_start) 117