On Friday, 24 August 2018 at 09:46:08 UTC, Jonathan M Davis wrote:

For any kind of normal operating system, you _have_ to use libc. It's part of the OS. Some pieces could be done without it, but on the whole, you use libc if you want to talk to the OS. That's just life. The only exceptions I'm aware of to that are embedded devices, and my understanding is that if anything, such devices are more and more likely to run a fullblown OS, making it that much less likely that you'd ever do anything without libc.

That is not true. You can write your own system calls. Here's "hello world" with no libc:

---object.d
module object;

alias immutable(char)[] string;

private long __d_sys_write(long arg1, in void* arg2, long arg3)
{
    long result;

    asm
    {
        mov RAX, 1;
        mov RDI, arg1;
        mov RSI, arg2;
        mov RDX, arg3;
        syscall;
    }

    return result;
}

void write(string text)
{
    __d_sys_write(2, text.ptr, text.length);
}

private void __d_sys_exit(long arg1)
{
    asm
    {
        mov RAX, 60;
        mov RDI, arg1;
        syscall;
    }
}

extern void main();
private extern(C) void _start()
{
    main();
    __d_sys_exit(0);
}

---main.d
module main;

void main()
{
    write("Hello, World\n");
}

$dmd -c -lib -conf= object.d main.d -of=main.o
$ld main.o -o main
$size main
   text    data     bss     dec     hex filename
    176       0       0     176      b0 main
$main
Hello, World

You just need to re-implement what you need in D. For Phobos, that might be a big job (or maybe not, I haven't looked into it). For druntime, it's not so bad -- it's mostly just memcpy, memcmp, malloc, free, and maybe a couple of others. Those are not trivial implementations, but it's not out of reach, and I think there is opportunity with, with CTFE, templates, __traits, inline asm, and a number of other D features, to do even better than C, and make it more type- and memory-safe while we're at it. Implementing those building-blocks in D, would be good for the language, would solve a number of issues I'm currently having with druntime updates, and would be a fun project for those that are interested in that kind of stuff.

Mike




Reply via email to