summaryrefslogtreecommitdiff
path: root/libgcc/config/i386/cygwin.S
blob: e121f2a2cf03118927c21f4ec55f952719ecd0f9 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* stuff needed for libgcc on win32.
 *
 *   Copyright (C) 1996-2017 Free Software Foundation, Inc.
 *   Written By Steve Chamberlain
 * 
 * This file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 * 
 * This file 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
 * General Public License for more details.
 * 
 * Under Section 7 of GPL version 3, you are granted additional
 * permissions described in the GCC Runtime Library Exception, version
 * 3.1, as published by the Free Software Foundation.
 * 
 * You should have received a copy of the GNU General Public License and
 * a copy of the GCC Runtime Library Exception along with this program;
 * see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include "auto-host.h"

#ifdef HAVE_GAS_CFI_SECTIONS_DIRECTIVE
	.cfi_sections	.debug_frame
# define cfi_startproc()		.cfi_startproc
# define cfi_endproc()			.cfi_endproc
# define cfi_adjust_cfa_offset(X) 	.cfi_adjust_cfa_offset X
# define cfi_def_cfa_register(X)	.cfi_def_cfa_register X
# define cfi_register(D,S)		.cfi_register D, S
# ifdef __x86_64__
#  define cfi_push(X)		.cfi_adjust_cfa_offset 8; .cfi_rel_offset X, 0
#  define cfi_pop(X)		.cfi_adjust_cfa_offset -8; .cfi_restore X
# else
#  define cfi_push(X)		.cfi_adjust_cfa_offset 4; .cfi_rel_offset X, 0
#  define cfi_pop(X)		.cfi_adjust_cfa_offset -4; .cfi_restore X
# endif
#else
# define cfi_startproc()
# define cfi_endproc()
# define cfi_adjust_cfa_offset(X)
# define cfi_def_cfa_register(X)
# define cfi_register(D,S)
# define cfi_push(X)
# define cfi_pop(X)
#endif /* HAVE_GAS_CFI_SECTIONS_DIRECTIVE */

#ifdef L_chkstk
/* Function prologue calls __chkstk to probe the stack when allocating more
   than CHECK_STACK_LIMIT bytes in one go.  Touching the stack at 4K
   increments is necessary to ensure that the guard pages used
   by the OS virtual memory manger are allocated in correct sequence.  */

	.global ___chkstk
	.global	__alloca
#ifdef __x86_64__
/* __alloca is a normal function call, which uses %rcx as the argument.  */
	cfi_startproc()
__alloca:
	movq	%rcx, %rax
	/* FALLTHRU */

/* ___chkstk is a *special* function call, which uses %rax as the argument.
   We avoid clobbering the 4 integer argument registers, %rcx, %rdx, 
   %r8 and %r9, which leaves us with %rax, %r10, and %r11 to use.  */
	.align	4
___chkstk:
	popq	%r11			/* pop return address */
	cfi_adjust_cfa_offset(-8)	/* indicate return address in r11 */
	cfi_register(%rip, %r11)
	movq	%rsp, %r10
	cmpq	$0x1000, %rax		/* > 4k ?*/
	jb	2f

1:	subq	$0x1000, %r10  		/* yes, move pointer down 4k*/
	orl	$0x0, (%r10)   		/* probe there */
	subq	$0x1000, %rax  	 	/* decrement count */
	cmpq	$0x1000, %rax
	ja	1b			/* and do it again */

2:	subq	%rax, %r10
	movq	%rsp, %rax		/* hold CFA until return */
	cfi_def_cfa_register(%rax)
	orl	$0x0, (%r10)		/* less than 4k, just peek here */
	movq	%r10, %rsp		/* decrement stack */

	/* Push the return value back.  Doing this instead of just
	   jumping to %r11 preserves the cached call-return stack
	   used by most modern processors.  */
	pushq	%r11
	ret
	cfi_endproc()
#else
	cfi_startproc()
___chkstk:
__alloca:
	pushl	%ecx			/* save temp */
	cfi_push(%eax)
	leal	8(%esp), %ecx		/* point past return addr */
	cmpl	$0x1000, %eax		/* > 4k ?*/
	jb	2f

1:	subl	$0x1000, %ecx  		/* yes, move pointer down 4k*/
	orl	$0x0, (%ecx)   		/* probe there */
	subl	$0x1000, %eax  	 	/* decrement count */
	cmpl	$0x1000, %eax
	ja	1b			/* and do it again */

2:	subl	%eax, %ecx	   
	orl	$0x0, (%ecx)		/* less than 4k, just peek here */
	movl	%esp, %eax		/* save current stack pointer */
	cfi_def_cfa_register(%eax)
	movl	%ecx, %esp		/* decrement stack */
	movl	(%eax), %ecx		/* recover saved temp */

	/* Copy the return register.  Doing this instead of just jumping to
	   the address preserves the cached call-return stack used by most
	   modern processors.  */
	pushl	4(%eax)
	ret
	cfi_endproc()
#endif /* __x86_64__ */
#endif /* L_chkstk */

#ifdef L_chkstk_ms
/* ___chkstk_ms is a *special* function call, which uses %rax as the argument.
   We avoid clobbering any registers.  Unlike ___chkstk, it just probes the
   stack and does no stack allocation.  */
	.global ___chkstk_ms
#ifdef __x86_64__
	cfi_startproc()
___chkstk_ms:
	pushq	%rcx			/* save temps */
	cfi_push(%rcx)
	pushq	%rax
	cfi_push(%rax)
	cmpq	$0x1000, %rax		/* > 4k ?*/
	leaq	24(%rsp), %rcx		/* point past return addr */
	jb	2f

1:	subq	$0x1000, %rcx  		/* yes, move pointer down 4k */
	orq	$0x0, (%rcx)   		/* probe there */
	subq	$0x1000, %rax  	 	/* decrement count */
	cmpq	$0x1000, %rax
	ja	1b			/* and do it again */

2:	subq	%rax, %rcx
	orq	$0x0, (%rcx)		/* less than 4k, just peek here */

	popq	%rax
	cfi_pop(%rax)
	popq	%rcx
	cfi_pop(%rcx)
	ret
	cfi_endproc()
#else
	cfi_startproc()
___chkstk_ms:
	pushl	%ecx			/* save temp */
	cfi_push(%ecx)
	pushl	%eax
	cfi_push(%eax)
	cmpl	$0x1000, %eax		/* > 4k ?*/
	leal	12(%esp), %ecx		/* point past return addr */
	jb	2f

1:	subl	$0x1000, %ecx  		/* yes, move pointer down 4k*/
	orl	$0x0, (%ecx)   		/* probe there */
	subl	$0x1000, %eax  	 	/* decrement count */
	cmpl	$0x1000, %eax
	ja	1b			/* and do it again */

2:	subl	%eax, %ecx
	orl	$0x0, (%ecx)		/* less than 4k, just peek here */

	popl	%eax
	cfi_pop(%eax)
	popl	%ecx
	cfi_pop(%ecx)
	ret
	cfi_endproc()
#endif /* __x86_64__ */
#endif /* L_chkstk_ms */