Page 1 of 1

Problem accessing libio functions from library

Posted: Sat May 21, 2016 3:36 pm
by Utsav_Chokshi
Hello All,

I am facing problem accessing Libio library's function from test library (sample library code I have written).

I guess I am missing some instruction or doing something wrong.

Please have look at following minimalistic (yet complete ) code.

My purpose is to show that I am able to call file.size from program itself successfully but not from library.
Program does not give error, it gets stuck there with general protection fault.

-----------------------------------------------------------
Library Code :
-----------------------------------------------------------
format MS COFF
public @EXPORT as 'EXPORTS'

__DEBUG__ = 1
__DEBUG_LEVEL__ = 1

include 'struct.inc'
include 'proc32.inc'
include 'macros.inc'
include 'dll.inc'
include 'libio.inc'
include 'debug-fdo.inc'
purge section,mov,add,sub

section '.flat' code readable align 16

proc lib_init
mov [mem.alloc], eax
mov [mem.free], ebx
mov [mem.realloc], ecx
mov [dll.load], edx
xor eax, eax
DEBUGF 2, "INFO : Library Initialized Successfully.\n"
ret
endp

proc mylib.test _file

DEBUGF 2, "INFO : Mylib.Test\n"
invoke file.size, [_file]
DEBUGF 2, "INFO : File Size is %d\n",ebx
cmp ebx, -1
jnz .error
jmp .quit
.error:
DEBUGF 3, "ERROR : file.size\n"
.quit:
ret
endp


align 16
@EXPORT:

export \
lib_init , 'lib_init' , \
mylib.test , 'test'

align 16
@IMPORT:
library \
libio, 'libio.obj'
import libio , \
libio.init, 'lib_init' , \
file.size , 'file_size'

include_debug_strings

section '.data' data readable writable align 16
mem.alloc dd ?
mem.free dd ?
mem.realloc dd ?
dll.load dd ?

----------------------------------------------------------------------------------
Test File Code :
----------------------------------------------------------------------------------
format binary as ""
use32
org 0x0
db 'MENUET01' ; signature
dd 0x01 ; header version
dd START ; entry point
dd I_END ; initialized size
dd E_END ; required memory
dd E_END ; stack pointer
dd 0 ; parameters
dd 0 ; path

__DEBUG__ = 1
__DEBUG_LEVEL__ = 1

include 'struct.inc'
include 'proc32.inc'
include 'macros.inc'
include 'dll.inc'
include 'libio.inc'
include 'debug-fdo.inc'

;code
START:
;init heap
mcall 68, 11
test eax, eax
jz .exit

;load libraries
stdcall dll.Load, @IMPORT
test eax, eax
jnz .error

;Test a procedure

DEBUGF 2, "INFO : Calling File.Size\n"
invoke file.size, torrent_filename1
DEBUGF 2, "File size is %d\n", ebx

DEBUGF 2, "INFO : Calling Mylib.Test\n"
invoke mylib.test, torrent_filename1
DEBUGF 2, "INFO : Successfully returned from Mylib.Test\n"

.error:
DEBUGF 3, "ERROR : Problem loading libraries."

.exit:
DEBUGF 2, "INFO : Program exited successfully."

;import
align 16
@IMPORT:

library \
libio, 'libio.obj',\
mylib, 'mylib.obj'

import libio,\
libio.init, 'lib_init' , \
file.size, 'file_size'

import mylib,\
lib_init, 'lib_init', \
mylib.test, 'test'

include_debug_strings

;data
torrent_filename1 db '/usbhd0/1/test.torrent',0

I_END:
rb 0x1000 ; stack
E_END:

---------------------------------------------------------
Steps perfromed to execute:
---------------------------------------------------------
fasm mylib.asm mylib.obj
fasm mylib_test.asm mylib_test
mcopy -moi kolibri.img mylib.obj ::LIB

Started qemu..

-------------------------------------------------------
Output on debug board :
-------------------------------------------------------
KolibriOS_DebugBoard_2.png
KolibriOS_DebugBoard_2.png (220.6 KiB)
Viewed 4913 times
Thanks for help.

Re: Problem accessing libio functions from library

Posted: Sat May 21, 2016 10:24 pm
by Pathoswithin
You are not importing libio in the mylib, lib_init gives you dll.load pointer — use it for that (you don't need to save it, nor include dll.inc it the library).

Re: Problem accessing libio functions from library

Posted: Sun May 22, 2016 12:45 am
by Utsav_Chokshi
I have imported libio in mylib actually.

By the way, problem got solved.

I replaced lib_init code with following :

Code: Select all

proc lib_init
         	 mov     [mem.alloc], eax
          	mov     [mem.free], ebx
         	 mov     [mem.realloc], ecx
         	 mov     [dll.load], edx
            	
          	invoke	dll.load, @IMPORT
      	  	or	eax, eax
      	  	jz	.libok

      	  	DEBUGF 3, "ERROR : Problem Initializing libraries.\n"
      	 	 xor	eax, eax
      	  	inc	eax
      	  	ret

  .libok:	DEBUGF 2, "INFO : Library Initialized Successfully.\n"
       		xor	eax,eax
		ret
endp
As per discussion with Ivan, We have concluded following :
" Previously, I had two file.size variables. One in library and another in application.
Without dll.load at lib_init, a call to file.size was refering to some garbage address as file.size was unitialized for library.
Now including dll.load code at lib_init, intialized file.size to correct address."

@Pathoswithin, Yes I do not need to include dll.inc as lib_init is giving me pointer to dll.load anyway. :)

Thanks.