1/* RISC-V PLT trampoline
2   Copyright (C) 2017-2022 Free Software Foundation, Inc.
3
4   This file is part of the GNU C Library.
5
6   The GNU C Library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Lesser General Public
8   License as published by the Free Software Foundation; either
9   version 2.1 of the License, or (at your option) any later version.
10
11   The GNU C Library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with the GNU C Library.  If not, see
18   <https://www.gnu.org/licenses/>.  */
19
20#include <sysdep.h>
21#include <sys/asm.h>
22
23/* Assembler veneer called from the PLT header code for lazy loading.
24   The PLT header passes its own args in t0-t2.  */
25
26#ifdef __riscv_float_abi_soft
27# define FRAME_SIZE (-((-10 * SZREG) & ALMASK))
28#else
29# define FRAME_SIZE (-((-10 * SZREG - 8 * SZFREG) & ALMASK))
30#endif
31
32ENTRY (_dl_runtime_resolve)
33  # Save arguments to stack.
34  addi sp, sp, -FRAME_SIZE
35  REG_S ra, 9*SZREG(sp)
36  REG_S a0, 1*SZREG(sp)
37  REG_S a1, 2*SZREG(sp)
38  REG_S a2, 3*SZREG(sp)
39  REG_S a3, 4*SZREG(sp)
40  REG_S a4, 5*SZREG(sp)
41  REG_S a5, 6*SZREG(sp)
42  REG_S a6, 7*SZREG(sp)
43  REG_S a7, 8*SZREG(sp)
44
45#ifndef __riscv_float_abi_soft
46  FREG_S fa0, (10*SZREG + 0*SZFREG)(sp)
47  FREG_S fa1, (10*SZREG + 1*SZFREG)(sp)
48  FREG_S fa2, (10*SZREG + 2*SZFREG)(sp)
49  FREG_S fa3, (10*SZREG + 3*SZFREG)(sp)
50  FREG_S fa4, (10*SZREG + 4*SZFREG)(sp)
51  FREG_S fa5, (10*SZREG + 5*SZFREG)(sp)
52  FREG_S fa6, (10*SZREG + 6*SZFREG)(sp)
53  FREG_S fa7, (10*SZREG + 7*SZFREG)(sp)
54#endif
55
56  # Update .got.plt and obtain runtime address of callee.
57  slli a1, t1, 1
58  mv a0, t0       # link map
59  add a1, a1, t1  # reloc offset (== thrice the .got.plt offset)
60  la a2, _dl_fixup
61  jalr a2
62  mv t1, a0
63
64  # Restore arguments from stack.
65  REG_L ra, 9*SZREG(sp)
66  REG_L a0, 1*SZREG(sp)
67  REG_L a1, 2*SZREG(sp)
68  REG_L a2, 3*SZREG(sp)
69  REG_L a3, 4*SZREG(sp)
70  REG_L a4, 5*SZREG(sp)
71  REG_L a5, 6*SZREG(sp)
72  REG_L a6, 7*SZREG(sp)
73  REG_L a7, 8*SZREG(sp)
74
75#ifndef __riscv_float_abi_soft
76  FREG_L fa0, (10*SZREG + 0*SZFREG)(sp)
77  FREG_L fa1, (10*SZREG + 1*SZFREG)(sp)
78  FREG_L fa2, (10*SZREG + 2*SZFREG)(sp)
79  FREG_L fa3, (10*SZREG + 3*SZFREG)(sp)
80  FREG_L fa4, (10*SZREG + 4*SZFREG)(sp)
81  FREG_L fa5, (10*SZREG + 5*SZFREG)(sp)
82  FREG_L fa6, (10*SZREG + 6*SZFREG)(sp)
83  FREG_L fa7, (10*SZREG + 7*SZFREG)(sp)
84#endif
85
86  addi sp, sp, FRAME_SIZE
87
88  # Invoke the callee.
89  jr t1
90END (_dl_runtime_resolve)
91