Default initializers (or: win32 bindings' struct sizes)

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Wed Jan 17 04:56:32 PST 2007


Lionello Lunesu wrote:
> What's more, the there are no linker errors for the default built-in initializers:
> 
> struct S { int s; }    // no linker error
> struct D { int d=0; }  // linker error!
> 
> The linking error occurs even with the -O -release -inline flags.
> 
> Why does DMD (I don't know about GDC) require that __init symbol for 
> custom initializers and not for the default ones? Is there any chance 
> that this requirement will be dropped?

Actually, it's not (only) about custom initializers:
*****
urxae at urxae:~/tmp$ cat test.d
import test2;
import std.stdio;

void main()
{
     S s;
     writefln(cast(int)s.c);
}
urxae at urxae:~/tmp$ cat test2.d
struct S { char c; }
urxae at urxae:~/tmp$ dmd test.d
gcc test.o -o test -m32 -lphobos -lpthread -lm -Xlinker 
-L/home/urxae/opt/dmd/lib
test.o: In function `_Dmain':
test.d:(.gnu.linkonce.t_Dmain+0x7): undefined reference to 
`_D5test21S6__initZ'
collect2: ld returned 1 exit status
--- errorlevel 1
*****

(Similarly fails for wchar, dchar, float, double and real)
It looks to be about everything but *all-zero* default initializers. 
Those seem to be the only ones not to need explicit initializers to be 
generated if the type is used as a local variable. Their initialization 
is inlined.

For some reason the inlining doesn't happen for explicitly 
zero-initialized variables, nor for non-zero or non-default initializers.

This despite the fact that global/static variables seem to get the 
initializer "inlined" into the '.data' or (if all-zero) '.bss' section[1].
So it's not that the compiler doesn't know what the initializers should 
be, it's that the code generated to initialize local variables at 
run-time depends on the initializer data being generated by compilation 
of the imported module if it's either non-default or not all-zero.

Preventing your link error would be a simple matter of treating 
initializer symbols similar to templates: put them in link-once sections 
in every object file where they're needed. Not that I'm saying this 
should necessarily be done; I'm just saying it should be relatively 
simple to implement.


[1]: At least, those are the names on Linux (and anywhere else that uses 
ELF binaries, for that matter). Not sure what they are on Windows, but 
the principle should be similar.



More information about the Digitalmars-d mailing list