Skip to content

Instantly share code, notes, and snippets.

@rprichard
Last active August 18, 2022 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rprichard/744ec9488cff0a39059bcf2ad3e73cda to your computer and use it in GitHub Desktop.
Save rprichard/744ec9488cff0a39059bcf2ad3e73cda to your computer and use it in GitHub Desktop.
Demonstrate LLVM behavior with LTO and libcalls
#!/bin/bash
#
# Both the builtins archive and libc.so expose __aeabi_uidiv. Normally, when
# generating a shared library, the linker 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.
set -e
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: -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
#
# ### 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
#!/bin/bash
#
# If the builtins library has LLVM bitcode, then the resulting DSO has an ABS
# 0-sized symbol for __aeabi_uidiv.
#
set -e
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 -flto
rm -f mybuiltins.a && $ar rcs mybuiltins.a mylibc.o
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 -fuse-ld=lld -nostdlib -O2 somelib.c -fpic -shared -o libsomelib.so mybuiltins.a -D$src $lto
($readelf --dyn-syms libsomelib.so | grep __aeabi_uidiv) || true
echo
done
done
# OUTPUT
#
# ### Compile flags: -DEXPLICIT
# 2: 000011b4 24 FUNC GLOBAL DEFAULT 6 __aeabi_uidiv
#
# ### Compile flags: -DEXPLICIT -flto
# 2: 000011b4 24 FUNC GLOBAL DEFAULT 6 __aeabi_uidiv
#
# ### Compile flags: -DEXPLICIT -flto=thin
# 2: 000011b0 24 FUNC GLOBAL DEFAULT 6 __aeabi_uidiv
#
# ### Compile flags: -DIMPLICIT
# 2: 000011c4 24 FUNC GLOBAL DEFAULT 6 __aeabi_uidiv
#
# ### Compile flags: -DIMPLICIT -flto
# 2: 00000000 0 NOTYPE GLOBAL DEFAULT ABS __aeabi_uidiv
#
# ### Compile flags: -DIMPLICIT -flto=thin
# 2: 00000000 0 NOTYPE GLOBAL DEFAULT ABS __aeabi_uidiv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment