Bug 902 – Duplicate zero-initialized template member variables
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2007-01-29T07:18:21Z
Last change time
2019-05-21T10:27:07Z
Keywords
link-failure
Assigned to
No Owner
Creator
Frits van Bommel
Comments
Comment #0 by fvbommel — 2007-01-29T07:18:21Z
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.
Comment #1 by thomas-dloop — 2007-04-06T06:29:31Z
I'm going to mark this issue as FIXED unless somebody can reproduce this with a
post DMD-1.004.
Comment #2 by fvbommel — 2007-04-06T07:12:17Z
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.