I’m trying to figure out whether IFunc requires OS support or not. I have read elsewhere that at least for GCC, this function only works on platforms with glib. There is very little documentation in the LLVM reference, so could someone shed some light on what is required to make it work?
It requires your OS’s C implementation (dynamic loader and C startup files) to support IRELATIVE relocations and STT_GNU_IFUNC symbols. On Linux, that typically means glibc, which does, but musl does not (yet). On FreeBSD it’s also supported, but NetBSD and OpenBSD appear to not support it. macOS and Windows don’t use ELF so don’t have IFUNCs at all, but may have equivalents.
I only know implementations in glibc and FreeBSD rtld, and Android bionic.
For a portable alternative, use a (global) function pointer:
Using a global function pointer offers more flexibility:
- It can be initialized at any appropriate time. The relocation resolving time ifunc is subject to static initialization order fiasco.
- The resolver can have custom arguments, not the fixed ones provided by the dynamic loader.
Thanks, that post was extremely interesting and useful to read.