Path: blob/main/libexec/rtld-elf/i386/rtld_start.S
34923 views
/*-1* Copyright 1996-1998 John D. Polstra.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES15* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,17* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT18* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,19* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY20* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*/2425.text26.align 427.globl .rtld_start28.type .rtld_start,@function29.rtld_start:30.cfi_startproc31.cfi_undefined %eip32xorl %ebp,%ebp # Clear frame pointer for good form33movl %esp,%esi # Save initial stack pointer34pushl %ebp35.cfi_def_cfa_offset 436movl %esp,%ebp37.cfi_offset %ebp,-438.cfi_def_cfa_register %ebp39andl $0xfffffff0,%esp # Align stack pointer40subl $16,%esp # A place to store exit procedure addr41movl %esp,%ebx # save address of exit proc42movl %esp,%ecx # construct address of obj_main43addl $4,%ecx44subl $4,%esp # Keep stack aligned45pushl %ecx # Pass address of obj_main46pushl %ebx # Pass address of exit proc47pushl %esi # Pass initial stack pointer to rtld48call _rtld # Call rtld(sp); returns entry point49addl $16,%esp # Remove arguments from stack50popl %edx # Get exit procedure address51movl %esi,%esp # Ignore obj_main52/*53* At this point, %eax contains the entry point of the main program, and54* %edx contains a pointer to a termination function that should be55* registered with atexit(). (crt1.o registers it.)56*/57.globl .rtld_goto_main58.rtld_goto_main: # This symbol exists just to make debugging easier.59jmp *%eax # Enter main program60.cfi_endproc616263/*64* Binder entry point. Control is transferred to here by code in the PLT.65* On entry, there are two arguments on the stack. In ascending address66* order, they are (1) "obj", a pointer to the calling object's Obj_Entry,67* and (2) "reloff", the byte offset of the appropriate relocation entry68* in the PLT relocation table.69*70* We are careful to preserve all registers, even the caller-save71* registers. That is because this code may be invoked by low-level72* assembly-language code that is not ABI-compliant.73*/74.align 475.globl _rtld_bind_start76.type _rtld_bind_start,@function77_rtld_bind_start:78pushf # Save eflags79pushl %eax # Save %eax80pushl %edx # Save %edx81pushl %ecx # Save %ecx82pushl 20(%esp) # Copy reloff argument83pushl 20(%esp) # Copy obj argument8485call _rtld_bind # Transfer control to the binder86/* Now %eax contains the entry point of the function being called. */8788addl $8,%esp # Discard binder arguments89movl %eax,20(%esp) # Store target over obj argument90popl %ecx # Restore %ecx91popl %edx # Restore %edx92popl %eax # Restore %eax93popf # Restore eflags94leal 4(%esp),%esp # Discard reloff, do not change eflags95ret # "Return" to target address9697.section .note.GNU-stack,"",%progbits9899100