www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 902] New: Duplicate zero-initialized template member variables

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=902

           Summary: Duplicate zero-initialized template member variables
           Product: D
           Version: 1.004
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Keywords: link-failure
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: fvbommel wxs.nl


I get a link error under some circumstances from the following sources:

util.d:
-----
template Foo(T) {
    const bool Foo = false;
}

bool foo() { return Foo!(void); }
-----

main.d:
-----
import util;

void main() {
    bool b = Foo!(void);
}
-----

Compiling separately, then linking:
-----
urxae urxae:~/tmp$ dmd -c main.d
urxae urxae:~/tmp$ dmd -c util.d
urxae urxae:~/tmp$ dmd main.o util.o
gcc main.o util.o -o main -m32 -lphobos -lpthread -lm -Xlinker
-L/home/urxae/opt/dmd/lib 
util.o:(.bss+0x0): multiple definition of `_D4util10__T3FooTvZ3Foob'
main.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
--- errorlevel 1
-----
Both object files contain '_D4util10__T3FooTvZ3Foob', aka 'bool
util.Foo!(void).Foo', in the .bss section which produces a conflict.

Compiling together, then linking:
-----
urxae urxae:~/tmp$ dmd main.d util.d -c
urxae urxae:~/tmp$ dmd main.o util.o
gcc main.o util.o -o main -m32 -lphobos -lpthread -lm -Xlinker
-L/home/urxae/opt/dmd/lib 
-----
Or compiling & linking in one step:
-----
urxae urxae:~/tmp$ dmd main.d util.d
gcc main.o util.o -o main -m32 -lphobos -lpthread -lm -Xlinker
-L/home/urxae/opt/dmd/lib 
-----
Neither produces any errors, and both put the symbol only in main.o/.bss, not
in util.o/.bss.


A slight modification to util.d:
-----
template Foo(T) {
    const bool Foo = true;
}

bool foo() { return Foo!(void); }
-----
compiles without errors in any method.
This is because setting Foo(T).Foo to true disqualifies it from being in .bss
(which can only contain all-zero data), and it is then put in section
.gnu.linkonce.d._D4util10__T3FooTvZ3Foob in both object files. Since this is a
link-once section, no conflict occurs.


Suggested fix: Foo(T).Foo instances should be put into a link-once section
regardless of their value. If that can be a .bss-like no-contents section
that's a nice bonus, but if not it still needs to be done to ensure correct
handling of template members.


-- 
Jan 29 2007
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=902






I'm going to mark this issue as FIXED unless somebody can reproduce this with a 
post DMD-1.004.


-- 
Apr 06 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=902






I can't reproduce the exact original problem on DMD v1.010 but it looks like
that's because it constant-propagates Foo!(void).Foo if it's a bool (and if
it's an uint, I didn't try any other primitive types).
However, changing the type to uint[1] breaks it again:
=====
$ cat util.d
template Foo(T) {
    const uint[1] Foo = 0;
}

uint[] foo() {
    return Foo!(void);
}
$ cat main.d
import util;

void main() {
    auto b = Foo!(void);
}
$ dmd -c main.d && dmd -c util.d && dmd main.o util.o
gcc main.o util.o -o main -m32 -lphobos -lpthread -lm -Xlinker
-L/home/urxae/opt/dmd/lib 
util.o:(.bss+0x0): multiple definition of `_D4util10__T3FooTvZ3FooG1k'
main.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
--- errorlevel 1
$ dmd -c main.d util.d && dmd main.o util.o
gcc main.o util.o -o main -m32 -lphobos -lpthread -lm -Xlinker
-L/home/urxae/opt/dmd/lib 
$ dmd main.d util.d
gcc main.o util.o -o main -m32 -lphobos -lpthread -lm -Xlinker
-L/home/urxae/opt/dmd/lib 
=====

So the problem remains, it's just disguised by apparently better
constant-propagation.
This stuff simply shouldn't be in .bss if it can be emitted from multiple
modules. 


-- 
Apr 06 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=902






Added to DStress as
http://dstress.kuehne.cn/complex/template_60


-- 
Apr 25 2007