Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][clang-cl][x86]In Windows 32-bit, the program generated using assembly language cannot run properly. #62010

Closed
romanholidaypancakes opened this issue Apr 8, 2023 · 6 comments
Labels
backend:X86 mc Machine (object) code

Comments

@romanholidaypancakes
Copy link

romanholidaypancakes commented Apr 8, 2023

  1. test.cpp
#include "stdio.h"
#include <windows.h>


int main() {
    printf("hello world");
    MessageBoxW(0, 0, L"hello world", 0);
    return 0;
}
  1. generate assembly
clang-cl -m32 test.cpp /clang:-S
  1. generating programs from assembler file
clang-cl user32.lib -m32 test.asm -o test32.exe
  1. When running test32.exe, it crashes upon calling MessageBoxW, indicating a failure to properly relocate imported functions. However, the same command runs successfully on the 64-bit version of the program.
@alvinhochun
Copy link
Contributor

The generated assembly is

	call	dword ptr [__imp__MessageBoxW@16]

When compiling from the source to binary directly, this is the disassembly:

007d103f ff1510417e00    call    dword ptr [test62010+0x14110 (007e4110)]

When compiling from the assembly, this is what I got instead:

00b8103f e8cc300100      call    test62010_b+0x14110 (00b94110)

Looks like LLVM may be doing something odd with Intel asm syntax: https://godbolt.org/z/YWvrn75YG

@romanholidaypancakes
Copy link
Author

https://godbolt.org/z/PP8W9W3W7

  • 32
_main:                                  # @main
        push    ebp
        mov     ebp, esp
        sub     esp, 16
        xor     eax, eax
        lea     eax, ["??_C@_1BI@HHJHKLLN@?$AAh?$AAe?$AAl?$AAl?$AAo?$AA?5?$AAw?$AAo?$AAr?$AAl?$AAd?$AA?$AA@"]
        mov     dword ptr [esp], 0
        mov     dword ptr [esp + 4], 0
        mov     dword ptr [esp + 8], eax
        mov     dword ptr [esp + 12], 0
        call    dword ptr [__imp__MessageBoxW@16]
        sub     esp, 16
        xor     eax, eax
        add     esp, 16
        pop     ebp
        ret
"??_C@_1BI@HHJHKLLN@?$AAh?$AAe?$AAl?$AAl?$AAo?$AA?5?$AAw?$AAo?$AAr?$AAl?$AAd?$AA?$AA@":
  • 64
main:                                   # @main
        sub     rsp, 40
        xor     eax, eax
        mov     edx, eax
        lea     r8, [rip + "??_C@_1BI@HHJHKLLN@?$AAh?$AAe?$AAl?$AAl?$AAo?$AA?5?$AAw?$AAo?$AAr?$AAl?$AAd?$AA?$AA@"]
        xor     r9d, r9d
        mov     rcx, rdx
        call    qword ptr [rip + __imp_MessageBoxW]
        xor     eax, eax
        add     rsp, 40
        ret
"??_C@_1BI@HHJHKLLN@?$AAh?$AAe?$AAl?$AAl?$AAo?$AA?5?$AAw?$AAo?$AAr?$AAl?$AAd?$AA?$AA@":

64 will use rip relocation

@romanholidaypancakes romanholidaypancakes changed the title In Windows 32-bit, the program generated using assembly language cannot run properly. [clang][clang-cl][x86]In Windows 32-bit, the program generated using assembly language cannot run properly. Apr 10, 2023
@romanholidaypancakes
Copy link
Author

the assembly generated by gcc can be compiled and run normally

	.file	"test.cpp"
	.intel_syntax noprefix
	.text
	.def	___main;	.scl	2;	.type	32;	.endef
	.section .rdata,"dr"
LC0:
	.ascii "hello world\0"
	.align 2
LC1:
	.ascii "h\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0\0\0"
	.text
	.globl	_main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
LFB516:
	.cfi_startproc
	lea	ecx, [esp+4]
	.cfi_def_cfa 1, 0
	and	esp, -16
	push	DWORD PTR [ecx-4]
	push	ebp
	.cfi_escape 0x10,0x5,0x2,0x75,0
	mov	ebp, esp
	push	ecx
	.cfi_escape 0xf,0x3,0x75,0x7c,0x6
	sub	esp, 20
	call	___main
	mov	DWORD PTR [esp], OFFSET FLAT:LC0
	call	_printf
	mov	DWORD PTR [esp+12], 0
	mov	DWORD PTR [esp+8], OFFSET FLAT:LC1
	mov	DWORD PTR [esp+4], 0
	mov	DWORD PTR [esp], 0
	mov	eax, DWORD PTR __imp__MessageBoxW@16
	call	eax
	sub	esp, 16
	mov	eax, 0
	mov	ecx, DWORD PTR [ebp-4]
	.cfi_def_cfa 1, 0
	leave
	.cfi_restore 5
	lea	esp, [ecx-4]
	.cfi_def_cfa 4, 4
	ret
	.cfi_endproc
LFE516:
	.ident	"GCC: (i686-win32-dwarf-rev0, Built by MinGW-W64 project) 8.1.0"
	.def	_printf;	.scl	2;	.type	32;	.endef

@llvmbot
Copy link
Collaborator

llvmbot commented Apr 28, 2023

@llvm/issue-subscribers-backend-x86

@alvinhochun alvinhochun added the mc Machine (object) code label May 1, 2023
@alvinhochun
Copy link
Contributor

alvinhochun added a commit that referenced this issue May 8, 2023
Clang on Windows targets often requires indirect calls through the
import address table (IAT), and also .refptr stubs for MinGW target.
On 32-bit this generates assembly in the form of
`call dword ptr [__imp__func]`, which MC had failed to handle correctly.
64-bit targets are not affected because rip-relative addressing is used.

Reported on: #62010

Depends on D149695, D149920

Differential Revision: https://reviews.llvm.org/D149579
@alvinhochun alvinhochun added this to the LLVM 17.0.0 Release milestone May 9, 2023
@alvinhochun
Copy link
Contributor

Should be fixed in trunk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 mc Machine (object) code
Projects
Development

No branches or pull requests

4 participants