The following code compiles fine to the object file, but linking fails with:
/usr/bin/ld: bug.o:(.data._D36TypeInfo_S3bug4mainFZ9__lambda1MFZ1S6__initZ+0x28): undefined reference to `_D3bug4mainFZ9__lambda1MFZ1S6__initZ'
---
__gshared int x;
void foo(T)()
{
auto x = typeid(T);
}
void main()
{
static assert(__traits(compiles,
{
struct S { int *p = &x; }
foo!S();
}));
}
---
The reason is that the init symbol of the TypeInfo is defined and requires a relocation to S.init which is undefined:
$ readelf -r bug.o
[...]
Relocation section '.rela.data._D36TypeInfo_S3bug4mainFZ9__lambda1MFZ1S6__initZ' at offset 0x900 contains 5 entries:
Offset Info Type Sym. Value Sym. Name + Addend
000000000000 001f00000001 R_X86_64_64 0000000000000000 _D15TypeInfo_Struct6__ + 0
000000000018 001b00000001 R_X86_64_64 0000000000000000 _D36TypeInfo_S3bug4mai + 88
000000000028 002000000001 R_X86_64_64 0000000000000000 _D3bug4mainFZ9__lambda + 0
000000000070 002100000001 R_X86_64_64 0000000000000000 _D10TypeInfo_l6__initZ + 0
000000000080 002200000001 R_X86_64_64 0000000000000000 _D6object__T10RTInfoIm + 0
This is blocking https://github.com/dlang/dmd/pull/14664.
Comment #1 by dlang-bot — 2023-01-31T16:53:27Z
@RazvanN7 created dlang/dmd pull request #14856 "Fix Issue 23661 - Using typeid from template inside __traits(compiles, ...) causes linker error" fixing this issue:
- Fix Issue 23661 - Using typeid from template inside __traits(compiles, ...) causes linker error
https://github.com/dlang/dmd/pull/14856
Comment #2 by robert.schadek — 2024-12-13T19:27:00Z