Incorrect function address placed in function pointer in dynamicallylinked shared library - Unix
This is a discussion on Incorrect function address placed in function pointer in dynamicallylinked shared library - Unix ; Background: I am creating a simple plugin framework in C with plugins
implemented in shared libraries. The plugins are loaded from a
'plugins' directory via dlopen(). The plugins create
'objects' (structs containing function pointers) for use in the main
application. ...

- Forum
- OS Forums
- Unix
- Incorrect function address placed in function pointer in dynamicallylinked shared library
-
Incorrect function address placed in function pointer in dynamicallylinked shared library
Background: I am creating a simple plugin framework in C with plugins
implemented in shared libraries. The plugins are loaded from a
'plugins' directory via dlopen(). The plugins create
'objects' (structs containing function pointers) for use in the main
application. Here is a sample 'object':
typedef void (*writer_function)(const char *value);
typedef struct writer_object {
int x;
writer_function write_func;
} writer_object;
My problem is this: In the object creation function in a plugin
(dynamically linked, shared library), I allocate the structure and
assign the address of the writer_function implemented in that plugin.
The creation function returns a pointer to this newly allocated
structure, which the main application can then use to call the
write_func. It appears that the address being placed in the structure
after initialization is incorrect.
When debugging with gdb, the address shown when doing 'print
write' (write() is the implementation of the writer_function in the
plugin) does not match that which is stored in the function pointer in
the structure. They are close, but not similar, and thus when the
main application attempts to call the write_func, it does not work.
Using 'call write' I am able to successfully execute the function in
gdb.
I am thinking it has something to do with the relocation of the code,
but I am rather new to dynamic libraries, so can't quite get my head
around this one.
Notes:
The shared library was compiled with gcc using the -fPIC and -g flags.
The interface for the writer_object was included in both the plugin
and the main application.
I can provide any information necessary regarding platform, more
sample code, etc...
Any insight is greatly appreciated.
-wm
-
Re: Incorrect function address placed in function pointer in dynamically linked shared library
w.wilson2@gmail.com writes:
> Background: I am creating a simple plugin framework in C with plugins
> implemented in shared libraries. The plugins are loaded from a
> 'plugins' directory via dlopen(). The plugins create
> 'objects' (structs containing function pointers) for use in the main
> application. Here is a sample 'object':
>
> typedef void (*writer_function)(const char *value);
>
> typedef struct writer_object {
> int x;
> writer_function write_func;
> } writer_object;
>
> My problem is this: In the object creation function in a plugin
> (dynamically linked, shared library), I allocate the structure and
> assign the address of the writer_function implemented in that plugin.
> The creation function returns a pointer to this newly allocated
> structure, which the main application can then use to call the
> write_func. It appears that the address being placed in the structure
> after initialization is incorrect.
>
> When debugging with gdb, the address shown when doing 'print
> write' (write() is the implementation of the writer_function in the
> plugin) does not match that which is stored in the function pointer in
> the structure. They are close, but not similar, and thus when the
> main application attempts to call the write_func, it does not work.
> Using 'call write' I am able to successfully execute the function in
> gdb.
Call the function something other than write, to avoid confusion with
the C library write() function.
--
Måns Rullgård
mans@mansr.com
-
Re: Incorrect function address placed in function pointer indynamically linked shared library
>
> Call the function something other than write, to avoid confusion with
> the C library write() function.
>
> --
> Måns Rullgård
> m...@mansr.com
Wow is my face red. Thanks. I suppose I have learned my lesson now
and will use 'namespaces' in my plugins.
-wm