1/* Copyright (C) 2011-2018 Free Software Foundation, Inc.
2
3   The GNU C Library is free software; you can redistribute it and/or
4   modify it under the terms of the GNU Lesser General Public
5   License as published by the Free Software Foundation; either
6   version 2.1 of the License, or (at your option) any later version.
7
8   In addition to the permissions in the GNU Lesser General Public
9   License, the Free Software Foundation gives you unlimited
10   permission to link the compiled version of this file with other
11   programs, and to distribute those programs without any restriction
12   coming from the use of this file. (The GNU Lesser General Public
13   License restrictions do apply in other respects; for example, they
14   cover modification of the file, and distribution when not linked
15   into another program.)
16
17   Note that people who make modified versions of this file are not
18   obligated to grant this special exception for their modified
19   versions; it is their choice whether to do so. The GNU Lesser
20   General Public License gives permission to release a modified
21   version without this exception; this exception also makes it
22   possible to release a modified version which carries forward this
23   exception.
24
25   The GNU C Library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28   Lesser General Public License for more details.
29
30   You should have received a copy of the GNU Lesser General Public
31   License along with the GNU C Library.  If not, see
32   <http://www.gnu.org/licenses/>.  */
33
34/* This is the canonical entry point, usually the first thing in the text
35   segment.  The ELF standard tells us that the stack is set up like this on
36   entry (the left side is the offset from "sp"), in units of
37   __SIZEOF_POINTER__ entries:
38
39		+0		argc
40		+1		argv[0]
41		...
42		+(argc+1)	NULL
43		+(argc+2)	envp[0]
44		...
45				NULL
46		...		ElfInfo
47
48   The ElfInfo is pairs of key/value long words following the envp
49   pointers and terminated by a zero-valued key.
50
51   Although not mandated by the standard, it happens to be the case
52   that we store the actual argv and envp strings immediately after
53   the ElfInfo data on the stack.
54
55   On entry r0 points to the shared library termination function, or 0
56   if there isn't one.
57*/
58
59#include <features.h>
60#include <sysdep.h>
61#include <arch/abi.h>
62
63/* Just create no-ops if we don't support PC-relative PLT relocations. */
64#ifdef NO_PLT_PCREL
65# define hw2_last_plt(x)	0
66# define hw1_plt(x)		0
67# define hw0_plt(x)		0
68#endif
69
70	.text
71	.global _start
72	.type   _start,@function
73	.align 8
74_start:
75	/* Linux starts us with sp pointing at the conventional Elf layout,
76	   but we need to allow two "caller" words for our ABI convention.  */
77	{
78	 /* Load argc (stored as a "long", equivalent to a pointer type). */
79	 LD_PTR r1, sp
80
81	 /* Save incoming 'sp', which points to the Elf argument block. */
82	 move r52, sp
83	}
84
85	{
86	 /* Allocate stack frame callee space for __libc_start_main. */
87	 ADDI_PTR r12, sp, -(2 * REGSIZE)
88	}
89
90	{
91	 /* Get our PC. */
92	 lnk r13
93
94	 /* sp is not necessarily properly aligned on startup because
95	    of the way ld.so pops off leading argv elements. So align it. */
96	 andi sp, r12, -8
97	}
98.Lmy_pc:
99
100	{
101	 /* Pass the address of the shared library termination function. */
102	 move r5, r0
103
104	 /* Compute location where __libc_start_main's caller is supposed to
105	    store its frame pointer. */
106	 ADDI_PTR r12, sp, REGSIZE
107
108	 /* Zero out callee space for return address. Unnecessary but free.
109	    This is just paranoia to help backtracing not go awry. */
110	 st sp, zero
111	}
112	{
113	 /* Zero out our frame pointer for __libc_start_main. */
114	 st r12, zero
115
116	 /* Zero out lr to make __libc_start_main the end of backtrace.  */
117	 move lr, zero
118
119	 /* Compute a pointer to argv. envp will be determined
120	    later in __libc_start_main.  We set up the first argument
121	    (the address of main) below. */
122	 ADDI_PTR r2, r52, __SIZEOF_POINTER__
123	}
124	{
125	 /* Pass the highest stack address to user code. */
126	 ADDI_PTR r6, sp, (2 * REGSIZE)
127
128	 /* Pass address of main() in r0, and of our own entry
129	    points to .fini and .init in r3 and r4.  */
130	 moveli r0, hw2_last(main - .Lmy_pc)
131	}
132	{
133	 shl16insli r0, r0, hw1(main - .Lmy_pc)
134	 moveli r3, hw2_last(_init - .Lmy_pc)
135	}
136	{
137	 shl16insli r0, r0, hw0(main - .Lmy_pc)
138	 shl16insli r3, r3, hw1(_init - .Lmy_pc)
139	}
140	{
141	 ADD_PTR r0, r0, r13
142	 shl16insli r3, r3, hw0(_init - .Lmy_pc)
143	}
144	{
145	 moveli r12, hw2_last_plt(__uClibc_main - .Lmy_pc)
146	 ADD_PTR r3, r3, r13
147	}
148	{
149	 shl16insli r12, r12, hw1_plt(__uClibc_main - .Lmy_pc)
150	 moveli r4, hw2_last(_fini - .Lmy_pc)
151	}
152	{
153	 shl16insli r12, r12, hw0_plt(__uClibc_main - .Lmy_pc)
154	 shl16insli r4, r4, hw1(_fini - .Lmy_pc)
155	}
156	{
157	 ADD_PTR r12, r12, r13
158	 shl16insli r4, r4, hw0(_fini - .Lmy_pc)
159	}
160	{
161	 ADD_PTR r4, r4, r13
162#ifdef NO_PLT_PCREL
163	 j plt(__uClibc_main)
164#else
165	 jr r12
166#endif
167	}
168
169	{
170	 /* Tell backtracer to give up (_start has no caller). */
171	 info INFO_OP_CANNOT_BACKTRACE
172	}
173.size _start, .-_start
174
175/* Define a symbol for the first piece of initialized data.  */
176	.data
177	.global __data_start
178	.align 8
179__data_start:
180	.long 0
181	.weak data_start
182	data_start = __data_start
183