liblinux++

Log | Files | Refs

commit 2678aca0d2ebde08c0e1228c626603da87f0a64c
parent 23416d957f1b1b85d64f7a290b467d58f586a863
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Sat, 20 Dec 2025 22:11:00 +0000

aarch64 compat

Diffstat:
MTupfile | 5++---
Aaarch64.ld | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Aaarch64.s | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcat.c | 2+-
Mgen.sh | 1+
Mpaste.c | 3+--
Msyscall.h | 2+-
Mtac.c | 3++-
Atest.c | 22++++++++++++++++++++++
9 files changed, 181 insertions(+), 8 deletions(-)

diff --git a/Tupfile b/Tupfile @@ -1,9 +1,8 @@ -CFLAGS = -Os -fno-asynchronous-unwind-tables -no-pie -g -fno-stack-protector -LDFLAGS = -static -nostartfiles -nostdlib -no-pie -Xlinker --gc-sections +CFLAGS = -g -Os -fno-asynchronous-unwind-tables -fno-pic -fno-pie -no-pie -g -fno-stack-protector +LDFLAGS = -static -nostartfiles -nolibc -nodefaultlibs -nostdlib -fno-pic -fno-pie -no-pie -Xlinker --gc-sections !cc = |> cc $(CFLAGS) -c -o %o %f |> obj/%B.o -: foreach *.s |> !cc |> : foreach *.c |> !cc |> {objs} run ./gen.sh diff --git a/aarch64.ld b/aarch64.ld @@ -0,0 +1,49 @@ +PHDRS +{ + rodata PT_LOAD; + text PT_LOAD; + data PT_LOAD; +} + +MEMORY +{ + userspace (rwx) : ORIGIN = 0x10000, LENGTH = 0x100000 - ORIGIN(userspace) +} + +SECTIONS +{ + .rodata (READONLY) : + ALIGN(CONSTANT(MAXPAGESIZE)) + { + *(.rodata*) + } >userspace :rodata + + .text (READONLY) : + ALIGN(CONSTANT(MAXPAGESIZE)) + { + *(.text*) + } >userspace :text + + .data : + ALIGN(CONSTANT(MAXPAGESIZE)) + { + *(.data*) + } >userspace :data + + + .bss (NOLOAD) : + ALIGN(CONSTANT(MAXPAGESIZE)) + { + *(.bss*) + } >userspace :data + + .mappable (OVERLAY) : + ALIGN(CONSTANT(MAXPAGESIZE)) + { + *(.mappable*) + mappable = .; + } >userspace + +} + +ENTRY(start) diff --git a/aarch64.s b/aarch64.s @@ -0,0 +1,102 @@ +.global start + +.text + +start: # start is the entry point known to the linker + ldr x0, [sp] // get argc from the stack + add x1, sp, #8 // take the address of argv from the stack + add x2, sp, #16 // take the address of envp from the stack + bl main + b exit + +.macro syscall, num, name +.global \name +.section .text.\name +\name: + mov w8, #\num + svc #0 + ret +.endm + +.text +.global exit + +syscall 63 read +syscall 64 write +syscall 56 openat +syscall 57 close +syscall 222 mmap + +/* +syscall 4 stat +syscall 5 fstat +syscall 6 lstat +syscall 7 poll +syscall 8 lseek +syscall 222 mmap +syscall 10 mprotect +syscall 11 munmap +syscall 12 brk +syscall 13 rt_sigaction +syscall 14 rt_sigprocmask +syscall 15 rt_sigreturn +syscall 16 ioctl +syscall 17 pread64 +syscall 18 pwrite64 +syscall 19 readv +syscall 20 writev +syscall 21 access +syscall 22 pipe +syscall 23 select +syscall 24 yield +syscall 25 mremap +syscall 26 msync +syscall 27 mincore +syscall 28 madvise +syscall 29 shmget +syscall 30 shmat +syscall 31 shmctl +syscall 32 dup +syscall 33 dup2 +syscall 34 pause +syscall 35 nanosleep +syscall 36 getitimer +syscall 37 alarm +syscall 38 setitimer +syscall 39 getpid +syscall 40 sendfile +syscall 41 socket +syscall 42 connect +syscall 43 accept +syscall 44 sendto +syscall 45 recvfrom +syscall 46 sendmsg +syscall 47 recvmsg +syscall 48 shutdown +syscall 49 bind +syscall 50 listen +syscall 51 getsockname +syscall 52 getpeername +syscall 53 socketpair +syscall 54 setsockopt +syscall 55 getsockopt +syscall 56 clone +syscall 57 fork +syscall 58 vfork +syscall 59 execve +*/ + +.section .text.exit +exit: # mov x0, r0 + mov w8, #93 + svc #0 + +/* +syscall 61 wait4 +syscall 62 kill +syscall 63 uname +syscall 64 semget +syscall 65 semop + +syscall 77 ftruncate +*/ diff --git a/cat.c b/cat.c @@ -20,7 +20,7 @@ int main(int argc, char* argv[]) { for (int i = 1; i < argc; ++i) { - int fd = open(argv[i], 0, /*O_RDONLY*/0); + int fd = openat(-100, argv[i], 0, /*O_RDONLY*/0); if (fd < 0) return 1; cat(fd); diff --git a/gen.sh b/gen.sh @@ -2,4 +2,5 @@ ARCH=`uname -m` +echo ": $ARCH.s |> !cc |>" echo ": foreach {objs} | $ARCH.ld obj/$ARCH.o |> cc "'$(LDFLAGS)'" -o %o -T $ARCH.ld obj/$ARCH.o %f |> %B" diff --git a/paste.c b/paste.c @@ -10,7 +10,6 @@ struct input extern struct input mappable[]; static int n_inputs; - static char getc(struct input* in) { if (in->start == in->end) @@ -29,7 +28,7 @@ int main(int argc, char* argv[]) { for (int i = 1; i < argc; ++i) { - int fd = open(argv[i], 0, 0); + int fd = openat(-100, argv[i], 0, 0); if (fd < 0) return 1; struct input* m = mmap(mappable + n_inputs++, sizeof(struct input), PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); diff --git a/syscall.h b/syscall.h @@ -2,7 +2,7 @@ extern int read(int fd, char* data, size_t count); extern int write(int fd, char const* data, size_t count); -extern int open(char const* name, int flags, int mode); +extern int openat(int fd, char const* name, int flags, int mode); extern int close(int fd); extern void* mmap(void* addr, unsigned long len, unsigned long prot, unsigned long flags, unsigned long fd, unsigned long off); diff --git a/tac.c b/tac.c @@ -1,5 +1,6 @@ +#define _SYS_MMAN_H #include "syscall.h" -#include <linux/mman.h> +#include <bits/mman-linux.h> extern char mappable[]; diff --git a/test.c b/test.c @@ -0,0 +1,22 @@ +#include "syscall.h" + +char const rodata[] = "rodata\n"; +char data[] = "data\n"; +char bss[4]; + +void text() +{ + write(1, rodata, sizeof(rodata) - 1); + write(1, data, sizeof(data) - 1); + + bss[0] = 'b'; + bss[1] = 's'; + bss[2] = 's'; + bss[3] = '\n'; + write(1, bss, sizeof(bss)); +} + +int main() +{ + text(); +}