dlopen, dlsym usage



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.

.



Relevant Pages

  • Re: dlopen, dlsym usage
    ... The simplest sample: ... int func() ... noly its name for EXECUTABLE module ...
    (comp.unix.programmer)
  • Re: allocating space for local variables
    ... int arr2; ... true or the space is allocated when the function "func" is called. ... assuming non portable code and the assertion that all locals are stack ... It cant be a memory thing since I assume x0 at some stage anyway so ...
    (comp.lang.c)
  • Re: allocating space for local variables
    ... int arr2; ... true or the space is allocated when the function "func" is called. ... assuming non portable code and the assertion that all locals are stack ... It cant be a memory thing since I assume x0 at some stage anyway so ...
    (comp.lang.c)
  • Re: void func () returning value?
    ... In the void case, you perform a function which does ... void func ... void func2 (int *in_arg) ... int func2 (int arg) ...
    (comp.lang.c)
  • Re: The importance of prototypes
    ... In the first call to func, the int value 42 was passed to the ... With a proper prototype in scope, the compiler knows that func ...
    (comp.lang.c)