summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAda Christine <adachristine18@gmail.com>2024-02-24 15:18:40 +0000
committerAda Christine <adachristine18@gmail.com>2024-02-24 15:18:40 +0000
commit0f3866a4dad84860e135855766c77e5a82737686 (patch)
tree97e7bac72928ca6affc362577214646f942ad4d7
parenta61b9d1c9ee85ec08ff3b8a61e80dc33e71f3472 (diff)
bad reloc code silly me
-rw-r--r--api/libc/stdio.h4
-rw-r--r--api/posix/unistd.h3
-rw-r--r--kjarna_runtime/Makefile3
-rw-r--r--kjarna_runtime/dynamic_x86_64.c4
-rw-r--r--kjarna_runtime/main.c3
-rw-r--r--kjarna_runtime/start_x86_64.S54
-rw-r--r--lib/stdio.c30
7 files changed, 69 insertions, 32 deletions
diff --git a/api/libc/stdio.h b/api/libc/stdio.h
index 1edec4a..63d1ad8 100644
--- a/api/libc/stdio.h
+++ b/api/libc/stdio.h
@@ -6,10 +6,12 @@
typedef struct FILE FILE;
+extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;
-extern int fputc(int c, FILE *f);
+int fputc(int c, FILE *f);
+int fputs(const char *str, FILE *f);
int vfprintf(FILE *f, const char *restrict format, va_list arguments);
int fprintf(FILE *f, const char *restrict format, ...);
diff --git a/api/posix/unistd.h b/api/posix/unistd.h
index 7487001..4964689 100644
--- a/api/posix/unistd.h
+++ b/api/posix/unistd.h
@@ -14,5 +14,8 @@ enum seek_whence
SEEK_END
};
+ssize_t read(int fd, void *buf, size_t length);
ssize_t write(int fd, const void *buf, size_t length);
+off_t lseek(int fd, off_t position, int whence);
+int close(int fd);
diff --git a/kjarna_runtime/Makefile b/kjarna_runtime/Makefile
index caffc79..03ac8b0 100644
--- a/kjarna_runtime/Makefile
+++ b/kjarna_runtime/Makefile
@@ -16,7 +16,8 @@ CFLAGS += -fPIC -fvisibility=hidden -mgeneral-regs-only
LDSCRIPT := kc.ld
CRT_OBJS := start_x86_64.o dynamic_x86_64.o elf64.o
-LIB_OBJS := kjarna.o memcmp.o memcpy.o memmove.o memset.o string.o
+LIB_OBJS := kjarna.o memcmp.o memcpy.o memmove.o memset.o string.o \
+ stdio.o
APP_OBJS := main.o
OBJS := $(CRT_OBJS) $(LIB_OBJS) $(APP_OBJS)
diff --git a/kjarna_runtime/dynamic_x86_64.c b/kjarna_runtime/dynamic_x86_64.c
index 3d85582..02538ec 100644
--- a/kjarna_runtime/dynamic_x86_64.c
+++ b/kjarna_runtime/dynamic_x86_64.c
@@ -129,9 +129,9 @@ static void do_rela(char *base, Elf64_Rela *rela)
void kc_dynamic_reloc(char *base, struct elf64_relatab *rela)
{
- // perform the actual relocation
+ size_t entries = rela->size / rela->entsize;
- for (size_t i = 0; rela->entries && (i < rela->size); i += rela->entsize)
+ for (size_t i = 0; rela->entries && (i < entries); i++)
{
do_rela(base, &rela->entries[i]);
}
diff --git a/kjarna_runtime/main.c b/kjarna_runtime/main.c
index 248958b..dc00f4a 100644
--- a/kjarna_runtime/main.c
+++ b/kjarna_runtime/main.c
@@ -1,11 +1,12 @@
#include <unistd.h>
+#include <stdio.h>
int main(int argc, char **argv)
{
(void)argc;
(void)argv;
- write(1, "hello, runtime world!\r\n", -1);
+ fputs("hello, runtime world!\n", stdout);
while (1);
diff --git a/kjarna_runtime/start_x86_64.S b/kjarna_runtime/start_x86_64.S
index 1db92a4..19db34f 100644
--- a/kjarna_runtime/start_x86_64.S
+++ b/kjarna_runtime/start_x86_64.S
@@ -21,7 +21,7 @@ _start:
pop %rdi
mov %rsp, %rbp
and $-0x10, %rsp
- call kjarna_entry
+ call kjarna_entry
mov %rbp, %rsp
pop %rbp
ret
@@ -33,31 +33,31 @@ _start:
.global _rtld_link
.type _rtld_link, @function
_rtld_link:
- push %rbp
- mov %rsp, %rbp
- push %r9
- push %r8
- push %rcx
- push %rdx
- push %rsi
- push %rdi
- mov 8(%rbp), %rdi
- mov 16(%rbp), %rsi
- push %rbp
- mov %rsp, %rbp
- and $-0x10, %rsp
- call kc_dynamic_link
- mov %rbp, %rsp
- pop %rbp
- pop %rdi
- pop %rsi
- pop %rdx
- pop %rcx
- pop %r8
- pop %r9
- mov %rbp, %rsp
- pop %rbp
- add $0x10, %rsp
- jmp *(%rax)
+ push %rbp
+ mov %rsp, %rbp
+ push %r9
+ push %r8
+ push %rcx
+ push %rdx
+ push %rsi
+ push %rdi
+ mov 8(%rbp), %rdi
+ mov 16(%rbp), %rsi
+ push %rbp
+ mov %rsp, %rbp
+ and $-0x10, %rsp
+ call kc_dynamic_link
+ mov %rbp, %rsp
+ pop %rbp
+ pop %rdi
+ pop %rsi
+ pop %rdx
+ pop %rcx
+ pop %r8
+ pop %r9
+ mov %rbp, %rsp
+ pop %rbp
+ add $0x10, %rsp
+ jmp *(%rax)
.size _rtld_link, . - _rtld_link
diff --git a/lib/stdio.c b/lib/stdio.c
new file mode 100644
index 0000000..70cc685
--- /dev/null
+++ b/lib/stdio.c
@@ -0,0 +1,30 @@
+#include <libc/stdio.h>
+#include <libc/string.h>
+#include <posix/unistd.h>
+
+struct FILE
+{
+ int fd;
+};
+
+static FILE stdin_stream = { STDIN_FILENO };
+static FILE stdout_stream = { STDOUT_FILENO };
+static FILE stderr_stream = { STDERR_FILENO };
+
+FILE *stdin = &stdin_stream;
+FILE *stdout = &stdout_stream;
+FILE *stderr = &stderr_stream;
+
+int fputc(int c, FILE *f)
+{
+ write(f->fd, (const char *)&c, 1);
+ return 1;
+}
+
+int fputs(const char *s, FILE *f)
+{
+ size_t slen = strlen(s);
+ write(f->fd, s, slen);
+ return (int)slen;
+}
+