Skip to content

Commit c7b64af

Browse files
committed
Split out define_rust_probestack!, specialize for apple
1 parent 10ffd32 commit c7b64af

File tree

1 file changed

+73
-36
lines changed

1 file changed

+73
-36
lines changed

src/probestack.rs

+73-36
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,75 @@ extern "C" {
4747
pub fn __rust_probestack();
4848
}
4949

50+
// A wrapper for our implementation of __rust_probestack, which allows us to
51+
// keep the assembly inline while controlling all CFI directives in the assembly
52+
// emitted for the function.
53+
//
54+
// This is the ELF version.
55+
#[cfg(all(
56+
any(target_arch = "x86_64", target_arch = "x86"),
57+
not(feature = "mangled-names"),
58+
not(target_vendor = "apple")
59+
))]
60+
macro_rules! define_rust_probestack {
61+
($body: expr) => {
62+
concat!(
63+
"
64+
// We are about to define a 'function within a function.' Because the
65+
// compiler will have emitted a .cfi_startproc at the beginning of
66+
// __rust_probestack_wrapper, we need .cfi_endproc before we can define
67+
// the contents of __rust_probestack.
68+
.cfi_endproc
69+
70+
.pushsection .text.__rust_probestack
71+
.globl __rust_probestack
72+
.type __rust_probestack, @function
73+
__rust_probestack:
74+
.cfi_startproc
75+
76+
",
77+
$body,
78+
"
79+
80+
.cfi_endproc
81+
82+
.size __rust_probestack, . - __rust_probestack
83+
.popsection
84+
85+
// Similar to above, we add .cfi_startproc here to match the
86+
// .cfi_endproc emitted at the end of __rust_probestack_wrapper.
87+
.cfi_startproc
88+
"
89+
)
90+
};
91+
}
92+
93+
// Same as above, but for Mach-O.
94+
#[cfg(all(
95+
any(target_arch = "x86_64", target_arch = "x86"),
96+
not(feature = "mangled-names"),
97+
target_vendor = "apple"
98+
))]
99+
macro_rules! define_rust_probestack {
100+
($body: expr) => {
101+
concat!(
102+
"
103+
.cfi_endproc
104+
.globl __rust_probestack
105+
__rust_probestack:
106+
.cfi_startproc
107+
108+
",
109+
$body,
110+
"
111+
112+
.cfi_endproc
113+
.cfi_startproc
114+
"
115+
)
116+
};
117+
}
118+
50119
#[naked]
51120
#[no_mangle]
52121
#[cfg(all(target_arch = "x86_64", not(feature = "mangled-names")))]
@@ -56,18 +125,7 @@ pub unsafe extern "C" fn __rust_probestack_wrapper() {
56125
//
57126
// The ABI here is that the stack frame size is located in `%rax`. Upon
58127
// return we're not supposed to modify `%rsp` or `%rax`.
59-
asm!("
60-
// We are about to define a 'function within a function.' Because the
61-
// compiler will have emitted a .cfi_startproc at the beginning of
62-
// __rust_probestack_wrapper, we need .cfi_endproc before we can define
63-
// the contents of __rust_probestack.
64-
.cfi_endproc
65-
66-
.pushsection .text.__rust_probestack
67-
.globl __rust_probestack
68-
.type __rust_probestack, @function
69-
__rust_probestack:
70-
.cfi_startproc
128+
asm!(define_rust_probestack!("
71129
pushq %rbp
72130
.cfi_adjust_cfa_offset 8
73131
.cfi_offset %rbp, -16
@@ -114,15 +172,7 @@ pub unsafe extern "C" fn __rust_probestack_wrapper() {
114172
.cfi_def_cfa_register %rsp
115173
.cfi_adjust_cfa_offset -8
116174
ret
117-
.cfi_endproc
118-
119-
.size __rust_probestack, . - __rust_probestack
120-
.popsection
121-
122-
// Similar to above, we add .cfi_startproc here to match the
123-
// .cfi_endproc emitted at the end of __rust_probestack_wrapper.
124-
.cfi_startproc
125-
" ::: "memory" : "volatile");
175+
") ::: "memory" : "volatile");
126176
::core::intrinsics::unreachable();
127177
}
128178

@@ -135,14 +185,7 @@ pub unsafe extern "C" fn __rust_probestack_wrapper() {
135185
// function basically can't tamper with anything.
136186
//
137187
// The ABI here is the same as x86_64, except everything is 32-bits large.
138-
asm!("
139-
.cfi_endproc
140-
141-
.pushsection .text.__rust_probestack
142-
.globl __rust_probestack
143-
.type __rust_probestack, @function
144-
__rust_probestack:
145-
.cfi_startproc
188+
asm!(define_rust_probestack!("
146189
push %ebp
147190
.cfi_adjust_cfa_offset 4
148191
.cfi_offset %ebp, -8
@@ -170,12 +213,6 @@ pub unsafe extern "C" fn __rust_probestack_wrapper() {
170213
.cfi_def_cfa_register %esp
171214
.cfi_adjust_cfa_offset -4
172215
ret
173-
.cfi_endproc
174-
175-
.size __rust_probestack, . - __rust_probestack
176-
.popsection
177-
178-
.cfi_startproc
179-
" ::: "memory" : "volatile");
216+
") ::: "memory" : "volatile");
180217
::core::intrinsics::unreachable();
181218
}

0 commit comments

Comments
 (0)