Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DllImport code does not work when compile from Linux #3311

Closed
harry-cpp opened this issue Jun 30, 2019 · 5 comments
Closed

DllImport code does not work when compile from Linux #3311

harry-cpp opened this issue Jun 30, 2019 · 5 comments
Assignees
Labels
need-info Issues that need more information from the author.
Milestone

Comments

@harry-cpp
Copy link
Contributor

Steps to Reproduce

  1. Add the code bellow anywhere
  2. Call it
[DllImport("dl")]
public static extern IntPtr dlopen(string path, int flags);

Expected Behavior

No crashing.

Actual Behavior

DllNotFoundException gets called.

Version Information

Last stable Linux build.

Log File

Full logcat: https://gist.github.com/cra0zy/8af965b4f44ec0b954b30f382ca0fdb8

@dellis1972 dellis1972 added this to the d16-3 milestone Jul 4, 2019
@grendello grendello added the need-info Issues that need more information from the author. label Jul 10, 2019
@grendello
Copy link
Contributor

The problem here is this:

06-29 14:21:21.020 3182 3182 E AndroidRuntime: android.runtime.JavaProxyThrowable: System.TypeInitializationException: The type initializer for 'MonoGame.OpenGL.GL' threw an exception. ---> Java.Lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "__cxa_finalize" referenced by "/system/lib/libdl.so"...

The __cxa_finalize symbol is a weak symbol reference in any native C++ app/library, used to run C++ destructors registered with atexit and is either resolved to the application native code/dso or to libc (which has the default implementation) which doesn't appear to be the case here. Why? I have no idea - I would need to be able to examine the app code (or a small repro that triggers the issue) in order to start figuring it out.

Here are the symbols in some DSOs involved:

Android/lib/arm64-v8a $ nm -C -D libmono-android.debug.so|grep __cxa_finalize
                 U __cxa_finalize

Android/lib/arm64-v8a $ nm -C -D libmonosgen-2.0.so|grep __cxa_finalize
                 U __cxa_finalize

ndk/platforms/android-21/arch-arm64/usr/lib$ nm -C -D libc.so|grep __cxa_finalize
000000000000c01c T __cxa_finalize

android-21/arch-arm64/usr/lib$ nm -C -D libdl.so|grep __cxa_finalize

Now, considering that libdl.so does not reference libc (in fact, it's dynamically linked but references no DSOs) and does not reference __cxa_finalize itself, the problem isn't in the library (at least for NDK platform 21 reference libraries I'm looking at here):

ndk/platforms/android-21/arch-arm64/usr/lib$ readelf -a libdl.so | grep NEEDED

further on, assuming your app is a Xamarin.Android one and includes Mono and XA runtimes, libdl is already referenced and loaded by the time your code runs (and so the __cxa_finalize symbol should be resolved to the one in libc.so):

Android/lib/arm64-v8a $ readelf -a libmonosgen-2.0.so | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libz.so]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so]
 0x0000000000000001 (NEEDED)             Shared library: [liblog.so]

Android/lib/arm64-v8a $ readelf -a libmono-android.debug.so | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libz.so]
 0x0000000000000001 (NEEDED)             Shared library: [liblog.so]
 0x0000000000000001 (NEEDED)             Shared library: [libxamarin-app.so]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so]

At this point @cra0zy, I have no slightest clue as to what might be going on. Your device's libdl.so appears to have __cxa_finalize reference which appears to be unresolved, while the reference libraries from NDK I looked at (API levels 16 and 21, which we compile against) include libdl.so without __cxa_finalize reference. Therefore, I'll need more info from you (and, preferably, a repro):

  • Android version
  • Device architecture
  • Platform your app native code is compiled against
  • NDK version

Hopefully we can then pin-point the issue (which doesn't seem to be a problem in Xamarin.Android atm, frankly)

@harry-cpp
Copy link
Contributor Author

So... a quick test later and the only way that this occurs is this specific device and only when compiling the above code from Linux... strange.

Device in question is: https://www.gsmarena.com/alcatel_pixi_4_(7)-7847.php

I can't provide you with more details right now.

@grendello
Copy link
Contributor

@cra0zy __cxa_finalize is a Linux implementation detail possibly? But, since Android is Linux as well... Odd, indeed. Regarding the device - would you be able to pull libc.so and libdl.so off of the device and attach here?

@harry-cpp
Copy link
Contributor Author

libs.zip

@harry-cpp
Copy link
Contributor Author

@grendello @dellis1972 So... turns out this issue is not limited to Linux SDK :/

@ghost ghost locked as resolved and limited conversation to collaborators Jun 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
need-info Issues that need more information from the author.
Projects
None yet
Development

No branches or pull requests

3 participants