xref: /freebsd/libexec/rtld-elf/i386/rtld_start.S (revision 4543ef516683042d46f3bd3bb8a4f3f746e00499)
1/*-
2 * Copyright 1996-1998 John D. Polstra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26	.text
27	.align	4
28	.globl	.rtld_start
29	.type	.rtld_start,@function
30.rtld_start:
31	.cfi_startproc
32	.cfi_undefined %eip
33	xorl	%ebp,%ebp		# Clear frame pointer for good form
34	movl	%esp,%esi		# Save initial stack pointer
35	pushl	%ebp
36	.cfi_def_cfa_offset 4
37	movl	%esp,%ebp
38	.cfi_offset %ebp,-4
39	.cfi_def_cfa_register %ebp
40	andl	$0xfffffff0,%esp	# Align stack pointer
41	subl	$16,%esp		# A place to store exit procedure addr
42	movl	%esp,%ebx		# save address of exit proc
43	movl	%esp,%ecx		# construct address of obj_main
44	addl	$4,%ecx
45	subl	$4,%esp			# Keep stack aligned
46	pushl	%ecx			# Pass address of obj_main
47	pushl	%ebx			# Pass address of exit proc
48	pushl	%esi			# Pass initial stack pointer to rtld
49	call	_rtld			# Call rtld(sp); returns entry point
50	addl	$16,%esp		# Remove arguments from stack
51	popl	%edx			# Get exit procedure address
52	movl	%esi,%esp		# Ignore obj_main
53/*
54 * At this point, %eax contains the entry point of the main program, and
55 * %edx contains a pointer to a termination function that should be
56 * registered with atexit().  (crt1.o registers it.)
57 */
58.globl .rtld_goto_main
59.rtld_goto_main:	# This symbol exists just to make debugging easier.
60	jmp	*%eax			# Enter main program
61	.cfi_endproc
62
63
64/*
65 * Binder entry point.  Control is transferred to here by code in the PLT.
66 * On entry, there are two arguments on the stack.  In ascending address
67 * order, they are (1) "obj", a pointer to the calling object's Obj_Entry,
68 * and (2) "reloff", the byte offset of the appropriate relocation entry
69 * in the PLT relocation table.
70 *
71 * We are careful to preserve all registers, even the caller-save
72 * registers.  That is because this code may be invoked by low-level
73 * assembly-language code that is not ABI-compliant.
74 */
75	.align	4
76	.globl	_rtld_bind_start
77	.type	_rtld_bind_start,@function
78_rtld_bind_start:
79	pushf				# Save eflags
80	pushl	%eax			# Save %eax
81	pushl	%edx			# Save %edx
82	pushl	%ecx			# Save %ecx
83	pushl	20(%esp)		# Copy reloff argument
84	pushl	20(%esp)		# Copy obj argument
85
86	call	_rtld_bind		# Transfer control to the binder
87	/* Now %eax contains the entry point of the function being called. */
88
89	addl	$8,%esp			# Discard binder arguments
90	movl	%eax,20(%esp)		# Store target over obj argument
91	popl	%ecx			# Restore %ecx
92	popl	%edx			# Restore %edx
93	popl	%eax			# Restore %eax
94	popf				# Restore eflags
95	leal	4(%esp),%esp		# Discard reloff, do not change eflags
96	ret				# "Return" to target address
97
98	.section .note.GNU-stack,"",%progbits
99