For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > April 2006 > dlopen, dlsym usage









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author dlopen, dlsym usage
georgeshagov

2006-04-28, 4:04 am

Good time of the day to everyone.

The simplest sample:


#include <dlfcn.h>

int func()
{
}

int main()
{
func();
void* pHandle = dlopen(0, RTLD_NOW | RTLD_GLOBAL);
void* pFn = dlsym(pHandle, "func");

printf("0x%x, 0x%x, Error: %s\n", pHandle, pFn, dlerror());
return 0;
}


the output:
../a.out
0x40013688, 0x0, Error: ./a.out: undefined symbol: func


nm ./a.out
0804956c D _DYNAMIC
08049640 D _GLOBAL_OFFSET_TABLE_
08048534 R _IO_stdin_used
w _Jv_RegisterClasses
0804955c d __CTOR_END__
08049558 d __CTOR_LIST__
08049564 d __DTOR_END__
08049560 d __DTOR_LIST__
08048554 r __FRAME_END__
08049568 d __JCR_END__
08049568 d __JCR_LIST__
0804966c A __bss_start
08049660 D __data_start
080484f0 t __do_global_ctors_aux
08048400 t __do_global_dtors_aux
08049664 d __dso_handle
w __gmon_start__
U __libc_start_main@@GLIBC_2.0
0804966c A _edata
08049670 A _end
08048514 T _fini
08048530 R _fp_hw
0804832c T _init
080483b0 T _start
080483d4 t call_gmon_start
0804966c b completed.1
08049660 W data_start
U dlerror@@GLIBC_2.0
U dlopen@@GLIBC_2.1
U dlsym@@GLIBC_2.0
08048430 t frame_dummy
08048464 T func
08048469 T main
08049668 d p.0
U printf@@GLIBC_2.0



the simplest question: How to get pointer to the function if I know
noly its name for EXECUTABLE module

dlopen dlsym perfectly works for .so modules, but it would appear they
do not for executables.

Can anybody help?

Thank you.

Frank Cusack

2006-04-28, 4:04 am

On 28 Apr 2006 00:05:46 -0700 "georgeshagov" <George.Shagov@db.com> wrote:
> Good time of the day to everyone.
>
> The simplest sample:
>
>
> #include <dlfcn.h>
>
> int func()
> {
> }
>
> int main()
> {
> func();
> void* pHandle = dlopen(0, RTLD_NOW | RTLD_GLOBAL);
> void* pFn = dlsym(pHandle, "func");
>
> printf("0x%x, 0x%x, Error: %s\n", pHandle, pFn, dlerror());
> return 0;
> }
>
>
> the output:
> ./a.out
> 0x40013688, 0x0, Error: ./a.out: undefined symbol: func

....
> the simplest question: How to get pointer to the function if I know
> noly its name for EXECUTABLE module


I don't see the error in your code, but try dlsym(RTLD_DEFAULT, ...)
instead of the explicit dlopen().

-frank
georgeshagov

2006-04-28, 4:04 am

it would appear the kernel I have is pretty old:

uname -a
Linux XXXXXXXXX 2.4.21-251-smp #1 SMP Thu Sep 23 17:22:54 UTC 2004 i686
unknown

there is not RTLD_DEFAULT here, what I did I tried dlsym(0, ...)
the result is absolutely identical

../a.out
0x40013688, 0x0, Error: ./a.out: undefined symbol: func

chris

2006-04-28, 8:04 am


georgeshagov wrote:
> it would appear the kernel I have is pretty old:
>
> uname -a
> Linux XXXXXXXXX 2.4.21-251-smp #1 SMP Thu Sep 23 17:22:54 UTC 2004 i686
> unknown
>
> there is not RTLD_DEFAULT here, what I did I tried dlsym(0, ...)
> the result is absolutely identical
>
> ./a.out
> 0x40013688, 0x0, Error: ./a.out: undefined symbol: func


Did you possibly compile your code with a C++ compiler (e.g. g++
instead of gcc), if so the function's symbol name is not func but some
mangled form. In that case try using extern "C" to force C-linkage for
the function.

georgeshagov

2006-04-28, 8:04 am

gcc dl.c -ldl

nm:
08048464 T func
08048469 T main

Maxim Yegorushkin

2006-04-28, 8:04 am


georgeshagov wrote:

[]

> printf("0x%x, 0x%x, Error: %s\n", pHandle, pFn, dlerror());


This format string will cause a warning on a 64-bit system. Use %p to
print pointers.

Paul Pluzhnikov

2006-04-28, 8:04 am

"georgeshagov" <George.Shagov@db.com> writes:

> dlopen dlsym perfectly works for .so modules, but it would appear they
> do not for executables.


This is because func() is not exported from the executable by default.
Read up on the -rdynamic flag (with which you must link the exe
for your example to work).

Before you relink, you may also wish to diff nm output you got with
the output from 'nm -D a.out'.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
georgeshagov

2006-04-28, 8:04 am

It works.
Thank you.

Robert Clark

2006-04-28, 10:01 pm

Hi,

georgeshagov wrote:
>
> #include <dlfcn.h>

#include <stdio.h> /* Required if you are going to use printf */
>
> int func()
> {
> }
> int main()
> {
> func();
> void* pHandle = dlopen(0, RTLD_NOW | RTLD_GLOBAL);
> void* pFn = dlsym(pHandle, "func");
>

if ( pFn == NULL ) {
> printf("0x%x, 0x%x, Error: %s\n", pHandle, pFn, dlerror());

}
else {
printf("all OK\n");
}
> return 0;
> }
>
>
> the output:
> ./a.out
> 0x40013688, 0x0, Error: ./a.out: undefined symbol: func


> the simplest question: How to get pointer to the function if I know
> noly its name for EXECUTABLE module
>
> dlopen dlsym perfectly works for .so modules, but it would appear they
> do not for executables.


If you're going to dlopen() an executable on Linux, then the executable
needs to be linked using the "-rdynamic" gcc flag.

Try:

% gcc sample.c -ldl -rdynamic
% ./a.out
% all OK
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com