1/*	$OpenBSD: ldasm.S,v 1.32 2017/08/27 21:59:52 deraadt Exp $ */
2
3/*
4 * Copyright (c) 2002 Dale Rahn
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 */
29
30
31#define DL_DATA_SIZE	(16*4)
32#include <sys/syscall.h>
33#include <machine/asm.h>
34
35	.text
36	.align  16,0xcc
37	.globl  _dl_start
38	.type   _dl_start,@function
39_dl_start:
40	movl	%esp,%eax		# save stack pointer for _rtld
41	subl	$DL_DATA_SIZE,%esp	# allocate dl_data
42	call	1f			# push &_DYNAMIC...
431:	addl	$(_DYNAMIC-1b),(%esp)	# ...for dl_boot_bind
44	movl	%esp,%ebx
45	movl	%ebx,%edi		# save dl_data arg for dl_boot
46	pushl	%ebx			# push dl_data for dl_boot_bind
47
48	mov	%eax, %esi		# save stack for dl_boot
49
50	pushl	%eax			# load saved SP for dl_boot_bind
51
52	call	_dl_boot_bind@PLT	# _dl_boot_bind(sp,dl_data)
53
54	pushl	%edi			# push saved dl_data
55	movl	%edi,%ebp
56	movl	(7*4)(%ebp),%eax
57	pushl	%eax			# push loff from dl_data
58
59	movl	%esi,%ebp
60	movl	$4,%eax
61	imull	0(%ebp),%eax
62	addl	$8,%eax
63	addl	%ebp,%eax
64	push	%eax			# push envp
65
66	leal	4(%ebp),%eax
67	push	%eax			# push argv
68
69	call	_dl_boot@PLT		# _dl_boot(argv,envp,loff,dl_data)
70
71	addl	$7*4,%esp		# pop args
72
73	addl	$DL_DATA_SIZE,%esp	# return dl_data
74
75	call	1f			# %edx = cleanup
761:	popl	%ebx
77	addl	$_GLOBAL_OFFSET_TABLE_+[.-1b], %ebx
78	leal	_dl_dtors@GOTOFF(%ebx), %edx
79
80	jmp	*%eax
81
82	.align 16,0xcc
83	.global _dl_bind_start
84	.type _dl_bind_start,@function
85_dl_bind_start:
86	pushf				# save registers
87	pushl   %eax
88	pushl   %ecx
89	pushl   %edx
90	pushl   %ebx
91	pushl   %ebp
92	pushl   %esi
93	pushl   %edi
94	pushl   %ds
95	pushl   %es
96
97	pushl   44(%esp)		# Copy of reloff
98	pushl   44(%esp)		# Copy of obj
99	call    _dl_bind@PLT		# Call the binder
100	addl    $8,%esp			# pop binder args
101	movl    %eax,44(%esp)		# Store function to be called in obj
102
103	popl    %es			# restore registers
104	popl    %ds
105	popl    %edi
106	popl    %esi
107	popl    %ebp
108	popl    %ebx
109	popl    %edx
110	popl    %ecx
111	popl    %eax
112	popf
113
114	leal    4(%esp),%esp		# Discard reloff, do not change eflags
115	ret
116