This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Relocation avoidance for function pointers
- From: Florian Weimer <fweimer at redhat dot com>
- To: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Fri, 7 Oct 2016 17:51:27 +0200
- Subject: Relocation avoidance for function pointers
- Authentication-results: sourceware.org; auth=none
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