This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Undefined references when compiling ctypes on binutils 2.29.1 with gcc -Wl,-z,undefs (Fedora 28)
Type: compile error Stage: resolved
Components: Build, Extension Modules Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, cstratak, matrixise, vstinner
Priority: normal Keywords: patch

Created on 2018-01-24 10:27 by cstratak, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
build.log cstratak, 2018-01-24 10:27
py2buildlog cstratak, 2018-02-06 15:36
Pull Requests
URL Status Linked Edit
PR 5550 merged christian.heimes, 2018-02-05 12:33
PR 5874 merged miss-islington, 2018-02-25 09:22
PR 5875 merged miss-islington, 2018-02-25 09:23
PR 5877 merged christian.heimes, 2018-02-25 10:02
Messages (20)
msg310583 - (view) Author: Charalampos Stratakis (cstratak) * Date: 2018-01-24 10:27
Original bug report: https://bugzilla.redhat.com/show_bug.cgi?id=1537489

As soon as strict symbol checking was enabled on the development branch of Fedora, our build of CPython started failing due to undefined symbols when compiling ctypes.

Build log is attached.

That seems to be because ctypes are not explicitly linked against -ldl.

This can be fixed by adding the -ldl flag at https://github.com/python/cpython/blob/master/setup.py#L1935 , not sure though how portable that is.
msg310586 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2018-01-24 11:04
The attached file is 1.3 MB and your build is using Fedora-specific patches. Please post the relevant part from a vanilla build without Fedora patches.
msg310591 - (view) Author: Charalampos Stratakis (cstratak) * Date: 2018-01-24 13:21
Relevant part from the build log:

