How to determine the address of an exported character

Let's say I'm writing a C ++ library that should export a variable, a data structure called, for example, ExpData . Thus, the programs associated with my libray can access this variable (there is a public header that defines it as extern void *ExpData[] ).

However, this data structure is internally a vtable of the C ++ class. The name of the class, for example, InternalType . So, looking at the generated assembly code, I found that InternalType vtable is exported as _ZTV12InternalType .

Then I need my library to export the ExpData variable, be allowed with the same address as _ZTV12InternalType , so that when an external program reads my ExpData library, it actually reads InternalType vtable.

To clarify, InternalType vtable assembly code:

  .type _ZTV12InternalType,@object # @_ZTV12InternalType .section .data.rel.ro._ZTV12InternalType,"aGw",@progbits,_ZTV12InternalType,comdat .weak _ZTV12InternalType .align 16 _ZTV12InternalType: .quad 0 .quad _ZTI12InternalType .quad _ZN12InternalType8vMethodXEi .size _ZTV12InternalType, 24 

So, I need a way to achieve this (or something else with the same effect):

  .type ExpData,@object .globl ExpData .type _ZTV12InternalType,@object .section .data.rel.ro._ZTV12InternalType,"aGw",@progbits,_ZTV12InternalType,comdat .weak _ZTV12InternalType .align 16 _ZTV12InternalType: ExpData: .quad 0 .quad _ZTI12InternalType .quad _ZN12InternalType8vMethodXEi .size _ZTV12InternalType, 24 

Is this possible on the C ++ side?

PS: I know that I should not rely on implementation-specific details, such as the name mangling and internal C ++ classes, but just think that my library will work in very specific environments.

EDIT

I could solve my problem by passing --defsym ExpData=_ZTV12InternalType to the linker. However, I do not want to apply implementation details to external resources. Let's say I decided to map the vtable class as a C structure called InternalTypeVTable . So, I can declare ExpData as InternalTypeVTable ExpData; . It would be great if I had to change only the source file, and not make the files and linker scripts.

+7
c ++ c assembly gcc linker
source share
1 answer

GCC __attribute__ ((alias())) does exactly what I need.

If I declare ExpData as:

 void *ExpData[0] __attribute__ ((alias("_ZTV12InternalType"))); 

in the library source file, I get the following assembly code:

  .globl ExpData ExpData = _ZTV12InternalType 

So, the exported ExpData character refers to the same memory address as _ZTV12InternalType .

+4
source share

All Articles