2001-09-19 10:37:31 +00:00
|
|
|
/* Startup code compliant to the ELF x86-64 ABI.
|
2021-01-02 19:32:25 +00:00
|
|
|
Copyright (C) 2001-2021 Free Software Foundation, Inc.
|
2001-09-19 10:37:31 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
Contributed by Andreas Jaeger <aj@suse.de>, 2001.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
2004-08-16 04:51:23 +00:00
|
|
|
In addition to the permissions in the GNU Lesser General Public
|
|
|
|
License, the Free Software Foundation gives you unlimited
|
|
|
|
permission to link the compiled version of this file with other
|
|
|
|
programs, and to distribute those programs without any restriction
|
|
|
|
coming from the use of this file. (The GNU Lesser General Public
|
|
|
|
License restrictions do apply in other respects; for example, they
|
|
|
|
cover modification of the file, and distribution when not linked
|
|
|
|
into another program.)
|
|
|
|
|
|
|
|
Note that people who make modified versions of this file are not
|
|
|
|
obligated to grant this special exception for their modified
|
|
|
|
versions; it is their choice whether to do so. The GNU Lesser
|
|
|
|
General Public License gives permission to release a modified
|
|
|
|
version without this exception; this exception also makes it
|
|
|
|
possible to release a modified version which carries forward this
|
|
|
|
exception.
|
|
|
|
|
2001-09-19 10:37:31 +00:00
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2001-09-19 10:37:31 +00:00
|
|
|
|
|
|
|
/* This is the canonical entry point, usually the first thing in the text
|
|
|
|
segment. The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry
|
|
|
|
point runs, most registers' values are unspecified, except for:
|
|
|
|
|
|
|
|
%rdx Contains a function pointer to be registered with `atexit'.
|
|
|
|
This is how the dynamic linker arranges to have DT_FINI
|
|
|
|
functions called for shared libraries that have been loaded
|
|
|
|
before this code runs.
|
|
|
|
|
|
|
|
%rsp The stack contains the arguments and environment:
|
2012-05-15 00:50:41 +00:00
|
|
|
0(%rsp) argc
|
|
|
|
LP_SIZE(%rsp) argv[0]
|
2001-09-19 10:37:31 +00:00
|
|
|
...
|
2012-05-15 00:50:41 +00:00
|
|
|
(LP_SIZE*argc)(%rsp) NULL
|
|
|
|
(LP_SIZE*(argc+1))(%rsp) envp[0]
|
2001-09-19 10:37:31 +00:00
|
|
|
...
|
2012-05-15 00:50:41 +00:00
|
|
|
NULL
|
2001-09-19 10:37:31 +00:00
|
|
|
*/
|
|
|
|
|
2012-03-16 19:49:23 +00:00
|
|
|
#include <sysdep.h>
|
2001-09-19 10:37:31 +00:00
|
|
|
|
2014-11-25 00:24:58 +00:00
|
|
|
ENTRY (_start)
|
2012-03-16 19:49:23 +00:00
|
|
|
/* Clearing frame pointer is insufficient, use CFI. */
|
|
|
|
cfi_undefined (rip)
|
2001-09-19 10:37:31 +00:00
|
|
|
/* Clear the frame pointer. The ABI suggests this be done, to mark
|
|
|
|
the outermost frame obviously. */
|
2005-03-31 10:02:53 +00:00
|
|
|
xorl %ebp, %ebp
|
2001-09-19 10:37:31 +00:00
|
|
|
|
|
|
|
/* Extract the arguments as encoded on the stack and set up
|
|
|
|
the arguments for __libc_start_main (int (*main) (int, char **, char **),
|
|
|
|
int argc, char *argv,
|
|
|
|
void (*init) (void), void (*fini) (void),
|
|
|
|
void (*rtld_fini) (void), void *stack_end).
|
|
|
|
The arguments are passed via registers and on the stack:
|
|
|
|
main: %rdi
|
|
|
|
argc: %rsi
|
|
|
|
argv: %rdx
|
|
|
|
init: %rcx
|
|
|
|
fini: %r8
|
|
|
|
rtld_fini: %r9
|
|
|
|
stack_end: stack. */
|
|
|
|
|
2012-05-15 00:50:41 +00:00
|
|
|
mov %RDX_LP, %R9_LP /* Address of the shared library termination
|
2001-09-19 10:37:31 +00:00
|
|
|
function. */
|
2012-05-15 00:50:41 +00:00
|
|
|
#ifdef __ILP32__
|
|
|
|
mov (%rsp), %esi /* Simulate popping 4-byte argument count. */
|
|
|
|
add $4, %esp
|
|
|
|
#else
|
2001-09-19 10:37:31 +00:00
|
|
|
popq %rsi /* Pop the argument count. */
|
2012-05-15 00:50:41 +00:00
|
|
|
#endif
|
|
|
|
/* argv starts just at the current stack top. */
|
|
|
|
mov %RSP_LP, %RDX_LP
|
2001-09-19 10:37:31 +00:00
|
|
|
/* Align the stack to a 16 byte boundary to follow the ABI. */
|
2012-05-15 00:50:41 +00:00
|
|
|
and $~15, %RSP_LP
|
2001-09-19 10:37:31 +00:00
|
|
|
|
2012-05-15 00:50:41 +00:00
|
|
|
/* Push garbage because we push 8 more bytes. */
|
|
|
|
pushq %rax
|
2001-09-19 10:37:31 +00:00
|
|
|
|
|
|
|
/* Provide the highest stack address to the user code (for stacks
|
|
|
|
which grow downwards). */
|
|
|
|
pushq %rsp
|
|
|
|
|
2021-02-25 11:10:57 +00:00
|
|
|
/* These used to be the addresses of .fini and .init. */
|
|
|
|
xorl %r8d, %r8d
|
|
|
|
xorl %ecx, %ecx
|
2003-05-28 21:04:20 +00:00
|
|
|
|
2021-02-25 11:10:57 +00:00
|
|
|
#ifdef PIC
|
2013-02-17 21:57:26 +00:00
|
|
|
mov main@GOTPCREL(%rip), %RDI_LP
|
2003-05-28 21:04:20 +00:00
|
|
|
#else
|
2013-02-17 21:57:26 +00:00
|
|
|
mov $main, %RDI_LP
|
2016-06-09 11:43:16 +00:00
|
|
|
#endif
|
2001-09-19 10:37:31 +00:00
|
|
|
|
|
|
|
/* Call the user's main function, and exit with its value.
|
2016-06-09 11:43:16 +00:00
|
|
|
But let the libc call main. Since __libc_start_main in
|
|
|
|
libc.so is called very early, lazy binding isn't relevant
|
|
|
|
here. Use indirect branch via GOT to avoid extra branch
|
|
|
|
to PLT slot. In case of static executable, ld in binutils
|
|
|
|
2.26 or above can convert indirect branch into direct
|
|
|
|
branch. */
|
|
|
|
call *__libc_start_main@GOTPCREL(%rip)
|
2001-09-19 10:37:31 +00:00
|
|
|
|
|
|
|
hlt /* Crash if somehow `exit' does return. */
|
2014-11-25 00:24:58 +00:00
|
|
|
END (_start)
|
2001-09-19 10:37:31 +00:00
|
|
|
|
|
|
|
/* Define a symbol for the first piece of initialized data. */
|
|
|
|
.data
|
|
|
|
.globl __data_start
|
|
|
|
__data_start:
|
|
|
|
.long 0
|
|
|
|
.weak data_start
|
|
|
|
data_start = __data_start
|