running build_ext
building '_ctypes' extension
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include -I. -IObjects -IInclude -IPython -I/usr/local/include -I/builddir/build/BUILD/Python-3.6.4/Include -I/builddir/build/BUILD/Python-3.6.4/build/optimized -c /builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/_ctypes.c -o build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/_ctypes.o
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include -I. -IObjects -IInclude -IPython -I/usr/local/include -I/builddir/build/BUILD/Python-3.6.4/Include -I/builddir/build/BUILD/Python-3.6.4/build/optimized -c /builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callbacks.c -o build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callbacks.o
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include -I. -IObjects -IInclude -IPython -I/usr/local/include -I/builddir/build/BUILD/Python-3.6.4/Include -I/builddir/build/BUILD/Python-3.6.4/build/optimized -c /builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callproc.c -o build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callproc.o
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include -I. -IObjects -IInclude -IPython -I/usr/local/include -I/builddir/build/BUILD/Python-3.6.4/Include -I/builddir/build/BUILD/Python-3.6.4/build/optimized -c /builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/stgdict.c -o build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/stgdict.o
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include -I. -IObjects -IInclude -IPython -I/usr/local/include -I/builddir/build/BUILD/Python-3.6.4/Include -I/builddir/build/BUILD/Python-3.6.4/build/optimized -c /builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/cfield.c -o build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/cfield.o
gcc -pthread -shared -Wl,-z,relro -Wl,-z,defs -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -g build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/_ctypes.o build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callbacks.o build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callproc.o build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/stgdict.o build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/cfield.o -L. -L/usr/local/lib64 -lffi -lpython3.6m -o build/lib.linux-x86_64-3.6/_ctypes.cpython-36m-x86_64-linux-gnu.so
build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/_ctypes.o: In function `PyCFuncPtr_FromDll':
/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/_ctypes.c:3391: undefined reference to `dlsym'
/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/_ctypes.c:3399: undefined reference to `dlerror'
build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/_ctypes.o: In function `CDataType_in_dll':
/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/_ctypes.c:675: undefined reference to `dlsym'
/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/_ctypes.c:683: undefined reference to `dlerror'
build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callproc.o: In function `py_dl_open':
/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callproc.c:1329: undefined reference to `dlopen'
/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callproc.c:1332: undefined reference to `dlerror'
build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callproc.o: In function `py_dl_sym':
/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callproc.c:1366: undefined reference to `dlsym'
/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callproc.c:1369: undefined reference to `dlerror'
build/temp.linux-x86_64-3.6/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callproc.o: In function `py_dl_close':
/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callproc.c:1348: undefined reference to `dlclose'
/builddir/build/BUILD/Python-3.6.4/Modules/_ctypes/callproc.c:1350: undefined reference to `dlerror'
collect2: error: ld returned 1 exit status
The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
atexit                pwd                   time               
Failed to build these modules:
_ctypes
msg310592 - (view) Author: Charalampos Stratakis (cstratak) * Date: 2018-01-24 13:25
Unfortunately when trying to reproduce it by compiling from source and adding the '-z defs' flag to the linker, undefined symbol issues appeared for all the extension modules of the stdlib.
msg310595 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-01-24 14:13
I'm unable to reproduce the issue on Fedora 27 (binutils-2.29-6.fc27.src.rpm, gcc-7.2.1-2.fc27.src.rpm). _ctypes.cpython-37dm-x86_64-linux-gnu.so is generated whereas it's not linked to libdl.so (as expected). The flag seems to be ignored. https://src.fedoraproject.org/rpms/redhat-rpm-config/c/078af192613e1beec34824a94dc5f6feeeea1568?branch=master says "The latter needs binutils 2.29.1-12.fc28 or later. I understand that my binutils are too old (2.29 vs 2.29.1).
msg310596 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2018-01-24 14:16
-ldl is the correct fix here. https://fedoraproject.org/wiki/UnderstandingDSOLinkChange explains the problem. tl;dr linking with dl is required because the linker no longer adds indirect dependencies.

We can't just add dl unconditionally. You have to add a check.
msg310597 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-01-24 14:36
> We can't just add dl unconditionally.

dlopen() is not used on Windows nor macOS older than 10.3. On old macOS, functions like NSCreateObjectFileImageFromFile() are used.

I don't think that the master branch still supports macOS 10.2.

Mac OS X 10.2 was released in 2002 and unsupported as of 2006.
msg311664 - (view) Author: Charalampos Stratakis (cstratak) * Date: 2018-02-05 12:12
So what would be the best way to proceed from here?

Add a check or just link to dl unconditionally?
msg311665 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2018-02-05 12:33
configure.ac already contains the check:

   AC_CHECK_LIB(dl, dlopen)

It should be enough to add:

    if sysconfig.get_config_var('HAVE_LIBDL'):
        libs.append('dl')

to ctypes.
msg311676 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-02-05 17:07
I use Fedora 27,

Please, could you give the instructions for your build, I could test locally and check if the patch fixes the issue.

Thank you
msg311677 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2018-02-05 17:09
Stéphane, the issue is only reproducible with latest Fedora rawhide.
msg311678 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-02-05 17:32
maybe I miss something

but I have downloaded the last fedora:rawhide with docker, just update it via dnf update and install all the dependencies for git and python

docker --rm -it fedora:rawhide /bin/bash

dnf update
dnf install git-core
dnf builddep python3

git clone --depth=1 https://github.com/python/cpython
cd cpython
./configure && make

and I have no issue with ctypes.

What are your instructions for the build?

Thank you
msg311722 - (view) Author: Charalampos Stratakis (cstratak) * Date: 2018-02-06 09:57
@Stephane,

Currently the flag has been disabled on rawhide due to too many breakages but the bug is still there if you add '-z defs' to the linker flags.

More info:
https://src.fedoraproject.org/rpms/redhat-rpm-config/c/8d6c6d0761089dfb7e1eacf35112b3d7a3f0d1af?branch=master
msg311726 - (view) Author: Charalampos Stratakis (cstratak) * Date: 2018-02-06 15:33
Just tested it for python3 on a rawhide system with the flag enabled and it works, ctypes is compiled successfully.

python2 is not affected the same way though, various modules fail there with different undefined references issues when setting the flag.
msg311727 - (view) Author: Charalampos Stratakis (cstratak) * Date: 2018-02-06 15:36
Attaching the build log from python2. Near the end you can see the undefined references errors.
msg312790 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2018-02-25 09:22
New changeset 5bb9692575f10f4a7c7f1c2c0c70956baf6d5c23 by Christian Heimes in branch 'master':
bpo-32647: Link ctypes extension with libdl. (#5550)
https://github.com/python/cpython/commit/5bb9692575f10f4a7c7f1c2c0c70956baf6d5c23
msg312802 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2018-02-25 11:31
New changeset 4bb9b9aea04b1a96632da1b30a58fb31cbe6ec92 by Christian Heimes in branch '2.7':
[2.7] bpo-32647: Link ctypes extension with libdl. (GH-5550) (#5877)
https://github.com/python/cpython/commit/4bb9b9aea04b1a96632da1b30a58fb31cbe6ec92
msg312805 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2018-02-25 12:07
New changeset 4cb373359d5ff29b222b10207516d294f3a54ad8 by Christian Heimes (Miss Islington (bot)) in branch '3.7':
[3.7] bpo-32647: Link ctypes extension with libdl. (GH-5550) (#5874)
https://github.com/python/cpython/commit/4cb373359d5ff29b222b10207516d294f3a54ad8
msg312806 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2018-02-25 12:07
New changeset 192bff4e2d196b8933829923ca1db77d8dee67e2 by Christian Heimes (Miss Islington (bot)) in branch '3.6':
[3.6] bpo-32647: Link ctypes extension with libdl. (GH-5550) (#5875)
https://github.com/python/cpython/commit/192bff4e2d196b8933829923ca1db77d8dee67e2
msg312938 - (view) Author: Charalampos Stratakis (cstratak) * Date: 2018-02-26 17:17
Tested the fix and ctypes is linked successfully with libdl when utilizing the strict symbol check.
History
Date User Action Args
2022-04-11 14:58:57adminsetgithub: 76828
2018-02-26 17:17:49cstrataksetstatus: open -> closed

messages: + msg312938
stage: patch review -> resolved
2018-02-25 12:07:38christian.heimessetmessages: + msg312806
2018-02-25 12:07:26christian.heimessetmessages: + msg312805
2018-02-25 11:31:20christian.heimessetmessages: + msg312802
2018-02-25 10:02:49christian.heimessetpull_requests: + pull_request5650
2018-02-25 09:23:23miss-islingtonsetpull_requests: + pull_request5648
2018-02-25 09:22:22miss-islingtonsetpull_requests: + pull_request5647
2018-02-25 09:22:16christian.heimessetmessages: + msg312790
2018-02-06 15:36:18cstrataksetfiles: + py2buildlog

messages: + msg311727
2018-02-06 15:33:39cstrataksetmessages: + msg311726
2018-02-06 09:57:00cstrataksetmessages: + msg311722
2018-02-05 17:32:53matrixisesetmessages: + msg311678
2018-02-05 17:09:42christian.heimessetmessages: + msg311677
2018-02-05 17:07:12matrixisesetnosy: + matrixise
messages: + msg311676
2018-02-05 12:33:55christian.heimessettype: compile error
messages: + msg311665
versions: + Python 3.8
2018-02-05 12:33:28christian.heimessetkeywords: + patch
stage: patch review
pull_requests: + pull_request5375
2018-02-05 12:12:16cstrataksetmessages: + msg311664
2018-01-24 14:36:04vstinnersetmessages: + msg310597
2018-01-24 14:16:58christian.heimessetmessages: + msg310596
2018-01-24 14:13:18vstinnersetnosy: + vstinner
title: Undefined references when compiling ctypes -> Undefined references when compiling ctypes on binutils 2.29.1 with gcc -Wl,-z,undefs (Fedora 28)
messages: + msg310595

versions: + Python 2.7, - Python 3.8
2018-01-24 13:25:31cstrataksetmessages: + msg310592
components: + Extension Modules
2018-01-24 13:21:39cstrataksetmessages: + msg310591
2018-01-24 11:04:43christian.heimessetnosy: + christian.heimes
messages: + msg310586
2018-01-24 10:27:35cstrataksetcomponents: + Build
2018-01-24 10:27:29cstratakcreate