xref: /freebsd/lib/csu/amd64/crt1_s.S (revision cb991a228179639cc0568fdd5d4b4b07b6f47b5a)
1c969310cSDmitry Chagin/*-
2c969310cSDmitry Chagin * SPDX-License-Identifier: BSD-2-Clause
3c969310cSDmitry Chagin *
4c969310cSDmitry Chagin * Copyright (C) 2023 Dmitry Chagin <dchagin@FreeBSD.org>
5c969310cSDmitry Chagin *
6c969310cSDmitry Chagin * Redistribution and use in source and binary forms, with or without
7c969310cSDmitry Chagin * modification, are permitted provided that the following conditions
8c969310cSDmitry Chagin * are met:
9c969310cSDmitry Chagin * 1. Redistributions of source code must retain the above copyright
10c969310cSDmitry Chagin *    notice, this list of conditions and the following disclaimer.
11c969310cSDmitry Chagin * 2. Redistributions in binary form must reproduce the above copyright
12c969310cSDmitry Chagin *    notice, this list of conditions and the following disclaimer in the
13c969310cSDmitry Chagin *    documentation and/or other materials provided with the distribution.
14c969310cSDmitry Chagin *
15c969310cSDmitry Chagin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16c969310cSDmitry Chagin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17c969310cSDmitry Chagin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18c969310cSDmitry Chagin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19c969310cSDmitry Chagin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20c969310cSDmitry Chagin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21c969310cSDmitry Chagin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22c969310cSDmitry Chagin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23c969310cSDmitry Chagin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24c969310cSDmitry Chagin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25c969310cSDmitry Chagin * SUCH DAMAGE.
26c969310cSDmitry Chagin */
27c969310cSDmitry Chagin
28c969310cSDmitry Chagin#include <machine/asm.h>
29c969310cSDmitry Chagin
30c969310cSDmitry Chagin
31c969310cSDmitry Chagin	.text
32c969310cSDmitry Chagin	.align 8
33c969310cSDmitry Chagin
34c969310cSDmitry Chagin	/*
35c969310cSDmitry Chagin	 * The program entry point
36c969310cSDmitry Chagin	 *		  %rdi		%rsi
37c969310cSDmitry Chagin	 * void _start(char **ap, void (*cleanup)(void)) __dead2
38c969310cSDmitry Chagin	 */
39c969310cSDmitry Chagin	.globl	_start
40c969310cSDmitry Chagin	.type	_start, @function
41c969310cSDmitry Chagin_start:
42c969310cSDmitry Chagin	.cfi_startproc
43c969310cSDmitry Chagin	.cfi_undefined %rip		/* Terminate call chain. */
44c969310cSDmitry Chagin	pushq	%rbp			/* Align stack, terminate call chain. */
45c969310cSDmitry Chagin	.cfi_def_cfa_offset 8
46c969310cSDmitry Chagin	movq	%rsp, %rbp
47c969310cSDmitry Chagin	.cfi_offset %rbp, -16
48c969310cSDmitry Chagin	.cfi_def_cfa_register %rbp
49c969310cSDmitry Chagin#ifdef GCRT
50c969310cSDmitry Chagin	subq	$16, %rsp
51c969310cSDmitry Chagin#endif
52*cb991a22SKonstantin Belousov	movq	%rsi, %rcx		/* cleanup */
53*cb991a22SKonstantin Belousov	movslq	(%rdi), %rax		/* long *ap; tmpargc = *ap */
54*cb991a22SKonstantin Belousov	leaq	0x8(%rdi), %rsi		/* argv = ap + 1 */
55*cb991a22SKonstantin Belousov	leaq	0x10(%rdi, %rax, 8), %rdx /* env = ap + 2 + tmpargc */
56*cb991a22SKonstantin Belousov	movl	%eax, %edi		/* argc = tmpargc */
57*cb991a22SKonstantin Belousov
58c969310cSDmitry Chagin#ifdef PIC
59c969310cSDmitry Chagin	/*
60e5c47379SJohn Baldwin	 * XXX. %rip relative addressing is not intended for use in the
61e5c47379SJohn Baldwin	 * large memory model due to the offset from %rip being limited
62e5c47379SJohn Baldwin	 * to 32 bits.
63c969310cSDmitry Chagin	 */
64e5c47379SJohn Baldwin	leaq	main@plt(%rip), %r8
65c969310cSDmitry Chagin#else
66c969310cSDmitry Chagin	movabsq	$main, %r8
67c969310cSDmitry Chagin#endif
68c969310cSDmitry Chagin#ifdef GCRT
69c969310cSDmitry Chagin	movabsq	$eprol, %r9
70c969310cSDmitry Chagin	movabsq	$etext, %rax
71c969310cSDmitry Chagin	movq    %rax, (%rsp)
72c969310cSDmitry Chagin	/*
73c969310cSDmitry Chagin	 *		      %edi  %rsi  %rdx  %rcx    %r8    %r9    (%rsp)
74c969310cSDmitry Chagin	 * __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext)
75c969310cSDmitry Chagin	 */
76c969310cSDmitry Chagin	callq	__libc_start1_gcrt
77c969310cSDmitry Chagineprol:
78c969310cSDmitry Chagin#else
79c969310cSDmitry Chagin	/* __libc_start1(argc, argv, env, cleanup, main) */
80c969310cSDmitry Chagin	callq	__libc_start1
81c969310cSDmitry Chagin#endif
82c969310cSDmitry Chagin	int3
83c969310cSDmitry Chagin	.cfi_endproc
84c969310cSDmitry Chagin	.size   _start, . - _start
85c969310cSDmitry Chagin
86c969310cSDmitry Chagin	.section .note.GNU-stack,"",%progbits
87