View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0018255FPCCompilerpublic2010-12-18 15:172012-05-06 18:57
ReporterTheMaster 
Assigned To 
PrioritynormalSeveritymajorReproducibilityalways
StatusfeedbackResolutionreopened 
Platformx86_64OSWindowsOS VersionSeven
Product Version2.4.3Product Build2010/12/17. but since ver 2.2.x 
Target VersionFixed in Version 
Summary0018255: Inline asm - wrong code generated
Descriptionfunction, started with
asm
  mov qword ptr s_rbx,rbx
  mov qword ptr s_rcx,rcx
...
produces
  push rbp
  mov rbp, rsp
  sub rsp, 20h
  mov dword ptr qword_40D000, ebx
  mov dword ptr qword_40D010, ecx
...
Steps To ReproduceJust compile the source file or check .exe
TagsNo tags attached.
FPCOldBugId
Fixed in Revision
Attached Fileszip file icon project.zip [^] (29,231 bytes) 2010-12-18 15:17

- Relationships
duplicate of 0016622closedFlorian Invalid REX prefix 

-  Notes
(0044334)
Marco van de Voort (manager)
2010-12-18 15:30

This is the normal prologue/epilogue for procedure, use the "nostackframe" directive to suppress this if necessary.

Please reopen if I understood it wrong
(0044340)
TheMaster (reporter)
2010-12-18 16:32

64 bit CPU registers compiled as 32 bits!
(0044342)
Marco van de Voort (manager)
2010-12-18 17:49

I can't duplicate this. I get (using objdump from recent mingw64); all are nicely 64-bit rvariants:.

Maybe your disassembling tool has the problem?

00000001000013f0 <P$PROJECT1_SAVEREGS>:
   1000013f0: 90 nop
   1000013f1: 90 nop
   1000013f2: 90 nop
   1000013f3: 48 89 1c 25 00 b0 00 mov %rbx,0xb000
   1000013fa: 00
   1000013fb: 48 89 0c 25 10 b0 00 mov %rcx,0xb010
   100001402: 00
   100001403: 48 89 14 25 20 b0 00 mov %rdx,0xb020
   10000140a: 00
   10000140b: 48 89 34 25 30 b0 00 mov %rsi,0xb030
   100001412: 00
   100001413: 48 89 3c 25 40 b0 00 mov %rdi,0xb040
   10000141a: 00
   10000141b: 48 89 2c 25 50 b0 00 mov %rbp,0xb050
   100001422: 00
   100001423: 4c 89 04 25 60 b0 00 mov %r8,0xb060
   10000142a: 00
   10000142b: 4c 89 0c 25 70 b0 00 mov %r9,0xb070
   100001432: 00
   100001433: 4c 89 14 25 80 b0 00 mov %r10,0xb080
   10000143a: 00
   10000143b: 4c 89 1c 25 90 b0 00 mov %r11,0xb090
   100001442: 00
   100001443: 4c 89 24 25 a0 b0 00 mov %r12,0xb0a0
   10000144a: 00
   10000144b: 4c 89 2c 25 b0 b0 00 mov %r13,0xb0b0
   100001452: 00
   100001453: 4c 89 34 25 c0 b0 00 mov %r14,0xb0c0
   10000145a: 00
   10000145b: 4c 89 3c 25 d0 b0 00 mov %r15,0xb0d0
   100001462: 00
   100001463: c3 retq
    ...

