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

x/mobile: ndk-build finds wrong library path for dlopen #17807

Open
ab-smith opened this Issue Nov 5, 2016 · 2 comments

Comments

Projects
None yet
4 participants
@ab-smith

ab-smith commented Nov 5, 2016

Hi,
Based on the Android NDK samples (https://github.com/googlesamples/android-ndk/tree/master) and mainly the hello-libs, I'm trying to call a prebuilt library.
In fact, it is pretty simple for a C-based library (using the examples), but when my .so is built using Go, the ndk-build linker seems to go crazy and uses an intermediate path to reference the .so library which causes the app crash.

Go versions

go version go1.7.1 darwin/amd64

gomobile version +6ecf8ee Sun Oct 16 10:25:40 2016

Go env

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/me/dev/go/"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/b_/y2nd3vgn4m1929dg56thjdn40000gn/T/go-build047047925=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

Steps

On the gomobile example directory, create an Android binding library :
cd $GOPATH/src/golang.org/x/mobile/example/bind/hello
gomobile bind -target android

extract the so library from the resulting aar.
create a simple .h header to reference the symbol (Java_go_hello_Hello_Greetings)

add the result to the sample project (the same thing as gperf)

without even referencing the function on the main cpp, build the project (gradle build) --> build successful

install the resulting apk

What did you expect to see?

a working app

What did you see instead?

java.lang.UnsatisfiedLinkError: dlopen failed: library "/Users/me/Desktop/vm-win/ndk-build-golang/hello-libs-2/app/src/main/cpp/../../../../distribution/libgo/lib/arm64-v8a/libgojni.so" not found

after decomposing the code and focusing only on the library build (basic ndk project) I still get the same thing :
java.lang.UnsatisfiedLinkError: dlopen failed: library "/Users/me/dev/android-ndk/hello-libs/app/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/libgojni.so" not found

When I check the symbols on the intermediate library (libhello-libs.so) I see that the libgojni.so is referenced with a full path instead of just the filename (which is the case of a C-based library)

(both libraries are at the same level when I extract the apk)

Thanks,

additional info

A C-based library is working but not the Go-based one. Maybe the latter is missing something ?

I've uploaded a buildable project to https://github.com/ab-smith/ndk-build-golang

I can make it work by patching the libhello-libs.so and fixing the path binary, but this is not a solution.

@quentinmit quentinmit changed the title from Gomobile/ndk-build wrong library path for dlopen to x/mobile: ndk-build finds wrong library path for dlopen Nov 7, 2016

@quentinmit quentinmit added this to the Unreleased milestone Nov 7, 2016

@david-berichon

This comment has been minimized.

david-berichon commented Nov 11, 2016

The issue here is that gomobile does not set a library SONAME at the linker step for building libgojni.so. If this attribute remain unset the further linking to that library, in your example for buildinglibhello-libs.so, will conduce to hardcode internally the current build tree path of your libgojni.so which will no more be available after a deployment on targeted.

To solve the issue you must instruct the linker to set the SONAME value of libgojni.so.
Since gomobile uses external linkers to achieve the final .aar package the parameter can be provided as following:

gomobile bind -target android -ldflags=-extldflags=-Wl,-soname,libgojni.so

You can have a look here for more information about shared library workflow.

@ab-smith

This comment has been minimized.

ab-smith commented Nov 11, 2016

Ok thanks

@gopherbot gopherbot added the mobile label Jul 20, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment