调整示例DLL代码时获取无效图像

时间:2017-10-18 12:49:20

标签: windows winapi assembly dll fasm

我从FASM示例目录中创建了一个简单的DLL示例代码,并根据我的需要进行调整。但是,当我做一些(从我的POV无辜)更改时,生成的二进制文件被破坏 - 运行使用此库的exe会产生错误代码0xC000007B aka INVALID_IMAGE_FORMAT。

DLL代码:

; DLL creation example

format PE GUI 4.0 DLL
entry DllEntryPoint

include 'win32a.inc'

section '.text' code readable executable

proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
        mov     eax,TRUE
        ret
endp

proc ShowErrorMessage hWnd,dwError
  local lpBuffer:DWORD
        lea     eax,[lpBuffer]
        invoke  FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM,0,[dwError],LANG_NEUTRAL,eax,0,0
        invoke  MessageBox,[hWnd],[lpBuffer],NULL,MB_ICONERROR+MB_OK
        ret
endp

proc ShowLastError hWnd
        ret
endp

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
          user,'USER32.DLL'

  import kernel,\
         GetLastError,'GetLastError',\
         SetLastError,'SetLastError',\
         FormatMessage,'FormatMessageA',\
         LocalFree,'LocalFree'

  import user,\
         MessageBox,'MessageBoxA'

section '.edata' export data readable

  export 'DLL.DLL',ShowErrorMessage,'ShowErrorMessage',ShowLastError,'ShowLastError'

section '.reloc' fixups data readable discardable

可执行代码:

format PE GUI 4.0
entry start

include 'win32a.inc'

section '.text' code readable executable

  start:
jmp ShowLastError

section '.idata' import data readable writeable

  library mydll,'DLL.DLL'

  import mydll,\
         ShowLastError,'ShowLastError'

当我改变时,比方说,

export 'DLL.DLL',ShowErrorMessage,'ShowErrorMessage',ShowLastError,'ShowLastError'

行到

export 'DLL.DLL',ShowLastError,'ShowLastError'

代码中断。如果我将ShowErrorMessage正文更改为ret,则会发生同样的情况。

我对此完全感到困惑。这是一个FASM错误,还是我做错了什么?

1 个答案:

答案 0 :(得分:1)

我无法找到解释,但至少我找到了解决方法。更改以下行

section '.reloc' fixups data readable discardable

data fixups
end data

解决了这个问题。