Linker : Make a map of typeinfo to vtable
Frederick Virchanza Gotham
cauldwell.thomas@gmail.com
Thu Jan 25 10:17:46 GMT 2024
On Mon, Jan 22, 2024 at 4:50 PM Michael Matz wrote:
>
> When you want to get from typeinfo to vtable pointer, why not just add
> that pointer directly into the typeinfo? A hashtable is quite a
> roundabout way to implement 'typeid(Type).vtable()' .
That would be one option, yes it would be possible to edit the source
code for the GNU g++ compiler so that the type '__vmi_class_type_info'
has the vtable pointer appended onto the end of it, and then the
'__vmi_class_type_info::flags' member could have a bit set in it to
indicate the presence of the vtable pointer. There are two drawbacks
to this option though:
(1) It involves editing the GNU g++ compiler, and I'd rather edit
the linker because then it can be extended easily to other languages
(such as D)
(2) If we have multiple object files with a weak vtable symbol,
and if some of the weak symbols don't have the vtable pointer
appended, then a symbol missing the vtable pointer might become
'strong'
(2) It will be necessary to recompile entire programs, whereas if
I edit just the linker, only the final linking step will need to be
redone (which is a big deal if you have been given proprietary object
files and static archives)
So for the time being I'm ploughing on ahead with editing the linker.
Please help me to get this working, here's what I've got so far:
https://github.com/bminor/binutils-gdb/compare/f11786a1951932217d348f3739e8deb31975a355...healytpk:linker-vtable:master
Inside the file, "ld/ldmain.c", the function 'main' calls 'ldwrite'
which calls 'bfd_final_link'. After 'ldwrite' is called, I am able to
use the pointer "link_info.output_bfd" in order to get a list of all
symbols along with their offsets inside their sections. From this data
I'm able to produce my array which maps typeinfo's to vtables. But
there's one problem -- 'ldwrite' has already been called by the time I
produce my array. So, somehow, I need to call 'bfd_final_link' in
order get the offsets for the vtables inside their section, but then I
need to append my own array onto the end of the section, and then
perform the final link.
Can anybody please point me in the right direction for how to do this?
Can I possibly make a duplicate of the "bfd_link_info" struct and pass
it to "bfd_final_link" so that I can determine the vtable offsets,
then append my array onto the end of the section, and then call
'bfd_final_link' a second time giving it the original "bfd_link_info"
struct? Would something like that work?
More information about the Binutils
mailing list