Using the PLT for vtables (or not)

Ian Lance Taylor ian@wasabisystems.com
Fri Dec 5 02:52:00 GMT 2003


Brian Ryner <bryner@brianryner.com> writes:

> On ELF, the vtable contains pointers to PLT entries.  I was wondering
> if anyone could comment on the reasons for constructing the vtable
> this way.  In particular, it seems like you could make virtual method
> calls more efficient (by avoiding a load and jump) if you put the
> vtable into writable memory, and initialized it with the real address
> of every method the first time an instance of a class is created.

ELF function calls to externally visible symbols are going to go
through the PLT unless you take special preventative measures.  What
you suggest isn't going to help: when you take the address of the
function to initialize the vtable, unless you take some special
action, you're going to get the address of the PLT.  So if it is OK to
take that special action, then you might as well take it when you
initialize the vtable at program start-up time.

The real question is whether it is OK to skip the PLT for vtable
entries.  Normally it is desirable to use the PLT because it permits
the main program to override calls made from within a shared library.
A vtable may be an exception to this, but that is not immediately
obvious to me.

Specifically, I could rewrite your paragraph to say something along
the lines of ``On ELF, function calls are made to the PLT.  This costs
a load or a jump for each function call.  If we know that the function
is defined in the same object, wouldn't it be better to call the
function directly?''

Ian



More information about the Gcc mailing list