News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Linking a MS VC++ 6.0 built library to MASM32 project

Started by mkey, January 04, 2012, 01:20:59 PM

Previous topic - Next topic

jj2007

Quote from: mkey on January 05, 2012, 07:05:26 PM
jj2007, there is no library in MASM that corresponds to that name. I'll look into creating one.

Maybe I wasn't clear on that point, but the errors above I get when building the MASM project, not while building the library.
It is the linker complaining, not the assembler. But I see you found the right approach: Binary search on all files in the C folders ;-)

Quote
While doing some text searches (lol noobish but effective) I found a few libs that seem to be using/defining __ftoll function, namely

crt.lib
crtmt.lib
pocrt.lib
glaux.lib

I shall dig more.

mkey

dedndave, it is, the MASM32\bin folder, which is on the path :/

Anyway, don't allow me to derail the topic at this point, we cornered this mofo.

jj2007, I know that the assembler is fine with my code, it doesn't check functions in libs :P

Anyway x2, interesting development. In the Pelles bin folder there is this certain pocrt.dll library, dumpbinned it and this is what I got

Dump of file pocrt.dll

File Type: DLL

  Section contains the following exports for pocrt.dll

           0 characteristics
    4DA9C944 time date stamp Sat Apr 16 18:52:20 2011
        0.00 version
           1 ordinal base
         874 number of functions
         874 number of names

    ordinal hint RVA      name

...
          4    3 00031A20 ___ftoll
...
         17   10 00031EF0 ___llmul
...
         66   41 0001CFE0 __log
...

  Summary

        3000 .data
        9000 .rdata
        2000 .reloc
        1000 .rsrc
       3A000 .text
        1000 .tls


So, Pelles uses some functions of their own and causes pain to people like me. Only chkstk is missing.

EDIT:

Added

includelib "C:\Program Files (x86)\PellesC\Lib\pocrt.lib"

and the damned think linked lol Let's now see if the library even fits my needs :D If things check out OK, I'll summarize the results and attach a working example in this thread.

Note 1: my exe gained 130kb, now it's a fat bastard.
Note 2: does this mean now I have to distribute pocrt.dll with my app? Sure as hell it does :D

mkey

But, you specify only the folder in the path variable, no? And all the mentioned files reside in MASM32\bin dir.

jj2007

Another option is to include the missing functions in the assembler code via ExternDef and see if they get called, and if yes, if you can simulate them.

mkey

This?

includelib "C:\Program Files (x86)\PellesC\Lib\pocrt.lib"

EXTERNDEF __log:far
EXTERNDEF __ftoll:far
EXTERNDEF __chkstk:far
EXTERNDEF __llmul:far


When you say "simulate" do you mean "debug/disassemble the blasted dll and rip the opcodes"? Because that doesn't sound very nice :P

dedndave

no need to specify a type with EXTERNDEF, as they may be dwords, bytes, or code, or who knows what
they are likely NEAR code
        EXTERNDEF __log
        EXTERNDEF __ftoll
        EXTERNDEF __chkstk
        EXTERNDEF __llmul

mkey

Well, if I don't specify the type I get this

error A2008: syntax error : in directive

If I use only externdef directive, without the aforementioned lib, the program doesn't build. If I do specify the lib, then externdef directive is not even needed, the program builds nicely. If I'm supposed to write those functions on  my own and have them included in my source, it's no problem to do that. But it's very odd to me the compiler would make the library dependent on one of its own dll files. Here I'm talking about the Pelleas compiler and the cyassl.lib I was building.

And still I don't get where is the __chkstk function? It's not in the pocrt.dll but pocrt.lib does define it. I checked my program imports but there is no mention of that function.

EDIT:

Dear lord, it's getting late and I should just stop. I just went through project properties in the Pelles IDE, switching things on and off, trying to trigger something. And it did trigger something, instead of the pocrt.lib and mscrvt.lib now the library asks for crt.lib (or crmt.lib if the library was compiled as multithreaded) when linking in MASM and there is no more dependency for pocrt.dll :/

Even after returning all the project properties at default values, the library compiles and links in ASM properly :D Now I'm trying to break it again lol maybe Pelles can do something with this info.

jj2007

Quote from: mkey on January 05, 2012, 08:48:38 PM
When you say "simulate" do you mean "debug/disassemble the blasted dll and rip the opcodes"? Because that doesn't sound very nice :P

