Relocation avoidance for function pointers

Florian Weimer fweimer@redhat.com
Fri Oct 7 15:51:00 GMT 2016


It is possible to compute the difference between two labels and store 
the result in a variable.  For PIC code, this can be used to avoid 
relocations in jump tables.

I try to do something similar for function pointers.  (Function pointer 
arithmetic is a GCC extension.)  However, GCC rejects that with “error: 
initializer element is not constant”.  Is there any way around this? 
The required relocation should be the same (for static functions anyway).

Here's what I've got so far:

int f (void);

static int
g (void)
{
   static const int label_offset = &&a - &&b;
  a:
   f ();
  b:
   return label_offset;
}

static int
h (void)
{
   static const long function_offset = &f - &g;
   return function_offset;
}

int
g_wrapper (void)
{
   return g ();
}

int
h_wrapper (void)
{
   return h ();
}


Thanks,
Florian



More information about the Gcc-help mailing list