Calling functions from main program from dlopened library function


 
Thread Tools Search this Thread
Operating Systems AIX Calling functions from main program from dlopened library function
# 1  
Old 12-13-2012
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.

Code:
# 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.....

Code:
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

# 2  
Old 12-18-2012
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling Bash Functions in the BG

I'm trying to call some functions in the background so that I can multitask in this script. Not working so hot. The functions I call don't ever seem to get called. I'm doing it the exact same way in another script and it's working like a champ so I'm very confused. Here's a pretty simple repro: ... (7 Replies)
Discussion started by: stonkers
7 Replies

2. Homework & Coursework Questions

I need help movingworking code into library function and calling it obj13-2.pl

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! I need help moving working code into library function called obj13-lib.pl and call the same function in obj13-2.pl I am a Linux newbie and this certificate is my first step... (0 Replies)
Discussion started by: cllinuxhelp
0 Replies

3. Shell Programming and Scripting

Is it possible make the shell read functions 1 by 1 and calling an other function?

Greetings, I m wondering if it's possible do do the following : I have a simple function called "FindMoveDelete" which does the following : FindMoveDelete() { find . -iname "$FILENAME*.ext" -exec mv {} "$PATH/$VAR" \; && find . -maxdepth 1 -type d -iname "$FILENAME*" -exec rm -rf {}... (6 Replies)
Discussion started by: Sekullos
6 Replies

4. Shell Programming and Scripting

main program is not calling small other programs

I am trying to understand a program in a book and this program suppose to call other programs which are in the same folder, the other programs are called 'lu' and 'add' but for some reason when it gets to the last line of each case to call these programs there is an error message saying ./rolo:... (2 Replies)
Discussion started by: bartsimpsong
2 Replies

5. UNIX for Dummies Questions & Answers

calling process and going back to the main loop

hi everyone , i want to read an option and depending on the option call the program .For ex #! /bin/ksh export JAVA_HOME=/home/oracle/jdk1.6.0_20 echo " Please enter mod-modeler, dev - sqldeveloper" read choice if ; then echo ' SQL DEVELOPER IS STARTING NOW ... ' cd... (0 Replies)
Discussion started by: kdev
0 Replies

6. Shell Programming and Scripting

Calling Functions of Other K Shell Program

Hi, I have a K shell a.ksh function abc { // Some logic } In b.ksh i have included the a.ksh ./a.ksh I want to call the abc function from this b.ksh script. Thanks Vijay (2 Replies)
Discussion started by: vijaykrc
2 Replies

7. Shell Programming and Scripting

Calling functions in scripts directly

Hi, I have a menu driven script that does various tasks, I want to be able to call functions directly from within other unix scripts from my menu script. Is this possible? (12 Replies)
Discussion started by: LiquidChild
12 Replies

8. Programming

Return value (int) from main to calling shell

What is the sytax to return an int from C program main back to calling shell? #!/usr/bin/ksh typeset -i NO_RECS $NO_RECS=process_file # Process file is a C program that is set up to return an int from main. The #program complies with no issues, but an error is generated when the... (3 Replies)
Discussion started by: flounder
3 Replies

9. Shell Programming and Scripting

Calling subscript but sleep halts the main script

Ok, I have written a main script which checks a directory contents every 30 secs then sleeps. The subscript does a usermod, if the user is logged on, it sleeps for 30 secs and then trys again over and over again. Here's the problem. when the subscript is called ./subscript.sh or exec... (1 Reply)
Discussion started by: doublejz
1 Replies

10. Programming

c++ calling main() function

i just finished a project for a c++ class that i wrote at home on my computer, compiled with gcc. when i brought the code into school it would not compile, it would complain that cannot call main() function. at school we use ancient borland c++ from 1995. anyway my program has 20 different... (3 Replies)
Discussion started by: norsk hedensk
3 Replies
Login or Register to Ask a Question