1144518Sdavidxu/*-
2144518Sdavidxu * Copyright 1996-1998 John D. Polstra.
3144518Sdavidxu * All rights reserved.
4144518Sdavidxu *
5144518Sdavidxu * Redistribution and use in source and binary forms, with or without
6144518Sdavidxu * modification, are permitted provided that the following conditions
7144518Sdavidxu * are met:
8144518Sdavidxu * 1. Redistributions of source code must retain the above copyright
9144518Sdavidxu *    notice, this list of conditions and the following disclaimer.
10144518Sdavidxu * 2. Redistributions in binary form must reproduce the above copyright
11144518Sdavidxu *    notice, this list of conditions and the following disclaimer in the
12144518Sdavidxu *    documentation and/or other materials provided with the distribution.
13144518Sdavidxu *
14144518Sdavidxu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15144518Sdavidxu * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16144518Sdavidxu * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17144518Sdavidxu * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18144518Sdavidxu * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19144518Sdavidxu * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20144518Sdavidxu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21144518Sdavidxu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22144518Sdavidxu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23144518Sdavidxu * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24144518Sdavidxu *
25144518Sdavidxu * $FreeBSD$
26144518Sdavidxu */
27144518Sdavidxu
28144518Sdavidxu	.text
29144518Sdavidxu	.align	4
30144518Sdavidxu	.globl	.rtld_start
31144518Sdavidxu	.type	.rtld_start,@function
32144518Sdavidxu.rtld_start:
33297706Skib	xorl	%ebp,%ebp		# Clear frame pointer for good form
34297706Skib	movl	%esp,%eax		# Save initial stack pointer
35297706Skib	movl	%esp,%esi		# Save initial stack pointer
36157457Sdavidxu	andl	$0xfffffff0,%esp	# Align stack pointer
37144518Sdavidxu	subl	$16,%esp		# A place to store exit procedure addr
38144518Sdavidxu	movl	%esp,%ebx		# save address of exit proc
39144518Sdavidxu	movl	%esp,%ecx		# construct address of obj_main
40157457Sdavidxu	addl	$4,%ecx
41144518Sdavidxu	subl	$4,%esp			# Keep stack aligned
42144518Sdavidxu	pushl	%ecx			# Pass address of obj_main
43144518Sdavidxu	pushl	%ebx			# Pass address of exit proc
44144518Sdavidxu	pushl	%eax			# Pass initial stack pointer to rtld
45144518Sdavidxu	call	_rtld@PLT		# Call rtld(sp); returns entry point
46144518Sdavidxu	addl	$16,%esp		# Remove arguments from stack
47144518Sdavidxu	popl	%edx			# Get exit procedure address
48144518Sdavidxu	movl	%esi,%esp		# Ignore obj_main
49157457Sdavidxu/*
50144518Sdavidxu * At this point, %eax contains the entry point of the main program, and
51144518Sdavidxu * %edx contains a pointer to a termination function that should be
52144518Sdavidxu * registered with atexit().  (crt1.o registers it.)
53144518Sdavidxu */
54144518Sdavidxu.globl .rtld_goto_main
55157457Sdavidxu.rtld_goto_main:	# This symbol exists just to make debugging easier.
56144518Sdavidxu	jmp	*%eax			# Enter main program
57144518Sdavidxu
58144518Sdavidxu
59/*
60 * Binder entry point.  Control is transferred to here by code in the PLT.
61 * On entry, there are two arguments on the stack.  In ascending address
62 * order, they are (1) "obj", a pointer to the calling object's Obj_Entry,
63 * and (2) "reloff", the byte offset of the appropriate relocation entry
64 * in the PLT relocation table.
65 *
66 * We are careful to preserve all registers, even the caller-save
67 * registers.  That is because this code may be invoked by low-level
68 * assembly-language code that is not ABI-compliant.
69 */
70	.align	4
71	.globl	_rtld_bind_start
72	.type	_rtld_bind_start,@function
73_rtld_bind_start:
74	pushf				# Save eflags
75	pushl	%eax			# Save %eax
76	pushl	%edx			# Save %edx
77	pushl	%ecx			# Save %ecx
78	pushl	20(%esp)		# Copy reloff argument
79	pushl	20(%esp)		# Copy obj argument
80
81	call	_rtld_bind@PLT		# Transfer control to the binder
82	/* Now %eax contains the entry point of the function being called. */
83
84	addl	$8,%esp			# Discard binder arguments
85	movl	%eax,20(%esp)		# Store target over obj argument
86	popl	%ecx			# Restore %ecx
87	popl	%edx			# Restore %edx
88	popl	%eax			# Restore %eax
89	popf				# Restore eflags
90	leal	4(%esp),%esp		# Discard reloff, do not change eflags
91	ret				# "Return" to target address
92
93	.section .note.GNU-stack,"",%progbits
94