xref: /freebsd/libexec/rtld-elf/i386/rtld_start.S (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
13124c3e0SJohn Polstra/*-
23124c3e0SJohn Polstra * Copyright 1996-1998 John D. Polstra.
33124c3e0SJohn Polstra * All rights reserved.
43124c3e0SJohn Polstra *
53124c3e0SJohn Polstra * Redistribution and use in source and binary forms, with or without
63124c3e0SJohn Polstra * modification, are permitted provided that the following conditions
73124c3e0SJohn Polstra * are met:
83124c3e0SJohn Polstra * 1. Redistributions of source code must retain the above copyright
93124c3e0SJohn Polstra *    notice, this list of conditions and the following disclaimer.
103124c3e0SJohn Polstra * 2. Redistributions in binary form must reproduce the above copyright
113124c3e0SJohn Polstra *    notice, this list of conditions and the following disclaimer in the
123124c3e0SJohn Polstra *    documentation and/or other materials provided with the distribution.
133124c3e0SJohn Polstra *
143124c3e0SJohn Polstra * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
153124c3e0SJohn Polstra * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
163124c3e0SJohn Polstra * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
173124c3e0SJohn Polstra * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
183124c3e0SJohn Polstra * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
193124c3e0SJohn Polstra * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
203124c3e0SJohn Polstra * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
213124c3e0SJohn Polstra * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223124c3e0SJohn Polstra * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
233124c3e0SJohn Polstra * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
243124c3e0SJohn Polstra */
253124c3e0SJohn Polstra
263124c3e0SJohn Polstra	.text
273124c3e0SJohn Polstra	.align	4
283124c3e0SJohn Polstra	.globl	.rtld_start
293124c3e0SJohn Polstra	.type	.rtld_start,@function
303124c3e0SJohn Polstra.rtld_start:
31*e541cf83SDmitry Chagin	.cfi_startproc
32*e541cf83SDmitry Chagin	.cfi_undefined %eip
333124c3e0SJohn Polstra	xorl	%ebp,%ebp		# Clear frame pointer for good form
349310a53dSDoug Rabson	movl	%esp,%esi		# Save initial stack pointer
35*e541cf83SDmitry Chagin	pushl	%ebp
36*e541cf83SDmitry Chagin	.cfi_def_cfa_offset 4
37*e541cf83SDmitry Chagin	movl	%esp,%ebp
38*e541cf83SDmitry Chagin	.cfi_offset %ebp,-4
39*e541cf83SDmitry Chagin	.cfi_def_cfa_register %ebp
409310a53dSDoug Rabson	andl	$0xfffffff0,%esp	# Align stack pointer
419310a53dSDoug Rabson	subl	$16,%esp		# A place to store exit procedure addr
4213575fc4SDoug Rabson	movl	%esp,%ebx		# save address of exit proc
4313575fc4SDoug Rabson	movl	%esp,%ecx		# construct address of obj_main
4413575fc4SDoug Rabson	addl	$4,%ecx
459310a53dSDoug Rabson	subl	$4,%esp			# Keep stack aligned
4613575fc4SDoug Rabson	pushl	%ecx			# Pass address of obj_main
4713575fc4SDoug Rabson	pushl	%ebx			# Pass address of exit proc
4886c63225SDmitry Chagin	pushl	%esi			# Pass initial stack pointer to rtld
490c4f9ecdSKonstantin Belousov	call	_rtld			# Call rtld(sp); returns entry point
509310a53dSDoug Rabson	addl	$16,%esp		# Remove arguments from stack
513124c3e0SJohn Polstra	popl	%edx			# Get exit procedure address
529310a53dSDoug Rabson	movl	%esi,%esp		# Ignore obj_main
533124c3e0SJohn Polstra/*
543124c3e0SJohn Polstra * At this point, %eax contains the entry point of the main program, and
553124c3e0SJohn Polstra * %edx contains a pointer to a termination function that should be
563124c3e0SJohn Polstra * registered with atexit().  (crt1.o registers it.)
573124c3e0SJohn Polstra */
583124c3e0SJohn Polstra.globl .rtld_goto_main
593124c3e0SJohn Polstra.rtld_goto_main:	# This symbol exists just to make debugging easier.
603124c3e0SJohn Polstra	jmp	*%eax			# Enter main program
61*e541cf83SDmitry Chagin	.cfi_endproc
623124c3e0SJohn Polstra
633124c3e0SJohn Polstra
643124c3e0SJohn Polstra/*
653124c3e0SJohn Polstra * Binder entry point.  Control is transferred to here by code in the PLT.
663124c3e0SJohn Polstra * On entry, there are two arguments on the stack.  In ascending address
673124c3e0SJohn Polstra * order, they are (1) "obj", a pointer to the calling object's Obj_Entry,
683124c3e0SJohn Polstra * and (2) "reloff", the byte offset of the appropriate relocation entry
693124c3e0SJohn Polstra * in the PLT relocation table.
703124c3e0SJohn Polstra *
7136daf049SEitan Adler * We are careful to preserve all registers, even the caller-save
723124c3e0SJohn Polstra * registers.  That is because this code may be invoked by low-level
733124c3e0SJohn Polstra * assembly-language code that is not ABI-compliant.
743124c3e0SJohn Polstra */
753124c3e0SJohn Polstra	.align	4
763124c3e0SJohn Polstra	.globl	_rtld_bind_start
773124c3e0SJohn Polstra	.type	_rtld_bind_start,@function
783124c3e0SJohn Polstra_rtld_bind_start:
793124c3e0SJohn Polstra	pushf				# Save eflags
803124c3e0SJohn Polstra	pushl	%eax			# Save %eax
813124c3e0SJohn Polstra	pushl	%edx			# Save %edx
823124c3e0SJohn Polstra	pushl	%ecx			# Save %ecx
833124c3e0SJohn Polstra	pushl	20(%esp)		# Copy reloff argument
843124c3e0SJohn Polstra	pushl	20(%esp)		# Copy obj argument
853124c3e0SJohn Polstra
860c4f9ecdSKonstantin Belousov	call	_rtld_bind		# Transfer control to the binder
873124c3e0SJohn Polstra	/* Now %eax contains the entry point of the function being called. */
883124c3e0SJohn Polstra
893124c3e0SJohn Polstra	addl	$8,%esp			# Discard binder arguments
903124c3e0SJohn Polstra	movl	%eax,20(%esp)		# Store target over obj argument
913124c3e0SJohn Polstra	popl	%ecx			# Restore %ecx
923124c3e0SJohn Polstra	popl	%edx			# Restore %edx
933124c3e0SJohn Polstra	popl	%eax			# Restore %eax
943124c3e0SJohn Polstra	popf				# Restore eflags
953124c3e0SJohn Polstra	leal	4(%esp),%esp		# Discard reloff, do not change eflags
963124c3e0SJohn Polstra	ret				# "Return" to target address
973ad6376eSKonstantin Belousov
983ad6376eSKonstantin Belousov	.section .note.GNU-stack,"",%progbits
99