Thread overview
[Issue 22170] interface thunk doesn't set EBX to GOT
Aug 02, 2021
Iain Buclaw
Aug 02, 2021
Iain Buclaw
Aug 03, 2021
Iain Buclaw
Aug 03, 2021
Iain Buclaw
Aug 03, 2021
Iain Buclaw
Aug 03, 2021
Dlang Bot
Aug 03, 2021
Dlang Bot
Aug 04, 2021
Dlang Bot
August 02, 2021
https://issues.dlang.org/show_bug.cgi?id=22170

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |backend, wrong-code
                 CC|                            |ibuclaw@gdcproject.org

--
August 02, 2021
https://issues.dlang.org/show_bug.cgi?id=22170

--- Comment #1 from Iain Buclaw <ibuclaw@gdcproject.org> ---
It looks like what DMD is doing is loading the GOT into EBX before every function call.

i.e: Abridged version of objdump
---
push   %ebp
mov    %esp,%ebp
sub    $0x28,%esp
mov    %ebx,-0x28(%ebp)
mov    %esi,-0x24(%ebp)

mov    -0x1c(%ebp),%ebx
call   147c8 <_D5mydll10multiply10FiZi@plt>
mov    -0x1c(%ebp),%ebx
call   *%esi
mov    -0x1c(%ebp),%ebx
call   145b0 <_D5mydll1S3addMFiZi@plt>
mov    -0x1c(%ebp),%ebx
call   145b0 <_D5mydll1S3addMFiZi@plt>
mov    -0x1c(%ebp),%ebx
call   14560 <_D5mydll1I6createFZCQs1C@plt>
mov    -0x1c(%ebp),%ebx
mov    (%eax),%ecx
call   *0x4(%ecx)

xor    %eax,%eax
mov    -0x28(%ebp),%ebx
mov    -0x24(%ebp),%esi
leave
ret
---

Surely it'd be more efficient to load GOT in the prologue, then restore the previous in the epilogue.

i.e: The above rewritten:
---
push   %ebp
mov    %esp,%ebp
sub    $0x28,%esp
mov    %ebx,-0x28(%ebp) # <- looks like a save (better push %ebx?)
mov    %esi,-0x24(%ebp)
mov    -0x1c(%ebp),%ebx # <- Added load GOT here

call   147c8 <_D5mydll10multiply10FiZi@plt>
call   *%esi
call   145b0 <_D5mydll1S3addMFiZi@plt>
call   145b0 <_D5mydll1S3addMFiZi@plt>
call   14560 <_D5mydll1I6createFZCQs1C@plt>
mov    (%eax),%ecx
call   *0x4(%ecx)

xor    %eax,%eax
mov    -0x28(%ebp),%ebx # <- looks like a restore (better pop %ebx?)
mov    -0x24(%ebp),%esi
leave
ret
---

So it seems that the save/restore is already being done for normal functions, but this isn't being done for thunks.

--
August 03, 2021
https://issues.dlang.org/show_bug.cgi?id=22170

--- Comment #2 from Iain Buclaw <ibuclaw@gdcproject.org> ---
GDC doesn't run into this by calling the aliased symbol directly in the thunk, e.g:

GDC
---
subl   $0x8,0x4(%esp)
jmp    0xf7f34ffb <_D5mydll1C3fooMFCQp1IZCQvQr>
---

DMD
---
sub    $0x8,%eax
jmp    0xf7f06970 <_D5mydll1C3fooMFCQp1IZCQvQr@plt>
---


GDC only generates thunks for symbols that are being emitted in this compilation, AFAIK it's not possible to have a thunk for an external symbol in DMD as well?

--
August 03, 2021
https://issues.dlang.org/show_bug.cgi?id=22170

--- Comment #3 from Iain Buclaw <ibuclaw@gdcproject.org> ---
The fact that GOT is loaded into EBX before every function call is at best a performance bug and not related to this issue, so moved it to issue 22172.

--
August 03, 2021
https://issues.dlang.org/show_bug.cgi?id=22170

--- Comment #4 from Iain Buclaw <ibuclaw@gdcproject.org> ---
(In reply to Iain Buclaw from comment #2)
> DMD
> ---
> sub    $0x8,%eax
> jmp    0xf7f06970 <_D5mydll1C3fooMFCQp1IZCQvQr@plt>
> ---
Removing the PLT (same as PIE https://github.com/dlang/dmd/blob/master/src/dmd/backend/elfobj.d#L3125), DMD only ever seems to generate a jmp to an offset of the target function.

---
sub    $0x8,%eax
jmp    0xf7f06970 <_D5mydll1C3fooMFCQp1IZCQvQr+4>
---

Which also looks wrong in comparison to GDC and triggers a segfault.

--
August 03, 2021
https://issues.dlang.org/show_bug.cgi?id=22170

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #5 from Dlang Bot <dlang-bot@dlang.rocks> ---
@ibuclaw created dlang/dmd pull request #12950 "fix Issue 22170 - interface thunk doesn't set EBX to GOT" fixing this issue:

- fix Issue 22170 - interface thunk doesn't set EBX to GOT

https://github.com/dlang/dmd/pull/12950

--
August 03, 2021
https://issues.dlang.org/show_bug.cgi?id=22170

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #6 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #12950 "fix Issue 22170 - interface thunk doesn't set EBX to GOT" was merged into stable:

- 1f723f0f17bc9fc9557e22cbfefc913bdfab6037 by Iain Buclaw:
  fix Issue 22170 - interface thunk doesn't set EBX to GOT

https://github.com/dlang/dmd/pull/12950

--
August 04, 2021
https://issues.dlang.org/show_bug.cgi?id=22170

--- Comment #7 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #12953 "merge stable" was merged into master:

- d0406f3afea2c8365eef162cf6c6636aaef5a105 by Iain Buclaw:
  fix Issue 22170 - interface thunk doesn't set EBX to GOT (#12950)

  * fix Issue 22170 - interface thunk doesn't set EBX to GOT

  * dshell: Add dll tests for issue 10462

https://github.com/dlang/dmd/pull/12953

--