News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Creating include files for UASM \ HJWasm \ Poasm 64-bit

Started by Vortex, September 27, 2016, 06:26:46 AM

Previous topic - Next topic

Vortex

Here is the new version of my library to include file converter. The tool converts 32-bit MS COFF import libraries to HJWasm \ Masm \ Poasm include files. Version 2.3 can now create include files for 64-bit programming.

Considering that the same named API functions both in 32-bit and 64-bit are taking the same number of parameters, I decided to extract the prototypes from the 32-bit libraries to create include files targeting 64-bit coding.

For example, this API found in kernel32.lib ( 32-bit import library ) :

_CreateFileA@28

28 \ 4 bytes ( DWORD ) = 7

CreateFileA taking 7 parameters.

Assuming that everything is QWORD, it's easy to replace all the DWORDs by QWORDs :

32-bit :

CreateFileA PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD

64-bit :

CreateFileA PROTO :QWORD,:QWORD,:QWORD,:QWORD,:QWORD,:QWORD,:QWORD

An example :

lib2inc \PellesC\lib\Win\kernel32.lib -p64
lib2inc \PellesC\lib\Win\user32.lib -p64
lib2inc \PellesC\lib\Win\gdi32.lib -p64
lib2inc H:\masm32\lib\msvcrt.lib -p64


Naturally, this method based on the assumptions above needs more testing. The attachment contains the tool, some HJWasm 64-bit and Poasm 64-bit projects using the include files created with lib2inc.

LiaoMi

Hi Vortex,

it seems to me there is an error in the program  ::), I took the library here https://github.com/mrfearless/libraries/blob/master/OpenSSL/OpenSSL%20x64/libcrypto.lib, and tried to make the include file, some functions will lose the first character  :icon_eek: B N_mul_word, so it should be right BN_mul_word PROTO C :VARARG, there is a ready-made INC file for comparison https://github.com/mrfearless/libraries/blob/master/OpenSSL/OpenSSL%20x64/libcrypto.inc

Best regards

Vortex

Hi LiaoMi,

Thanks for your message. The VARARG functions of that 64-bit import library are not decorated with a leading  the reason of the problem. Could you try this one using the 32-bit import library?

lib2inc.exe libcrypto.lib -p64
find "BN_mul_word" libcrypto.inc

---------- LIBCRYPTO.INC
BN_mul_word PROTO   :VARARG

find "BN_mul_word" libcrypto-fearless.inc

---------- LIBCRYPTO-FEARLESS.INC
BN_mul_word PROTO C :VARARG

LiaoMi

Quote from: Vortex on February 09, 2018, 05:42:58 AM
Hi LiaoMi,

Thanks for your message. The VARARG functions of that 64-bit import library are not decorated with a leading  the reason of the problem. Could you try this one using the 32-bit import library?

lib2inc.exe libcrypto.lib -p64
find "BN_mul_word" libcrypto.inc

---------- LIBCRYPTO.INC
BN_mul_word PROTO   :VARARG

find "BN_mul_word" libcrypto-fearless.inc

---------- LIBCRYPTO-FEARLESS.INC
BN_mul_word PROTO C :VARARG


Hi Vortex,

32 bit version works fine, although its not always possible to find such an identity, in case there is only x64 library or they have different functions, then there'll be no luck :P

Best regards

Vortex

Hello LiaoMi,

OK, I will add another option to the tool.


Vortex

Hello LiaoMi,

Since the import libraries targeting 64-bit programming are not decorated like the 32-bit versions, it's not possible to get the number of parameters taken by any function. This is why I suggested using the 32-bit libraries to get the number of parameters :

_CreateFileA@28

28 \ 4 bytes ( DWORD ) = 7 -> 7 QWORDs

An approach or a workaround is to create VARARG prototypes :

-l64     : output prototypes for JWasm \ HJWasm \ Poasm 64-bit ( compatibility mode )

; include file generated by lib2inc V2.4

AcquireSRWLockExclusive PROTO :VARARG
AcquireSRWLockShared PROTO :VARARG
ActivateActCtx PROTO :VARARG
AddAtomA PROTO :VARARG
AddAtom equ <AddAtomA>

