<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/57207>57207</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
LLD selects symbol from later dynamic library over earlier archive, for LTO-generated libcall
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
rprichard
</td>
</tr>
</table>
<pre>
On Android, both the builtins archive and libc.so expose `__aeabi_uidiv`. Normally, when generating a shared library, LLD links `__aeabi_uidiv` from the builtins archive because it comes first on the linker command-line. However, when the only reference to `__aeabi_uidiv` results from LTO code generation, then the linker instead prefers the `__aeabi_uidiv` from libc.so. Is this inconsistency by design?
See https://gist.github.com/rprichard/744ec9488cff0a39059bcf2ad3e73cda#file-demo1-sh, or Google-internal issue http://b/241259844. (Weird things also happen if the `__aeabi_uidiv` in the builtins archive is LLVM bitcode. See demo2.sh on the gist.)
```
llvm=/x/clang14
cc="$llvm/bin/clang -target armv7a-linux-androideabi32"
ar=$llvm/bin/llvm-ar
readelf=$llvm/bin/llvm-readelf
cat >mylibc.c <<EOF
unsigned int __aeabi_uidiv(unsigned int x, unsigned int y) {
return 42; // approximation
}
EOF
$cc mylibc.c -fpic -c
rm -f mybuiltins.a && $ar rcs mybuiltins.a mylibc.o
$cc mylibc.o -nostdlib -fpic -shared -fuse-ld=lld -o libmylibc.so
cat >somelib.c <<EOF
unsigned long long __aeabi_uidiv(unsigned int x, unsigned int y);
unsigned long long foo(unsigned int x, unsigned int y) {
#if defined(IMPLICIT)
return x / y;
#elif defined(EXPLICIT)
return __aeabi_uidiv(x, y);
#else
#error must define IMPLICIT or EXPLICIT
#endif
}
EOF
for src in EXPLICIT IMPLICIT; do
for lto in "" -flto -flto=thin; do
echo "### Compile flags: -D$src $lto"
$cc -O2 -nostdlib -fuse-ld=lld somelib.c -fpic -shared -o libsomelib.so mybuiltins.a libmylibc.so -D$src $lto
($readelf -s libsomelib.so | grep __aeabi_uidiv) || true
echo
done
done
```
Output:
```
### Compile flags: -DEXPLICIT
2: 000011c0 24 FUNC GLOBAL DEFAULT 6 __aeabi_uidiv
11: 000011c0 24 FUNC GLOBAL DEFAULT 6 __aeabi_uidiv
### Compile flags: -DEXPLICIT -flto
2: 000011bc 24 FUNC GLOBAL DEFAULT 6 __aeabi_uidiv
11: 000011bc 24 FUNC GLOBAL DEFAULT 6 __aeabi_uidiv
### Compile flags: -DEXPLICIT -flto=thin
2: 000011bc 24 FUNC GLOBAL DEFAULT 6 __aeabi_uidiv
11: 000011bc 24 FUNC GLOBAL DEFAULT 6 __aeabi_uidiv
### Compile flags: -DIMPLICIT
2: 000011d0 24 FUNC GLOBAL DEFAULT 6 __aeabi_uidiv
11: 000011d0 24 FUNC GLOBAL DEFAULT 6 __aeabi_uidiv
### Compile flags: -DIMPLICIT -flto
1: 00000000 0 FUNC GLOBAL DEFAULT UND __aeabi_uidiv
9: 00000000 0 FUNC GLOBAL DEFAULT UND __aeabi_uidiv
### Compile flags: -DIMPLICIT -flto=thin
1: 00000000 0 FUNC GLOBAL DEFAULT UND __aeabi_uidiv
9: 00000000 0 FUNC GLOBAL DEFAULT UND __aeabi_uidiv
```
`__aeabi_uidiv` is linked from the archive either when (a) the `__aeabi_uidiv` call is explicit in the source (so LTO notices it early on?), or (b) when LTO is turned off.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzVV19zozYQ_zT4ZQcGBDb2gx8SO7lmxnfpTHNt326EELZaIXkkyMXfvisBjvHZ10kmD60HA9KufvtXi7bQ5WH5qOBGlUaLMiArKHSzg2bHoWiFbISyQA3biWcOVJUgRcEiq4G_7LXlEMzib98op4X41opSPOM4gi_a1FTKg0P7vuMKtlxxQxFrCxTsjhrugQw1nmezWeNQ_W0vwUFldH1Zn4Iz2qISogGma26hEsY2oJVnd4jcOEqNioc45BH8or_zZ26OmjlGreQBDK-44YpxaPRFNQy3rWxsp87m6RGBS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