September 28, 2023
https://issues.dlang.org/show_bug.cgi?id=24168

          Issue ID: 24168
           Summary: Corrupted if TLS values are passed in ref parameters
                    when compiling with -fPIE
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: hinoda2002@outlook.jp

DMD version: v2.105.2
Linux Kernel: 5.15
Distro: Ubuntu 22.04


Code:
----
int i = 42;

bool foo(ref int a)
{
    return a == 42;
}

void main()
{
    assert(foo(i));
}
---

Output(console):
---
$ dmd -fPIE -g -run pie.d
core.exception.AssertError@pie.d(10): Assertion failure
----------------
??:? [0x55b076c2bc48]
??:? [0x55b076c3cabb]
??:? [0x55b076c2bba1]
??:? [0x55b076c26721]
??:? [0x55b076c26501]
??:? [0x55b076c21fa5]
??:? [0x55b076c21800]
./pie.d:10 [0x55b076c21759]
??:? [0x55b076c21de6]
??:? [0x55b076c21c95]
??:? [0x55b076c21d6e]
??:? [0x55b076c21c95]
??:? [0x55b076c21bfe]
??:? [0x55b076c219e7]
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/core/internal/entrypoint.d:29
[0x55b076c21781]
??:? [0x7f596d0ffd8f]
??:? __libc_start_main [0x7f596d0ffe3f]
??:? [0x55b076c21654]
---

Output(binary):
---
$ objdump -dr -Mintel pie.o
(snip)
Disassembly of section .text._Dmain:

0000000000000000 <_Dmain>:
   0:   55                      push   rbp
   1:   48 8b ec                mov    rbp,rsp
   4:   66 48 8d 3d 00 00 00    data16 lea rdi,[rip+0x0]        # c
<_Dmain+0xc>
   b:   00
                        8: R_X86_64_TPOFF32     _D3pie1ii-0x4
   c:   e8 00 00 00 00          call   11 <_Dmain+0x11>
                                                                    d:
R_X86_64_PC32        _D3pie3fooFKiZb-0x4
  11:   84 c0                   test   al,al
  13:   75 11                   jne    26 <_Dmain+0x26>
                                              15:   be 0a 00 00 00          mov
   esi,0xa
          1a:   48 8d 3d 00 00 00 00    lea    rdi,[rip+0x0]        # 21
<_Dmain+0x21>
                        1d: R_X86_64_PC32       .rodata.str1.1-0x4
  21:   e8 00 00 00 00          call   26 <_Dmain+0x26>
                                                                    22:
R_X86_64_PLT32      _d_assertp-0x4
  26:   31 c0                   xor    eax,eax
  28:   5d                      pop    rbp
  29:   c3                      ret
---

I guess this is a codegen bug, _Dmain should be something like this:

---
0000000000000000 <_Dmain>:
   0:   55                      push   rbp
   1:   48 8b ec                mov    rbp,rsp
   4:   31 c0                   xor    eax,eax
   6:   64 48 8b 08             mov    rcx,QWORD PTR fs:[rax]
   a:   48 8d b9 00 00 00 00    lea    rdi,[rcx+0x0]
                        d: R_X86_64_TPOFF32     _D3pie1ii
  11:   e8 00 00 00 00          call   16 <_Dmain+0x16>
                                                                    12:
R_X86_64_PC32       _D3pie3fooFPiZb-0x4
  16:   84 c0                   test   al,al
  18:   75 11                   jne    2b <_Dmain+0x2b>
  1a:   be 0a 00 00 00          mov    esi,0xa
                                              1f:   48 8d 3d 00 00 00 00    lea
   rdi,[rip+0x0]        # 26 <_Dmain+0x26>
                        22: R_X86_64_PC32       .rodata.str1.1-0x4
  26:   e8 00 00 00 00          call   2b <_Dmain+0x2b>
                        27: R_X86_64_PLT32      _d_assertp-0x4
  2b:   31 c0                   xor    eax,eax
  2d:   5d                      pop    rbp
  2e:   c3                      ret
---

--