summaryrefslogtreecommitdiff
path: root/libc/sysdeps/linux/bfin/crt1.S
blob: f091a733dbc4490ced6ff3ad1b0df4acc781da64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.

This file is part of the GNU C Library.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB.  If
not, see <http://www.gnu.org/licenses/>.  */


/* When we enter this piece of code, the user stack looks like this:
*      argc            argument counter (integer)
*      argv[0]         program name (pointer)
*      argv[1...N]     program args (pointers)
*      NULL
*      env[0...N]      environment variables (pointers)
*      NULL
	
*   When we are done here, we want
*	R0=argc
*	R1=*argv[0]
*	R2=*envp[0]  
*/

#include <features.h>

#undef USE_GOT
#if defined (__UCLIBC_FORMAT_SHARED_FLAT__) || defined (__UCLIBC_FORMAT_FLAT_SEP_DATA__)
#define USE_GOT
#endif

#if !(defined L_Scrt1 && defined __UCLIBC_FORMAT_SHARED_FLAT__)

.text
.align 2
.global __start;
.type	__start,STT_FUNC;
.weak	__init;
.weak	__fini;
.global	___uClibc_main;
.type	___uClibc_main,STT_FUNC;

/* Stick in a dummy reference to main(), so that if an application
 * is linking when the main() function is in a static library (.a)
 * we can be sure that main() actually gets linked in */

.type	_main,STT_FUNC;

__start:

#if defined(__BFIN_FDPIC__) && !defined(L_Scrt1)
	/* P0 contains a pointer to the program's load map.  */
	call	.Lcall;
.Lcall:
	R4 = RETS;
	SP += -12;
	R0.L = .Lcall;
	R0.H = .Lcall;
	R1.L = __ROFIXUP_LIST__;
	R1.H = __ROFIXUP_LIST__;
	R2.L = __ROFIXUP_END__;
	R2.H = __ROFIXUP_END__;
	R1 = R1 - R0;
	R1 = R1 + R4;
	R2 = R2 - R0;
	R2 = R2 + R4;
	R0 = P0;
	CALL	___self_reloc;
	SP += 12;
	P3 = R0;
#endif

/*	clear the frame pointer and the L registers.  */
	FP = 0;
	L0 = 0;
	L1 = 0;
	L2 = 0;
	L3 = 0;

#ifdef __ID_SHARED_LIB__
	/* We know we have a local copy, so we can avoid the GOT.  */
	CALL ___shared_flat_add_library;
#endif
/*	Load register R1 (argc) from the stack to its final resting place */
	P0 = SP;
	R1 = [P0++];

/*	Copy argv pointer into R2 -- which its final resting place */
	R2 = P0;

	SP += -28;

#ifndef __BFIN_FDPIC__
	R7 = 0;
#endif
	/* Pass highest stack pointer to the app.  */
	[SP + 24] = P2;
	/* Store the pointer to ld.so's fini that we got in P1.  */
	[SP + 20] = R7;

/*	Ok, now run uClibc's main() -- shouldn't return */
#if (defined L_crt1 || defined L_Scrt1) && defined __UCLIBC_CTOR_DTOR__

#ifdef __BFIN_FDPIC__
	R3 = [P3 + __init@FUNCDESC_GOT17M4];
#elif defined USE_GOT
	R3 = [P5 + ___shared_flat_init@GOT];
#else
	R3.H = __init;
	R3.L = __init;
#endif
	[SP+12] = R3;


#ifdef __BFIN_FDPIC__
	R3 = [P3 + __fini@FUNCDESC_GOT17M4];
#elif defined USE_GOT
	R3 = [P5 + ___shared_flat_fini@GOT];
#else	
	R3.H = __fini;
	R3.L = __fini;
#endif
	[SP+16] = R3;
#else /* no ctor/dtor handling */
	R3 = 0;
	[SP + 12] = R3;
	[SP + 16] = R3;
#endif

#ifdef __BFIN_FDPIC__
	R0 = [P3 + _main@FUNCDESC_GOT17M4];
#elif defined USE_GOT
	R0 = [P5 + _main@GOT];
#else
	R0.H = _main;
	R0.L = _main;
#endif
#ifdef USE_GOT
	P0 = [P5 + ___uClibc_main@GOT];
	jump (P0)
#else
	jump.l	___uClibc_main;
#endif

#else
	.text
	.global lib_main
	.hidden lib_main
	.type lib_main,@function
lib_main:
	RETS = [SP++];
	/* We know we have a local copy, so we can avoid the GOT.  */
	JUMP.L ___shared_flat_add_library;

	.hidden _current_shared_library_p5_offset_
#endif

.section .note.GNU-stack,"",%progbits