Author Topic: Creating include files for UASM \ HJWasm \ Poasm 64-bit  (Read 1074 times)

Vortex

  • Member
  • *****
  • Posts: 1757
Creating include files for UASM \ HJWasm \ Poasm 64-bit
« on: September 27, 2016, 06:26:46 AM »
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 ) :

Code: [Select]
_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 :

Code: [Select]
CreateFileA PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
64-bit :

Code: [Select]
CreateFileA PROTO :QWORD,:QWORD,:QWORD,:QWORD,:QWORD,:QWORD,:QWORD
An example :
Code: [Select]
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.
« Last Edit: February 13, 2018, 05:51:48 AM by Vortex »

LiaoMi

  • Member
  • **
  • Posts: 206
Re: Creating include files for HJWasm \ Poasm 64-bit
« Reply #1 on: February 08, 2018, 09:56:51 PM »
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

  • Member
  • *****
  • Posts: 1757
Re: Creating include files for HJWasm \ Poasm 64-bit
« Reply #2 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?

Code: [Select]
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

  • Member
  • **
  • Posts: 206
Re: Creating include files for HJWasm \ Poasm 64-bit
« Reply #3 on: February 10, 2018, 12:04:12 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?

Code: [Select]
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

  • Member
  • *****
  • Posts: 1757
Re: Creating include files for HJWasm \ Poasm 64-bit
« Reply #4 on: February 10, 2018, 09:14:07 PM »
Hello LiaoMi,

OK, I will add another option to the tool.

ragdog

  • Member
  • ****
  • Posts: 554
Re: Creating include files for HJWasm \ Poasm 64-bit
« Reply #5 on: February 10, 2018, 10:29:40 PM »
Nice Tool Vortex :t

Vortex

  • Member
  • *****
  • Posts: 1757
Re: Creating include files for UASM \ HJWasm \ Poasm 64-bit
« Reply #6 on: February 13, 2018, 06:02:27 AM »
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 :

Code: [Select]
_CreateFileA@28
28 \ 4 bytes ( DWORD ) = 7 -> 7 QWORDs

An approach or a workaround is to create VARARG prototypes :

Code: [Select]
-l64     : output prototypes for JWasm \ HJWasm \ Poasm 64-bit ( compatibility mode )
Code: [Select]
; 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 :

Quote
Samples\UASM\ScrollDemo2

aw27

  • Member
  • *****
  • Posts: 1038
  • Let's Make ASM Great Again!
Re: Creating include files for UASM \ HJWasm \ Poasm 64-bit
« Reply #7 on: February 13, 2018, 11:37:35 PM »
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

  • Member
  • *****
  • Posts: 7997
  • Assembler is fun ;-)
    • MasmBasic
Re: Creating include files for UASM \ HJWasm \ Poasm 64-bit
« Reply #8 on: February 14, 2018, 02:40:08 AM »
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.

True (WinBase.h):
Code: [Select]
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

  • Member
  • *****
  • Posts: 1038
  • Let's Make ASM Great Again!
Re: Creating include files for UASM \ HJWasm \ Poasm 64-bit
« Reply #9 on: February 14, 2018, 03:00:52 AM »
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

  • Member
  • *****
  • Posts: 1757
Re: Creating include files for UASM \ HJWasm \ Poasm 64-bit
« Reply #10 on: February 14, 2018, 05:58:54 AM »
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 :

Code: [Select]
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 :
Code: [Select]
; 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 :
Code: [Select]
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

Quote
Even 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.

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

Grincheux

  • Member
  • ***
  • Posts: 330
  • Never be pleased, Always improve
    • Asm for fun
Re: Creating include files for UASM \ HJWasm \ Poasm 64-bit
« Reply #11 on: February 15, 2018, 04:24:21 AM »
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?
Kenavo (Bye)
----------------------
Asm for Fun
My Links
"La garde meurt mais ne rend pas"
Cambronne à Waterloo

Vortex

  • Member
  • *****
  • Posts: 1757
Re: Creating include files for UASM \ HJWasm \ Poasm 64-bit
« Reply #12 on: February 15, 2018, 05:03:37 AM »
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

  • Member
  • ***
  • Posts: 300
    • LetTheLightIn
Re: Creating include files for UASM \ HJWasm \ Poasm 64-bit
« Reply #13 on: February 15, 2018, 06:40:45 AM »
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.
fearless

CM690II Case, HX1000 PSU, Asus Z97, Intel i7-4790K, Seidon 120v Cooler, 16GB DDR3, MSI GTX 980TI

www.LetTheLight.in  My Github  Twitter