How do I get the address of the current function?

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Fri Jun 29 21:14:39 AEST 1990


In article <90Jun29.013400edt.20194 at me.utoronto.ca>, writer at me.utoronto.ca (Tim Writer) writes:
> As I feared, you misunderstood me.  I want to *compute* the address of the
> currently executing function.  In other words, I want the computer to tell
> me what is the address of the function it is executing.

The things you are guaranteed about function pointers are
	(1) every function has a definite unique "address" which is not NULL
	(2) "all function pointers smell the same"
	(3) given the address of a function, you can call it.
That's about it.  In particular, there is no reason to expect the
"function address" used by C to be or to resemble any hardware address
within the machine code generated for the function.  C's "function pointer"
might be any of the following:
	(a) An address near the beginning of the machine code
	(b) An address near the end of the machine code
	(c) An index into an array of addresses or ECBs
	(d) The (data) address of an Entry Control Block (Apollo, PR1ME)
	(e) The address of an instruction which is initially a trap, and
	    then gets overwritten by a jump to dynamically loaded code
	(f) The address of a location somewhere in the literal pool for
	    a function (IBM RT PC)
	...
If the C compiler you're using goes in for literal pools, you may be
in luck.  The address of the literal pool is likely to be around in a
register.  I don't know whether SPARC compilers do this.

It's important to realise that you have *two* problems:
-- how does this COMPILER represent a function pointer?
-- how do I get one of those at run-time on this HARWARE?

-- 
"private morality" is an oxymoron, like "peaceful war".



More information about the Comp.lang.c mailing list