3

I have to call functions in the main executable from a shared library loaded with LD_PRELOAD.

The executable exports all symbols and contains debug information. Unfortunately I don't have access to it's source code.

Currently I'm getting undefined symbol errors when trying to load that shared library. Is there a way to do this?

PS: Target platform is FreeBSD/x86.

2

2 Answers 2

3

Can you create a function pointer by doing a typedef and use 'dlsym()' to get the address of the symbol. You can then invoke the function through the function pointer like a normal function call. Note: You do not need dlopen() since the main executable with symbols exported is loaded into process address space.

Prototype:

void *dlsym(void *handle, const char *symbol);

Assume the exported function is:

int foo(char *arg);

Your function pointer:

typedef (int)(*fooPtr)(char *);

In you code:

/* You can send NULL for first argument */
fooPtr fp = dlsym(NULL, "foo");
assert(0 != fp);
int ret = fp("hello world");
0
gcc -Wl,--export-dynamic

...should do the trick.

Documentation on --export-dynamic

4
  • As I said I don't have access to the main executable's source. Nov 1, 2011 at 19:52
  • Then I think you are out of luck. You can only reference exported symbols.
    – Nemo
    Nov 1, 2011 at 22:10
  • Hmm, there's no way to generate the dynamic export table? IDA shows me all exported functions + the executable was compiled with debug information Nov 2, 2011 at 15:51
  • Good question. I hope you find an answer :-)
    – Nemo
    Nov 2, 2011 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.