11.9Sjoerg/*	$NetBSD: rtld_start.S,v 1.9 2011/12/11 22:07:26 joerg Exp $	*/
21.3Sfvdl
31.3Sfvdl/*
41.3Sfvdl * Copyright (c) 2001 Wasabi Systems, Inc.
51.3Sfvdl * All rights reserved.
61.3Sfvdl *
71.3Sfvdl * Written by Frank van der Linden for Wasabi Systems, Inc.
81.3Sfvdl *
91.3Sfvdl * Redistribution and use in source and binary forms, with or without
101.3Sfvdl * modification, are permitted provided that the following conditions
111.3Sfvdl * are met:
121.3Sfvdl * 1. Redistributions of source code must retain the above copyright
131.3Sfvdl *    notice, this list of conditions and the following disclaimer.
141.3Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
151.3Sfvdl *    notice, this list of conditions and the following disclaimer in the
161.3Sfvdl *    documentation and/or other materials provided with the distribution.
171.3Sfvdl * 3. All advertising materials mentioning features or use of this software
181.3Sfvdl *    must display the following acknowledgement:
191.3Sfvdl *      This product includes software developed for the NetBSD Project by
201.3Sfvdl *      Wasabi Systems, Inc.
211.3Sfvdl * 4. The name of Wasabi Systems, Inc. may not be used to endorse
221.3Sfvdl *    or promote products derived from this software without specific prior
231.3Sfvdl *    written permission.
241.3Sfvdl *
251.3Sfvdl * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
261.3Sfvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
271.3Sfvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
281.3Sfvdl * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
291.3Sfvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
301.3Sfvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
311.3Sfvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
321.3Sfvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
331.3Sfvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
341.3Sfvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
351.3Sfvdl * POSSIBILITY OF SUCH DAMAGE.
361.3Sfvdl */
371.1Sfvdl
381.1Sfvdl#include <machine/asm.h>
391.1Sfvdl
401.1Sfvdl	.text
411.1Sfvdl	.align	16
421.1Sfvdl	.globl	.rtld_start
431.8Sjoerg	.hidden	.rtld_start
441.1Sfvdl	.type	.rtld_start,@function
451.1Sfvdl.rtld_start:
461.1Sfvdl	subq	$16,%rsp		# make room of obj_main and exit proc
471.5Sfvdl	movq	%rsp,%r12		# stack pointer arg to _rtld
481.1Sfvdl	pushq	%rbx			# save ps_strings
491.1Sfvdl
501.9Sjoerg	andq	$~15,%rsp		# align rsp, to be restored from r12
511.9Sjoerg
521.6Smycroft	leaq	_GLOBAL_OFFSET_TABLE_(%rip),%rax
531.6Smycroft	leaq	_DYNAMIC(%rip),%rdi	# &_DYNAMIC
541.6Smycroft	movq	%rdi,%rbx
551.6Smycroft	subq	(%rax),%rbx		# relocbase
561.5Sfvdl
571.5Sfvdl	movq	%rbx,%rsi
581.8Sjoerg	call	_rtld_relocate_nonplt_self
591.5Sfvdl
601.5Sfvdl	movq	%r12,%rdi
611.5Sfvdl	movq	%rbx,%rsi
621.8Sjoerg	call	_rtld			# _rtld(sp, relocbase)
631.1Sfvdl
641.9Sjoerg	leaq	-8(%r12),%rsp		# restore saved rsp, including %rbx
651.1Sfvdl	popq	%rbx			# %rbx = ps_strings
661.1Sfvdl	popq	%rdx			# %rdx = cleanup
671.1Sfvdl	popq	%rcx			# %rcx = obj_main
681.1Sfvdl	jmp	*%rax
691.1Sfvdl
701.1Sfvdl	.align	4
711.1Sfvdl	.globl	_rtld_bind_start
721.8Sjoerg	.hidden	_rtld_bind_start
731.1Sfvdl	.type	_rtld_bind_start,@function
741.1Sfvdl_rtld_bind_start:	# (obj, reloff)
751.7Sjoerg	# This function is called with a misaligned stack from the PLT
761.7Sjoerg	# due to the additional argument pushed.
771.7Sjoerg	# At this point %rsp % 16 == 8.
781.1Sfvdl	pushfq				# save caller-saved registers
791.1Sfvdl	pushq	%rax
801.1Sfvdl	pushq	%rcx
811.1Sfvdl	pushq	%rdx
821.1Sfvdl	pushq	%rsi
831.1Sfvdl	pushq	%rdi
841.1Sfvdl	pushq	%r8
851.1Sfvdl	pushq	%r9
861.1Sfvdl	pushq	%r10
871.1Sfvdl	pushq	%r11
881.1Sfvdl
891.2Sfvdl	movq	80(%rsp),%rdi
901.2Sfvdl	movq	88(%rsp),%rsi
911.2Sfvdl
921.8Sjoerg	call	_rtld_bind		# call the binder
931.2Sfvdl	movq	%rax,88(%rsp)		# store function in obj
941.1Sfvdl
951.1Sfvdl	popq	%r11
961.1Sfvdl	popq	%r10
971.1Sfvdl	popq	%r9
981.1Sfvdl	popq	%r8
991.1Sfvdl	popq	%rdi
1001.1Sfvdl	popq	%rsi
1011.1Sfvdl	popq	%rdx
1021.1Sfvdl	popq	%rcx
1031.1Sfvdl	popq	%rax
1041.1Sfvdl	popfq
1051.2Sfvdl
1061.2Sfvdl	leaq	8(%rsp),%rsp
1071.1Sfvdl
1081.1Sfvdl	ret
109