Author Topic: Link to a static lib in .a format  (Read 302 times)

jj2007

  • Member
  • *****
  • Posts: 7634
  • Assembler is fun ;-)
    • MasmBasic
Link to a static lib in .a format
« on: August 08, 2017, 08:05:36 AM »
Whatever I tried, it's always "unresolved symbol" for the linker. Any idea if it's possible at all?
jj

Code: [Select]
include \masm32\include\masm32rt.inc
includelib C:\TDM-GCC-32\lib\gcc\mingw32\4.9.2\libgcc.a

.code
start:
  exit
;   mulvsi3 PROTO :REAL8, :REAL8
  ExternDef mulvsi3:DWORD
  ; mulvsi3 PROTO :DWORD, :DWORD
  call mulvsi3

end start

TWell

  • Member
  • ****
  • Posts: 748
Re: Link to a static lib in .a format
« Reply #1 on: August 08, 2017, 08:24:24 AM »
Could be possible with polink to link function __mulvsi3 ;)

nidud

  • Member
  • *****
  • Posts: 1386
    • https://github.com/nidud/asmc
Re: Link to a static lib in .a format
« Reply #2 on: August 08, 2017, 08:27:29 AM »
It's C functions and the public name is ___mulvsi3 so this builds:
Code: [Select]
include \masm32\include\masm32rt.inc
includelib C:\cygwin\lib\gcc\i686-pc-cygwin\5.4.0\libgcc.a

__mulvsi3 proto c :REAL8, :REAL8
.data
a real8 1.0
b real8 2.0
.code
start:
  invoke __mulvsi3,a,b
  exit

end start

jj2007

  • Member
  • *****
  • Posts: 7634
  • Assembler is fun ;-)
    • MasmBasic
Re: Link to a static lib in .a format
« Reply #3 on: August 08, 2017, 10:19:09 PM »
Yep, that works, thanks to both of you :t

Btw does somebody know how the linker knows the arg list? With a wrong PROTO, the linker throws an error (and rightly so); but I can't see (using PeView) that information "attached" e.g. to the _acoshq in the offset table of the C:\TDM-GCC-32\lib\gcc\mingw32\4.9.2\libquadmath.a ::)

TWell

  • Member
  • ****
  • Posts: 748
Re: Link to a static lib in .a format
« Reply #4 on: August 09, 2017, 01:07:22 AM »
linker see only symbols, no args?
in i386:
 _acoshq is a __cdecl symbol. Type = DT_FUNCTION, Storage Class = IMAGE_SYM_CLASS_EXTERNAL
__stdcall function have declaration _function@x, where x is parameters size.

jj2007

  • Member
  • *****
  • Posts: 7634
  • Assembler is fun ;-)
    • MasmBasic
Re: Link to a static lib in .a format
« Reply #5 on: August 09, 2017, 01:20:37 AM »
OK, I see:

  __mulvsi3 proto c :REAL4      ; wrong but works because it's C
  call __mulvsi3

Thanks :icon14:

aw27

  • Member
  • ****
  • Posts: 785
Re: Link to a static lib in .a format
« Reply #6 on: August 09, 2017, 02:37:31 AM »
; wrong but works because it's C

It will not happen with C++, though.  :greensml:

TWell

  • Member
  • ****
  • Posts: 748
Re: Link to a static lib in .a format
« Reply #7 on: August 09, 2017, 03:40:21 AM »
If someone don't want to use libgcc.a, llvm offers some functions in different licence.
See llvm builtins

Vortex

  • Member
  • *****
  • Posts: 1711
Re: Link to a static lib in .a format
« Reply #8 on: August 09, 2017, 04:40:14 AM »
Extracting and disassembling the module  __mulvsi3 :

Code: [Select]
C:\TDM-GCC-32\lib\gcc\mingw32\5.1.0>md temp

C:\TDM-GCC-32\lib\gcc\mingw32\5.1.0>cd temp

C:\TDM-GCC-32\lib\gcc\mingw32\5.1.0\temp>\PellesC\bin\polib.exe /EXPLODE ..\libgcc.a
POLIB: fatal error: The filename, directory name, or volume label syntax is incorrect.

C:\TDM-GCC-32\lib\gcc\mingw32\5.1.0\temp>H:\masm32\bin\objconv.exe -fmasm _mulvsi3.o Disasm-mulvsi3.asm

Input file: _mulvsi3.o, output file: Disasm-mulvsi3.asm
Converting from COFF32 to Disassembly32

  7 Debug sections removed
  0 Exception sections removed

Code: [Select]
.386
option dotname
.model flat

public ___mulvsi3

extern _abort: near

_text   SEGMENT PARA PUBLIC 'CODE'

_text   LABEL NEAR

___mulvsi3 LABEL NEAR
        push    ebx
        sub     esp, 8
        mov     eax, dword ptr [esp+14H]
        imul    dword ptr [esp+10H]
        mov     ecx, eax
        sar     ecx, 31
        cmp     ecx, edx
        jnz     ?_001
        add     esp, 8
        pop     ebx
        ret

?_001   LABEL NEAR
        call    _abort
        nop

_text   ENDS

_data   SEGMENT DWORD PUBLIC 'DATA'

_data   ENDS

.bss    SEGMENT DWORD PUBLIC 'BSS'

.bss    ENDS

.text.unlikely SEGMENT DWORD PUBLIC 'CODE'

.text.unlikely ENDS

.rdata$zzz SEGMENT DWORD PUBLIC 'CONST'

        db 47H, 43H, 43H, 3AH, 20H, 28H, 74H, 64H
        db 6DH, 2DH, 31H, 29H, 20H, 35H, 2EH, 31H
        db 2EH, 30H, 00H, 00H

.rdata$zzz ENDS

END

Simplifying the code :

Code: [Select]
.386
.model flat,stdcall

PUBLIC mulvsi3

.data

mulvsi3 dd ?

.code

__mulvsi3 PROC C x:DWORD,y:DWORD

    mov     eax,y
    imul    x
    mov     ecx,eax
    sar     ecx,31
    cmp     ecx, edx
    jnz     _exit
    ret
   
_exit:

    mov     mulvsi3,1  ; code to simulate
    ret                ; the abort case

__mulvsi3 ENDP

END

Testing the function :

Code: [Select]
include \masm32\include\masm32rt.inc

__mulvsi3 PROTO C :DWORD,:DWORD

EXTERN mulvsi3:DWORD

.data

msg         db 'Result = %d',0

.code

start:

    invoke  __mulvsi3,36,24

    invoke  crt_printf,ADDR msg,eax

    invoke  ExitProcess,0

END start