我想把以下汇编代码编译成共享库(dll)
.extern _GetProcAddress
.global _main
_main:
CALL _GetProcAddress
使用以下表述:
i686-w64-mingw32-gcc -shared -o file.dll file.S
但是我收到以下链接错误:
`_GetProcAddress' referenced in section `.text' of /tmp/ccamwU4N.o: defined in discarded section `.text' of /usr/lib/gcc/i686-w64-mingw32/4.6/../../../../i686-w64-mingw32/lib/../lib/libkernel32.a(dxprbs00553.o)
如何修复此错误并调用winapi函数?
答案 0 :(得分:1)
此代码:
// file: winapi0.s
.extern ___main
.extern _GetStdHandle@4
.extern _WriteConsoleA@20
.data
_text:
.ascii "Hello World!\12\0"
.section .text.startup,"x"
.p2align 2,,3
.globl _main
_main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $52, %esp
call ___main
movl $-11, (%esp)
call _GetStdHandle@4
pushl %edx
movl $0, 16(%esp)
leal -12(%ebp), %edx
movl %edx, 12(%esp)
movl $14, 8(%esp)
movl $_text, 4(%esp)
movl %eax, (%esp)
call _WriteConsoleA@20
subl $20, %esp
xorl %eax, %eax
movl -4(%ebp), %ecx
leave
leal -4(%ecx), %esp
ret
为我编译就好了:
gcc -Wall -O2 winapi0.s -o winapi0.exe
它可以打印出来:
Hello World!
这是由gcc翻译成带有-S
开关的程序集的C代码的略微清理版本:
// file: winapi.c
#include <windows.h>
#include <tchar.h>
TCHAR text[] = _T("Hello World!\n");
int main(void)
{
DWORD err;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),
text,
sizeof(text) / sizeof(text[0]),
&err,
NULL);
return 0;
}
我原来是这个:
.file "winapi.c"
.def ___main; .scl 2; .type 32; .endef
.section .text.startup,"x"
.p2align 2,,3
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
LFB14:
.cfi_startproc
leal 4(%esp), %ecx
.cfi_def_cfa 1, 0
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
.cfi_escape 0x10,0x5,0x2,0x75,0
pushl %ecx
.cfi_escape 0xf,0x3,0x75,0x7c,0x6
subl $52, %esp
call ___main
movl $-11, (%esp)
call _GetStdHandle@4
pushl %edx
movl $0, 16(%esp)
leal -12(%ebp), %edx
movl %edx, 12(%esp)
movl $14, 8(%esp)
movl $_text, 4(%esp)
movl %eax, (%esp)
call _WriteConsoleA@20
subl $20, %esp
xorl %eax, %eax
movl -4(%ebp), %ecx
.cfi_def_cfa 1, 0
leave
leal -4(%ecx), %esp
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE14:
.globl _text
.data
_text:
.ascii "Hello World!\12\0"
.def _GetStdHandle@4; .scl 2; .type 32; .endef
.def _WriteConsoleA@20; .scl 2; .type 32; .endef
对于64位情况,你不能这样做吗?
答案 1 :(得分:0)
链接包含要调用的函数的Windows DLL的导入库。 我相信该符号包含在kernel32.dll中。您需要创建一个导入库,然后与其链接。