PyO3 / PyO3/pyo3

Pyo3 fails to link when used inside of a proc-macro (or any rlib)

Open
#5,347 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

Bug Description

When attempting to use pyo3 inside of a proc macro, it fails to link.
The extension-module feature does not fix this.

Steps to Reproduce

git clone https://github.com/bend-n/mpy; cargo test

Backtrace
error: linking with `cc` failed: exit status: 1
  |
  = note:  "cc" "-Wl,--version-script=/tmp/rustcnJ3Ivg/list" "-Wl,--no-undefined-version" "-m64" "/tmp/rustcnJ3Ivg/symbols.o" "<35 object files omitted>" "/tmp/rustcnJ3Ivg/rmeta.o" "<1 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "/workspaces/mpy/target/debug/deps/{libsyn-1d03b89245feae7e,libquote-7d60de7e2c115858,libproc_macro2-6f9902815a90d0fd,libunicode_ident-00291e71c3df335a,libpyo3-346cffa685c03042,libmemoffset-4e383f14ad0576a3,libonce_cell-6c5ecd365f058077,libpyo3_ffi-d2f9536bda801ee1,liblibc-b8d662a471b68e1a,libunindent-f4f50b2efd572470}.rlib" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/{libproc_macro-*,librustc_literal_escaper-*,librustc_std_workspace_std-*,libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,libcfg_if-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,liblibc-*,librustc_std_workspace_core-*,liballoc-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-L" "/tmp/rustcnJ3Ivg/raw-dylibs" "-B<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld" "-fuse-ld=lld" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/usr/local/python/3.12.1/lib" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/workspaces/mpy/target/debug/deps/libmpy-f38bf62b40fd1f2d.so" "-Wl,--gc-sections" "-shared" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stderr'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by stdio2.h:79 (/usr/include/x86_64-linux-gnu/bits/stdio2.h:79)
          >>>               getpath.o:(getpath_warn) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stderr'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by stdio2.h:79 (/usr/include/x86_64-linux-gnu/bits/stdio2.h:79)
          >>>               getopt.o:(_PyOS_GetOpt) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stderr'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by stdio2.h:79 (/usr/include/x86_64-linux-gnu/bits/stdio2.h:79)
          >>>               getopt.o:(_PyOS_GetOpt) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stderr'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by stdio2.h:79 (/usr/include/x86_64-linux-gnu/bits/stdio2.h:79)
          >>>               getopt.o:(_PyOS_GetOpt) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stderr'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by stdio2.h:79 (/usr/include/x86_64-linux-gnu/bits/stdio2.h:79)
          >>>               getopt.o:(_PyOS_GetOpt) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stderr'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by stdio2.h:79 (/usr/include/x86_64-linux-gnu/bits/stdio2.h:79)
          >>>               getopt.o:(_PyOS_GetOpt) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stderr'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by stdio2.h:79 (/usr/include/x86_64-linux-gnu/bits/stdio2.h:79)
          >>>               getopt.o:(_PyOS_GetOpt) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stderr'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by sysmodule.c:2032 (./Python/sysmodule.c:2032)
          >>>               sysmodule.o:(sys__debugmallocstats) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stderr'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by sysmodule.c:2036 (./Python/sysmodule.c:2036)
          >>>               sysmodule.o:(sys__debugmallocstats) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stderr'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by sysmodule.c:2033 (./Python/sysmodule.c:2033)
          >>>               sysmodule.o:(sys__debugmallocstats) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stdout'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by posixmodule.c.h:10410 (./Modules/clinic/posixmodule.c.h:10410)
          >>>               posixmodule.o:(os_get_terminal_size) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stderr'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by sysmodule.c:3529 (./Python/sysmodule.c:3529)
          >>>               sysmodule.o:(_PySys_Create) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stdout'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by sysmodule.c:3845 (./Python/sysmodule.c:3845)
          >>>               sysmodule.o:(PySys_WriteStdout) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stderr'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by sysmodule.c:3855 (./Python/sysmodule.c:3855)
          >>>               sysmodule.o:(PySys_WriteStderr) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stdout'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by sysmodule.c:3887 (./Python/sysmodule.c:3887)
          >>>               sysmodule.o:(PySys_FormatStdout) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stderr'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by sysmodule.c:3897 (./Python/sysmodule.c:3897)
          >>>               sysmodule.o:(PySys_FormatStderr) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'environ'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by posixmodule.c:1567 (./Modules/posixmodule.c:1567)
          >>>               posixmodule.o:(posixmodule_exec) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stdin'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by pegen_errors.c:352 (Parser/pegen_errors.c:352)
          >>>               pegen_errors.o:(RAISE_ERROR_KNOWN_LOCATION) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stdin'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by pegen_errors.c:352 (Parser/pegen_errors.c:352)
          >>>               pegen_errors.o:(_PyPegen_raise_error_known_location) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'stdin'; recompile with -fPIC
          >>> defined in /lib/x86_64-linux-gnu/libc.so.6
          >>> referenced by pythonrun.c:195 (Python/pythonrun.c:195)
          >>>               pythonrun.o:(PyRun_InteractiveOneObjectEx) in archive /workspaces/mpy/target/debug/deps/libpyo3_ffi-d2f9536bda801ee1.rlib
          
          rust-lld: error: too many errors emitted, stopping now (use --error-limit=0 to see all errors)
          collect2: error: ld returned 1 exit status
Your operating system and version

Ubuntu 24.04

Your Python version (python --version)

Python 3.12.1

Your Rust version (rustc --version)

rustc 1.91.0-nightly (040a98af7 2025-08-20)

Your PyO3 version

0.25.1

How did you install python? Did you use a virtualenv?

preinstalled

Additional Info

No response

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 by reproducing the failure with git clone https://github.com/bend-n/mpy followed by cargo test, then inspect the linker invocation and the libpyo3_ffi objects named in the error. Done means using PyO3 inside a proc-macro or other rlib links successfully rather than producing the reported relocation errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.