/* PLT trampolines.  Nios II version.
   Copyright (C) 2005-2016 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library.  If not, see
   <http://www.gnu.org/licenses/>.  */

/* This code is used in dl-runtime.c to call the `fixup' function
   and then redirect to the address it returns. */

	.text
	.align 4
	.globl _dl_linux_resolver
	.globl _dl_linux_resolve
	.type  _dl_linux_resolve,@function

_dl_linux_resolve:
/* The runtime resolver receives the original function arguments in r4
   through r7, the shared library identifier from GOT[1]? in r14, and the
   relocation index times four in r15. It updates the corresponding PLT GOT
   entry so that the PLT entry will transfer control directly to the target
   in the future, and then transfers control to the target. */
	/* Save arguments and return address.  */
	subi sp, sp, 28
	stw r22, 24(sp)
	stw r8, 20(sp)  /* save r8, because this might be a call to mcount */
	stw r7, 16(sp)
	stw r6, 12(sp)
	stw r5, 8(sp)
	stw r4, 4(sp)
	stw ra, 0(sp)

	/* Get pointer to linker struct.  */
	mov r4, r14

	/* Get the relocation offset.  We're given a multiple of 4 and
	   need a multiple of 12, so multiply by 3. */
	slli r5, r15, 1
	add r5, r5, r15

	/* Call the fixup routine.  */
	nextpc r22
1:	movhi r2, %hiadj(_gp_got - 1b)
	addi r2, r2, %lo(_gp_got - 1b)
	add r22, r22, r2
	ldw r2, %call(_dl_linux_resolver)(r22)
	callr r2

	/* Restore the arguments and return address.  */
	ldw ra, 0(sp)
	ldw r4, 4(sp)
	ldw r5, 8(sp)
	ldw r6, 12(sp)
	ldw r7, 16(sp)
	ldw r8, 20(sp)
	ldw r22, 24(sp)
	addi sp, sp, 28

	/* Jump to the newly found address.  */
	jmp r2

    .size _dl_linux_resolve, . - _dl_linux_resolve