I agree, copying would not be nice. What I mean is

- insert a chkstk proc in your assembler code
- put a MsgBox there
- see if it ever gets called (most probably not)
- if no, just leave a stub there
- if yes, check if you can get the same functionality from elsewhere, e.g. a CRT function. Write a wrapper, in other words.

One option is obviously to roll your own chkstk.

Anyway, congrats that you got it working :U

Vortex

Hi mkey,

Here is how to obtain _chkstk :

First, be sure that you have the following static library : \PellesC\Lib\crt.lib

Now, you need to extract all the member modules from this library :

\PellesC\Bin\polib.exe /explode crt.lib

I have the latest Pelles C development suit and the command above produced 984 MS COFF object modules. One of them is _chkstk.obj

You can link statically this object file with your main project modules. Analyzing this object file :

objconv.exe -fmasm _chkstk.obj _chkstk.asm

The output is :

; Disassembly of file: _chkstk.obj
; Fri Jan 06 20:32:31 2012
; Mode: 32 bits
; Syntax: MASM/ML
; Instruction set: 80386

.386
.model flat

public ___chkstk


_text   SEGMENT PARA PUBLIC 'CODE'                      ; section number 1

___chkstk PROC NEAR
        push    ecx                                     ; 0000 _ 51
        cmp     eax, 4096                               ; 0001 _ 3D, 00001000
        lea     ecx, [esp+8H]                           ; 0006 _ 8D. 4C 24, 08
        jc      ?_002                                   ; 000A _ 72, 14
?_001:  sub     ecx, 4096                               ; 000C _ 81. E9, 00001000
        sub     eax, 4096                               ; 0012 _ 2D, 00001000
        test    dword ptr [ecx], eax                    ; 0017 _ 85. 01
        cmp     eax, 4096                               ; 0019 _ 3D, 00001000
        jnc     ?_001                                   ; 001E _ 73, EC
?_002:  sub     ecx, eax                                ; 0020 _ 29. C1
        mov     eax, esp                                ; 0022 _ 89. E0
        test    dword ptr [ecx], eax                    ; 0024 _ 85. 01
        mov     esp, ecx                                ; 0026 _ 89. CC
        mov     ecx, dword ptr [eax]                    ; 0028 _ 8B. 08
        mov     eax, dword ptr [eax+4H]                 ; 002A _ 8B. 40, 04
        push    eax                                     ; 002D _ 50
        ret                                             ; 002E _ C3
___chkstk ENDP

        nop                                             ; 002F _ 90

_text   ENDS

END


As you can see, this function does not have any dependency.

You can get objconv from :

http://www.agner.org/optimize/#objconv

mkey

Thanks for details, but as you may see there was something off with that build as it linked to Pelles system dlls.

I ran into even more problems with the static library, internal functions failed at odd stages so finally I decided to count my losses and go with a dynamic library. Size is a bit larger now, but it took like half an hour to set up. I'll post here some results of my work, the dll, lib, inc file and a small pop3 mail client which uses ssl (more or less) successfully. That library works nicely, the only problem I had was with certificates as I can't validate any servers I'm connecting to. Still working on that one.

mkey

OK, finally got this example ready.

Note that this is very basic stuff, uses blocking sockets and CyaSSL dll to connect to POP3 server over SSL, checks the number of new messages and displays the latest message as raw text. Very, very basic.

- the CyaSSL dll has been compiled from latest CyaSSL package with multithreading off, logging on (uncomment in source to turn logging on) and only client support by means of VC++ 6.0
- the prepared inc file contains only some of the exported functions, the ones I actually used. Consult the CyaSSL source and documentation for more
- certificate checking is off by default, only gmail CA file has been included and it should be working normally. If you want more CA files, add them in cert folder and include in source, check the comments. I haven't mastered the skill to find proper CA files, yet. They can be exported from the browser, but which ones exactly is unclear to me at this point in time
- since this program does not support anything but plain text passwords, it does not work with Hotmail. It does work normally with Gmail, haven't tried any of the other mail services
- the source comes as is, no guarantees, you're free to do with it whatever you like

I'm always open for any questions and suggestions.

Some keywords:
SSL CyaSSL MASM ASM ASSEMBLY LIBRARY