~skeeto/public-inbox

1

[u-config/tcc] shorter syscall assembly

Details
Message ID
<hnub4cmq6iog36zt63gbandmtuym6uk47upix4zk7abnnzrgng@zrand3yyap5q>
DKIM signature
pass
Download raw message
I'm suspecting the `tcc` branch is experimental and not for serious use,
but one small trick is to pass the syscall number at the end. This
allows for shorter syscall asm implementation by (ab)using the fact that
argument registers for syscall and regular function call are nearly
identical. POC patch (mostly untested) attached.

- NRK
Details
Message ID
<20240206142202.5pemkocuakqylbzh@nullprogram.com>
In-Reply-To
<hnub4cmq6iog36zt63gbandmtuym6uk47upix4zk7abnnzrgng@zrand3yyap5q> (view parent)
DKIM signature
missing
Download raw message
Clever, NRK! Aesthetically I don't like the syscall number going last, but 
your macro fixes that up nicely. With inline asm, GCC and Clang inline the 
syscall functions, place arguments straight into the constraints, and 
basically figure this out automatically — a great advantage of inline asm.

> experimental and not for serious use,

Yup, that branch was to figure out how to do it without inline asm, then 
capture that knowledge. The no-libc side has changed since starting that 
branch, and even for semi-serious use would need some porting. Since I've 
gotten a better handle on _start, it would also reduce to two hand-written 
instructions.

In an update I'd also probably just delete the string.h definitions and 
instead gesture towards external definitions: my own libmemory, or musl 
libc.a. On Debian after deleting those functions:

$ tcc -nostdlib crt0.s linux_amd64_main.c /usr/lib/x86_64-linux-musl/libc.a

With your patch, a better _start, and deleting string.h, crt0.s reduces to 
15 hand-written instructions. The fewer the better. I applied your patch, 
thanks!
Reply to thread Export thread (mbox)