1. Eu->dll on Windows, can't get it to work

Using v4.1 (current downloadable beta 2) 32-bit, trying to compile to Euphoria code to a .dll. Using gcc (TDM-GCC), compiling and running a whole program works fine, but if I make a dll it exits immediately as soon as I try to call any function in the dll. Even just putting a single simple function with no includes in a file and turning that into a dll, it won't work. open_dll and define_c_func return valid numbers, but if I call the function the program immediately exits. Tried some other mingw-gcc versions, I either get linker errors and it won't compile anything or same result. (Again, I can compile a whole program and the result runs fine, just dll won't work.) I've made dlls before, but not since the 2.5x days I think.

Is this just broken or is there some trick to it that I've forgotten?

new topic     » topic index » view message » categorize

2. Re: Eu->dll on Windows, can't get it to work

AndySerpa said...

Is this just broken or is there some trick to it that I've forgotten?

Compiling with GCC will produce CDECL code. Add a "+" to the name of your function, like this:

constant my_func_id = define_c_func( my_dll, "+my_func", {C_POINTER,C_INT}, C_INT ) 

Read more here: define_c_func.

said...

On Windows, you can add a '+' character as a prefix to the function name. This indicates to Euphoria that the function uses the cdecl calling convention. By default, Euphoria assumes that C routines accept the stdcall convention.

What the manual doesn't tell you, and it's a really neat trick, is that it will safely ignore that leading "+" character on other platforms, so you don't need to have all sorts of silly ifdefs in your code for Windows or Linux.

-Greg

new topic     » goto parent     » topic index » view message » categorize

3. Re: Eu->dll on Windows, can't get it to work

Tried that. No effect. Even tried making a procedure that takes no arguments and just prints a number. Immediate exit when called.

new topic     » goto parent     » topic index » view message » categorize

4. Re: Eu->dll on Windows, can't get it to work

AndySerpa said...

Tried that. No effect. Even tried making a procedure that takes no arguments and just prints a number. Immediate exit when called.

What version of Euphoria are you running?

-Greg

new topic     » goto parent     » topic index » view message » categorize

5. Re: Eu->dll on Windows, can't get it to work

4.1 beta 2 32-bit Just downloaded again to make sure that's what I had and all the parts matched up. The translator options in the default eu.cfg file were not in my file the first go around (-plat WINDOWS -arch ix86) but they are now. Again no difference. I was/am using "-gcc -con" on the euc command-line.

On Windows 10 Pro 64-bit

If I use "Dependency Walker" on the resulting dll (which I use to find the exported function names in the dll), it tells me of some errors and/or missing things it should be linking to, but it gives the same errors on a compiled whole program Eu->C .exe and those seem to run fine.

BTW, is Watcom still supposed to work? I tried installing that, but it wouldn't compile anything.

new topic     » goto parent     » topic index » view message » categorize

6. Re: Eu->dll on Windows, can't get it to work

AndySerpa said...

4.1 beta 2 32-bit Just downloaded again to make sure that's what I had and all the parts matched up. The translator options in the default eu.cfg file were not in my file the first go around (-plat WINDOWS -arch ix86) but they are now. Again no difference. I was/am using "-gcc -con" on the euc command-line.

On Windows 10 Pro 64-bit

Does it work with the Beta 1 download? Maybe try building Euphoria from source?

AndySerpa said...

If I use "Dependency Walker" on the resulting dll (which I use to find the exported function names in the dll), it tells me of some errors and/or missing things it should be linking to, but it gives the same errors on a compiled whole program Eu->C .exe and those seem to run fine.

I don't think those errors indicate a problem. I think that's more a problem with Dependency Walker than the library.

AndySerpa said...

BTW, is Watcom still supposed to work? I tried installing that, but it wouldn't compile anything.

Nope. We voted that out years ago.

-Greg

new topic     » goto parent     » topic index » view message » categorize

7. Re: Eu->dll on Windows, can't get it to work

Ok, I got a simple function to work as long as it only takes and returns E_INTEGER args instead of E_OBJECT. Problem with Euphorian data?

new topic     » goto parent     » topic index » view message » categorize

8. Re: Eu->dll on Windows, can't get it to work

I am also using 4.1 Beta 2. I did some debugging and it looks like it has to do with heap allocation. See the segfault at the end of the debugging output below.

I would try going back to Beta 1 or build from source and see if it's still a problem. I couldn't find any open tickets about this so we may have found a bug.

-- dll_test.e 
 
public function add_two( atom value ) 
    return (value + 2) 
end function 
  
public procedure print_hex( atom value ) 
    printf( 1, "#%08x\n", value ) 
end procedure 
-- test_dll.ex 
include std/dll.e 
include std/machine.e 
 
atom dll_test = open_dll( "dll_test.dll" ) 
printf( 2, "dll_test = #%08x\n", dll_test ) 
 
constant add_two = define_c_func( dll_test, "+add_two", {E_ATOM}, E_ATOM ) 
printf( 2, "add_two = %d\n", add_two ) 
 
constant print_hex = define_c_proc( dll_test, "+print_hex", {E_ATOM} ) 
printf( 2, "print_hex = %d\n", print_hex ) 
 
atom value = rand( 100 ) 
printf( 2, "value = %d\n", value ) 
 
value = c_func( add_two, {value} ) 
printf( 2, "add_two( value ) = %d\n", value ) 
 
puts( 2, "print_hex( value ) = " ) 
c_proc( print_hex, {value} ) 

>euc -dll dll_test.e 
Build directory: build-715921\ 
Translating code, pass: 1 2 3  generating 
Compiling with GCC 
Compiling  13% init-.c 
Compiling  50% dll_test.c 
Compiling  75% main-.c 
Linking 100% ..\dll_test.dll 

>gdb32 eui 
GNU gdb (GDB) 7.9.1 
Copyright (C) 2015 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law.  Type "show copying" 
and "show warranty" for details. 
This GDB was configured as "mingw32". 
Type "show configuration" for configuration details. 
For bug reporting instructions, please see: 
<http://www.gnu.org/software/gdb/bugs/>. 
Find the GDB manual and other documentation resources online at: 
<http://www.gnu.org/software/gdb/documentation/>. 
For help, type "help". 
Type "apropos word" to search for commands related to "word"... 
Reading symbols from eui...done. 
(gdb) run test_dll.ex 
Starting program: C:\Euphoria\bin\eui.exe test_dll.ex 
[New Thread 15932.0x1bc4] 
[New Thread 15932.0x2ac0] 
[New Thread 15932.0x2fd4] 
[New Thread 15932.0x114c] 
dll_test = #71180000 
value = 87 
 
Program received signal SIGSEGV, Segmentation fault. 
0x00000001 in ?? () 
(gdb) bt 
#0  0x00000001 in ?? () 
#1  0x0000001c in ?? () 
#2  0x8014cc8c in ?? () 
#3  0x76ecdecb in ntdll!RtlAllocateHeap () from C:\Windows\SYSTEM32\ntdll.dll 
Backtrace stopped: previous frame inner to this frame (corrupt stack?) 

-Greg

new topic     » goto parent     » topic index » view message » categorize

9. Re: Eu->dll on Windows, can't get it to work

And btw, I had to remove the '+' to get the integer function to work. Also it exits if the function tries to print anything to the screen, it seems.

new topic     » goto parent     » topic index » view message » categorize

10. Re: Eu->dll on Windows, can't get it to work

Just a few months ago, I had reason to investigate the E_XXX constants, and ended up writing this (draft):

Phix docs said...

DOC: E_INTEGER/E_ATOM/E_SEQUENCE/E_OBJECT are <i><b>not</b></b> supported. They don&rsquo;t seem to work as advertised on OpenEuphoria either:
http://openeuphoria.org/forum/127457.wc http://openeuphoria.org/forum/124939.wc http://openeuphoria.org/forum/123657.wc http://openeuphoria.org/forum/119179.wc

Neither does http://openeuphoria.org/forum/113522.wc inspire me with confidence, and nor does http://openeuphoria.org/forum/118514.wc though it is the closest (/only) thing my searches yielded that might actually be useful.

One thing that definitely affects Phix, but I can't say about OE, is that if a dll were compiled with it's own copy of the heap manager, then if any function in the dll allocated something and returned it to the calling exe, the latter would most probably try and release it in the wrong heap, which would end pretty badly. I also noted that delete_routine and friends would be utterly hosed, at least for Phix.

The only way I could see round the problem (in Phix) would be that Phix-compiled .dll/so files [when implemented!] should be restricted to C_INT(etc)/C_FLOAT/C_DOUBLE/C_POINTER and in the latter case, should my_dll return a pointer to some memory it had allocated, the calling code would need to my_dll_free(ptr) it after it had retrieved a copy of the returned data.

Likewise the parameters to any dll routine would need restricting to atoms, exactly like call_back.

Pete

new topic     » goto parent     » topic index » view message » categorize

11. Re: Eu->dll on Windows, can't get it to work

Phix docs said...

They don&rsquo;t seem to work as advertised on OpenEuphoria either:

I think they do work as advertised.

This might be a bug related to E_SEQUENCE. Maybe, if we don't pass in any euphorian objects but stick to passing C_INT and so on back and forth, the issue of heap corruption won't happen? I haven't tried this. It might be worse though - we might have a generic issue with using multiple translated shared objects, period.

In any case, I don't think we advertise anywhere to use multiple translated shared objects.... though it would be nice if that worked.

I'm not sure if this was fixed or not. But it's unrelated to E_OBJECT et al - the issue here has to do with mangling of function names, and a tangent of having the same function name as a C backend function.

This is a generic memory leak issue with translated shared objects, that applies regardless of whether or not E_OBJECT et al are used. It's a long standing bug that should be fixed, but it's not related.

This references the same memory leak issue above.

Phix docs said...

Neither does http://openeuphoria.org/forum/113522.wc inspire me with confidence,

I don't see why. We don't advertise backwards compatibility with v3.1 or older E_OBJECT et al. In that thread, a library was provided for those who do need such compatibility - so as far as I'm concerned, the issue was fixed.

Phix docs said...

and nor does http://openeuphoria.org/forum/118514.wc though it is the closest (/only) thing my searches yielded that might actually be useful.

You don't see the 4.0<->3.1 shim library as useful?

In any case, we don't advertise that ram_space (from std/eumem.e) will work across translated shared objects or between a tso and the main OE program.

petelomax said...

One thing that definitely affects Phix, but I can't say about OE, is that if a dll were compiled with it's own copy of the heap manager, then if any function in the dll allocated something and returned it to the calling exe, the latter would most probably try and release it in the wrong heap, which would end pretty badly.

Well, maybe http://openeuphoria.org/forum/127457.wc is an example of this problem for OE. OTOH, something allocated with allocate() should work fine in OE if using DEP protection - we use VirtualAlloc/mmap in that case.

new topic     » goto parent     » topic index » view message » categorize

12. Re: Eu->dll on Windows, can't get it to work

Just to repeat, although I did get a simple function that passes an integer back and forth to work, it will also crash without passing (or returning) any parameters at all depending on what it did, or that same function that was working will fail just by adding a print statement.

e.g. the function

function simpletest(integer x) 
return x+1 
end function 

works. But this crashes:

function simpletest(integer x) 
? x 
return x+1 
end function 

So the problem seems larger than Euphoria data types.

new topic     » goto parent     » topic index » view message » categorize

13. Re: Eu->dll on Windows, can't get it to work

AndySerpa said...

Just to repeat, although I did get a simple function that passes an integer back and forth to work, it will also crash without passing (or returning) any parameters at all depending on what it did, or that same function that was working will fail just by adding a print statement.

e.g. the function

function simpletest(integer x) 
return x+1 
end function 

works. But this crashes:

function simpletest(integer x) 
? x 
return x+1 
end function 

So the problem seems larger than Euphoria data types.

the return value might be an E_TYPE as well? do you get any better results with a procedure instead of a function. I seem to recall reading somewhere that all calls were functions in the actual calling code but maybe discards the results.

I can't get a dll built in 4.x or 4.1x that doesn't mangle the function names with gcc all of them giving me the silent exit treatment.

forgot to add: we really need to start tagging eu*.a with the compiler version. -v reports the repo number on the executable so maybe the string could be embedded in the libs and executable along with the compiler version? even if you have to hex view it.

gcc -v >> builddir/ver.cache could go with the distribution too

I'm not convinced there aren't linking problems with some versions of gcc beyond the obvious incompatible version linking errors you get sometimes. though it does seem to work most times with translated code ok wrapping dlls seems to be another story.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu