Bug 14872 – [2.068.2] Label address in asm [x86-64]
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2015-08-05T15:49:03Z
Last change time
2020-08-27T08:44:27Z
Keywords
iasm, pull, wrong-code
Assigned to
No Owner
Creator
Daniel
Comments
Comment #0 by wyrlon — 2015-08-05T15:49:03Z
32bit x86 asm works fine, but I can't get 64bit working.
import std.stdio;
void main()
{
size_t addr1=123;
size_t addr2=456;
version(D_InlineAsm_X86_64)
asm
{
lea RAX, lbl1;
mov addr1, RAX;
lea RAX, lbl2;
mov addr2, RAX;
}
else version(D_InlineAsm_X86)
asm
{
lea EAX, lbl1;
mov addr1, EAX;
lea EAX, lbl2;
mov addr2, EAX;
}
lbl1:
writeln(addr1); // equals 1 for X86-64
lbl2:
writeln(addr2); // equals 1 for X86-64
}
Comment #1 by wyrlon — 2015-09-29T19:54:44Z
I tested that it works with ldc2 and saw some interesting things when disassembling at asm.dlang.org
dmd -m32
lea %cs:0x28,%eax
mov %eax,-0x8(%ebp)
lea %cs:0x30,%eax
mov %eax,-0x4(%ebp)
dmd -m64
cs
mov %rax,%rax
mov %rax,-0x10(%rbp)
cs
mov %rax,%rax
mov %rax,-0x8(%rbp)
ldc2 -m64
leaq .L_Dmain_lbl1, %rax
movq %rax, -72(%rbp)
leaq .L_Dmain_lbl2, %rax
movq %rax, -80(%rbp)
Comment #2 by dlang-bugzilla — 2017-07-21T07:16:14Z
(In reply to Daniel from comment #0)
> 32bit x86 asm works fine, but I can't get 64bit working.
I think that has something to do with Intel x64 being position-independent, meaning that "lea RAX, lbl1" is not going to do what you want. It looks like in case of DMD, it's generating nonsense code (mov eax, eax), but it could still use the offset relative to some base. In any case, it certainly won't be the actual in-address memory of the code at that label (so that you can "jmp" to it).
So, your example is a bit nonsensical, but DMD should still not be emitting garbage code, and either generate an error or use some other kind of offset (so that it's at least possible to use the offset in calculations to produce something useful).
Comment #3 by dlang-bot — 2020-08-27T05:43:44Z
@WalterBright created dlang/dmd pull request #11631 "fix Issue 14872 - [2.068.2] Label address in asm [x86-64]" fixing this issue:
- fix Issue 14872 - [2.068.2] Label address in asm [x86-64]
https://github.com/dlang/dmd/pull/11631
Comment #4 by dlang-bot — 2020-08-27T08:44:27Z
dlang/dmd pull request #11631 "fix Issue 14872 - [2.068.2] Label address in asm [x86-64]" was merged into master:
- b8a8d6f183342bb5bd9944595218661efcb22b08 by Walter Bright:
fix Issue 14872 - [2.068.2] Label address in asm [x86-64]
https://github.com/dlang/dmd/pull/11631