feat(app): nolibc hello world
This commit is contained in:
29
app/entry.s
Normal file
29
app/entry.s
Normal 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
18
app/nolibc.c
Normal 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
38
app/nolibc.ld
Normal 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*)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user