commit 80fbc61891a63b385eabf6d73085d363a33d439b
parent 1016044500a65dc921a61d8e330dd9baa9bc217e
Author: evenfri <evenfri@noreply.codeberg.org>
Date: Thu, 23 Jul 2026 19:45:55 +0200
feat: close, open, read syscalls stack_chk (#1)
Co-authored-by: evenfri <evenfri256@gmail.com>
Reviewed-on: https://codeberg.org/evenfri/libc/pulls/1
Diffstat:
8 files changed, 97 insertions(+), 1 deletion(-)
diff --git a/close.c b/close.c
@@ -0,0 +1,19 @@
+/*
+ * close.c — close system call
+ * Copyright (C) 2026. All rights reserved.
+ */
+
+#include "libc.h"
+
+int close(int fd) {
+ long ret;
+
+ asm volatile (
+ "syscall"
+ : "=a"(ret)
+ : "a"(sys_close), "D"(fd)
+ : "rcx", "r11", "memory"
+ );
+
+ return ret;
+}
diff --git a/libc.h b/libc.h
@@ -18,12 +18,24 @@ typedef long ssize_t;
#define stdout 1
#define stderr 2
+/* open flags */
+#define o_rdonly 00
+#define o_wronly 01
+#define o_rdwr 02
+#define o_creat 0100
+
/* syscall numbers */
+#define sys_read 0
#define sys_write 1
+#define sys_open 2
+#define sys_close 3
#define sys_exit 60
/* function prototypes */
+ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);
+int open(const char *filename, int flags, int mode);
+int close(int fd);
void _exit(int status);
void exit(int status);
diff --git a/mkfile b/mkfile
@@ -8,8 +8,12 @@ lib = libc.a
ofiles = start.$o \
_exit.$o \
exit.$o \
+ read.$o \
write.$o \
+ open.$o \
+ close.$o \
strlen.$o \
+ stack_chk.$o \
hfiles = libc.h \
diff --git a/open.c b/open.c
@@ -0,0 +1,19 @@
+/*
+ * open.c — open system call
+ * Copyright (C) 2026. All rights reserved.
+ */
+
+#include "libc.h"
+
+int open(const char *filename, int flags, int mode) {
+ long ret;
+
+ asm volatile (
+ "syscall"
+ : "=a"(ret)
+ : "a"(sys_open), "D"(filename), "S"(flags), "d"(mode)
+ : "rcx", "r11", "memory"
+ );
+
+ return ret;
+}
diff --git a/read.c b/read.c
@@ -0,0 +1,19 @@
+/*
+ * read.c — read system call
+ * Copyright (C) 2026. All rights reserved.
+ */
+
+#include "libc.h"
+
+ssize_t read(int fd, void *buf, size_t count) {
+ long ret;
+
+ asm volatile (
+ "syscall"
+ : "=a"(ret)
+ : "a"(sys_read), "D"(fd), "S"(buf), "d"(count)
+ : "rcx", "r11", "memory"
+ );
+
+ return ret;
+}
diff --git a/stack_chk.c b/stack_chk.c
@@ -0,0 +1,14 @@
+/*
+ * stack_chk.c — stack smashing protection stub
+ * Copyright (C) 2026. All rights reserved.
+ */
+
+#include "libc.h"
+
+/* stack canary guard word */
+unsigned long __stack_chk_guard = 0x5900f50fa0a3f200UL;
+
+void __stack_chk_fail(void) {
+ write(stderr, "err: stack smashing detected\n", 29);
+ _exit(127);
+}
diff --git a/start.s b/start.s
@@ -9,6 +9,8 @@ _start:
lea rdx, [rsi + rdi*8 + 8]
+ sub rsp, 8
+
call main
# exit(rax)
diff --git a/wrapers/cc b/wrapers/cc
@@ -1,4 +1,11 @@
#!/bin/sh
INC=$(cc -print-file-name=include)
-exec cc -nostdlib -nostdinc -ffreestanding -fno-builtin -isystem "$INC" "$@"
+exec cc -fstack-protector-all \
+ -mstack-protector-guard=global \
+ -nostdlib \
+ -nostdinc \
+ -ffreestanding \
+ -fno-builtin \
+ -isystem "$INC" \
+ "$@"