0000000100001470 <P$PROJECT1_RESTOREREGS>:
   100001470: 48 83 ec 28 sub $0x28,%rsp
   100001474: 48 8b 1c 25 00 b0 00 mov 0xb000,%rbx
   10000147b: 00
   10000147c: 48 8b 0c 25 10 b0 00 mov 0xb010,%rcx
   100001483: 00
   100001484: 48 8b 14 25 20 b0 00 mov 0xb020,%rdx
   10000148b: 00
   10000148c: 48 8b 34 25 30 b0 00 mov 0xb030,%rsi
   100001493: 00
   100001494: 48 8b 3c 25 40 b0 00 mov 0xb040,%rdi
   10000149b: 00
   10000149c: 48 8b 2c 25 50 b0 00 mov 0xb050,%rbp
   1000014a3: 00
   1000014a4: 4c 8b 04 25 60 b0 00 mov 0xb060,%r8
   1000014ab: 00
   1000014ac: 4c 8b 0c 25 70 b0 00 mov 0xb070,%r9
   1000014b3: 00
   1000014b4: 4c 8b 14 25 80 b0 00 mov 0xb080,%r10
   1000014bb: 00
   1000014bc: 4c 8b 1c 25 90 b0 00 mov 0xb090,%r11
   1000014c3: 00
   1000014c4: 4c 8b 24 25 a0 b0 00 mov 0xb0a0,%r12
   1000014cb: 00
   1000014cc: 4c 8b 2c 25 b0 b0 00 mov 0xb0b0,%r13
   1000014d3: 00
   1000014d4: 4c 8b 34 25 c0 b0 00 mov 0xb0c0,%r14
   1000014db: 00
   1000014dc: 4c 8b 3c 25 d0 b0 00 mov 0xb0d0,%r15
   1000014e3: 00
   1000014e4: 48 83 c4 28 add $0x28,%rsp
   1000014e8: c3 retq
   1000014e9: 00 00 add %al,(%rax)
   1000014eb: 00 00 add %al,(%rax)
   1000014ed: 00 00 add %al,(%rax)
    ...
(0044344)
TheMaster (reporter)
2010-12-18 17:51
edited on: 2010-12-18 17:55

You can see wrong opcodes in compiled .exe (attached to original message). Checked it by hex editor again. SaveRegs procedure:

401400: 55 push rbp
401401: 4889E5 mov rbp,rsp
401404: 4883EC20 sub rsp,020 ;' '
401408: 891C2500D04000 mov [00040D000],ebx
40140F: 890C2510D04000 mov [00040D010],ecx
401416: 89142520D04000 mov [00040D020],edx
40141D: 89342530D04000 mov [00040D030],esi
401424: 893C2540D04000 mov [00040D040],edi
40142B: 892C2550D04000 mov [00040D050],ebp
401432: 4489042560D04000 mov [00040D060],r8d
40143A: 44890C2570D04000 mov [00040D070],r9d
401442: 4489142580D04000 mov [00040D080],r10d
40144A: 44891C2590D04000 mov [00040D090],r11d
401452: 44892425A0D04000 mov [00040D0A0],r12d
40145A: 44892C25B0D04000 mov [00040D0B0],r13d
401462: 44893425C0D04000 mov [00040D0C0],r14d
40146A: 44893C25D0D04000 mov [00040D0D0],r15d

but RestoreRegs procedure looks fine...

(0044368)
Marco van de Voort (manager)
2010-12-19 12:30

I realized that I might have used 2.5.1 t.b.t with 2.4.x.
(0044371)
Marco van de Voort (manager)
2010-12-19 14:07
edited on: 2010-12-19 14:11

... indeed it IS reproducable with 2.4.3 as of today.

Possible fix: r15595:

r15595 | florian | 2010-07-17 22:57:06 +0200 (Sat, 17 Jul 2010) | 1 line
Changed paths:
   M /trunk/compiler/x86/rax86.pas
   A /trunk/tests/webtbs/tw16622.pp

* don't force OT_BITS32 on x86-64, resolves 0016622

(0044384)
Marco van de Voort (manager)
2010-12-19 18:08

I merged the above fix to 2.4.x branch.
(0044387)
Jonas Maebe (manager)
2010-12-19 18:16

I think that is a bad idea given that it can easily break other things (see the last comment). Changes that have a high chance of introducing regressions should not be merged to the fixes branch.
(0044846)
TheMaster (reporter)
2011-01-03 23:03
edited on: 2011-01-03 23:05

Another problem with this code, if it compiled to DLL.
---
Library project1;
{$ASMMODE INTEL}
Var
  s_rbx,s_rcx,s_rdx : Int64;

Begin
asm
  mov qword ptr s_rbx,rbx
  mov qword ptr s_rcx,rcx
  mov qword ptr s_rdx,rdx
  xor rbx,rbx
  xor rcx,rcx
  xor rdx,rdx
  mov rbx,qword ptr s_rbx
  mov rcx,qword ptr s_rcx
  mov rdx,qword ptr s_rdx
end;
End.
---

Reloc's looks good, but something bad with offsets.

