alloca without runtime?
岩倉 澪 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu May 4 05:17:01 PDT 2017
On Sunday, 30 April 2017 at 05:07:31 UTC, 岩倉 澪 wrote:
> I've been playing around with using D with no runtime on Linux,
> but recently I was thinking it would be nice to have an alloca
> implementation. I was thinking I could just bump the stack
> pointer (with alignment considerations) but from what I
> understand compilers sometimes generate code that references
> variables relative to RSP instead of RBP? I've seen people
> saying that a proper alloca can't be implemented without help
> from the compiler...
>
> I took a peek in druntime and found rt.alloca which has
> __alloca implemented with inline asm. I tried throwing that in
> my project and calling it but it segfaults on rep movsq. The
> comments in the code suggest it is trying to copy temps on the
> stack but I seem to get a really large garbage RCX, I don't
> fully follow what is going on yet.
>
> Is there any way I can get a working alloca without using
> druntime, c runtime, etc?
As a follow-up, here is a simple example of what I mean:
first, let's create a main.d, we'll define our own entry point
and make a call to alloca in main:
extern (C):
void _start()
{
asm nothrow @nogc
{
naked;
xor RBP, RBP;
pop RDI;
mov RSI, RSP;
and RSP, -16;
call main;
mov RDI, RAX;
mov RAX, 60;
syscall;
ret;
}
}
pragma(startaddress, _start);
int main(int argc, char** argv)
{
import rt.alloca;
void* a = __alloca(42);
return 0;
}
Next, let's make an rt directory and copy the source of
druntime's rt.alloca into rt/alloca.d
Now let's compile these:
dmd -betterC -debuglib= -defaultlib= -boundscheck=off -vgc -vtls
-c -gc main.d rt/alloca.d
Great, now we need to strip symbols out to make this work, like
so:
objcopy -R '.data.*[0-9]TypeInfo_*' -R '.[cd]tors.*' -R .eh_frame
-R minfo -R .group.d_dso -R .data.d_dso_rec -R .text.d_dso_init
-R .dtors.d_dso_dtor -R .ctors.d_dso_ctor -N __start_minfo -N
__stop_minfo main.o
objcopy -R '.data.*[0-9]TypeInfo_*' -R '.[cd]tors.*' -R .eh_frame
-R minfo -R .group.d_dso -R .data.d_dso_rec -R .text.d_dso_init
-R .dtors.d_dso_dtor -R .ctors.d_dso_ctor -N __start_minfo -N
__stop_minfo alloca.o
With that out of the way, we are ready to link:
ld main.o alloca.o
And when we try to run ./a.out we get a segfault.
What I want is a way to allocate on the stack (size of allocation
not necessarily known at compile-time) and for the compiler to be
aware that it can't generate code that refers to variables on the
stack relative to rsp, or anything else that might break the
naive implementation of alloca as simply bumping rsp with inline
asm. Apparently this "magic" __alloca can't be used outside of
the compiler, or is there a way to make it work?
More information about the Digitalmars-d-learn
mailing list