Initial commit

This commit is contained in:
g1n 2021-12-03 16:05:10 +02:00
commit 71059284d7
12 changed files with 164 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
builds/*
*.o
*.a
*~

11
LICENSE Normal file
View File

@ -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.

10
README.org Normal file
View File

@ -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~

7
examples/hello.c Normal file
View File

@ -0,0 +1,7 @@
#include "../src/liblinux.h"
int main() {
char hellostr[] = "Hello, World!\n";
write(1, hellostr, sizeof(hellostr));
return 0;
}

8
examples/input.c Normal file
View File

@ -0,0 +1,8 @@
#include "../src/liblinux.h"
int main() {
char str[10];
read(2, str, 10);
write(1, str, sizeof(str));
return 0;
}

18
src/Makefile Normal file
View File

@ -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

22
src/liblinux.c Normal file
View File

@ -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);
}

16
src/liblinux.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef LIBLINUX_H
#define LIBLINUX_H
#include "syscall.h"
#include <stddef.h>
#include <sys/types.h>
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

34
src/liblinux/start.c Normal file
View File

@ -0,0 +1,34 @@
#include "syscall.h"
//#include <linux/unistd.h>
#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();
}

8
src/liblinux/start.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef LIBLINUX_START_H
#define LIBLINUX_START_H
#include <linux/unistd.h>
void _start();
#endif

17
src/liblinux/syscall.c Normal file
View File

@ -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;
}

8
src/liblinux/syscall.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef LIBLINUX_SYSCALL_H
#define LIBLINUX_SYSCALL_H
#include <linux/unistd.h> // FIXME
long syscall(long syscall, ...);
#endif