[Issue 902] New: Duplicate zero-initialized template member variables
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Jan 29 05:18:27 PST 2007
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 at digitalmars.com
ReportedBy: fvbommel at 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 at urxae:~/tmp$ dmd -c main.d
urxae at urxae:~/tmp$ dmd -c util.d
urxae at 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 at urxae:~/tmp$ dmd main.d util.d -c
urxae at 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 at 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.
--
More information about the Digitalmars-d-bugs
mailing list