Blame sysdeps/x86_64/start.S

Packit Service 82fcde
/* Startup code compliant to the ELF x86-64 ABI.
Packit Service 82fcde
   Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
   Contributed by Andreas Jaeger <aj@suse.de>, 2001.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   In addition to the permissions in the GNU Lesser General Public
Packit Service 82fcde
   License, the Free Software Foundation gives you unlimited
Packit Service 82fcde
   permission to link the compiled version of this file with other
Packit Service 82fcde
   programs, and to distribute those programs without any restriction
Packit Service 82fcde
   coming from the use of this file. (The GNU Lesser General Public
Packit Service 82fcde
   License restrictions do apply in other respects; for example, they
Packit Service 82fcde
   cover modification of the file, and distribution when not linked
Packit Service 82fcde
   into another program.)
Packit Service 82fcde
Packit Service 82fcde
   Note that people who make modified versions of this file are not
Packit Service 82fcde
   obligated to grant this special exception for their modified
Packit Service 82fcde
   versions; it is their choice whether to do so. The GNU Lesser
Packit Service 82fcde
   General Public License gives permission to release a modified
Packit Service 82fcde
   version without this exception; this exception also makes it
Packit Service 82fcde
   possible to release a modified version which carries forward this
Packit Service 82fcde
   exception.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
/* This is the canonical entry point, usually the first thing in the text
Packit Service 82fcde
   segment.  The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry
Packit Service 82fcde
   point runs, most registers' values are unspecified, except for:
Packit Service 82fcde
Packit Service 82fcde
   %rdx		Contains a function pointer to be registered with `atexit'.
Packit Service 82fcde
		This is how the dynamic linker arranges to have DT_FINI
Packit Service 82fcde
		functions called for shared libraries that have been loaded
Packit Service 82fcde
		before this code runs.
Packit Service 82fcde
Packit Service 82fcde
   %rsp		The stack contains the arguments and environment:
Packit Service 82fcde
		0(%rsp)				argc
Packit Service 82fcde
		LP_SIZE(%rsp)			argv[0]
Packit Service 82fcde
		...
Packit Service 82fcde
		(LP_SIZE*argc)(%rsp)		NULL
Packit Service 82fcde
		(LP_SIZE*(argc+1))(%rsp)	envp[0]
Packit Service 82fcde
		...
Packit Service 82fcde
						NULL
Packit Service 82fcde
*/
Packit Service 82fcde
Packit Service 82fcde
#include <sysdep.h>
Packit Service 82fcde
Packit Service 82fcde
ENTRY (_start)
Packit Service 82fcde
	/* Clearing frame pointer is insufficient, use CFI.  */
Packit Service 82fcde
	cfi_undefined (rip)
Packit Service 82fcde
	/* Clear the frame pointer.  The ABI suggests this be done, to mark
Packit Service 82fcde
	   the outermost frame obviously.  */
Packit Service 82fcde
	xorl %ebp, %ebp
Packit Service 82fcde
Packit Service 82fcde
	/* Extract the arguments as encoded on the stack and set up
Packit Service 82fcde
	   the arguments for __libc_start_main (int (*main) (int, char **, char **),
Packit Service 82fcde
		   int argc, char *argv,
Packit Service 82fcde
		   void (*init) (void), void (*fini) (void),
Packit Service 82fcde
		   void (*rtld_fini) (void), void *stack_end).
Packit Service 82fcde
	   The arguments are passed via registers and on the stack:
Packit Service 82fcde
	main:		%rdi
Packit Service 82fcde
	argc:		%rsi
Packit Service 82fcde
	argv:		%rdx
Packit Service 82fcde
	init:		%rcx
Packit Service 82fcde
	fini:		%r8
Packit Service 82fcde
	rtld_fini:	%r9
Packit Service 82fcde
	stack_end:	stack.	*/
Packit Service 82fcde
Packit Service 82fcde
	mov %RDX_LP, %R9_LP	/* Address of the shared library termination
Packit Service 82fcde
				   function.  */
Packit Service 82fcde
#ifdef __ILP32__
Packit Service 82fcde
	mov (%rsp), %esi	/* Simulate popping 4-byte argument count.  */
Packit Service 82fcde
	add $4, %esp
Packit Service 82fcde
#else
Packit Service 82fcde
	popq %rsi		/* Pop the argument count.  */
Packit Service 82fcde
#endif
Packit Service 82fcde
	/* argv starts just at the current stack top.  */
Packit Service 82fcde
	mov %RSP_LP, %RDX_LP
Packit Service 82fcde
	/* Align the stack to a 16 byte boundary to follow the ABI.  */
Packit Service 82fcde
	and  $~15, %RSP_LP
Packit Service 82fcde
Packit Service 82fcde
	/* Push garbage because we push 8 more bytes.  */
Packit Service 82fcde
	pushq %rax
Packit Service 82fcde
Packit Service 82fcde
	/* Provide the highest stack address to the user code (for stacks
Packit Service 82fcde
	   which grow downwards).  */
Packit Service 82fcde
	pushq %rsp
Packit Service 82fcde
Packit Service 82fcde
#ifdef PIC
Packit Service 82fcde
	/* Pass address of our own entry points to .fini and .init.  */
Packit Service 82fcde
	mov __libc_csu_fini@GOTPCREL(%rip), %R8_LP
Packit Service 82fcde
	mov __libc_csu_init@GOTPCREL(%rip), %RCX_LP
Packit Service 82fcde
Packit Service 82fcde
	mov main@GOTPCREL(%rip), %RDI_LP
Packit Service 82fcde
#else
Packit Service 82fcde
	/* Pass address of our own entry points to .fini and .init.  */
Packit Service 82fcde
	mov $__libc_csu_fini, %R8_LP
Packit Service 82fcde
	mov $__libc_csu_init, %RCX_LP
Packit Service 82fcde
Packit Service 82fcde
	mov $main, %RDI_LP
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
	/* Call the user's main function, and exit with its value.
Packit Service 82fcde
	   But let the libc call main.  Since __libc_start_main in
Packit Service 82fcde
	   libc.so is called very early, lazy binding isn't relevant
Packit Service 82fcde
	   here.  Use indirect branch via GOT to avoid extra branch
Packit Service 82fcde
	   to PLT slot.  In case of static executable, ld in binutils
Packit Service 82fcde
	   2.26 or above can convert indirect branch into direct
Packit Service 82fcde
	   branch.  */
Packit Service 82fcde
	call *__libc_start_main@GOTPCREL(%rip)
Packit Service 82fcde
Packit Service 82fcde
	hlt			/* Crash if somehow `exit' does return.	 */
Packit Service 82fcde
END (_start)
Packit Service 82fcde
Packit Service 82fcde
/* Define a symbol for the first piece of initialized data.  */
Packit Service 82fcde
	.data
Packit Service 82fcde
	.globl __data_start
Packit Service 82fcde
__data_start:
Packit Service 82fcde
	.long 0
Packit Service 82fcde
	.weak data_start
Packit Service 82fcde
	data_start = __data_start