Rizin
unix-like reverse engineering framework and cli tools
sfsyscall.h
Go to the documentation of this file.
1 /* sfsyscall.h --- SFLib syscall macros for Linux/arm64 - pancake */
2 
3 #ifndef SFSYSCALL_H
4 #define SFSYSCALL_H
5 
6 /* $Id$ */
7 
8 #ifdef SF_USE_ERRNO
9 
10 static int errno = 1234;
11 
12 #define __sfsyscall_return(type, res) \
13  do { \
14  if ((unsigned long)(res) >= (unsigned long)(-125)) { \
15  errno = -(res); \
16  res = -1; \
17  } \
18  return (type)(res); \
19  } while (0)
20 
21 #else /* SF_USE_ERRNO */
22 
23 #define __sfsyscall_return(type, res) \
24  do { \
25  return (type)(res); \
26  } while (0)
27 
28 #endif /* SF_USE_ERRNO */
29 
30 /* syscall macros */
31 
32 #define _sfsyscall0(type, name) \
33  type name(void) { \
34  long __res; \
35  __asm__ volatile("###> " #name " <###\n\t" \
36  "svc 0" \
37  : "=a"(__res) \
38  : "0"(__NR_##name) \
39  : "memory", "cc"); \
40  __sfsyscall_return(type, __res); \
41  }
42 
43 #define _sfsyscall1(type, name, type1, arg1) \
44  type name(type1 arg1) { \
45  long __res; \
46  register type1 r1 asm("r0"); \
47  r1 = arg1; \
48  asm volatile("###> " #name "(%2) <###\n\t" \
49  "mov r7, %0\n\t" \
50  "svc 0" \
51  : "=g"(__res) \
52  : "0"(__NR_##name), "r"(r1)); \
53  __sfsyscall_return(type, __res); \
54  }
55 
56 #define _sfsyscall2(type, name, type1, arg1, type2, arg2) \
57  type name(type1 arg1, type2 arg2) { \
58  long __res; \
59  register type1 r1 asm("r1"); \
60  register type2 r2 asm("r2"); \
61  r1 = arg1; \
62  r2 = arg2; \
63  __asm__ volatile("###> " #name "(%2, %3) <###\n\t" \
64  "svc 0" \
65  : "=g"(__res) \
66  : "0"(__NR_##name), \
67  "r"(r1), \
68  "r"(r2)); \
69  __sfsyscall_return(type, __res); \
70  }
71 
72 #define _sfsyscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
73  type name(type1 arg1, type2 arg2, type3 arg3) { \
74  long __res; \
75  register type1 r0 asm("r0"); \
76  register type2 r1 asm("r1"); \
77  register type3 r2 asm("r2"); \
78  r0 = arg1; \
79  r1 = arg2; \
80  r2 = arg3; \
81  __asm__ __volatile__("###> " #name "(%2, %3, %4) <###\n\t" \
82  "mov r7, %1\n\t" \
83  "svc 0" \
84  : "=g"(__res) \
85  : "0"(__NR_##name), \
86  "r"(r0), \
87  "r"(r1), \
88  "r"(r2) \
89  : "memory", "cc"); \
90  __sfsyscall_return(type, __res); \
91  }
92 
93 #define _sfsyscall4(type, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
94  type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
95  long __res; \
96  register type1 r1 asm("r1"); \
97  ; \
98  register type2 r2 asm("r2"); \
99  register type3 r3 asm("r3"); \
100  register type4 r4 asm("r4"); \
101  r1 = arg1; \
102  r2 = arg2; \
103  r3 = arg3; \
104  r4 = arg4; \
105  asm volatile("###> " #name "(%2, %3, %4, %5) <###\n\t" \
106  "svc 0" \
107  : "=g"(__res) \
108  : "0"(__NR_##name), \
109  "r"(r1), \
110  "r"(r2), \
111  "r"(r3), \
112  "r"(r4) \
113  : "memory", "cc"); \
114  __sfsyscall_return(type, __res); \
115  }
116 
117 #define _sfsyscall5(type, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5) \
118  type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
119  type5 arg5) { \
120  long __res; \
121  register type1 r1 asm("r1"); \
122  register type2 r2 asm("r2"); \
123  register type3 r3 asm("r3"); \
124  register type4 r4 asm("r4"); \
125  register type5 r5 asm("r5"); \
126  r1 = arg1; \
127  r2 = arg2; \
128  r3 = arg3; \
129  r4 = arg4; \
130  r5 = arg5; \
131  asm volatile("###> " #name "(%2, %3, %4, %5, %6) <###\n\t" \
132  "svc 0" \
133  : "=g"(__res) \
134  : "0"(__NR_##name), \
135  "r"(r1), \
136  "r"(r2), \
137  "r"(r3), \
138  "r"(r4), \
139  "r"(r5) \
140  : "memory", "cc"); \
141  __sfsyscall_return(type, __res); \
142  }
143 
144 #define _sfsyscall6(type, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5, type6, arg6) \
145  type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
146  type5 arg5, type6 arg6) { \
147  long __res; \
148  register type1 r1 asm("r1"); \
149  register type2 r2 asm("r2"); \
150  register type3 r3 asm("r3"); \
151  register type4 r4 asm("r4"); \
152  register type5 r5 asm("r5"); \
153  register type6 r6 asm("r6"); \
154  r1 = arg1; \
155  r2 = arg2; \
156  r3 = arg3; \
157  r4 = arg4; \
158  r5 = arg5; \
159  r6 = arg6; \
160  asm volatile("###> " #name "(%2, %3, %4, %5, %6, %7) <###\n\t" \
161  "svc 0" \
162  : "=a"(__res) \
163  : "0"(__NR_##name), \
164  "r"(r1), \
165  "r"(r2), \
166  "r"(r3), \
167  "r"(r4), \
168  "r"(r5), \
169  "r"(r6) \
170  : "memory", "cc"); \
171  __sfsyscall_return(type, __res); \
172  }
173 
174 #endif /* SFSYSCALL_H */