[ThinLTO][Mach-O] IR symbol resolution depends on input order
Summary
Two different IR names can describe the same Mach-O symbol: normal @symbol and verbatim @"\01_symbol" both produce _symbol. If the reference module is processed before the prevailing definition, ThinLTO can internalize the definition and fail to resolve the reference. Reversing the input order works.
This was found in a bindgen generated reference to a C++ function.
Environment and affected version
- Target:
arm64-apple-macosx13.0.0. - Reproducer validation host: Apple Silicon, macOS 26.7 (25G229).
- LLVM:
24.0.0git, unpatched upstream main, Release with assertions. - Tested commit:
a33341a459a53c7096157440d6cd2c03bc02e3e7(2026-10-05 23:39:24 UTC)
Steps
Run in an empty directory with upstream LLVM tools on PATH:
cat > definition.cc <<'EOF'
__attribute__((noinline, visibility("hidden")))
int symbol(int value) {
return value;
}
EOF
cat > reference.cc <<'EOF'
extern int symbol(int) asm("__Z6symboli");
int main() {
return symbol(2) == 2 ? 0 : 1;
}
EOF
clang++ --target=arm64-apple-macosx13.0 -O2 -fvisibility=hidden \
-flto=thin -c definition.cc -o definition.o
clang++ --target=arm64-apple-macosx13.0 -O2 -fvisibility=hidden \
-flto=thin -c reference.cc -o reference.o
ld64.lld -dylib -arch arm64 -platform_version macos 13.0 13.0 \
-u _main reference.o definition.o -o reference-first.dylib
ld64.lld -dylib -arch arm64 -platform_version macos 13.0 13.0 \
-u _main definition.o reference.o -o definition-first.dylib
The reference first link fails:
ld64.lld: error: undefined symbol: __Z6symboli
The definition first link succeeds. Recompile both files without -flto=thin and both link orders succeed.
Reduced IR reproducer
Run in an empty directory with LLVM tools on PATH:
cat > definition.ll <<'EOF'
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
target triple = "arm64-apple-macosx13.0.0"
define hidden i32 @symbol(i32 %value) noinline {
ret i32 %value
}
EOF
cat > reference.ll <<'EOF'
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
target triple = "arm64-apple-macosx13.0.0"
declare i32 @"\01_symbol"(i32)
define i32 @main() {
%result = call i32 @"\01_symbol"(i32 2)
ret i32 %result
}
EOF
opt -module-summary definition.ll -o definition.bc
opt -module-summary reference.ll -o reference.bc
llvm-lto2 run reference.bc definition.bc -o reference-first \
-r=reference.bc,_main,plx -r=reference.bc,_symbol,l \
-r=definition.bc,_symbol,pl --select-save-temps=precodegen
llvm-dis reference-first.2.5.precodegen.bc -o reference-first.ll
grep 'define.*symbol' reference-first.ll
llvm-lto2 run definition.bc reference.bc -o definition-first \
-r=reference.bc,_main,plx -r=reference.bc,_symbol,l \
-r=definition.bc,_symbol,pl --select-save-temps=precodegen
llvm-dis definition-first.1.5.precodegen.bc -o definition-first.ll
grep 'define.*symbol' definition-first.ll
Actual behavior
Reference first processing loses the externally resolvable definition: the saved definition module pre-codegen IR no longer contains a definition of symbol. Definition first processing retains the hidden externally resolvable definition.
To observe the user facing link failure on the same inputs:
ld64.lld -dylib -arch arm64 -platform_version macos 13.0 13.0 \
-u _main reference.bc definition.bc -o reference-first.dylib
ld64.lld -dylib -arch arm64 -platform_version macos 13.0 13.0 \
-u _main definition.bc reference.bc -o definition-first.dylib
The first link fails with an undefined _symbol; the reversed order links.
Expected behavior
Both orders should retain a resolvable definition and link successfully. In the saved definition module IR, symbol should remain a hidden definition, not become an internal definition.
Additional information
In llvm/lib/LTO/LTO.cpp, LTO::addModuleToGlobalRes contains an existing workaround for different IR names mapping to the same linker symbol. However, assigning the prevailing definition's IR name can overwrite the earlier reference name before the mismatch is tested.
can this issue be assigned?