Skip to content
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

Undefined references when compiling ctypes on binutils 2.29.1 with gcc -Wl,-z,undefs (Fedora 28) #76828

Closed
stratakis mannequin opened this issue Jan 24, 2018 · 20 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes build The build process and cross-build extension-modules C modules in the Modules dir

Comments

@stratakis
Copy link
Mannequin

stratakis mannequin commented Jan 24, 2018

BPO 32647
Nosy @vstinner, @tiran, @matrixise, @stratakis
PRs
  • bpo-32647: Link ctypes extension with libdl. #5550
  • [3.7] bpo-32647: Link ctypes extension with libdl. (GH-5550) #5874
  • [3.6] bpo-32647: Link ctypes extension with libdl. (GH-5550) #5875
  • [2.7] bpo-32647: Link ctypes extension with libdl. (GH-5550) #5877
  • Files
  • build.log
  • py2buildlog
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-02-26.17:17:49.306>
    created_at = <Date 2018-01-24.10:27:29.867>
    labels = ['extension-modules', '3.8', 'build', '3.7']
    title = 'Undefined references when compiling ctypes on binutils 2.29.1 with gcc -Wl,-z,undefs (Fedora 28)'
    updated_at = <Date 2018-02-26.17:17:49.305>
    user = 'https://github.com/stratakis'

    bugs.python.org fields:

    activity = <Date 2018-02-26.17:17:49.305>
    actor = 'cstratak'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-02-26.17:17:49.306>
    closer = 'cstratak'
    components = ['Build', 'Extension Modules']
    creation = <Date 2018-01-24.10:27:29.867>
    creator = 'cstratak'
    dependencies = []
    files = ['47407', '47426']
    hgrepos = []
    issue_num = 32647
    keywords = ['patch']
    message_count = 20.0
    messages = ['310583', '310586', '310591', '310592', '310595', '310596', '310597', '311664', '311665', '311676', '311677', '311678', '311722', '311726', '311727', '312790', '312802', '312805', '312806', '312938']
    nosy_count = 4.0
    nosy_names = ['vstinner', 'christian.heimes', 'matrixise', 'cstratak']
    pr_nums = ['5550', '5874', '5875', '5877']
    priority = 'normal'
    resolution = None
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'compile error'
    url = 'https://bugs.python.org/issue32647'
    versions = ['Python 2.7', 'Python 3.6', 'Python 3.7', 'Python 3.8']

    @stratakis
    Copy link
    Mannequin Author

    stratakis mannequin commented Jan 24, 2018

    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.

    @stratakis stratakis mannequin added 3.8 only security fixes 3.7 (EOL) end of life build The build process and cross-build labels Jan 24, 2018
    @tiran
    Copy link
    Member

    tiran commented Jan 24, 2018

    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.

    @stratakis
    Copy link
    Mannequin Author

    stratakis mannequin commented Jan 24, 2018

    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

    @stratakis
    Copy link
    Mannequin Author

    stratakis mannequin commented Jan 24, 2018

    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.

    @stratakis stratakis mannequin added the extension-modules C modules in the Modules dir label Jan 24, 2018
    @vstinner
    Copy link
    Member

    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).

    @vstinner vstinner removed the 3.8 only security fixes label Jan 24, 2018
    @vstinner vstinner changed the title Undefined references when compiling ctypes Undefined references when compiling ctypes on binutils 2.29.1 with gcc -Wl,-z,undefs (Fedora 28) Jan 24, 2018
    @tiran
    Copy link
    Member

    tiran commented Jan 24, 2018

    -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.

    @vstinner
    Copy link
    Member

    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.

    @stratakis
    Copy link
    Mannequin Author

    stratakis mannequin commented Feb 5, 2018

    So what would be the best way to proceed from here?

    Add a check or just link to dl unconditionally?

    @tiran
    Copy link
    Member

    tiran commented Feb 5, 2018

    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.

    @tiran tiran added 3.8 only security fixes build The build process and cross-build labels Feb 5, 2018
    @matrixise
    Copy link
    Member

    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

    @tiran
    Copy link
    Member

    tiran commented Feb 5, 2018

    Stéphane, the issue is only reproducible with latest Fedora rawhide.

    @matrixise
    Copy link
    Member

    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

    @stratakis
    Copy link
    Mannequin Author

    stratakis mannequin commented Feb 6, 2018

    @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

    @stratakis
    Copy link
    Mannequin Author

    stratakis mannequin commented Feb 6, 2018

    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.

    @stratakis
    Copy link
    Mannequin Author

    stratakis mannequin commented Feb 6, 2018

    Attaching the build log from python2. Near the end you can see the undefined references errors.

    @tiran
    Copy link
    Member

    tiran commented Feb 25, 2018

    New changeset 5bb9692 by Christian Heimes in branch 'master':
    bpo-32647: Link ctypes extension with libdl. (bpo-5550)
    5bb9692

    @tiran
    Copy link
    Member

    tiran commented Feb 25, 2018

    New changeset 4bb9b9a by Christian Heimes in branch '2.7':
    [2.7] bpo-32647: Link ctypes extension with libdl. (GH-5550) (bpo-5877)
    4bb9b9a

    @tiran
    Copy link
    Member

    tiran commented Feb 25, 2018

    New changeset 4cb3733 by Christian Heimes (Miss Islington (bot)) in branch '3.7':
    [3.7] bpo-32647: Link ctypes extension with libdl. (GH-5550) (bpo-5874)
    4cb3733

    @tiran
    Copy link
    Member

    tiran commented Feb 25, 2018

    New changeset 192bff4 by Christian Heimes (Miss Islington (bot)) in branch '3.6':
    [3.6] bpo-32647: Link ctypes extension with libdl. (GH-5550) (bpo-5875)
    192bff4

    @stratakis
    Copy link
    Mannequin Author

    stratakis mannequin commented Feb 26, 2018

    Tested the fix and ctypes is linked successfully with libdl when utilizing the strict symbol check.

    @stratakis stratakis mannequin closed this as completed Feb 26, 2018
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes build The build process and cross-build extension-modules C modules in the Modules dir
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants