Calling functions from main program from dlopened library function

Hello All,

I am trying to call a function from the calling main program from a dlopened library function, below is the entire code, when I execute it it crashes with sigill. Can you guys help me out I guess I am missing out on the linker flag or something here. besides I am new to AIX and really appreciate your help on this.

# cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <errno.h>
typedef void (*FP)(void);

void printme (void) {
 printf ("printed me hurray!!!\n");
}


int
main( int argc, char** argv )
{
 void* handle;
 void* symptr;
 FP fp;
 /*** Load the shared object containing the definition of the ***/
 /*** function . defsym(). ***/
 if ( NULL == ( handle = dlopen( "./libdefsym.so", RTLD_NOW ) ) ){
   perror( dlerror() );
  exit( 1 );
 }
 /**** Obtain the address of the function defsym. ***/
 if ( NULL == ( symptr = dlsym( handle, "defsym" ) ) ) {
  if ( 0 == errno ) {
   fprintf( stderr, "Symbol defsym not found. Exiting\n"
   );
   exit( 1 );
  }
  perror( dlerror() );
  exit( 1 );
 }
 fp = ( FP )symptr;
 /*** Call the function via the obtained function address. ***/
 fp();
 /*** Unload the previously loaded shared object. ***/
 if ( 0 != ( dlclose( handle ) ) ) {
  perror( dlerror() );
  exit( 1 );
 }
 exit( 0 );
}

defsym.c
#include <stdio.h>
#include <stdlib.h>
void
defsym()
{
 printf( "defsym called.\n" );
 printme();
}

cat toexp.exp
#!.
printme

Makefile
CC=gcc
CFLAGS= -g -maix64
all: main libdefsym.so
main: main.o
    $(CC) -maix64 -o main main.o
main.o: main.c
    $(CC) $(CFLAGS) -c main.c
libdefsym.so: defsym.o
    ld -G -bnoentry -bexpall -bE:toexp.exp -o libdefsym.so defsym.o -lc
defsym.o: defsym.c
    $(CC) $(CFLAGS) -c defsym.c
.PHONY: clean
clean:
    rm *.o *.so main


# dbx ./main core
Type 'help' for help.
[using memory image in core]
reading symbolic information ...internal error: 1283-228 expected char ',', found ';-31,448,256;;'
internal error: 1283-228 expected char ';', found ',256;;'
internal error: 1283-232 index("256;;", ':') failed
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: 1283-228 expected char ',', found 's__LC_locale:,1088,64;__meth_ptr:13,1152,64;__data_ptr:13,1216,64;;'
internal error: 1283-228 expected char ',', found '__LC_locale:,1088,64;__meth_ptr:13,1152,64;__data_ptr:13,1216,64;;'
internal error: 1283-228 expected char ';', found '_LC_locale:,1088,64;__meth_ptr:13,1152,64;__data_ptr:13,1216,64;;'
internal error: unexpected value 44 at line 5176 in file stabstring.c
internal error: 1283-228 expected char ',', found '1088,64;__meth_ptr:13,1152,64;__data_ptr:13,1216,64;;'
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: 1283-228 expected char ',', found 's_LC_locale_objhdl:,128,64;;'
internal error: 1283-228 expected char ',', found '_LC_locale_objhdl:,128,64;;'
internal error: 1283-228 expected char ';', found 'LC_locale_objhdl:,128,64;;'
internal error: unexpected value 44 at line 5176 in file stabstring.c
internal error: 1283-228 expected char ',', found '128,64;;'
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c


Illegal instruction (illegal opcode) in . at 0x0
warning: Unable to access address 0x0 from core
(dbx) where
.() at 0x0
internal error: 1283-228 expected char ',', found ';-31,448,256;;'
internal error: 1283-228 expected char ';', found ',256;;'
internal error: 1283-232 index("256;;", ':') failed
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: 1283-228 expected char ',', found 's__LC_locale:,1088,64;__meth_ptr:13,1152,64;__data_ptr:13,1216,64;;'
internal error: 1283-228 expected char ',', found '__LC_locale:,1088,64;__meth_ptr:13,1152,64;__data_ptr:13,1216,64;;'
internal error: 1283-228 expected char ';', found '_LC_locale:,1088,64;__meth_ptr:13,1152,64;__data_ptr:13,1216,64;;'
internal error: unexpected value 44 at line 5176 in file stabstring.c
internal error: 1283-228 expected char ',', found '1088,64;__meth_ptr:13,1152,64;__data_ptr:13,1216,64;;'
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: 1283-228 expected char ',', found 's_LC_locale_objhdl:,128,64;;'
internal error: 1283-228 expected char ',', found '_LC_locale_objhdl:,128,64;;'
internal error: 1283-228 expected char ';', found 'LC_locale_objhdl:,128,64;;'
internal error: unexpected value 44 at line 5176 in file stabstring.c
internal error: 1283-228 expected char ',', found '128,64;;'
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c
internal error: unexpected value 120 at line 5176 in file stabstring.c
defsym(), line 8 in "defsym.c"
main(argc = 1, argv = 0x0ffffffffffff9e0), line 36 in "main.c"
(dbx)

---------- Post updated 12-13-12 at 05:40 PM ---------- Previous update was 12-12-12 at 11:33 PM ----------

Using the below make file I was able to solve this issue in hand.....

CC=gcc
CFLAGS= -g -maix64

all: libcommon.a main libdefsym.so
libcommon.a: staticmain.o
        ar -rv libcommon.a staticmain.o
staticmain.o: staticmain.c
        $(CC) $(CFLAGS) -c staticmain.c
main: main.o
        $(CC) -o main -Wl,-brtl -Wl,-bE:toexp.exp -L.  -lcommon main.o -maix64 -ldl
        #$(CC) -o main -Wl,-bI:defsym.imp main.o -maix64 -ldl
main.o: main.c
        $(CC) $(CFLAGS) -c main.c

libdefsym.so: defsym.o
        $(CC) -Wl,-bnoentry -Wl,-bexpall -Wl,-G -shared -L/opt/altiris/notification/nsagent/lib/ -ldl -L. -maix64 -o libdefsym.so defsym.o -lc
        #$(CC) -shared -L/opt/altiris/notification/nsagent/lib/ -ldl -L. -maix64 -Wl,-bE:defsym.imp -Wl,-bI:toexp.exp -o libdefsym.so defsym.o -lc
defsym.o: defsym.c
        $(CC) $(CFLAGS) -fPIC  -c defsym.c
.PHONY: clean
clean:
        rm *.o *.so main

Maybe you need lazy? Man Page for dlsym (linux Section 3) - The UNIX and Linux Forums