153 points · by guerrilla · 5y ago · 75 comments
The part most relevant to this discussion:
> I stopped developing this because I discovered the Linux itself has a better solution that they use for their own tools:
> https://github.com/torvalds/linux/blob/master/tools/include/...
The kernel developers implemented nolibc.h with 3 levels of abstraction: system call entry point, function wrappers and libc interface. They set errno in the libc interface. Personally I don't see the point.
> The third level is the libc call definition.
> It exposes the lower raw sys_<name>() calls in a way that looks like what a libc usually does, takes care of specific input values, and of setting errno upon error.
Defining NOLIBC_IGNORE_ERRNO solves the problem!
> Currently, the only supported architecture and compiler is x86_64 and GCC, respectively
Let's wait until its architecture-independence has arrived in practice.
The Linux kernel nolibc.h already has support and is just better in almost every way. I wasn't sure if there was any point in continuing liblinux after I discovered it since it achieves almost everything I tried to do and doesn't even require a build system, shared libraries or anything. The support for auxiliary values is missing though. Maybe they'd accept a patch...?
I also asked Greg on reddit about nolibc.h once:
https://old.reddit.com/r/linux/comments/fx5e4v/im_greg_kroah...
Turns out there was a klibc project that never turned up in my original research!
* Remember that glibc is part of the GNU project and is meant to be portable beyond Linux, especially to Hurd. It might not make sense for them to implement interfaces that wouldn't work or would require significant work to emulate on Hurd, the BSDs and Solaris.
But since C is the de facto standard for OS libraries, most languages end up supporting calls to C libraries. Thus C libraries do become the language-agnostic library.
https://github.com/torvalds/linux/blob/master/Documentation/...
On Windows and other operating systems, things such as system call numbers can and do change. There's no way to interface with the kernel without linking to vendor libraries.
So on Linux you can ditch libc and write your own code. A Rust liblinux is definitely possible, I tested it. You can even have a compiler generate the system calls for you! It just needs to support the simple calling convention. No libraries necessary. I've seen a project that does just that:
https://github.com/sunfishcode/mustang
It supports threads, filesystem, networking, and more!
The library for making Linux system calls is:
On Linux, however, we have, for example, glibc on one hand which claims that it doesn't have a goal of forwarding all of the Linux's syscall, and several failed initiatives on the other hand to allow the user's code to perform syscalls only from inside of glibc (which spells doom to anything that doesn't use libc, e.g. Golang) because of security reasons. And the history of still having to support INT 80h together with SYSCALL on x64 on the third hand.
I didn't know about these efforts. Please elaborate.
You could always call syscalls directly, using syscall() (https://man7.org/linux/man-pages/man2/syscall.2.html), but a wrapper library is nicer.
One of my objectives was to produce ELFs with no symbols other than those defined in my own code. Glibc adds a lot of symbols.
Ohh, if only we could make C the second class citizen. C brings with itself meaningless legacy from 1969 into modern systems, like you might think openat2 was created recently, but it's designed for PDP-7, because it's C and stuff was always done this way.
Every Linux process interfaces through the kernel via the system call interface. It is stable and language agnostic. The interface is defined at the processor architecture level.
https://github.com/torvalds/linux/blob/master/Documentation/...
https://man7.org/linux/man-pages/man2/syscall.2.html
Any compiler can emit this sort of code, including JIT compilers! It's possible to discard the entire C user space and rewrite everything in Rust. Would be a monumental effort but still.
Note that no matter what there will be at least one C library remaining: the vDSO the kernel maps into the address space of every process.
https://github.com/torvalds/linux/blob/master/Documentation/...
https://news.ycombinator.com/item?id=25997506
Apparently for BSD, the ABI is not that stable, glibc works around bugs on some platforms (adding memory barriers etc), and there are security features that check the origin of syscalls. I wonder whether this is truly acceptable on Linux...
This is not a problem on Linux.
> glibc works around bugs on some platforms (adding memory barriers etc)
That's really interesting. Do you have examples of such fixes? I'm more familiar with musl sources, glibc proved to be somewhat difficult to navigate.
Well, yeah, but that's a tautology, no?
A willingness to discard everything indicates a willingness to rewrite everything. So, yeah, it is possible to rewrite everything if you are willing to rewrite everything.
Linux lets you do this. The interface is stable. People can build their custom user space on top of it and enjoy a great kernel full of drivers. Wouldn't it be amazing if someone came up with a 100% Lisp user space? A 100% Rust user space?
What replacement are you proposing that every language can call into? How would a migration of existing Java, Python, Ruby, C++ etc programs be done to this new interface? Do programs need to be modified at the source level? Just recompiled? How will the existing programs that cannot be recompiled be handled?
I quite aware that you can make the C ABI a shim into the new first class ABI to make existing programs work, but why would anyone target the new ABI when the existing one still works?
"access to a very limited subset of Linux system calls" :P Still a cool project though and a good base.
make system-calls.missing
The library does provide access to every system call through the generic system_call function. It just doesn't have compile time type checking.