You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在macOS上为libffi.8.dylib签名?解决签名后Rosetta报错问题

Pyinstaller签名后Rosetta加载libffi.dylib报错解决方案及签名疑问解答

问题概述

使用Pyinstaller将Python应用打包为Unix可执行文件时,未签名状态运行正常,但签名后执行出现以下Rosetta错误:

rosetta error: /var/db/oah/526469a4f4887ee6ca553807c3196c9533da7ef706c684764b83169e8658f8e2/ba3f613b849de777b4a89fdb1760c3c2343ac52546e2629e28f83fbf530a8f27/libffi.8.dylib.aot: unable to mmap __TEXT: 1

疑问:能否对此类dylib文件进行代码签名?

报错复现步骤

  • 创建Python 3.9版本的conda环境
  • 激活环境并安装numpy和pyinstaller
  • 创建仅包含import numpy的Python脚本
  • 执行打包并签名命令:
    pyinstaller -y --clean --codesign-identity='<apple-id>' --osx-entitlements-file='<path-to-entitlements.plist-file>' <name-of-python-script>
    
  • 运行dist文件夹中生成的Unix可执行文件触发报错

核心解答与解决方案

1. 关于.aot文件的签名问题

这类.aot文件是Rosetta 2在运行x86_64架构应用时自动生成的动态翻译缓存文件,不属于打包的应用资产,不需要也不应该手动签名。系统会自动管理这类缓存的生成与加载,手动签名反而会破坏其正常加载逻辑。

2. 报错原因

签名后的应用受苹果代码签名机制限制,默认不允许加载未签名的系统临时缓存文件,导致Rosetta无法读取生成的libffi.8.dylib.aot缓存,进而触发mmap错误。

3. 解决办法

方法一:调整签名权限(推荐)

编辑你的entitlements.plist文件,添加以下权限,允许应用加载系统生成的未签名缓存:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.security.cs.disable-library-validation</key>
    <true/>
    <key>com.apple.security.cs.allow-unsigned-executable-memory</key>
    <true/>
</dict>
</plist>

重新执行打包签名命令即可。

方法二:拆分打包与签名流程

避免使用Pyinstaller内置的签名参数,先完成打包,再手动递归签名应用包:

  1. 仅打包不签名:
    pyinstaller -y --clean <name-of-python-script>
    
  2. 手动签名应用:
    codesign --force --deep --sign "<apple-id>" --entitlements "<path-to-entitlements.plist-file>" dist/<your-app-name>
    

方法三:使用原生架构环境(彻底解决)

如果在Apple Silicon机器上,直接创建arm64架构的conda环境,避免依赖Rosetta翻译:

  1. 创建arm64架构的Python 3.9环境:
    CONDA_SUBDIR=osx-arm64 conda create -n py39-arm python=3.9
    
  2. 激活环境后安装numpy和pyinstaller,打包生成原生arm64应用,无需Rosetta即可运行,彻底规避缓存加载问题。

内容的提问来源于stack exchange,提问作者Michael Pesce

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 22:15:39