commit 71059284d7cfe765f57fc4f68d30cb029b591e78 Author: g1n Date: Fri Dec 3 16:05:10 2021 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4695f24 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +builds/* +*.o +*.a +*~ + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..181ed90 --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +MIT License + +Copyright (c) 2021 GRU + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + diff --git a/README.org b/README.org new file mode 100644 index 0000000..b5e5ce3 --- /dev/null +++ b/README.org @@ -0,0 +1,10 @@ +#+TITLE: GRU liblinux - C library for easier calling linux syscalls from C + +* Setup + + - run ~make~ in ~src/~ (~liblinux.a~ will be in ~builds/~ directory) + - to build examples run ~make examples~ in ~src/~ and you will see executables in ~builds/~ directory + +* Usage + - to use this library add ~-Lpath/to/liblinuxdir -l:liblinux.a~ to your ~CFLAGS~ + diff --git a/examples/hello.c b/examples/hello.c new file mode 100644 index 0000000..f89c41b --- /dev/null +++ b/examples/hello.c @@ -0,0 +1,7 @@ +#include "../src/liblinux.h" + +int main() { + char hellostr[] = "Hello, World!\n"; + write(1, hellostr, sizeof(hellostr)); + return 0; +} diff --git a/examples/input.c b/examples/input.c new file mode 100644 index 0000000..3386b3d --- /dev/null +++ b/examples/input.c @@ -0,0 +1,8 @@ +#include "../src/liblinux.h" + +int main() { + char str[10]; + read(2, str, 10); + write(1, str, sizeof(str)); + return 0; +} diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..eadad03 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,18 @@ +CC=cc +CFLAGS=--std=c11 -ffreestanding -fno-stack-protector -nostdlib -Wall -Wextra -O3 +CFILES=liblinux.c liblinux/syscall.c liblinux/start.c +OBJFILES=../builds/liblinux.o ../builds/liblinux/start.o ../builds/liblinux/syscall.o +LIBFILE=../builds/liblinux.a + +lib: + mkdir -p ../builds/liblinux + $(CC) -c liblinux.c $(CFLAGS) -o ../builds/liblinux.o + $(CC) -c liblinux/syscall.c $(CFLAGS) -o ../builds/liblinux/syscall.o + $(CC) -c liblinux/start.c $(CFLAGS) -o ../builds/liblinux/start.o + ar ruv $(LIBFILE) $(OBJFILES) + ranlib $(LIBFILE) + +examples: + $(CC) ../examples/hello.c $(CFLAGS) -L../builds -l:$(LIBFILE) -o ../builds/hello + $(CC) ../examples/input.c $(CFLAGS) -L../builds -l:$(LIBFILE) -o ../builds/input + diff --git a/src/liblinux.c b/src/liblinux.c new file mode 100644 index 0000000..b3cbc4e --- /dev/null +++ b/src/liblinux.c @@ -0,0 +1,22 @@ +#include "liblinux.h" +#include "liblinux/syscall.h" + +void exit(int status) { + syscall(__NR_exit, status); +} + +int open(const char *pathname, int flags) { + return syscall(__NR_close, pathname, flags); +} + +int close(int fd) { + return syscall(__NR_close, fd); +} + +ssize_t write(int fd, const void *buf, size_t count) { + return syscall(__NR_write, fd, buf, count); +} + +ssize_t read(int fd, void *buf, size_t count) { + return syscall(__NR_read, fd, buf, count); +} diff --git a/src/liblinux.h b/src/liblinux.h new file mode 100644 index 0000000..8fbc295 --- /dev/null +++ b/src/liblinux.h @@ -0,0 +1,16 @@ +#ifndef LIBLINUX_H +#define LIBLINUX_H + +#include "syscall.h" +#include +#include + +void exit(int status); // FIXME: noreturn +int open(const char *pathname, int flags); +int close(int fd); +ssize_t write(int fd, const void *buf, size_t count); +ssize_t read(int fd, void *buf, size_t count); + +// TODO + +#endif diff --git a/src/liblinux/start.c b/src/liblinux/start.c new file mode 100644 index 0000000..4f36499 --- /dev/null +++ b/src/liblinux/start.c @@ -0,0 +1,34 @@ +#include "syscall.h" +//#include +#include "start.h" +/*void _init() { + __asm("push %rbp\n" + "movq %rsp, %rbp\n"); +} + +void _fini() { + __asm("push %rbp\n" + "movq %rsp, %rbp\n"); +}*/ + +void _exit() { + /*__asm__("movl %0, %%eax\n" + "syscall" + : + : "r" (__NR_exit));*/ + syscall(__NR_exit); +} + +void _start() { + __asm("xor %ebp, %ebp\n" + "mov (%rsp), %edi\n" + "lea 8(%rsp), %rsi\n" + "lea 16(%rsp,%rdi,8), %rdx\n" + "xor %eax, %eax\n" + "call main\n" + "mov %eax, %edi\n" + "xor %eax, %eax\n"); + _exit(); +} + + diff --git a/src/liblinux/start.h b/src/liblinux/start.h new file mode 100644 index 0000000..34c83dd --- /dev/null +++ b/src/liblinux/start.h @@ -0,0 +1,8 @@ +#ifndef LIBLINUX_START_H +#define LIBLINUX_START_H + +#include + +void _start(); + +#endif diff --git a/src/liblinux/syscall.c b/src/liblinux/syscall.c new file mode 100644 index 0000000..941e4ac --- /dev/null +++ b/src/liblinux/syscall.c @@ -0,0 +1,17 @@ +long syscall(long syscall, long _1, long _2, long _3, long _4, long _5, long _6) { + register long rax __asm__("rax") = syscall; + register long rdi __asm__("rdi") = _1; + register long rsi __asm__("rsi") = _2; + register long rdx __asm__("rdx") = _3; + register long r10 __asm__("r10") = _4; + register long r8 __asm__("r8") = _5; + register long r9 __asm__("r9") = _6; + + __asm__ volatile ( + "syscall" + : "+r" (rax), + "+r" (r8), "+r" (r9), "+r" (r10) + : "r" (rdi), "r" (rsi), "r" (rdx) + : "rcx", "r11", "cc", "memory"); + return rax; +} diff --git a/src/liblinux/syscall.h b/src/liblinux/syscall.h new file mode 100644 index 0000000..b445dbe --- /dev/null +++ b/src/liblinux/syscall.h @@ -0,0 +1,8 @@ +#ifndef LIBLINUX_SYSCALL_H +#define LIBLINUX_SYSCALL_H + +#include // FIXME + +long syscall(long syscall, ...); + +#endif