質問

I'm working on a test environment of a C library. The library extensively use global variables, what I want to check in the test codes. Unfortunately I have to load the library dynamically (using libdl) to be able to reset the function static variables. This way I have to load every global using dlsym() and I have to cast them one by one manually to the correct type. Is there any way to automatize that and get the type info of the variables somehow? As far as I see libdl has no such feature. I wondered that I might be able to link to gdb, using it to access to shared library globals, but I didn't managed to find any clue about that possibility either.

役に立ちましたか?

解決

No, there is no way to get the type of some dlsym-ed symbol, because an ELF shared object don't (always) carry any type information (except for C++, using name mangling).

And in principle, an ELF shared object might be produced without any C compiler, so the very notion of type of a given symbol might not exist, or the type be incompatible with C conventions.

However, you could restrict yourself to shared libraries with debug information. The DWARF format does carry type (and even source location) information about symbols. You might parse it with e.g. libdwarf or some other library.

You may consider alternative ways: for instance, you could have your own GCC plugin or MELT extension (MELT is a domain specific language to extend GCC) which would be used when compiling (with GCC) the shared libraries and would register the type information somewhere.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top