-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminilib.c
373 lines (304 loc) · 7.66 KB
/
minilib.c
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <sys/syscall.h>
#include <sys/types.h>
#pragma once
typedef unsigned long long u64;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;
typedef long long i64;
typedef int32_t i32;
typedef int16_t i16;
typedef int8_t i8;
static __inline long __syscall0(long n)
{
unsigned long ret;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n) : "rcx", "r11", "memory");
return ret;
}
static __inline long __syscall1(long n, long a1)
{
unsigned long ret;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1) : "rcx", "r11", "memory");
return ret;
}
static __inline long __syscall2(long n, long a1, long a2)
{
unsigned long ret;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2)
: "rcx", "r11", "memory");
return ret;
}
static __inline long __syscall3(long n, long a1, long a2, long a3)
{
unsigned long ret;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
"d"(a3) : "rcx", "r11", "memory");
return ret;
}
static __inline long __syscall4(long n, long a1, long a2, long a3, long a4)
{
unsigned long ret;
register long r10 __asm__("r10") = a4;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
"d"(a3), "r"(r10): "rcx", "r11", "memory");
return ret;
}
static __inline long __syscall5(long n, long a1, long a2, long a3, long a4, long a5)
{
unsigned long ret;
register long r10 __asm__("r10") = a4;
register long r8 __asm__("r8") = a5;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
"d"(a3), "r"(r10), "r"(r8) : "rcx", "r11", "memory");
return ret;
}
static __inline long __syscall6(long n, long a1, long a2, long a3, long a4, long a5, long a6)
{
unsigned long ret;
register long r10 __asm__("r10") = a4;
register long r8 __asm__("r8") = a5;
register long r9 __asm__("r9") = a6;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
"d"(a3), "r"(r10), "r"(r8), "r"(r9) : "rcx", "r11", "memory");
return ret;
}
#define __scc(X) ((long) (X))
#define __syscall1(n,a) __syscall1(n,__scc(a))
#define __syscall2(n,a,b) __syscall2(n,__scc(a),__scc(b))
#define __syscall3(n,a,b,c) __syscall3(n,__scc(a),__scc(b),__scc(c))
#define __syscall4(n,a,b,c,d) __syscall4(n,__scc(a),__scc(b),__scc(c),__scc(d))
#define __syscall5(n,a,b,c,d,e) __syscall5(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e))
#define __syscall6(n,a,b,c,d,e,f) __syscall6(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),__scc(f))
#define __syscall7(n,a,b,c,d,e,f,g) (__syscall)(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),__scc(f),__scc(g))
#define __SYSCALL_NARGS_X(a,b,c,d,e,f,g,h,n,...) n
#define __SYSCALL_NARGS(...) __SYSCALL_NARGS_X(__VA_ARGS__,7,6,5,4,3,2,1,0,)
#define __SYSCALL_CONCAT_X(a,b) a##b
#define __SYSCALL_CONCAT(a,b) __SYSCALL_CONCAT_X(a,b)
#define __SYSCALL_DISP(b,...) __SYSCALL_CONCAT(b,__SYSCALL_NARGS(__VA_ARGS__))(__VA_ARGS__)
#define __syscall(...) __SYSCALL_DISP(__syscall,__VA_ARGS__)
#define syscall(...) __syscall_ret(__syscall(__VA_ARGS__))
#define PROT_READ 1
#define PROT_WRITE 2
#define PROT_EXEC 4
#define MAP_FILE 0
#define MAP_PRIVATE 0x02
#define MAP_FIXED 0x10
#define MAP_ANONYMOUS 0x20
#define O_RDONLY 0
#define ARCH_SET_GS 0x1001
#define ARCH_SET_FS 0x1002
#define ARCH_GET_FS 0x1003
#define ARCH_GET_GS 0x1004
#define SEEK_SET 0
#define SEEK_END 2
static long errno = 0;
i64 __syscall_ret(u64 r) {
if (r > -4096UL) {
errno = -r;
return -1;
}
return r;
}
static inline void dbgbreak(void) {
asm("int $3");
}
ssize_t write(int fd, uint8_t *buf, u64 size) {
int ret = syscall(SYS_write, fd, buf, size);
if (ret != size) {
return -1;
}
return ret;
}
void exit(int code) {
syscall(SYS_exit, code);
for (;;) { }
}
int open(char *pathname, int flags) {
return syscall(SYS_open, pathname, flags);
}
int close(int fd) {
return syscall(SYS_close, fd);
}
i64 lseek(int fd, i64 offset, u32 flag) {
return syscall(SYS_lseek, fd, offset, flag);
}
i64 read(int fd, uint8_t *buf, u64 size) {
return syscall(SYS_read, fd, buf, size);
}
int arch_prctl(int code, u8 *addr) {
u64 addr64;
void *prctl_arg = &addr64;
int ret = syscall(SYS_arch_prctl, code, prctl_arg);
if (!ret) {
if ((uintptr_t)addr64 != addr64) {
return -1;
}
*addr = (uintptr_t)addr64;
}
return ret;
}
void *mmap(void *start, size_t len, int prot, int flags, int fd, i64 off) {
return (void *)syscall(SYS_mmap, start, len, prot, flags, fd, off);
}
void *malloc(size_t size) {
int len = size + sizeof(size);
int *ptr_len = mmap(0, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
*ptr_len = len;
return (void *)(ptr_len + 1);
}
int itoa(uint64_t i, uint8_t base, uint8_t *buf) {
static const char bchars[] = "0123456789ABCDEF";
int pos = 0;
int o_pos = 0;
int top = 0;
uint8_t tbuf[64];
if (i == 0 || base > 16) {
buf[0] = '0';
buf[1] = '\0';
return 2;
}
while (i != 0) {
tbuf[pos] = bchars[i % base];
pos++;
i /= base;
}
top = pos--;
for (o_pos = 0; o_pos < top; pos--, o_pos++) {
buf[o_pos] = tbuf[pos];
}
buf[o_pos] = 0;
return o_pos + 1;
}
int putc(int fd, char c) {
return write(fd, (uint8_t *)&c, 1);
}
void print(int fd, char *c) {
uint8_t obuf[128];
int i = 0;
for (; *c != 0; i++) {
if (i > 64) {
write(fd, obuf, i);
i = 0;
}
obuf[i] = *c;
c++;
}
if (i > 0) {
write(fd, obuf, i);
}
}
void putn(int fd, u64 i, u8 base) {
uint8_t tbuf[64];
if (base > 16) return;
int size = itoa(i, base, tbuf);
write(fd, tbuf, size - 1);
}
void dprintf(int fd, char *fmt, ...) {
__builtin_va_list args;
__builtin_va_start(args, fmt);
u32 min_len = 0;
for (char *c = fmt; *c != 0; c++) {
if (*c != '%') {
uint8_t obuf[128];
int i = 0;
for (; *c != 0 && *c != '%'; i++) {
if (i > 128) {
write(fd, obuf, i);
i = 0;
}
obuf[i] = *c;
c++;
}
if (i > 0) {
write(fd, obuf, i);
}
c--;
continue;
}
consume_moar:
c++;
switch (*c) {
case '0': {
c++;
min_len = *c - '0';
goto consume_moar;
} break;
case 's': {
char *s = __builtin_va_arg(args, char *);
print(fd, s);
} break;
case 'd': {
i64 i = __builtin_va_arg(args, i64);
putn(fd, i, 10);
} break;
case 'x': {
u64 i = __builtin_va_arg(args, u64);
uint8_t tbuf[64];
int sz = itoa(i, 16, tbuf);
int pad_sz = min_len - (sz - 1);
while (pad_sz > 0) {
putc(fd, '0');
pad_sz--;
}
write(fd, tbuf, sz - 1);
min_len = 0;
} break;
case 'b': {
u64 i = __builtin_va_arg(args, u64);
uint8_t tbuf[64];
int sz = itoa(i, 2, tbuf);
int pad_sz = min_len - (sz - 1);
while (pad_sz > 0) {
putc(fd, '0');
pad_sz--;
}
write(fd, tbuf, sz - 1);
min_len = 0;
} break;
}
}
__builtin_va_end(args);
}
#define printf(...) do { dprintf(1, __VA_ARGS__); } while (0)
int memeq(const void *dst, const void *src, size_t n) {
const char *_dst = dst;
const char *_src = src;
for (size_t i = 0; i < n; i++) {
if (_src[i] != _dst[i]) {
return 0;
}
}
return 1;
}
void *memcpy(void *dst, const void *src, size_t n) {
if (n == 0) {
return dst;
}
char *_dst = dst;
const char *_src = src;
size_t i = n - 1;
while (i > 0) {
_dst[i] = _src[i];
i--;
}
_dst[0] = _src[0];
return dst;
}
void *memset(void *dst, int val, unsigned long n) {
char *_dst = dst;
for (size_t i = 0; i < n; i++) {
_dst[i] = val;
}
return dst;
}
void __stack_chk_fail(void) {
syscall(SYS_exit, 1);
}
int main(int, char **);
void __main(int argc, char **argv) {
int ret = main(argc, argv);
exit(ret);
}