Does anyone know of a way of accessing and modifying variables declared
static within a function from outside that function? Please no
homilies on why it's bad practice: the context is very particular and
involves automatically generated code. I know several other ways of
attacking my problem, but this would be the cleanest if it could be made
to work.
A little more context. I use C as the output of a code generating
system which can, and frequently does, output very many functions that have a
consistent set of declarations, including variable names, at the top.
One of these is typically a static integer, call it "done", which causes
an immediate return if it has been set to 1, otherwise the function
proceeds. I take it that this is a well-known, trivial and banal piece
of code for permitting a function to run just once.
Now, however, circumstances have arisen where it might be desirable to
be able to reset this flag from outside the function in which it is
declared. Compiling, as I do, using gcc with -rdynamic, I am routinely
able, using dlopen and dlsym, to find the address of a function by name.
I had (naively) imagined that dladdr() would allow me to get the addresses
of symbols declared static within functions, but as I understand it
dladdr() only accesses global symbols. So what I am looking for is a
method for finding the address of a static integer variable called
"done" declared within "function001", as opposed to all other static
integer variables called "done" declared within all the many other
similar functions, in such a way that I can change its value - so
just getting a copy won't do.
I am asking in this group rather than in a gcc specific place because in
principle this is not a gcc-specific issue and my software compiles
using other compilers with comparable tricks. The more generic and
surgical a solution the better. But my functions may have a variable and
unpredictable number of arguments.
Pointers, RTFMs with page references and philosophical musings all
welcome. But an illustrative code fragment would be perfect.
--
Steve Blinkhorn <st***@prd.co.uk>