Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 13 Feb 2015 08:42:33 +0100
From:      Marcus von Appen <mva@FreeBSD.org>
To:        Craig Rodrigues <rodrigc@FreeBSD.org>
Cc:        freebsd-python@freebsd.org
Subject:   Re: ctypes problem loading libc.so on FreeBSD current
Message-ID:  <20150213074233.GA1064@medusa.sysfault.org>
In-Reply-To: <CAG=rPVehh4ZBBKdmJb8tVy_H=CgbALwUqcC7yofwrJHPVC-1zQ@mail.gmail.com>
References:  <CAG=rPVehh4ZBBKdmJb8tVy_H=CgbALwUqcC7yofwrJHPVC-1zQ@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--IJpNTDwzlM2Ie8A6
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On, Fri Feb 13, 2015, Craig Rodrigues wrote:

> Hi,
>
> I am looking at this blog post for loading libc with ctypes:
>
> http://blogs.freebsdish.org/rpaulo/2008/11/30/sysctlbyname3-and-others-from-python/
>
> I am using this version of python from ports:
>
>
> Name           : python
> Version        : 2.7_2,2
> Installed on   : Mon Jan 12 22:10:27 PST 2015
> Origin         : lang/python
> Architecture   : freebsd:11:x86:64
> Prefix         : /usr/local
> Categories     : python lang ipv6
> Licenses       :
> Maintainer     : python@FreeBSD.org
> WWW            : http://www.python.org/
> Comment        : The "meta-port" for the default version of Python
> interpreter
> Annotations    :
>     repo_type      : binary
>     repository     : FreeBSD
> Flat size      : 38.0B
> Description    :
> Python is an interpreted object-oriented programming language, and is
> often compared to Tcl, Perl or Scheme.
> This is a meta port to the Python interpreter and provides symbolic links
> to bin/python, bin/pydoc, bin/idle and so on to allow compatibility with
> version agnostic python scripts.
>
> WWW: http://www.python.org/
>
>
>
> I have this test program
>
> import ctypes
>
> mylib = ctypes.CDLL("libc.so")
> print(mylib)
>
> On FreeBSD 9, this program works, and I get a handle to libc.so.
>
> On FreeBSD-CURRENT, I get this:
>
>   File "a.py", line 3, in <module>
>     mylib = ctypes.CDLL("libc.so")
>   File "/usr/local/lib/python2.7/ctypes/__init__.py", line 365, in __init__
>     self._handle = _dlopen(self._name, mode)
> OSError: /usr/lib/libc.so: invalid file format
>
>
> The problem seems to be that on FreeBSD 9,
> /usr/lib/libc.so is a symlink to /lib/libc.so.7, while
> on FreeBSD-CURRENT, /usr/lib/libc.so contains this:
>
> /* $FreeBSD: head/lib/libc/libc.ldscript 258283 2013-11-17 22:52:17Z peter
> $ */
> GROUP ( /lib/libc.so.7 /usr/lib/libc_nonshared.a
> /usr/lib/libssp_nonshared.a )
>
>
> Any ideas what the problem is?

You described the problem above. ctypes is unable to load linker
scripts. The general advise is to use find_library():

>>> import ctypes
>>> import ctypes.util
>>> clib = ctypes.util.find_library("c")
>>> print clib
libc.so.7
>>> dll = ctypes.CDLL(clib)

Cheers
Marcus

--IJpNTDwzlM2Ie8A6
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEYEARECAAYFAlTdqukACgkQi68/ErJnpkfOOACgjAZ0WLC/8oCYZ05bSn+OXAyF
L+0An0GbuTKrdJbiPhJCcZuE7N/ng6m3
=3z9g
-----END PGP SIGNATURE-----

--IJpNTDwzlM2Ie8A6--



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