[Python-Dev] Undefined dlopen When Building Module On Android

Frank, Matthew I matthew.i.frank at intel.com
Sun Jan 25 00:08:39 CET 2015


Android's dlopen() works slightly differently than the normal Unix dlopen() in at least two different ways.  I haven't seen your particular problem, but that's probably because I'm cross-building CPython (building everything I need on a Linux machine, and then copying the install directory to the Android machine.)

(1) When building for Android you need to explicitly include the "-ldl" flag on the command line for the link step.  (In Linux the dlopen() routine is included in libc, so "-ldl" is not necessary).  I suspect that some part of your distutils was not linked correctly.  (Since I'm cross-building I've never run distutils on the Android side, which is probably why I've not seen this.)  Your best bet would be to run under a debugger and figure out which line of C code in which .so file is throwing the error.  Then going back to the build scripts and looking at how that .so file is getting linked.  (For an example of someone else having a similar problem see for example, http://stackoverflow.com/questions/25846927/git-built-on-android-throws-undefined-reference-to-dlopen-error).

(2) The other possibility has to do with a quirk in Android's dlopen() implementation.  On most legacy Unix systems (including Linux) when you dlopen() a library Z then all the already loaded libraries A, B, C, ... are searched for any dependences of Z (even if Z was not explicitly linked against any of A, B, C,...).  On Android (perhaps for security reasons) that's not true, if Z depends on A, then you need to have "-lA" when you link Z.  An example (and patch) for this problem is http://bugs.python.org/issue21668.

-Matt

> -----Original Message-----
> From: Cyd Haselton [mailto:chaselton at gmail.com]
> Sent: Friday, January 23, 2015 10:50 AM
> To: Brett Cannon
> Cc: Guido van Rossum; Python-Dev
> Subject: Re: [Python-Dev] Undefined dlopen When Building Module On Android
> 
> I guess I'll keep waiting...given the zero responses I've gotten from the android side.
> :-(.
> 
> In the meantime...in case anyone is interested... since I have the working binary, I
> executed it and went through each command in setup.py to see what throws the
> 'undefined reference to dlopen' error.
> Turns out that the distutils module is the culprit...specifically distutils.core.
> 
> 
> On Thu, Jan 22, 2015 at 8:33 AM, Brett Cannon <brett at python.org> wrote:
> > A mobile SIG is being formed, but it doesn't have a mailing list yet,
> > else that would be a good place to ask this question.
> >
> > On Wed Jan 21 2015 at 5:54:39 PM Guido van Rossum <guido at python.org> wrote:
> >>
> >> Maybe try a list focused on Android development? Few people in the
> >> Python core development community have any Android experience. But
> >> the issues and context you offer seem to be related to the Android world, not to
> Python.
> >> (dlopen is used by a lot of systems, not just Python.)
> >>
> >> On Wed, Jan 21, 2015 at 2:43 PM, Cyd Haselton <chaselton at gmail.com> wrote:
> >>>
> >>> On Mon, Jan 19, 2015 at 5:23 PM, Cyd Haselton <chaselton at gmail.com>
> >>> wrote:
> >>> > On Mon, Jan 19, 2015 at 8:51 AM, Cyd Haselton
> >>> > <chaselton at gmail.com>
> >>> > wrote:
> >>> >> Hello,
> >>> >> I'm struggling with a build issue on Android; I've posted to the
> >>> >> general python list with no result, so I'm re-posting here in
> >>> >> hopes that someone can help.  If this is the wrong place feel
> >>> >> free to let me know.
> >>> >>
> >>> >> I'm attempting to build Python 2.7.8 on my Android device; I'm
> >>> >> using an environment that simulates a Linux filesystem within the
> >>> >> Android terminal using a port of fakechroot.  Within that
> >>> >> environment I've ported and/or bootstrapped a number of Linux
> >>> >> utilities (curl, git, openssl, gcc)
> >>> >>
> >>> >> I run ./configure, then make, and the executable and library are
> >>> >> built.  The problem occurs when build_ext is run; the newly built
> >>> >> python executable builds, then links _struct, and immediately
> >>> >> afterwards I get an 'undefined reference to dlopen' error.
> >>> >>
> >>> >> If I run ./python setup.py --verbose -library-dirs /path/to/lib
> >>> >> --libraries='c dl m' -f, the 'undefined reference to dlopen'
> >>> >> error is thrown again.
> >>> >>
> >>> >> If I run ./python setup.py --verbose -library-dirs /path/to/lib
> >>> >> --libraries='-lc -ldl -lm' -f the build continues past
> >>> >> _struct...even though ld throws the expected 'unable to find -l-lc' and other
> errors.
> >>> >>
> >>> >> Let me know if you need me to provide additional information.
> >>> >> Any help would be greatly appreciated.
> >>> >>
> >>> >> Cyd
> >>> >
> >>> >
> >>> > Additionally I took a strace of the error producing command. The
> >>> > following is (hopefully) a relevant portion minus the various 'no
> >>> > such file' lines before the correct lib is found (which it always
> >>> > is)
> >>> >
> >>> > 16513
> >>> > open("/data/data/jackpal.androidterm/kbox2/bld/python/Python-
> 2.7.8/Lib/distutils/unixccompiler.py",
> >>> > O_RDONLY|O_LARGEFILE) = 3           16513
> >>> >
> >>> > open("/data/data/jackpal.androidterm/kbox2/bld/python/Python-
> 2.7.8/Lib/distutils/unixccompiler.pyc",
> >>> > O_RDONLY|O_LARGEFILE) = 4       16513 vfork()
> >>> >  = 16525
> >>> > 16513 wait4(16525,  <unfinished ...>
> >>> > 16525 open("/data/data/jackpal.androidterm/kbox2/bin/sh",
> >>> > O_RDONLY|O_LARGEFILE) = 3
> >>> > 16525 execve("/data/data/jackpal.androidterm/kbox2/bin/sh", ["sh",
> >>> > "-c", "gcc --sysroot=/usr/gcc-4.9.2/sysroot -print-multiarch >
> >>> > build/temp.linux-armv7l-2.7/multiarch 2> /dev/null"], [/* 58 vars
> >>> > */]) = 0
> >>> >
> >>> > *snip call to libc intercepted by libfakechroot*
> >>> >
> >>> > 16525 open("/system/lib/libc.so", O_RDONLY|O_LARGEFILE|0x80000) =
> >>> > 3
> >>> > 16525 open("/system/lib/libm.so", O_RDONLY|O_LARGEFILE|0x80000) =
> >>> > 3
> >>> > 16525 open("/dev/__properties__",
> >>> > O_RDONLY|O_LARGEFILE|O_NOFOLLOW|0x80000) = 3
> >>> > 16525 open("build/temp.linux-armv7l-2.7/multiarch",
> >>> > O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
> >>> > 16525 open("/data/data/jackpal.androidterm/kbox2/dev/null",
> >>> > O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
> >>> > 16525 fork()                            = 16526
> >>> > 16525 wait4(-1,  <unfinished ...>
> >>> > 16526 open("/acct/uid/10186/tasks", O_RDWR|O_CREAT|O_LARGEFILE,
> >>> > 0666) = -1 EACCES (Permission denied)
> >>> >
> >>> > See attached for remainder
> >>>
> >>> Should I be posting this issue elsewhere?
> >>> Is more information needed?
> >>>
> >>> _______________________________________________
> >>> Python-Dev mailing list
> >>> Python-Dev at python.org
> >>> https://mail.python.org/mailman/listinfo/python-dev
> >>>
> >>> Unsubscribe:
> >>> https://mail.python.org/mailman/options/python-dev/guido%40python.or
> >>> g
> >>
> >>
> >>
> >>
> >> --
> >> --Guido van Rossum (python.org/~guido)
> >> _______________________________________________
> >> Python-Dev mailing list
> >> Python-Dev at python.org
> >> https://mail.python.org/mailman/listinfo/python-dev
> >> Unsubscribe:
> >> https://mail.python.org/mailman/options/python-dev/brett%40python.org



More information about the Python-Dev mailing list