System specs:
$ uname -a
Linux 5.4.0-66-generic #74-Ubuntu SMP Wed Jan 27
22:54:38 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
$ gcc -v
...
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)
$ inxi
CPU: Quad Core Intel Core i7-1065G7 (-MT MCP-) speed/min/max: 1002/400/3900 MHz
Kernel: 5.4.0-66-generic x86_64 Up: 6d 7h 59m Mem: 4831.2/7730.1 MiB (62.5%)
Storage: 476.94 GiB (28.7% used) Procs: 349 Shell: bash 5.0.17 inxi: 3.0.38
I am trying to use IFUNC with lazy binding, but no matter what I do, the resolver keeps running before main is called - Which means that the resolver runs before runtime.
There is no BIND_NOW symbol in the target ELF.
LD_BIND_NOW isn't set (I tried to do the same while binding it to 0).
I can't see any reason in the documentation about why this is happening.
main.c:
#include <stdio.h>
#include <stdlib.h>
int foo_1() { return 1; }
int foo_2() { return 2; }
int foo_3() { return 3; }
extern int foo(void);
int foo(void) __attribute__((ifunc ("resolve_foo")));
int main() {
printf("main started\n");
printf("foo() = %d\n", foo());
return 0;
}
static void *resolve_foo(void) {
int res = 0;//atoi(getenv("FOO"));
printf("resolver started\n");
if (res == 1)
return foo_1;
if (res == 2)
return foo_2;
return foo_3;
}
Executing:
$ gcc -zlazy -o t main.c
$ ./t
resolver started
main started
foo() = 3