https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111605
Bug ID: 111605
Summary: Cross compilation doesn't work with `-fuse-ld=mold`
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: rui314 at gmail dot com
Target Milestone: ---
When I pass `-fuse-ld=mold` to a gcc cross compiler, gcc searches for `ld.mold`
from the crosstool's bin directories and then looks for `<triple>-ld.mold` from
$PATH. Here is a gcc's strace when invoked with `-fuse-ld=mold`.
[pid 3905484] newfstatat(AT_FDCWD,
"/usr/lib/gcc-cross/riscv64-linux-gnu/11/ld.mold", 0x7ffdd5a491a0, 0) = -1
ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/usr/lib/gcc-cross/riscv64-linux-gnu/11/ld.mold", 0x7ffdd5a491a0, 0) = -1
ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/usr/lib/gcc-cross/riscv64-linux-gnu/ld.mold", 0x7ffdd5a491a0, 0) = -1 ENOENT
(No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/usr/lib/gcc-cross/riscv64-linux-gnu/11/ld.mold", 0x7ffdd5a491a0, 0) = -1
ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/usr/lib/gcc-cross/riscv64-linux-gnu/ld.mold", 0x7ffdd5a491a0, 0) = -1 ENOENT
(No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/usr/lib/gcc-cross/riscv64-linux-gnu/11/../../../../riscv64-linux-gnu/bin/ld.mold",
0x7ffdd5a491a0, 0) = -1 ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/home/ruiu/.cargo/bin/riscv64-linux-gnu-ld.mold", 0x7ffdd5a491a0, 0) = -1
ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/home/ruiu/.local/bin/riscv64-linux-gnu-ld.mold", 0x7ffdd5a491a0, 0) = -1
ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD, "/home/ruiu/bin/riscv64-linux-gnu-ld.mold",
0x7ffdd5a491a0, 0) = -1 ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/home/ruiu/.cabal/bin/riscv64-linux-gnu-ld.mold", 0x7ffdd5a491a0, 0) = -1
ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/home/ruiu/prefix/bin/riscv64-linux-gnu-ld.mold", 0x7ffdd5a491a0, 0) = -1
ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/home/ruiu/bin/qemu/bin/riscv64-linux-gnu-ld.mold", 0x7ffdd5a491a0, 0) = -1
ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD, "/usr/lib/ccache/riscv64-linux-gnu-ld.mold",
0x7ffdd5a491a0, 0) = -1 ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD, "/home/ruiu/bin/riscv64-linux-gnu-ld.mold",
0x7ffdd5a491a0, 0) = -1 ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/home/ruiu/golang/bin/riscv64-linux-gnu-ld.mold", 0x7ffdd5a491a0, 0) = -1
ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/home/ruiu/.mozbuild/node/bin/riscv64-linux-gnu-ld.mold", 0x7ffdd5a491a0, 0) =
-1 ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/home/ruiu/bin/depot_tools/riscv64-linux-gnu-ld.mold", 0x7ffdd5a491a0, 0) = -1
ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD, "/usr/local/sbin/riscv64-linux-gnu-ld.mold",
0x7ffdd5a491a0, 0) = -1 ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD, "/usr/local/bin/riscv64-linux-gnu-ld.mold",
0x7ffdd5a491a0, 0) = -1 ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD, "/usr/sbin/riscv64-linux-gnu-ld.mold",
0x7ffdd5a491a0, 0) = -1 ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD, "/usr/bin/riscv64-linux-gnu-ld.mold",
0x7ffdd5a491a0, 0) = -1 ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD, "/sbin/riscv64-linux-gnu-ld.mold",
0x7ffdd5a491a0, 0) = -1 ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD, "/bin/riscv64-linux-gnu-ld.mold",
0x7ffdd5a491a0, 0) = -1 ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD, "/usr/games/riscv64-linux-gnu-ld.mold",
0x7ffdd5a491a0, 0) = -1 ENOENT (No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD,
"/usr/local/games/riscv64-linux-gnu-ld.mold", 0x7ffdd5a491a0, 0) = -1 ENOENT
(No such file or directory)
[pid 3905484] newfstatat(AT_FDCWD, "/snap/bin/riscv64-linux-gnu-ld.mold",
0x7ffdd5a491a0, 0) = -1 ENOENT (No such file or directory)
This behavior is appropriate for BFD linker which has one executable for each
cross target. However, it's incorrect for the mold linker because mold supports
all targets by a single executable. Therefore, when gcc searches mold from
$PATH, it should look for just `ld.mold`, without the triple.
The same is true for LLD.
The link to the upstream bug: https://github.com/rui314/mold/issues/1114