public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Linker : Make a map of typeinfo to vtable
@ 2024-01-19 22:29 Frederick Virchanza Gotham
  2024-01-19 22:52 ` Frederick Virchanza Gotham
  2024-01-22 16:50 ` Michael Matz
  0 siblings, 2 replies; 9+ messages in thread
From: Frederick Virchanza Gotham @ 2024-01-19 22:29 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 2236 bytes --]

Over on the mailing list for new proposals for the C++ programming
language, I am proposing that we be able to get a pointer to a class's
vtable from its typeinfo.

So I want to edit the source code for the GNU linker in order to add two
new symbols to every binary:

    (1) __map_typeinfohash_vtable
    (2) __map_typeinfohash_vtable_size

The first one is an array mapping the hashes of type_info's to vtable
pointers. The second one is the number of elements in the array. The array
would look something like the following in C++:

    pair<size_t, void const*> const g_vtables[] = {
         { typeid(MyClass).hash_code(), &_ZTI7MyClass },
         { typeid(YourClass).hash_code(), &_ZTI9YourClass },
    };

Next I can write a function called 'std::get_polymorphic_facilitator' which
would
perform a binary search through this array, in order to convert a typeinfo
to a vtable pointer. So if a massive program like Chromium has 6,000
classes, then a worst case binary search would be about
13 checks. On a desktop PC that's less than a microsecond.

Afterward I would fork libstdc++ to edit the <typeinfo> standard header
file to add the following inline function:

    inline void const *get_polymorphic_facilitator(type_info const &ti)
    {
        extern pair<size_t,void const*> const *const
__map_typeinfohash_vtable;

        extern size_t const __map_typeinfohash_vtable_size;

        return lower_bound( p, p + __map_typeinfohash_vtable_size,
ti.hash_code(), OnlyCompareFirst()
)->second;
    }

But first of all I need to edit the source code for 'ld' so that, after it
has generated all the vtables and typeinfo's for every class, I can then
create my aforementioned array which maps typeinfo's to vtables. Or,
instead of waiting until after all the vtables and typeinfo's have been
generated, I can populate my array as and when these things are generated
(I imagine this might be easier than waiting until they're all generated,
but whatever works).

I've never edited a compiler or linker before, so could I please ask for a
little guidance here? Could someone point me toward the right source file
for 'ld' where I should start making changes to implement this? I would
appreciate any tips you can give me please.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-01-27 10:56 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-19 22:29 Linker : Make a map of typeinfo to vtable Frederick Virchanza Gotham
2024-01-19 22:52 ` Frederick Virchanza Gotham
2024-01-20 19:44   ` Frederick Virchanza Gotham
2024-01-21  3:27     ` Frederick Virchanza Gotham
2024-01-21 22:35       ` Frederick Virchanza Gotham
2024-01-22 16:50 ` Michael Matz
2024-01-25 10:17   ` Frederick Virchanza Gotham
2024-01-25 23:02     ` Frederick Virchanza Gotham
2024-01-27 10:56       ` Frederick Virchanza Gotham

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).