forked from cirosantilli/linux-kernel-module-cheat
/
hello.c
33 lines (29 loc) · 775 Bytes
/
hello.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
/* x86_64 freestanding C inline assemby Linux hello world
* https://cirosantilli.com/linux-kernel-module-cheat#linux-system-calls
*/
#define _XOPEN_SOURCE 700
#include <inttypes.h>
#include <sys/types.h>
ssize_t my_write(int fd, const void *buf, size_t size) {
ssize_t ret;
__asm__ __volatile__ (
"syscall"
: "=a" (ret)
: "0" (1), "D" (fd), "S" (buf), "d" (size)
: "cc", "rcx", "r11", "memory"
);
return ret;
}
void my_exit(int exit_status) {
ssize_t ret;
__asm__ __volatile__ (
"syscall"
: "=a" (ret)
: "0" (60), "D" (exit_status)
: "cc", "rcx", "r11", "memory"
);
}
void _start(void) {
char msg[] = "hello\n";
my_exit(my_write(1, msg, sizeof(msg)) != sizeof(msg));
}