This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: correct way of having a function pointer inside a GTY()-ed struct?
- From: Joern Rennecke <amylaar at spamcop dot net>
- To: Richard Guenther <richard dot guenther at gmail dot com>
- Cc: Basile STARYNKEVITCH <basile at starynkevitch dot net>, gcc Mailing List <gcc at gcc dot gnu dot org>
- Date: Sun, 22 Mar 2009 01:20:36 -0400
- Subject: Re: correct way of having a function pointer inside a GTY()-ed struct?
Basile STARYNKEVITCH:
> So my point is that I want to put inside the GTY-ed struct
basilysroutine_st
> an ignored (I mean GTY((skip))-ed) field called routaddr which is
a function
> pointer
> (function of type basilysroutfun_t - which is typedef-ed above).
>
> How can I achieve that, in the cleanest way possible?
Richard Guenther:
Just do it and fix whatever causes that not to work?!
This is likely to break if this struct gets saved as part of a precompiled
header, and for some instance of using this header, the function ends up in
a different place than during header compilation due to address space
randomization.
Compare also PR31634.
Possible solutions are:
i) Use an enum instead, and use a switch to call the appropriate function.
ii) if the switch is too slow, you might convert a pointer to an enum
before pch-saving, and convert back after reading the pch file.
Of course, if your function pointers can point into dynamically shared
objects (dsos)that are not automatically loaded when the compiler binary
starts, you have to worry about dso persistence first...
And having a pointer to a nested-function trampoline could also be very nasty.
I don't think it is possible to have a valid such trampoline when you do a pch
save, but can you rule that out for the future?
So, if the function pointer might need to be saved in a pch file, I think
it would be better to explicitly require the function pointer to only
assume values that you know can be restored with whatever scheme you use
to save/restore function pointers.