kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

a.c (753B)


      1 /* STT_GNU_IFUNC: my_fn is dispatched via a runtime resolver.
      2  * impl_a returns 42, impl_b returns 99; resolve() picks impl_a so a
      3  * call to my_fn() must return 42 once the linker's iplt trampoline
      4  * is wired up and the slot is populated. */
      5 
      6 extern int impl_a(void);
      7 extern int impl_b(void);
      8 extern int (*resolve(void))(void);
      9 
     10 int impl_a(void) { return 42; }
     11 int impl_b(void) { return 99; }
     12 
     13 /* `volatile` keeps the compiler from constant-folding the choice and
     14  * inlining my_fn into a direct branch to impl_a (which would defeat
     15  * the trampoline test). */
     16 int (*resolve(void))(void) {
     17   volatile int x = 1;
     18   return x ? impl_a : impl_b;
     19 }
     20 
     21 int my_fn(void) __attribute__((ifunc("resolve")));
     22 
     23 int test_main(void) { return my_fn() == 42 ? 0 : 1; }