]> git.lizzy.rs Git - rust.git/blob - src/rt/arch/x86_64/morestack.S
rt: Write CFI instructions that (might) work on mac in __morestack
[rust.git] / src / rt / arch / x86_64 / morestack.S
1     .text
2
3 // __morestack
4 //
5 // LLVM generates a call to this to allocate more stack space in a functiono
6 // prolog when we run out.
7
8 #if defined(__APPLE__) || defined(_WIN32)
9 #define UPCALL_NEW_STACK        _upcall_new_stack
10 #define UPCALL_DEL_STACK        _upcall_del_stack
11 #define UPCALL_CALL_C           _upcall_call_shim_on_c_stack
12 #define MORESTACK               ___morestack
13 #else
14 #define UPCALL_NEW_STACK        upcall_new_stack
15 #define UPCALL_DEL_STACK        upcall_del_stack
16 #define UPCALL_CALL_C           upcall_call_shim_on_c_stack
17 #define MORESTACK               __morestack
18 #endif
19
20         // Naturally, nobody can agree as to
21         // which arguments should go in which
22         // registers:
23 #if defined(_WIN32)
24 #  define ARG0 %rcx
25 #  define ARG1 %rdx
26 #  define ARG2 %r8
27 #else
28 #  define ARG0 %rdi
29 #  define ARG1 %rsi
30 #  define ARG2 %rdx
31 #endif
32
33 .globl UPCALL_NEW_STACK
34 .globl UPCALL_DEL_STACK
35 .globl UPCALL_CALL_C
36 .globl MORESTACK
37
38 // FIXME: What about _WIN32?    
39 #if defined(__linux__)
40         .hidden MORESTACK
41 #else
42 #if defined(__APPLE__)
43         .private_extern MORESTACK
44 #endif
45 #endif
46
47 #ifdef __ELF__
48         .type MORESTACK,@function
49 #endif
50
51 #if defined(__linux__) || defined(__APPLE__)
52 MORESTACK:
53 #if defined(__ELF__)
54         .cfi_startproc
55 #endif
56         
57         // Set up a normal backtrace
58         pushq %rbp
59 #if defined(__ELF__)
60         .cfi_def_cfa_offset 16
61         .cfi_offset %rbp, -16
62 #endif
63         movq %rsp, %rbp
64 #if defined(__ELF__)
65         .cfi_def_cfa_register %rbp
66 #endif
67
68         // During unwinding we want to skip our caller since it's not
69         // a complete frame and will make the unwinder sad
70 #if defined(__linux__)
71         // Don't understand this line
72         .cfi_offset 16, 0
73         // Tell the unwinding where to get the stack pointer for
74         // our grandparent frame
75         .cfi_offset %rsp, -24
76 #endif
77
78         // Save the grandparent stack pointer for the unwinder
79         leaq 16(%rbp), %rax
80         pushq %rax
81
82         // FIXME: libgcc also saves rax. not sure if we need to
83
84         // Save argument registers
85         pushq   %rdi
86         pushq   %rsi
87         pushq   %rdx
88         pushq   %rcx
89         pushq   %r8
90         pushq   %r9
91
92         // Calculate the address of the stack arguments.
93         // We have the base pointer, __morestack's return address,
94         // and __morestack's caller's return address to skip
95         movq %rbp, %rcx
96         addq $24, %rcx  // Base pointer, return address x2
97
98         pushq %r11 // Size of stack arguments
99         pushq %rcx // Address of stack arguments
100         pushq %r10 // The amount of stack needed
101         pushq $0   // Out pointer
102
103         movq UPCALL_NEW_STACK@GOTPCREL(%rip), %rsi
104         movq %rsp, %rdi
105 #ifdef __APPLE__
106         call UPCALL_CALL_C
107 #endif
108 #ifdef __linux__
109         call UPCALL_CALL_C@PLT
110 #endif
111
112         // Pop the new_stack_args struct
113         popq %rax
114         addq $24, %rsp
115
116         // Pop the saved arguments
117         popq %r9
118         popq %r8
119         popq %rcx
120         popq %rdx
121         popq %rsi
122         popq %rdi
123
124         // Pop the unwinding %rsp
125         addq $8, %rsp
126
127         movq 8(%rbp),%r10       // Grab the return pointer.
128         incq %r10               // Skip past the `ret` in our parent frame
129         movq %rax,%rsp          // Switch to the new stack.
130
131         call *%r10              // Reenter the caller function
132
133         // Switch back to the rust stack
134         movq %rbp, %rsp
135
136         // Align the stack again
137         pushq $0
138         
139         movq UPCALL_DEL_STACK@GOTPCREL(%rip), %rsi
140         movq $0, %rdi
141 #ifdef __APPLE__
142         call UPCALL_CALL_C
143 #endif
144 #ifdef __linux__
145         call UPCALL_CALL_C@PLT
146 #endif
147
148         addq $8, %rsp
149         popq %rbp
150 #ifdef __linux__
151         .cfi_restore %rbp
152         .cfi_def_cfa %rsp, 8
153 #endif
154         ret
155         
156 #if defined(__ELF__)
157         .cfi_endproc
158 #endif
159
160 #else
161 MORESTACK:
162         ret
163 #endif