python / python/mypy

mypyc-compiled extensions missing math library linking causing undefined symbol errors

Open
#20,567 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report
When building mypy with mypyc enabled (MYPY_USE_MYPYC=1), the compiled C extensions are missing explicit linking to the math library (libm), causing undefined symbol errors for standard math functions.

[./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so]:
    linux-vdso.so.1 (0x0000ffffb4c9c000)
    libc.so.6 => /lib64/libc.so.6 (0x0000ffffb4632000)
    /lib/ld-linux-aarch64.so.1 (0x0000ffffb4c4f000)
undefined symbol: sin	(./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: cos	(./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: tan	(./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: sqrt	(./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: exp	(./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: fmod	(./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: pow	(./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: log	(./lib/python3.12/site-packages/30fcd23745efe32ce681__mypyc.cpython-312-aarch64-linux-gnu.so)

[./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so]:
    linux-vdso.so.1 (0x0000ffff80b96000)
    libc.so.6 => /lib64/libc.so.6 (0x0000ffff7ee32000)
    /lib/ld-linux-aarch64.so.1 (0x0000ffff80b49000)
undefined symbol: sin	(./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: cos	(./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: tan	(./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: sqrt	(./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: exp	(./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: fmod	(./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: pow	(./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)
undefined symbol: log	(./lib/python3.12/site-packages/3204bda914b7f2c6f497__mypyc.cpython-312-aarch64-linux-gnu.so)

To Reproduce

PACKAGE_VERSION="v1.19.1"
ARCHIVE_NAME="${PACKAGE_VERSION}.tar.gz"
curl -fLO "https://github.com/python/mypy/archive/refs/tags/${ARCHIVE_NAME}"
tar xf "${ARCHIVE_NAME}"
cd "mypy-${PACKAGE_VERSION#v}"

export MYPY_USE_MYPYC="1"
pip3 install . -v -v -v --compile --no-deps --no-cache-dir --no-build-isolation --no-binary :all:

Expected Behavior
No unexpected undefined symbols

Actual Behavior

Unexpected undefined symbols

Your Environment

  • OS: Amz Linux 2023 aarch64
  • Python: 3.12.7
  • mypy: 1.19.1

Proposed Solution

The math library needs to be added to the extension modules during the build process. A potential patch location is in setup.py after the extensions are created:

# After mypyc_build_ext() creates ext_modules, add math library
for ext in ext_modules:
    if "m" not in ext.libraries:
        ext.libraries.append("m")

However, this approach may over-link modules that don't need libm. A more precise solution would require:

  1. Identifying which specific mypyc-generated extensions actually use math functions
  2. Only adding -lm to those specific extensions
  3. This may require changes in mypyc itself to track which C math functions are used during compilation

Temporary Workaround is:

# Patch setup.py to add math library to all extensions
sed -i '
/log_trace=log_trace,$/,/^    assert ext_modules$/ {
    /^    )$/ a\
\
    for ext in ext_modules:\
        if "m" not in ext.libraries:\
            ext.libraries.append("m")
}
' "./setup.py"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in setup.py around mypyc_build_ext() and the creation of ext_modules, then reproduce the build with MYPY_USE_MYPYC=1 using the documented pip command on the affected environment. Determine which generated extensions require libm and ensure the built extensions load without undefined math symbols, avoiding unnecessary linking where possible.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.