AddAtomW PROTO :VARARG
AddConsoleAliasA PROTO :VARARG
AddConsoleAlias equ <AddConsoleAliasA>

AddConsoleAliasW PROTO :VARARG
AddDllDirectory PROTO :VARARG
AddIntegrityLabelToBoundaryDescriptor PROTO :VARARG
AddLocalAlternateComputerNameA PROTO :VARARG
AddLocalAlternateComputerName equ <AddLocalAlternateComputerNameA>


Maybe not the best method but as I said no any resources to get the number of parameters.

Version 2.4.3 can be downloaded from :

http://vortex.masmcode.com/files/lib2inc24r3.zip

A quick example in the zip archive :

QuoteSamples\UASM\ScrollDemo2

aw27

Quote
Assuming that everything is QWORD, it's easy to replace all the DWORDs by QWORDs
But it is not.  :(
I prefer to do it by hand, look in the C include files and convert one by one as needed. It is the only safe way.

jj2007

Quote from: aw27 on February 13, 2018, 11:37:35 PMI prefer to do it by hand, look in the C include files and convert one by one as needed. It is the only safe way.

True (WinBase.h):HANDLE
WINAPI
CreateFileA(
    __in     LPCSTR lpFileName,
    __in     DWORD dwDesiredAccess,
    __in     DWORD dwShareMode,
    __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
    __in     DWORD dwCreationDisposition,
    __in     DWORD dwFlagsAndAttributes,
    __in_opt HANDLE hTemplateFile
    );


Four DWORDs, three QWORDs. Doing that manually is tough, however. It is not so difficult, though, to write a parser. WINAPI is a good keyword to start with. And if occasionally you put a QWORD instead of a DWORD, no harm will be done - the receiving end knows to do with rcx, rdx & friends.

aw27

Even tough in most cases it will do no direct harm because the stack receives dwords in chunks of qwords as well, it defeats the whole purpose of prototyping.

Vortex

Quote from: aw27 on February 13, 2018, 11:37:35 PM
But it is not.  :(
I prefer to do it by hand, look in the C include files and convert one by one as needed. It is the only safe way.

lib2inc can convert msvcrt.lib to msvcrt.inc :

lib2inc H:\masm32\lib\kernel32.lib -p64
lib2inc H:\masm32\lib64\msvcrt.lib -l64


Removing the two conflicting prototypes div PROTO :VARARG and fabs PROTO :VARARG from msvcrt.inc :
; include file generated by lib2inc V2.4

$I10_OUTPUT PROTO :VARARG
_CxxThrowException PROTO :VARARG
_Getdays PROTO :VARARG
_Getmonths PROTO :VARARG
_Gettnames PROTO :VARARG
.
.
.
wcstol PROTO :VARARG
wcstombs PROTO :VARARG
wcstoul PROTO :VARARG
wcsxfrm PROTO :VARARG
wctomb PROTO :VARARG
wprintf PROTO :VARARG
wscanf PROTO :VARARG


A quick test :
OPTION win64:3

include kernel32.inc
include msvcrt.inc

.data

format db '%s',13,10,'%s',0
str1 db 'Testing the printf function.',0
str2 db 'Source code assembled with UASM',0

.code

start PROC

    invoke  printf,ADDR format,ADDR str1,ADDR str2
    invoke  ExitProcess,0

start ENDP

END start


QuoteEven tough in most cases it will do no direct harm because the stack receives dwords in chunks of qwords as well
Exactly, this is the trick.

Quoteit defeats the whole purpose of prototyping.
Yes, prototyping is not available sometimes but quick workarounds can do the job.

Grincheux

I already used this tool for creating sqlite inc file. SQLite puts the exports between quotes and that was not recognized by the tool. Is it now possible?

Vortex

Hi Grincheux,

lib2inc does not enclose symbols between quotes, maybe you used my lib2def tool. Could you provide me a link to download the import library for SQLite?

fearless

I have some static libraries with some includes for some popular libs, like SQLite: https://github.com/mrfearless/libraries/tree/master/SQLite

Main repo is here: https://github.com/mrfearless/libraries

Some have both x86 and x64 static libs with includes, others dont - depends on the project, original source, and how awkward it was to compile, plus msvcrt in a lot of them and some projects only compiled with certain versions of visual studio, which can be a pain.