aboutsummaryrefslogtreecommitdiffstats
path: root/get_stack_direction.c
blob: 2f639da2838b2f1124e4c1cf7c86392aefffe6e5 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


#ifdef SUPPORTED
# undef SUPPORTED
#endif


#if defined(__GNUC__)
# define SUPPORTED

__attribute__((__noinline__))
static void *
get_callee_frame_address(void)
{
	return __builtin_frame_address(0);
}


__attribute__((__noinline__))
static int
get_stack_direction(void)
{
	void *callerptr, *calleeptr;
	uintptr_t caller, callee;
	callerptr = __builtin_frame_address(0);
	calleeptr = get_callee_frame_address();
	caller = (uintptr_t)callerptr;
	callee = (uintptr_t)calleeptr;
	return callee > caller ? +1 : callee < caller ? -1 : 0;
}

#endif


int
libsimple_get_stack_direction(void)
{
#ifdef SUPPORTED
	int r = get_stack_direction();
	if (!r)
		errno = ENOTSUP;
	return r;
#else
	errno = ENOTSUP;
	return 0;
#endif
}


#else
#include "test.h"

int
main(void)
{
	return 0;
}

#endif