4 ms·
> Calling system calls by crafting your own assembly is generally a bad idea as the ABI may break underneath you. syscall should have a stable ABI at the very
by saagarjha 6y ago
> Calling system calls by crafting your own assembly is generally a bad idea as the ABI may break underneath you.
syscall should have a stable ABI at the very least, because this would otherwise break all statically linked code.
- josephcsible 6y agoYep, that's wrong. There's no way Linus would ever let such a change get merged.
- ufo 6y agoThat is true for Linux but might not be true for other operating systems though.
- kyberias 6y agoThe article is specifically about _Linux_ system calls.
- yjftsjthsd-h 6y agoIt is absolutely not true on many (most?) operating systems; Linux is actually an outlier, and we mostly forget that it's the odd one out because it's so popular. Off the top of my head, I believe both NT and Solaris define libc as the stable interface that userspace uses; I don't recall exactly what the BSDs do, but I suspect that they at least strongly encourage using libc and not trying to talk to the kernel yourself (IIRC OpenBSD does this because some of their security measures are managed by libc). Go has hit this a few times because they don't want to depend on libc if they can avoid it, but on a lot of systems they really can't avoid it. Ah, here we go: https://github.com/golang/go/issues/36435 https://github.com/golang/go/issues/36435 > Upcoming changes to the OpenBSD kernel will prevent system calls from being made unless they are coming from libc.so (with some exceptions, for example, a static binary). There are also likely to be changes to the APIs used for system calls. As such, the Go runtime (and other packages) need to stop using direct syscalls, rather calling into libc functions instead (as has always been done for Solaris and now also for macOS). (and the "with some exceptions" is why I say "strongly encouraged")
- saagarjha 6y agomacOS, in some sense a BSD (at least nominally), would like you to not make system calls yourself as well. Actually, not linking against libc has a number of hilarious consequences, one of which is that you bypass the platform sandbox because apparently the engineers thought it couldn't be possible to write a program without it :P
- khrbtxyz 6y agoIs there an example somewhere on how to link without libc and make my own syscalls? I tried this a while ago (can't remember which version of macOS it was), fiddling with Csu, nasm etc. but couldn't quite figure it out.
- saagarjha 6y ago-static -nostdlib, and make sure you have an entry point set.
- deleted 6y ago[deleted]
- jmillikin 6y agohttps://john-millikin.com/unix-syscalls#darwin https://john-millikin.com/unix-syscalls#darwin is a small, "hello world" example.
- saagarjha 6y ago> Note that I have left out the instructions to statically link binaries because they are documented as unsupported That's a bit annoying, especially since you're already using raw syscall numbers anyways. Here's how to make it static: .intel_syntax noprefix #include <sys/syscall.h> #define UNIX_SYSCALL 0x2000000 .globl start start: mov rax, UNIX_SYSCALL | SYS_write mov rdi, 1 lea rsi, text[rip] lea rdx, length syscall mov rax, UNIX_SYSCALL | SYS_exit xor rdi, rdi syscall text: .asciz "Hello, world!\n" .equ length, . - text Compile that with clang -static -nostdlib.
- kyberias 6y agoYes, that doesn't make much sense. Linux syscall interface is stable whether or not you use glibc or not.
- raxxorrax 6y agoDid it ever change the last 15 years for linux or windows systems? There are additional commands and perhaps some rules changed about which register need parameters and which need to be saved. But I cannot remember fundamental changes here.
- ChrisSD 6y agoNote that a "syscall" means calling into the kernel directly. Only Linux has stable syscalls. As mentioned below, on Windows syscalls are highly unstable. They change with every single update to the OS. You have to call functions in ntdll and they in turn will call the kernel. Think of it like a kind of libc but one that must be dynamically linked. You can't statically link it because it's tied to the exact version of Windows you're using. Of course Window's actual stable interface is the Win32 API, which will call ntdll which in turn makes the syscall.
- raxxorrax 6y agoThey don't have them documented, but I didn't think they changed that much. But apparently they did. Just found this neat site: https://j00ru.vexillium.org/syscalls/nt/64/ https://j00ru.vexillium.org/syscalls/nt/64/
- ChrisSD 6y agoNote that they can potentially change with every single update to the OS. That's why that site lists the syscall for every update. They are not stable.