Recent

Author Topic: [SOLVED] Error while accessing a function in a dll  (Read 5449 times)

LordJonas

  • Newbie
  • Posts: 2
[SOLVED] Error while accessing a function in a dll
« on: December 17, 2010, 08:38:34 pm »
Hi...

I'm porting some code from my C/C++ to Pascal. In this particular case i'm writting a dll to detect all features in cpu. The code is mostly assembler.

The dll was build with no error's or problems. The real question is when i try to call the function from a simple console app. Every time it runs give the error: "Project teste.exe raised execption class 'External: SIGSEGV'.

here is the code of the dll:

Code: [Select]
library cpulib;

{$ASMMODE INTEL}

const
  ID_BIT = $200000;

function HasCPUID : Boolean; assembler;
asm
   pushfd                    { save original EFLAGS to stack                   }
   pop    eax                { store EFLAGS in EAX                             }
   mov    edx,eax            { save in EDX for later testing                   }
   xor    eax, ID_BIT        { flip ID bit in EFLAGS                           }
   push   eax                { save new EFLAGS value on stack                  }
   popfd                     { replace current EFLAGS value                    }
   pop    eax                { get new EFLAGS                                  }
   xor    eax, edx           { check if ID bit changed                         }
   jz     @exit              { no, CPUID not available                         }
   mov    eax, True          { yes, CPUID is available                         }
   @exit:
end;

function HasFPU : Boolean; assembler;
var
   _FCW : word;
   _FSW : word;
asm
   mov    eax, False         { Initialize return register                      }
   mov    _FSW, $5A5A        { store a non-zero value                          }
   fninit                    { must use non-wait form                          }
   fnstsw _FSW               { store the status                                }
   cmp    _FSW, 0            { was the correct status read ?                   }
   jne    @exit              { no, FPU not available                           }
   fnstcw _FCW               { yes, now save control word                      }
   mov    dx, _FCW           { get the control word                            }
   and    dx, $103F          { mask the proper status bits                     }
   cmp    dx, $3F            { is a numeric processor installed ?              }
   jne    @exit              { no, FPU not installed                           }
   mov    eax, True          { yes, FPU is installed                           }
   @exit:
end;

exports
       HasCPUID,
       HasFPU;

end.                                                

and here is the code of the console app:

Code: [Select]
program teste;

function HasCPUID: Boolean; external 'cpulib.dll';

begin
     if (HasCPUID) then
        Writeln('The CPUID instruction is present.')
     else
         Writeln('No CPUID instruction available.');
end.                                                            

Any comments about the problem would be great....

TIA
« Last Edit: January 02, 2011, 12:59:37 pm by LordJonas »

Dibo

  • Hero Member
  • *****
  • Posts: 1057
Re: Error while accessing a function in a dll
« Reply #1 on: December 17, 2010, 09:38:47 pm »
Maybe it should have stdcall (on linux cdecl). Like:
Code: Pascal  [Select][+][-]
  1. function HasCPUID : Boolean; assembler; stdcall;
  2. asm
  3. ....
..and
Code: Pascal  [Select][+][-]
  1. function HasCPUID: Boolean; external 'cpulib.dll'; stdcall;

More tips:
http://wiki.lazarus.freepascal.org/shared_library
http://wiki.freepascal.org/Lazarus/FPC_Libraries

Edit: Maybe try first with non-assembler function
« Last Edit: December 17, 2010, 09:41:47 pm by Dibo »

OpenLieroXor

  • New Member
  • *
  • Posts: 38
Re: Error while accessing a function in a dll
« Reply #2 on: December 17, 2010, 10:01:26 pm »
I'd suggest you to try running it without debugger, as the GDB tends to throw lots of SIGSEGV exceptions on some machines.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12851
  • FPC developer.
Re: Error while accessing a function in a dll
« Reply #3 on: December 18, 2010, 07:18:08 pm »
This will always crash, regardless from DLL or not.

The number of push/pops is unbalanced so you destroy the stack frame.

LordJonas

  • Newbie
  • Posts: 2
Re: Error while accessing a function in a dll
« Reply #4 on: December 19, 2010, 07:56:42 pm »
Hi.

Thanks for pointing that... indeed the code was incorrect.

It should be:

Code: [Select]
function HasCPUID : Boolean; assembler;
asm
   pushfd                    { save original EFLAGS to stack                   }
   pop    eax                { store EFLAGS in EAX                             }
   mov    edx,eax            { save in EDX for later testing                   }
   xor    eax, ID_BIT        { flip ID bit in EFLAGS                           }
   push   eax                { save new EFLAGS value on stack                  }
   popfd                     { replace current EFLAGS value                    }
   pushfd                    { get new EFLAGS                                  }
   pop    eax                { store new EFLAGS in EAX                         }
   xor    eax, edx           { check if ID bit changed                         }
   jz     @exit              { no, CPUID not available                         }
   mov    eax, True          { yes, CPUID is available                         }
   @exit:
end;                                                 

 

TinyPortal © 2005-2018