Mono interop: loading 32 bit shared library does not work on my 64 bit system

I have a problem with a simple interoperability example on my system. I've built a simple 32 bit shared library called libtest.so (C ++)

g++ -c -fpic test.cpp -m32
g++ -shared -fpic -o libtest.so test.o -m32

      

My system: Ubuntu Linux 10.04 x86_64

Mono C # compiler version 2.4.4.0

Also, I have a sample C # program using my shared library:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

public class Test
{
        [DllImport("libdl.so")] 
        static extern IntPtr dlopen(string filename, int flags); 

        [DllImport("libdl.so")] 
        static extern IntPtr dlclose(IntPtr handle);

        [DllImport ("./libtest.so")]
        private static extern void HelloWorld();

        [DllImport ("./libtest.so",EntryPoint="Test")]
        private static extern int Testl(int a,int b); 

        public static int Main(string[] args)
        {   
                IntPtr handle = dlopen("./libtest.so",2);
                if(handle == IntPtr.Zero)
                {   
                       Console.WriteLine("Error loading shared library");
                        return -1; 
                }   

                HelloWorld();
                int ret = Testl(116,1);
                Console.WriteLine("Result from shared-Librarry Call: " + ret);

                dlclose(handle);
                return 0;
        }   
}

      

Problem: Loading the library doesn't work.

exporting MONO_LOG_LEVEL = debug gives me the following advice: Mono-INFO: error loading library DllImport './libtest.so: invalid ELF class: ELFCLASS32'.

Well, I guess mono is running my program in 64-bit mode and so it can't call the 32-bit shared library? If I build the shared library in 64 bit mode (no -m32) everything works fine.

My monomial compiler 2.4.4. has no way to specify the platform with / platform: x86, and so I installed version 2.10, but using it doesn't work either.

/opt/mono-2.10/bin/gmcs /platform:x86 sharpCall.cs

      

Is it possible to load 32-bit shared libraries on a 64-bit system?

+3


source to share


3 answers


The problem is that your system has 64-bit Mono installed, which can only P / Invoke to 64-bit native libraries, it cannot P / Invoke to 32-bit native libraries.

The -platform: x86 flag is for the C # compiler, not the runtime, and does not indicate the runtime to use 32-bit memory space.



You need to install Mono 32 bit on your Ubuntu system if you want to P / Invoke to 32 bit native libraries.

+3


source


You cannot load a 32 bit module into a 64 bit process. Either start a 32 bit process or compile your own module as a 64 bit module.



0


source


Is there a way to load 32-bit shared libraries on a 64-bit system?

Yes, but only if you compile a program that uses the specified shared libraries into a 32-bit process.

Well, I guess mono runs my program in 64-bit mode and so it cannot call the 32-bit shared library? If I build the shared library in 64-bit mode (no -m32) everything works fine !!

Of course it happens. Just compile the program with the m32 flag and you shouldn't have any problems.

0


source







All Articles