January 30, 2023
https://issues.dlang.org/show_bug.cgi?id=23661

          Issue ID: 23661
           Summary: Using typeid from template inside __traits(compiles,
                    ...) causes linker error
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: blocker
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: teodor.dutu@gmail.com

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.

--