Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 25 Aug 2015 10:43:33 +0300
From:      Konstantin Belousov <kostikbel@gmail.com>
To:        David Naylor <dbn@freebsd.org>
Cc:        freebsd-python@freebsd.org
Subject:   Re: ctypes not working (Fwd: [pypy-dev] 2.6.1 and freebsd-9)
Message-ID:  <20150825074333.GB2072@kib.kiev.ua>
In-Reply-To: <1770820.q6b9lEKF14@dragon.local>
References:  <1770820.q6b9lEKF14@dragon.local>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Aug 24, 2015 at 11:11:32PM +0200, David Naylor wrote:
> Hi list,
> 
> Please see the correspondence below from the pypy team about some failing 
> tests.  Specifically, why is test.py producing None whereas test.c is 
> producing the desired results?  

Because dlopen symbol is magic.  It is provided by the shared libc.so
to satisfy the static linker, but real definition comes from the dynamic
linker.  The libc.so dlopen() is a null stub.

You are asking for the dlopen symbol from libc, which is returned to
you in faith, and which cannot load a library for real. While in the C
example, you use normal symbol resolution and get the dlopen from the
loader.

To get a handle to real dlopen with dlopen/dlsym, you should do in C:
	dlopenX = dlsym(RTLD_DEFAULT, "dlopen");
I briefly looked at the python 2.7.8 documentation for ctypes, but I do
not see a way to express this with the module. Most clean way for ctypes
to offer the missing functionality would be to provide a phabricated
object, access to the symbols of which would do dlsym(RTLD_DEFAULT, XXX)
instead of dlsym(some handle, XXX).

A complete working C example is below.

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
	void *(*dlopen1)(const char *path, int mode);
	void *libm_handle;

	dlopen1 = dlsym(RTLD_DEFAULT, "dlopen");
	if (dlopen1 == NULL) {
		fprintf(stderr, "dlsym %s\n", dlerror());
		exit(1);
	}
	libm_handle = dlopen1("libm.so", RTLD_LOCAL /* why local ? */);
	if (libm_handle == NULL) {
		fprintf(stderr, "dlopen1 %s\n", dlerror());
		exit(1);
	}
	printf("libm.so %p\n", libm_handle);
	return (0);
}




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20150825074333.GB2072>