Thanks gharald and debs.
Although this library is not specifically designed for external access, the stack versions sbase3, either in DLL or .o formats can be conveniently accessed from within a 32-bit C program by observing these rules;
1) a routine with no return value is a "void" type. If it returns an int, it's an "int" type and so on.
2) a routine with arguments should be called with relevant and correct arguments (types and number of arguments)
3. For win32 source, you need to tag the routines names with a leading underscore (_) as shown in the code below. Don't do that for Linux source.
Below is an example on how you can view the stack frame and the current register dumps from C functions. This way, you can have an in-depth look to what's really going on with the the registers and the stack as it goes along the execution path.
/***************************************************
Example: Calling sbase3 (stack version) from C
Win32 : gcc -m32 this.c sbase3.dll -o this.exe
Linux32: gcc -m32 this.c sbase3.o -o this
On 32-bit systems, no need for -m32 switch
***************************************************/
#include <stdio.h>
extern void _dumpreg(int);
extern void _stackview(int);
int testfunc(int, int, char);
int main()
{
int x=10;
x = testfunc(3,2,'A');
//printf("x = %d\n",x);
_dumpreg(0);
}
int testfunc(int a, int b, char c)
{
int x=-2,y=-1;
_stackview(14); //increase to view more
return b*2; //EAX will capture this.
}
Yielding this output
00000041 |0061FF18 ;argument 3 = 'A'
00000002 |0061FF14 ;argument 2 = 2
00000003 |0061FF10 ;argument 1 = 3
00401412 |0061FF0C ;EIP. Return address
0061FF38 |0061FF08 ;Caller's EBP
B7D13186 |0061FF04 ;C's thingies
741DD250 |0061FF00
FFFFFFFE |0061FEFC ;local. x = -2
FFFFFFFF |0061FEF8 ;local. y = -1
0061FEE0 |0061FEF4 ;C thingies
00401C70 |0061FEF0
00000041 |0061FEEC ;what is C doing here? This is the third argument re-appearing.
004012A0 |0061FEE8 ;C push ESI/EDI. Nobody knows why
004012A0 |0061FEE4* ;C push ESI/EDI
EAX|00000004 EBX|00297000 ECX|00000001 ;EAX = return value. X will capture it
EDX|00000000 ESI|004012A0 EDI|004012A0
EBP|0061FF38 ESP|0061FF14 EIP|0040141A
You can test calling other routines from within C. But not all routines are suitable for such purposes and I haven't thoroughly tested them all. This is for "sbase3" only. Other binaries are not suitable for such purposes (due to different calling conventions etc).