liblinux++

Log | Files | Refs

commit f3729556ee9fdf381bdd7a99f737804f0c44b69f
parent 60c196243360163e4cb21c7d1d46cb8883410363
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Thu, 19 Dec 2024 16:41:07 +0000

Trim program headers

Diffstat:
Mecho.c | 15+++++++++------
Mstart.s | 2+-
Mx86_64.ld | 47+++++++++++++++++++++++++++++------------------
3 files changed, 39 insertions(+), 25 deletions(-)

diff --git a/echo.c b/echo.c @@ -1,19 +1,22 @@ #include "syscall.h" -static unsigned short bufsize; -static char buf[0x1000 - sizeof bufsize]; +static struct +{ + unsigned short size; + char data[0x1000 - sizeof(unsigned short)]; +} buf; static void putflush() { - if (bufsize && write(1, buf, bufsize) != bufsize) + if (buf.size && write(1, buf.data, buf.size) != buf.size) exit(1); - bufsize = 0; + buf.size = 0; } static void putch(char c) { - buf[bufsize++] = c; - if (c == '\n' || bufsize == sizeof buf) + buf.data[buf.size++] = c; + if (c == '\n' || buf.size == sizeof buf.data) putflush(); } diff --git a/start.s b/start.s @@ -5,7 +5,7 @@ .text start: # start is the entry point known to the linker - mov rdi, [rsp] # get argc from the stack + mov edi, [rsp] # get argc from the stack lea rsi, [rsp+8] # take the address of argv from the stack lea rdx, [rsp+16] # take the address of envp from the stack call main # %edi, %rsi, %rdx are the three args (of which first two are C standard) to main diff --git a/x86_64.ld b/x86_64.ld @@ -1,35 +1,46 @@ -SECTIONS +PHDRS { - . = 0x10000; - .text (READONLY) : - { - *(.text) - } + rodata PT_LOAD; + text PT_LOAD; + data PT_LOAD; +} + +MEMORY +{ + userspace (rwx) : ORIGIN = 0x10000, LENGTH = 0x7ffff7000000 - ORIGIN(userspace) +} +SECTIONS +{ .rodata (READONLY) : - ALIGN(0x1000) + ALIGN(CONSTANT(MAXPAGESIZE)) { - *(.rodata.*) - *(.rodata) - } + *(.rodata*) + } >userspace :rodata + .text (READONLY) : + ALIGN(CONSTANT(MAXPAGESIZE)) + { + *(.text*) + } >userspace :text + .data : - ALIGN(0x1000) + ALIGN(CONSTANT(MAXPAGESIZE)) { - *(.data) - } + *(.data*) + } >userspace :data .bss (NOLOAD) : - ALIGN(0x1000) { - *(.bss) - } + *(.bss*) + } >userspace :data .mappable (OVERLAY) : - ALIGN(0x1000) + ALIGN(CONSTANT(MAXPAGESIZE)) { + *(.mappable*) mappable = .; - } + } >userspace } ENTRY(start)