sub_1100013E0 proc near
push rbp
mov rbp, rsp
sub rsp, 20h
call sub_1100040E0
mov +1000D000h], rbx
mov +1000D010h], rcx
mov +1000D020h], rdx
xor rbx, rbx
xor rcx, rcx
xor rdx, rdx
mov rbx, +1000D000h]
mov rcx, +1000D010h]
mov rdx, +1000D020h]
leave
retn
sub_1100013E0 endp

(0044975)
TheMaster (reporter)
2011-01-07 20:46
edited on: 2011-01-09 11:08

Relocations for .text section in HIGHLOW format. For .data section it is DIR64 (as it need). Removed relocs for .text section, changed opcodes in HIEW manually, for example:
.100013ED: 48891D2500D000 mov [00000001`10D01419],rbx <- changed
.100013F4: 90 nop
.100013F5: 48891C2510D00010 mov [01000D010],rbx <- old opcode type
...
.1000140E: 488B1DFBBB0000 mov rbx,[00000001`1000D010]
.10001415: 90 nop
.10001416: 488B1C2510D00010 mov rbx,[01000D010]

(0049034)
Sergei Gorelkin (developer)
2011-06-11 04:44

This is not fixable (the only possible fix is to detect and reject such code).
In 64-bit mode, absolute 64-bits offsets are available only in a single form of 'mov' instruction (with accumulator as source or destination). All other offsets are truncated to 32 bits, and the resulting image can work only if loaded below 4 gigabytes. Since dlls must be loadable at any address, they cannot use instructions with absolute addressing, and must use rip-relative addressing instead.

You need to replace 'mov qword ptr s_rbx, rbx' with 'mov qword ptr [rip+s_rbx], rbx' in your assembler code.

- Issue History
Date Modified Username Field Change
2010-12-18 15:17 TheMaster New Issue
2010-12-18 15:17 TheMaster File Added: project.zip
2010-12-18 15:30 Marco van de Voort Status new => resolved
2010-12-18 15:30 Marco van de Voort Resolution open => no change required
2010-12-18 15:30 Marco van de Voort Assigned To => Marco van de Voort
2010-12-18 15:30 Marco van de Voort Note Added: 0044334
2010-12-18 16:32 TheMaster Status resolved => feedback
2010-12-18 16:32 TheMaster Resolution no change required => reopened
2010-12-18 16:32 TheMaster Note Added: 0044340
2010-12-18 17:49 Marco van de Voort Note Added: 0044342
2010-12-18 17:51 TheMaster Note Added: 0044344
2010-12-18 17:52 TheMaster Note Edited: 0044344
2010-12-18 17:55 TheMaster Note Edited: 0044344
2010-12-18 17:55 TheMaster Note Edited: 0044344
2010-12-19 12:30 Marco van de Voort Note Added: 0044368
2010-12-19 14:07 Marco van de Voort Note Added: 0044371
2010-12-19 14:11 Marco van de Voort Note Edited: 0044371
2010-12-19 14:52 Jonas Maebe Relationship added duplicate of 0016622
2010-12-19 14:52 Jonas Maebe Duplicate ID 0 => 16622
2010-12-19 14:52 Jonas Maebe Status feedback => resolved
2010-12-19 14:52 Jonas Maebe Resolution reopened => duplicate
2010-12-19 18:08 Marco van de Voort Note Added: 0044384
2010-12-19 18:16 Jonas Maebe Note Added: 0044387
2011-01-03 23:03 TheMaster Status resolved => feedback
2011-01-03 23:03 TheMaster Resolution duplicate => reopened
2011-01-03 23:03 TheMaster Note Added: 0044846
2011-01-03 23:05 TheMaster Note Edited: 0044846
2011-01-07 20:46 TheMaster Note Added: 0044975
2011-01-09 11:08 TheMaster Note Edited: 0044975
2011-01-09 16:15 TheMaster Note Added: 0045017
2011-01-10 12:19 TheMaster Note Deleted: 0045017
2011-06-11 04:44 Sergei Gorelkin Note Added: 0049034
2012-05-06 18:57 Marco van de Voort Assigned To Marco van de Voort =>



MantisBT 1.2.12[^]
Copyright © 2000 - 2012 MantisBT Group
Powered by Mantis Bugtracker