xref: /minix3/libexec/ld.elf_so/arch/x86_64/rtld_start.S (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc/*	$NetBSD: rtld_start.S,v 1.9 2011/12/11 22:07:26 joerg Exp $	*/
2e83f7ba2SBen Gras
3e83f7ba2SBen Gras/*
4e83f7ba2SBen Gras * Copyright (c) 2001 Wasabi Systems, Inc.
5e83f7ba2SBen Gras * All rights reserved.
6e83f7ba2SBen Gras *
7e83f7ba2SBen Gras * Written by Frank van der Linden for Wasabi Systems, Inc.
8e83f7ba2SBen Gras *
9e83f7ba2SBen Gras * Redistribution and use in source and binary forms, with or without
10e83f7ba2SBen Gras * modification, are permitted provided that the following conditions
11e83f7ba2SBen Gras * are met:
12e83f7ba2SBen Gras * 1. Redistributions of source code must retain the above copyright
13e83f7ba2SBen Gras *    notice, this list of conditions and the following disclaimer.
14e83f7ba2SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
15e83f7ba2SBen Gras *    notice, this list of conditions and the following disclaimer in the
16e83f7ba2SBen Gras *    documentation and/or other materials provided with the distribution.
17e83f7ba2SBen Gras * 3. All advertising materials mentioning features or use of this software
18e83f7ba2SBen Gras *    must display the following acknowledgement:
19e83f7ba2SBen Gras *      This product includes software developed for the NetBSD Project by
20e83f7ba2SBen Gras *      Wasabi Systems, Inc.
21e83f7ba2SBen Gras * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22e83f7ba2SBen Gras *    or promote products derived from this software without specific prior
23e83f7ba2SBen Gras *    written permission.
24e83f7ba2SBen Gras *
25e83f7ba2SBen Gras * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26e83f7ba2SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27e83f7ba2SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28e83f7ba2SBen Gras * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29e83f7ba2SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30e83f7ba2SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31e83f7ba2SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32e83f7ba2SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33e83f7ba2SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34e83f7ba2SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35e83f7ba2SBen Gras * POSSIBILITY OF SUCH DAMAGE.
36e83f7ba2SBen Gras */
37e83f7ba2SBen Gras
38e83f7ba2SBen Gras#include <machine/asm.h>
39e83f7ba2SBen Gras
40e83f7ba2SBen Gras	.text
41e83f7ba2SBen Gras	.align	16
42e83f7ba2SBen Gras	.globl	.rtld_start
43e83f7ba2SBen Gras	.hidden	.rtld_start
44e83f7ba2SBen Gras	.type	.rtld_start,@function
45e83f7ba2SBen Gras.rtld_start:
46e83f7ba2SBen Gras	subq	$16,%rsp		# make room of obj_main and exit proc
47e83f7ba2SBen Gras	movq	%rsp,%r12		# stack pointer arg to _rtld
48e83f7ba2SBen Gras	pushq	%rbx			# save ps_strings
49e83f7ba2SBen Gras
50*f14fb602SLionel Sambuc	andq	$~15,%rsp		# align rsp, to be restored from r12
51*f14fb602SLionel Sambuc
52e83f7ba2SBen Gras	leaq	_GLOBAL_OFFSET_TABLE_(%rip),%rax
53e83f7ba2SBen Gras	leaq	_DYNAMIC(%rip),%rdi	# &_DYNAMIC
54e83f7ba2SBen Gras	movq	%rdi,%rbx
55e83f7ba2SBen Gras	subq	(%rax),%rbx		# relocbase
56e83f7ba2SBen Gras
57e83f7ba2SBen Gras	movq	%rbx,%rsi
58e83f7ba2SBen Gras	call	_rtld_relocate_nonplt_self
59e83f7ba2SBen Gras
60e83f7ba2SBen Gras	movq	%r12,%rdi
61e83f7ba2SBen Gras	movq	%rbx,%rsi
62e83f7ba2SBen Gras	call	_rtld			# _rtld(sp, relocbase)
63e83f7ba2SBen Gras
64*f14fb602SLionel Sambuc	leaq	-8(%r12),%rsp		# restore saved rsp, including %rbx
65e83f7ba2SBen Gras	popq	%rbx			# %rbx = ps_strings
66e83f7ba2SBen Gras	popq	%rdx			# %rdx = cleanup
67e83f7ba2SBen Gras	popq	%rcx			# %rcx = obj_main
68e83f7ba2SBen Gras	jmp	*%rax
69e83f7ba2SBen Gras
70e83f7ba2SBen Gras	.align	4
71e83f7ba2SBen Gras	.globl	_rtld_bind_start
72e83f7ba2SBen Gras	.hidden	_rtld_bind_start
73e83f7ba2SBen Gras	.type	_rtld_bind_start,@function
74e83f7ba2SBen Gras_rtld_bind_start:	# (obj, reloff)
75e83f7ba2SBen Gras	# This function is called with a misaligned stack from the PLT
76e83f7ba2SBen Gras	# due to the additional argument pushed.
77e83f7ba2SBen Gras	# At this point %rsp % 16 == 8.
78e83f7ba2SBen Gras	pushfq				# save caller-saved registers
79e83f7ba2SBen Gras	pushq	%rax
80e83f7ba2SBen Gras	pushq	%rcx
81e83f7ba2SBen Gras	pushq	%rdx
82e83f7ba2SBen Gras	pushq	%rsi
83e83f7ba2SBen Gras	pushq	%rdi
84e83f7ba2SBen Gras	pushq	%r8
85e83f7ba2SBen Gras	pushq	%r9
86e83f7ba2SBen Gras	pushq	%r10
87e83f7ba2SBen Gras	pushq	%r11
88e83f7ba2SBen Gras
89e83f7ba2SBen Gras	movq	80(%rsp),%rdi
90e83f7ba2SBen Gras	movq	88(%rsp),%rsi
91e83f7ba2SBen Gras
92e83f7ba2SBen Gras	call	_rtld_bind		# call the binder
93e83f7ba2SBen Gras	movq	%rax,88(%rsp)		# store function in obj
94e83f7ba2SBen Gras
95e83f7ba2SBen Gras	popq	%r11
96e83f7ba2SBen Gras	popq	%r10
97e83f7ba2SBen Gras	popq	%r9
98e83f7ba2SBen Gras	popq	%r8
99e83f7ba2SBen Gras	popq	%rdi
100e83f7ba2SBen Gras	popq	%rsi
101e83f7ba2SBen Gras	popq	%rdx
102e83f7ba2SBen Gras	popq	%rcx
103e83f7ba2SBen Gras	popq	%rax
104e83f7ba2SBen Gras	popfq
105e83f7ba2SBen Gras
106e83f7ba2SBen Gras	leaq	8(%rsp),%rsp
107e83f7ba2SBen Gras
108e83f7ba2SBen Gras	ret
109