The following code compiles fine to the object file, but linking fails with:
/usr/bin/ld: implicit.o:(.data._D41TypeInfo_S8bug4mainFZ9__lambda1MFZ1S6__initZ+0x28): undefined reference to `_D8bug4mainFZ9__lambda1MFZ1S6__initZ'
---
__gshared int x;
void main()
{
static assert(__traits(compiles,
{
struct S { int *p = &x; }
auto t = typeid(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-24T13:14:45Z
@RazvanN7 created dlang/dmd pull request #14844 "Fix Issue 23650 - Using typeid with struct defined in in __traits(compiles, ...) causes linker error" fixing this issue:
- Fix Issue 23650 - Using typeid with struct defined in in __traits(compiles, ...) causes linker error
https://github.com/dlang/dmd/pull/14844
Comment #2 by dlang-bot — 2023-01-26T10:25:42Z
dlang/dmd pull request #14844 "Fix Issue 23650 - Using typeid with struct defined in __traits(compiles, ...) causes linker error" was merged into master:
- 8c4151300d4d4160fb00208d65b2e38e3b7e4b23 by RazvanN7:
Fix Issue 23650 - Using typeid with struct defined in __traits(compiles, ...) causes linker error
https://github.com/dlang/dmd/pull/14844