calling external functions within shared libs

Hi,
I have a doubt about some points.
I have a binary which can load some ‘add on’ libraries, a little bit
like io-net with the npm-tcpip.so, npm-qnet.so.

These libraries can call some functions located in the binary.
My problem is:
1- Do I have to create a so file with the functions, physically located
in the binary, for the make?
2- What will happen at run time with the runtime linker. Will this
functions directly linked with those are in the binary, or linked with
those are in the so file?

Thanks,
Alain.

Alain Bonnefoy <alain.bonnefoy@icbt.com> wrote:

These libraries can call some functions located in the binary.
My problem is:
1- Do I have to create a so file with the functions, physically located
in the binary, for the make?

I could be mis-understanding your question, but it seems like you have
an almost circular dependancy. The main binary, requires to load a
shared lib, which in turn has dependancies upon the main binary.

Why not move the functions that are common to both main, and the shared lib
into another shared lib that both can link in dynamicly.

2- What will happen at run time with the runtime linker. Will this
functions directly linked with those are in the binary, or linked with
those are in the so file?

I’m sorry but I don’t understand what you’re asking. Perhaps if you post
a piece of code to illustrate what you’re trying to do, I (we) can
understand more fully.

Thanks
-Adam
amallory@qnx.com

Alain Bonnefoy <alain.bonnefoy@icbt.com> wrote:

Hi,
I have a doubt about some points.
I have a binary which can load some ‘add on’ libraries, a little bit
like io-net with the npm-tcpip.so, npm-qnet.so.

These libraries can call some functions located in the binary.
My problem is:
1- Do I have to create a so file with the functions, physically located
in the binary, for the make?

Usually, since it is your main binary load all the .so, your main
binary can then “pass down” all the function pointers, so the .so
could use them.

That’s how the io_net_self_t doing anyway.

-xtang

2- What will happen at run time with the runtime linker. Will this
functions directly linked with those are in the binary, or linked with
those are in the so file?

Thanks,
Alain.

Operating System for Tech Supp a écrit :

Alain Bonnefoy <> alain.bonnefoy@icbt.com> > wrote:
These libraries can call some functions located in the binary.
My problem is:
1- Do I have to create a so file with the functions, physically located
in the binary, for the make?

I could be mis-understanding your question, but it seems like you have
an almost circular dependancy. The main binary, requires to load a
shared lib, which in turn has dependancies upon the main binary.

Why not move the functions that are common to both main, and the shared lib
into another shared lib that both can link in dynamicly.

2- What will happen at run time with the runtime linker. Will this
functions directly linked with those are in the binary, or linked with
those are in the so file?

I’m sorry but I don’t understand what you’re asking. Perhaps if you post
a piece of code to illustrate what you’re trying to do, I (we) can
understand more fully.

Thanks
-Adam
amallory@qnx.com

Ok, it’s difficult to explain but really easy to do ;o).
I found the solution!

libext_func1.so:
#include <stdio.h>
#include <stdlib.h>

extern void mgr_add_func_to_list(int (*)());

int ext_func1(void);

void init_external_func(void) {
printf(“We are in init_external_func 1 to give to ext_func_manager the
external func 1\n”);
mgr_add_func_to_list(ext_func1);
}

int ext_func1(void) {
printf(“external func 1 has been called\n”);

return(0);
}

libext_func2.so:
#include <stdio.h>
#include <stdlib.h>

extern void mgr_add_func_to_list(int (*)());

int ext_func2(void);

void init_external_func(void) {
printf(“We are in init_external_func 2 to give to ext_func_manager the
external func 2\n”);
mgr_add_func_to_list(ext_func2);
}

int ext_func2(void) {
printf(“external func 2 has been called\n”);

return(0);
}

ext_func_manager:
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

void mgr_add_func_to_list(int (*func)());

int (*list_init_func[2])();
int (*list_func[2])();
int index_func = 0;

int main(int argc, char **argv) {
void *handle;
int i = 0;

puts(“ext_func_manager is going to load the libraries\n”);
for (i = 0; i < 2; i++) {
if ((handle = dlopen(argv[i+1], 0)) == NULL) {
puts(dlerror());
exit(EXIT_FAILURE);
}
// We qre looking for the initialisation function belonging to the
shared library opened above.
if ((list_init_func = (int (*)())dlsym(handle,
“init_external_func”)) == NULL) {
puts(dlerror());
exit(EXIT_FAILURE);
}
}
puts(“ext_func_manager has loaded the libs\nIt now tries to execute the
init funcs\n”);

for (i = 0; i < 2; i++) {
// execute the initialisation functions
list_init_func();
}

puts(“ext_func_manager has done the init\nIt now tries to execute the
funcs\n”);

for (i = 0; i < 2; i++) {
// Here we execute the real work functions
list_func();
}

puts(“ext_func_manager says: That’s all folks, bye\n”);
return(EXIT_SUCCESS);
}

void mgr_add_func_to_list(int (*func)()) {
list_func[index_func++] = func;
}

#ext_func_manager libext_func1.so libext_func2.so

Fantastic!!

Thanks,
Alain.

These libraries can call some functions located in the binary.
My problem is:
1- Do I have to create a so file with the functions, physically located
in the binary, for the make?
2- What will happen at run time with the runtime linker. Will this
functions directly linked with those are in the binary, or linked with
those are in the so file?

The easiest way to do this is to link your program with
-Wl,-E

The -E option to ld tells the linker to build a .so symbol table into your
executable to do exactly what you want. You do not need a separate
…so for the functions in the binary.

Cheers,
Andrew