Mold Linker Version 3.0.0 Release – Rewritten in Rust
https://github.com/rui314/mold/releases/tag/v3.0.0📌 高速連結器 mold 3.0:從 C++ 全面改寫成 Rust
TL;DR:mold linker 的首個 Rust 版本正式發布,效能維持不變,相容性朝 GNU ld 再靠近一步。
一個以「快」聞名的連結器,為什麼要冒著效能風險把整套 C++ 程式碼重寫成 Rust?mold 3.0.0 的發布說明給出了答案:這不是為了炫技,而是為了讓專案能走得更遠。
🤔 mold 的目標:取代 GNU ld 成為 Linux 預設連結器
mold 是知名的高速連結器(linker)。根據發布說明,mold 2.42.1 是 C++ 版本的最後一版,而 3.x 系列的目標是補齊與 GNU ld 之間剩餘的相容性缺口(特別是 linker script 支援),並為 mold 成為 Linux 發行版預設連結器鋪路。要達成這個目標,專案選擇了一條大膽路線:整個重寫成 Rust。
🧩 「Drop-in replacement」:命令列、架構、輸出都不變
mold 3.0 被定位為 2.42.1 的 drop-in replacement(直接替換):接受相同的命令列選項,支援相同的目標架構,產出相同的輸出內容(除了本版修復的錯誤)。連結效能也與 2.42.1 相當。作者團隊表示,他們在所有支援的目標架構上執行測試套件,比對大量真實世界工作負載與選項組合下的連結器輸出,並建置了所有 Gentoo 套件,結果沒有發現回歸(regression)。
改寫成 Rust 帶來的額外好處是記憶體安全:面對損壞的輸入檔案,C++ 版本可能發生記憶體越界讀取而直接 segfault;Rust 版本因為有邊界檢查,遇到同樣的錯誤存取會改以 panic 的方式停止,而不是未定義行為的崩潰。
建置方式也因此改變:mold 現在用 Cargo 取代 CMake 建置,需要 Rust 1.95 以上版本與一個 C 編譯器,執行 cargo build --release 建置、./install-mold.sh 安裝(支援 PREFIX 與 DESTDIR 參數)。CMake 的選項全數移除,原本的 MOLD_TARGETS CMake 變數改為 Cargo features。它不再依賴 oneTBB,但仍靜態連結 mimalloc 3.5.3(可用 --features system-allocator 改用系統 malloc),並視系統是否有 zlib 決定是否連結系統版本,同時內建 zstd 與 BLAKE3。測試套件也從 ctest 改為 cargo test。
📊 一長串的 bug 修復與架構支援擴充
除了語言遷移,這個版本也修了相當多邊緣案例,例如:使用 version script 或 --default-symver 建立靜態連結執行檔時的崩潰;輸出檔案同時是輸入檔案時(mold -r -o foo.o foo.o bar.o)的崩潰或輸出損壞;--gc-sections 誤刪 --init/--fini 指定函式的問題;以及多項在 AArch64、ARM32、RISC-V、LoongArch、PPC32/64、m68k、SH4 等架構上的 relocation 修正。另外,mold 不再於啟動時保留 8 GiB 虛擬位址空間,因此可以在 ulimit -v 限制下正常運作。
💡 為什麼這個重寫值得注意
對連結器這種工具鏈底層基礎設施而言,「效能不退步、相容性變好、記憶體安全變強」是很難同時滿足的三個目標。mold 團隊選擇用 Rust 的邊界檢查取代 C++ 手動記憶體管理,換來的是面對惡意或損壞輸入時更可預期的失敗模式(panic 而非未定義行為),這對被整合進編譯器工具鏈、CI 系統的基礎工具來說是實質的風險降低。
⚠️ 升級前需注意建置流程的變動
如果你是自行從原始碼建置 mold 的使用者,升級到 3.0 需要改用 Cargo 工具鏈,並重新確認 MOLD_LIBDIR 等環境變數設定(若將函式庫安裝在非預設路徑,如 /usr/lib64)。這對仰賴既有 CMake 建置腳本的發行版維護者來說,是需要額外投入的遷移成本。
🎯 給工程師的啟示:工具鏈的語言遷移是可行路徑
mold 3.0 的案例提供了一個具體範本:效能敏感的底層基礎設施,也可以在不犧牲效能與相容性的前提下,逐步從 C++ 遷移到 Rust。如果你的團隊在維護類似的系統工具,這份發布說明中詳盡的相容性驗證方法(跨架構測試、真實世界套件建置比對)值得參考。
🔗 來源
- 標題:Mold Linker Version 3.0.0 Release – Rewritten in Rust
- 作者/機構:rui314(Hacker News 轉載 GitHub Release)
- 連結:https://github.com/rui314/mold/releases/tag/v3.0.0
#Rust #Linker #SystemsProgramming #OpenSource #CompilerToolchain #MemorySafety #Linux #DevTools #PerformanceEngineering #GitHub
原始資料 Hacker News · 收集於 2026-10-05
摘要原文
rui314 / mold Public Uh oh! There was an error while loading. Please reload this page . Notifications You must be signed in to change notification settings Fork 571 Star mold 3.0.0 Latest Latest Compare Choose a tag to compare Sorry, something went wrong. Filter Loading Sorry, something went wrong. Uh oh! There was an error while loading. Please reload this page . No results found View all tags rui314 released this 05 Oct 10:23 · 11 commits to main since this release v3.0.0 8de38c3 mold 3.0.0 is a new major release of the high-speed linker. As we announced in the 2.42.1 release notes , we have rewritten mold from C++ to Rust, and this is the first release of the Rust version. 2.42.1 is the last release of the C++ version. The goal of mold 3.x is to close the remaining compatibility gaps with GNU ld, particularly in linker script support, and to pave the way for mold to be adopted as the default linker in Linux distributions. mold 3.0 is meant to be a drop-in replacement for 2.42.1. It accepts the same command-line options, supports the same target architectures, and produces the same output except for the bug fixes listed below. Linking performance remains on par with 2.42.1. We verified compatibility by running the test suite on every supported target, comparing linker output across a broad range of real-world workloads and option combinations, and building all Gentoo packages. We found no regressions. The move to Rust also makes mold safer with corrupted input files. The C++ version could read memory out of bounds on such input and crash with a segmentation fault. In mold 3.0, those reads are bounds-checked, so mold stops with a panic at the faulty access instead. If you build mold yourself, note that the build system has changed. See "Build Changes" below. Build Changes mold is now built with Cargo instead of CMake. It requires Rust 1.95 or later and a C compiler. Run cargo build --release to build mold and ./install-mold.sh to install it; the install script accepts PREFIX and DESTDIR . The CMake options have been removed. If you install mold's libraries into a directory other than $PREFIX/lib , such as /usr/lib64 , set the MOLD_LIBDIR environment variable to that directory both when building and when installing mold, so that mold -run can find mold-wrapper.so . mold no longer depends on oneTBB . It statically links mimalloc 3.5.3 as before; build with --features system-allocator to use the system's malloc instead. mold links the system's zlib if available and includes zstd and BLAKE3; set ZSTD_SYS_USE_PKG_CONFIG=1 to link the system's zstd. ( 56ee0f8 ) The MOLD_TARGETS CMake variable has been replaced with Cargo features. As before, distributions should build mold for all targets. The test suite now runs with cargo test instead of ctest . install-build-deps.sh has been replaced with install-test-deps.sh , which you need only to run the tests. Bug Fixes and Compatibility Improvements Fixed a crash when creating a statically-linked executable with a version script or --default-symver . ( f10b0c4 ) Fixed a crash or a corrupted output when the output file is also an input file, as in mold -r -o foo.o foo.o bar.o . ( d2b88ab ) --gc-sections no longer removes the functions given by --init and --fini . ( c9e4cc4 ) Common symbols of the same name with different sizes now get the largest size and the strictest alignment, as in GNU ld and lld. ( d5781d5 ) --icf=safe no longer folds functions exported from a shared library, and --icf=all no longer crashes with --emit-relocs . ( ceab12c , 3dcd1df ) A versioned reference such as foo@VER in a shared library no longer pulls in an archive member that defines an unversioned foo . ( #1657 ) ( 44bfa2a ) Fixed several cases of nondeterministic output, in --dependency-file , --repro , -r with many COMDAT groups of the same name, and some dynamic relocations. ( 5164561 , 6d33903 , 51e23d4 , 696ed71 ) Fixed several -r bugs: C++ exceptions broke in code from the second and later input files; -x , -X and -s removed symbols that relocations referred to; --gc-sections dropped data that relocations referred to; sh_link of SHF_LINK_ORDER sections such as .ARM.exidx was not set; and mold crashed with --strip-debug and DWARF type units. ( 0e41bb9 , 3717460 , b2c3d1d , 9aacb62 , a247ca2 , e41ed90 ) GOT-relative relocations such as R_X86_64_GOTOFF64 , and R_ARM_REL32 , against symbols defined in shared libraries silently got wrong addresses. They are now handled correctly or reported as errors. ( #1668 ) ( 659780e , 7fe68bf ) Errors for PC-relative relocations that can't be used in position-independent output now explain the cause and how to fix it. ( 39e6a01 , 17b7b85 ) The following now cause errors instead of a broken output or a crash: --defsym aliasing a symbol defined in a shared library, an undefined non-weak hidden symbol in a shared library or with --unresolved-symbols=ignore-all , a --defsym value that doesn't fit in 64 bits, an allocated section that is also compressed, and --strip-all with --emit-relocs . ( 56b862b , b3566fa , e3a89f2 , a980054 , 8233a75 ) --noinhibit-exec no longer crashes on references to symbols in discarded COMDAT groups. ( 8f17166 , 80baa5d ) Options starting with a single dash are now read as GNU ld reads them. For example, -entry=main was read as -e ntry=main . ( #1671 , #1670 ) ( 830a860 ) --dynamic-list-data , --lto-pseudo-probe-for-profiling and --thinlto-index-only no longer consume the next argument, and --no-color-diagnostics is now accepted. ( 2ca893e , 04b493e ) If a version script lists a symbol in more than one version node, the first one now takes precedence, as in GNU ld and lld. ( b7048f5 ) Fixed relative paths with -C , --chroot and --repro . ( e3bc349 , 04e7dd8 , a368d22 ) mold no longer reserves 8 GiB of virtual address space at startup, so it works under ulimit -v . ( cc69390 , b18be91 ) MOLD_JOBS now works if XDG_RUNTIME_DIR is an empty string. ( 8a360ff ) Files created by --separate-debug-file no longer contain a .gnu_debuglink section. ( #1656 ) ( b56649e ) mold no longer creates the meaningless __start_EHDR , __stop_EHDR , __start_PHDR and __stop_PHDR symbols. ( 0ee71d3 ) [AArch64][PPC32][ARM32] Fixed calls to static functions in other sections through range extension thunks, which jumped to wrong addresses in large programs such as the ARM64 debug build of Chromium. ( 4165d0e , 9b91621 ) [AArch64] Added support for more relocation types, such as R_AARCH64_TSTBR14 and R_AARCH64_GOT_LD_PREL19 . ( 96d0282 , a4b64d8 ) [ARM32] Added support for more relocation types, such as R_ARM_ALU_PC_G0 and R_ARM_THM_PC12 , and fixed -r output for big-endian ARM. ( c0f9cd6 , 4c9aad3 , 4c26f16 ) [ARM32][i386] -r no longer corrupts instructions referred to by some branch and TLS relocations. ( 1b17bdf ) [RISC-V][LoongArch] Fixed calls to functions folded by --icf=all when relaxation shrinks them, and R_RISCV_64 and R_LARCH_64 relocations in 32-bit objects. ( 52febe9 , 5b22233 ) [RISC-V] Fixed TLSDESC with object files created by Clang 18, --emit-relocs with TLSDESC relaxation, the EF_RISCV_TSO flag, R_RISCV_ALIGN in -r output, and overflow checks for 32-bit PC-relative relocations. ( fdf8422 , 7c8b805 , 6b60f75 , 4ba7792 , 85d55f3 ) [LoongArch] Added support for TLSDESC in the extreme code model, and fixed wrong values of *64_PC_HI12 relocations and spurious overflow errors for TLS relocations. ( a36a161 , 8133938 , 13e6cc0 ) [PPC32][m68k] A GOT too large for the 16-bit offsets that -fpic code uses is now reported as an error instead of being silently truncated. ( acc8854 , 614e2a0 ) [PPC64] Fixed IFUNC calls in statically-linked executables and @got references in hand-written assembly, and mold now synthesizes the _savegpr0_* family of functions for PPC64V1 as well. ( d3b2a74 , b34a308 , efda2fc ) [SH4] Fixed a crash when calling a function that returns a structure through the PLT, and C++ exceptions in programs linked from -r output. (226 points, 154 comments on Hacker News)
由 claude-code/sonnet 自動生成