Build JIT Error on 3.13.1: KeyError: 6

Problem

I’m trying to build Python 3.13.1 with JIT compiler on Nix. Things work well on my Macintosh laptop after specifying llvm18 as the dependency. However, building JIT compiler on x86_64-linux fails due to KeyError exception, which means the index is out of range.

  + Exception Group Traceback (most recent call last):
  |   File "/tmp/nix-build-python3-3.13.1.drv-0/Python-3.13.1/./Tools/jit/build.py", line 28, in <module>
  |     args.target.build(pathlib.Path.cwd(), comment=comment, force=args.force)
  |   File "/tmp/nix-build-python3-3.13.1.drv-0/Python-3.13.1/Tools/jit/_targets.py", line 214, in build
  |     stencil_groups = asyncio.run(self._build_stencils())
  |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/nix/store/1lgnb8w6jd64lfvb6fwlg00m3nvpjar0-python3-minimal-3.12.7/lib/python3.12/asyncio/runners.py", line 194, in run
  |     return runner.run(main)
  |            ^^^^^^^^^^^^^^^^
  |   File "/nix/store/1lgnb8w6jd64lfvb6fwlg00m3nvpjar0-python3-minimal-3.12.7/lib/python3.12/asyncio/runners.py", line 118, in run
  |     return self._loop.run_until_complete(task)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/nix/store/1lgnb8w6jd64lfvb6fwlg00m3nvpjar0-python3-minimal-3.12.7/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
  |     return future.result()
  |            ^^^^^^^^^^^^^^^
  |   File "/tmp/nix-build-python3-3.13.1.drv-0/Python-3.13.1/Tools/jit/_targets.py", line 189, in _build_stencils
  |     async with asyncio.TaskGroup() as group:
  |                ^^^^^^^^^^^^^^^^^^^
  |   File "/nix/store/1lgnb8w6jd64lfvb6fwlg00m3nvpjar0-python3-minimal-3.12.7/lib/python3.12/asyncio/taskgroups.py", line 145, in __aexit__
  |     raise me from None
  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "/tmp/nix-build-python3-3.13.1.drv-0/Python-3.13.1/Tools/jit/_targets.py", line 181, in _compile
    |     return await self._parse(o)
    |            ^^^^^^^^^^^^^^^^^^^^
    |   File "/tmp/nix-build-python3-3.13.1.drv-0/Python-3.13.1/Tools/jit/_targets.py", line 89, in _parse
    |     self._handle_section(wrapped_section["Section"], group)
    |   File "/tmp/nix-build-python3-3.13.1.drv-0/Python-3.13.1/Tools/jit/_targets.py", line 330, in _handle_section
    |     value, base = group.symbols[section["Info"]]
    |                   ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
    | KeyError: 6
    +------------------------------------

Is there any bug in the _ELF target definition in current JIT implementation?

MRE

Here is an example Nix file for Python 3.13.1 with JIT compiler included:

# Python 3.13.1 with JIT compiler.
with import (builtins.fetchTarball "https://github.com/NixOS/nixpkgs/tarball/890e5f24567d6d940b14241c8e05cb28129e3c59") { };
let
  llvmPackages = llvmPackages_18;
in
python313.overrideAttrs (
  final: prev: {
    nativeBuildInputs = [
      llvmPackages.libllvm      # this must be prepended
      llvmPackages.clang
    ] ++ prev.nativeBuildInputs;

    env = prev.env // {
      PYTHON_FOR_REGEN = lib.getExe pkgsBuildBuild.python3Minimal;
    };

    configureFlags = prev.configureFlags ++ [ "--enable-experimental-jit=yes-off" ];
  }
)

Save this file to for example test-python313-jit.nix and then

nix-build ./test-python313-jit.nix

The build process works fine on macOS 15, but fails on my GNU/Linux x86_64 server.

I’ve debugged for a while and I find the following command may be useful:

nix-shell ./test-python313-jit.nix
runPhase unpackPhase
runPhase configurePhase # Generate pyconfig.h for generating jit_stencils.h
$PYTHON_FOR_REGEN -m pdb \
                  -c 'b _targets.py:89, wrapped_section["Section"]["Info"] > len(group.symbols)' \
                  -c 'c' ./Tools/jit/build.py -v x86_64-unknown-linux-gnu

PDB will probably break and if continue then the compilation will complain errors like above.

More information: build for Python 3.14.0a2 also succeeds on aarch64-darwin but fails on x86_64-linux due to the same exception.