feat(app): nolibc hello world

This commit is contained in:
2026-04-20 16:00:17 +02:00
parent d829209488
commit d5be4cfe0b
5 changed files with 103 additions and 1 deletions

29
app/entry.s Normal file
View File

@@ -0,0 +1,29 @@
.global _start, syscall5
_start:
/* Load argc -> rdi, argv[0] -> rsi */
xor %rbp, %rbp
pop %rdi
mov %rsp, %rsi
and $0xFFFFFFFFFFFFFFF0, %rsp
call main
/* Exit with ret val of main */
mov %rax, %rdi
mov $0x3C, %rax
syscall
syscall5:
mov %rdi, %rax /* %rax (syscall number) = func param 1 (%rdi) */
mov %rsi, %rdi /* %rdi (syscall param 1) = func param 2 (%rsi) */
mov %rdx, %rsi /* %rsi (syscall param 2) = func param 3 (%rdx) */
mov %rcx, %rdx /* %rdx (syscall param 3) = func param 4 (%rcx) */
mov %r8, %r10 /* %r10 (syscall param 4) = func param 5 (%r8) */
mov %r9, %r8 /* %r8 (syscall param 5) = func param 6 (%r9) */
syscall /* Enter a syscall (return value in %rax) */
ret /* Return value is already in %rax, we can return. */
.section .note.GNU-stack,"",@progbits

18
app/nolibc.c Normal file
View File

@@ -0,0 +1,18 @@
void *syscall5(void *number, void *arg1, void *arg2, void *arg3, void *arg4,
void *arg5);
typedef unsigned long int size_t;
typedef long int ssize_t;
static ssize_t write(int fd, void const *data, size_t nbytes) {
return (ssize_t)syscall5((void *)1, /* SYS_write, call number 1 */
(void *)(size_t)fd, (void *)data, (void *)nbytes,
0, /* Ignored */
0 /* Ignored */
);
}
int main() {
write(0, "Hello World!\n", 13);
return 0;
}

38
app/nolibc.ld Normal file
View File

@@ -0,0 +1,38 @@
ENTRY(_start)
PHDRS
{
text PT_LOAD FLAGS(5); /* R + X */
data PT_LOAD FLAGS(6); /* R + W */
}
SECTIONS
{
. = 0x400000;
.text : ALIGN(16) {
*(.text .text.*)
}:text
.rodata : {
*(.rodata .rodata.*)
}:text
.data : {
*(.data .data.*)
}:data
.bss : {
*(.bss .bss.*)
*(COMMON)
}:data
/DISCARD/ : {
*(.note*)
*(.comment*)
*(.eh_frame*)
*(.symtab*)
*(.strtab*)
}
}